Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / s4u / network-ns3 / s4u-network-ns3.cpp
1 /* Copyright (c) 2007-2021. 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 msg_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   auto* payload = new double(msg_size);
36
37   count_finished++;
38   timer_start = 1;
39
40   /* time measurement */
41   start_time = simgrid::s4u::Engine::get_clock();
42   mbox->put(payload, msg_size);
43
44   XBT_DEBUG("Finished");
45 }
46
47 static void timer(int argc, char* argv[])
48 {
49   xbt_assert(argc == 3, "Strange number of arguments expected 2 got %d", argc - 1);
50   double first_sleep = std::stod(argv[1]);
51   double sleep_time  = std::stod(argv[2]);
52
53   XBT_DEBUG("Timer started");
54
55   if (first_sleep)
56     simgrid::s4u::this_actor::sleep_for(first_sleep);
57
58   do {
59     XBT_DEBUG("Get sleep");
60     simgrid::s4u::this_actor::sleep_for(sleep_time);
61   } while (timer_start);
62
63   XBT_DEBUG("Finished");
64 }
65
66 static void worker(int argc, char* argv[])
67 {
68   xbt_assert(argc == 2, "Strange number of arguments expected 1 got %d", argc - 1);
69
70   int id                      = std::stoi(argv[1]);
71   simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(argv[1]);
72
73   XBT_DEBUG("Worker started");
74
75   auto payload = mbox->get_unique<double>();
76
77   count_finished--;
78   if (count_finished == 0) {
79     timer_start = 0;
80   }
81
82   double elapsed_time = simgrid::s4u::Engine::get_clock() - start_time;
83
84   XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, *payload, masternames[id], workernames[id]);
85   XBT_DEBUG("FLOW[%d] : transferred in  %f seconds", id, elapsed_time);
86
87   XBT_DEBUG("Finished");
88 }
89
90 int main(int argc, char* argv[])
91 {
92   simgrid::s4u::Engine e(&argc, argv);
93   xbt_assert(argc > 2,
94              "Usage: %s platform_file deployment_file\n"
95              "\tExample: %s platform.xml deployment.xml\n",
96              argv[0], argv[0]);
97
98   e.load_platform(argv[1]);
99
100   e.register_function("master", master);
101   e.register_function("worker", worker);
102   e.register_function("timer", timer);
103
104   e.load_deployment(argv[2]);
105
106   e.run();
107
108   return 0;
109 }