Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
71da8a09a1e58ab6be038c4190f5b28cb94edc05
[simgrid.git] / examples / s4u / network-ns3 / s4u-network-ns3.cpp
1 /* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/s4u.hpp"
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
9
10 int timer_start; // set as 1 in the master actor
11
12 #define NTASKS 1500
13 double start_time;
14 const char* workernames[NTASKS];
15 const char* masternames[NTASKS];
16 int count_finished = 0;
17
18 static void master(int argc, char* argv[])
19 {
20   xbt_assert(argc == 4, "Strange number of arguments expected 3 got %d", argc - 1);
21
22   XBT_DEBUG("Master started");
23
24   /* data size */
25   double task_comm_size = std::stod(argv[1]);
26   int id                = std::stoi(argv[3]); // unique id to control statistics
27
28   /* worker name */
29   workernames[id] = xbt_strdup(argv[2]);
30
31   simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(argv[3]);
32
33   masternames[id] = simgrid::s4u::Host::current()->get_cname();
34
35   double* payload = new double();
36   *payload        = task_comm_size;
37
38   count_finished++;
39   timer_start = 1;
40
41   /* time measurement */
42   start_time = simgrid::s4u::Engine::get_clock();
43   mbox->put(payload, task_comm_size);
44
45   XBT_DEBUG("Finished");
46 }
47
48 static void timer(int argc, char* argv[])
49 {
50   xbt_assert(argc == 3, "Strange number of arguments expected 2 got %d", argc - 1);
51   double first_sleep = std::stod(argv[1]);
52   double sleep_time  = std::stod(argv[2]);
53
54   XBT_DEBUG("Timer started");
55
56   if (first_sleep)
57     simgrid::s4u::this_actor::sleep_for(first_sleep);
58
59   do {
60     XBT_DEBUG("Get sleep");
61     simgrid::s4u::this_actor::sleep_for(sleep_time);
62   } while (timer_start);
63
64   XBT_DEBUG("Finished");
65 }
66
67 static void worker(int argc, char* argv[])
68 {
69   xbt_assert(argc == 2, "Strange number of arguments expected 1 got %d", argc - 1);
70
71   int id                      = std::stoi(argv[1]);
72   simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(argv[1]);
73
74   XBT_DEBUG("Worker started");
75
76   const double* payload = static_cast<double*>(mbox->get());
77
78   count_finished--;
79   if (count_finished == 0) {
80     timer_start = 0;
81   }
82
83   double elapsed_time = simgrid::s4u::Engine::get_clock() - start_time;
84
85   XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, *payload, masternames[id], workernames[id]);
86   XBT_DEBUG("FLOW[%d] : transferred in  %f seconds", id, elapsed_time);
87   delete payload;
88
89   XBT_DEBUG("Finished");
90 }
91
92 int main(int argc, char* argv[])
93 {
94   simgrid::s4u::Engine e(&argc, argv);
95   xbt_assert(argc > 2,
96              "Usage: %s platform_file deployment_file\n"
97              "\tExample: %s platform.xml deployment.xml\n",
98              argv[0], argv[0]);
99
100   e.load_platform(argv[1]);
101
102   e.register_function("master", master);
103   e.register_function("worker", worker);
104   e.register_function("timer", timer);
105
106   e.load_deployment(argv[2]);
107
108   e.run();
109
110   return 0;
111 }