Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[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 struct MasterWorkerNames {
14   std::string master;
15   std::string worker;
16 };
17 using MasterWorkerNamesMap = std::unordered_map<int, MasterWorkerNames>;
18
19 struct Payload {
20   double msg_size;
21   double start_time;
22 };
23
24 static void master(MasterWorkerNamesMap& names, const std::vector<std::string>& args)
25 {
26   xbt_assert(args.size() == 4, "Strange number of arguments expected 3 got %zu", args.size() - 1);
27
28   XBT_DEBUG("Master started");
29
30   /* data size */
31   double msg_size = std::stod(args[1]);
32   int id          = std::stoi(args[3]); // unique id to control statistics
33
34   /* master and worker names */
35   names.try_emplace(id, MasterWorkerNames{sg4::Host::current()->get_name(), args[2]});
36
37   sg4::Mailbox* mbox = sg4::Mailbox::by_name(args[3]);
38
39   auto* payload = new Payload{msg_size, sg4::Engine::get_clock()};
40   mbox->put(payload, static_cast<uint64_t>(msg_size));
41
42   XBT_DEBUG("Finished");
43 }
44
45 static void worker(const MasterWorkerNamesMap& names, const std::vector<std::string>& args)
46 {
47   xbt_assert(args.size() == 2, "Strange number of arguments expected 1 got %zu", args.size() - 1);
48
49   int id                      = std::stoi(args[1]);
50   sg4::Mailbox* mbox          = sg4::Mailbox::by_name(args[1]);
51
52   XBT_DEBUG("Worker started");
53
54   auto payload = mbox->get_unique<Payload>();
55
56   double elapsed_time = sg4::Engine::get_clock() - payload->start_time;
57
58   XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, payload->msg_size, names.at(id).master.c_str(),
59            names.at(id).worker.c_str());
60   XBT_DEBUG("FLOW[%d] : transferred in  %f seconds", id, elapsed_time);
61
62   XBT_DEBUG("Finished");
63 }
64
65 int main(int argc, char* argv[])
66 {
67   sg4::Engine e(&argc, argv);
68   xbt_assert(argc > 2,
69              "Usage: %s platform_file deployment_file\n"
70              "\tExample: %s platform.xml deployment.xml\n",
71              argv[0], argv[0]);
72
73   e.load_platform(argv[1]);
74
75   MasterWorkerNamesMap master_worker_names;
76   e.register_function("master", [&master_worker_names](auto args) { master(master_worker_names, args); });
77   e.register_function("worker", [&master_worker_names](auto args) { worker(master_worker_names, args); });
78
79   e.load_deployment(argv[2]);
80
81   e.run();
82
83   return 0;
84 }