Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
adding sleep sets to reduction techniques
[simgrid.git] / examples / cpp / network-ns3 / s4u-network-ns3.cpp
1 /* Copyright (c) 2007-2023. 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 #include <string>
8 #include <unordered_map>
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
11 namespace sg4 = simgrid::s4u;
12
13 double start_time;
14 std::unordered_map<int, std::string> workernames;
15 std::unordered_map<int, std::string> masternames;
16
17 static void master(std::vector<std::string> args)
18 {
19   xbt_assert(args.size() == 4, "Strange number of arguments expected 3 got %zu", args.size() - 1);
20
21   XBT_DEBUG("Master started");
22
23   /* data size */
24   double msg_size = std::stod(args[1]);
25   int id          = std::stoi(args[3]); // unique id to control statistics
26
27   /* worker name */
28   workernames[id] = args[2];
29
30   sg4::Mailbox* mbox = sg4::Mailbox::by_name(args[3]);
31
32   masternames[id] = sg4::Host::current()->get_name();
33
34   auto* payload = new double(msg_size);
35
36   /* time measurement */
37   start_time = sg4::Engine::get_clock();
38   mbox->put(payload, static_cast<uint64_t>(msg_size));
39
40   XBT_DEBUG("Finished");
41 }
42
43 static void worker(std::vector<std::string> args)
44 {
45   xbt_assert(args.size() == 2, "Strange number of arguments expected 1 got %zu", args.size() - 1);
46
47   int id                      = std::stoi(args[1]);
48   sg4::Mailbox* mbox          = sg4::Mailbox::by_name(args[1]);
49
50   XBT_DEBUG("Worker started");
51
52   auto payload = mbox->get_unique<double>();
53
54   double elapsed_time = sg4::Engine::get_clock() - start_time;
55
56   XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, *payload, masternames.at(id).c_str(),
57            workernames.at(id).c_str());
58   XBT_DEBUG("FLOW[%d] : transferred in  %f seconds", id, elapsed_time);
59
60   XBT_DEBUG("Finished");
61 }
62
63 int main(int argc, char* argv[])
64 {
65   sg4::Engine e(&argc, argv);
66   xbt_assert(argc > 2,
67              "Usage: %s platform_file deployment_file\n"
68              "\tExample: %s platform.xml deployment.xml\n",
69              argv[0], argv[0]);
70
71   e.load_platform(argv[1]);
72
73   e.register_function("master", master);
74   e.register_function("worker", worker);
75
76   e.load_deployment(argv[2]);
77
78   e.run();
79
80   return 0;
81 }