Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Replace redundant type with "auto" (examples/).
[simgrid.git] / examples / s4u / app-token-ring / s4u-app-token-ring.cpp
1 /* Copyright (c) 2017-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 #include <algorithm>
8 #include <string>
9 #include <map>
10 #include <vector>
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_token_ring, "Messages specific for this s4u example");
13
14 class RelayRunner {
15   size_t token_size = 1000000; /* The token is 1MB long*/
16   simgrid::s4u::Mailbox* my_mailbox;
17   simgrid::s4u::Mailbox* neighbor_mailbox;
18   unsigned int rank      = 0;
19
20 public:
21   explicit RelayRunner() = default;
22
23   void operator()()
24   {
25     try {
26       rank = std::stoi(simgrid::s4u::this_actor::get_name());
27     } catch (const std::invalid_argument& ia) {
28       throw std::invalid_argument(std::string("Actors of this example must have a numerical name, not ") + ia.what());
29     }
30     my_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(rank));
31     if (rank + 1 == simgrid::s4u::Engine::get_instance()->get_host_count())
32       /* The last actor sends the token back to rank 0 */
33       neighbor_mailbox = simgrid::s4u::Mailbox::by_name("0");
34     else
35       /* The others actors send to their right neighbor (rank+1) */
36       neighbor_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(rank + 1));
37
38     if (rank == 0) {
39       /* The root actor (rank 0) first sends the token then waits to receive it back */
40       XBT_INFO("Host \"%u\" send 'Token' to Host \"%s\"", rank, neighbor_mailbox->get_cname());
41       std::string msg = "Token";
42       neighbor_mailbox->put(&msg, token_size);
43       const auto* res = static_cast<std::string*>(my_mailbox->get());
44       XBT_INFO("Host \"%u\" received \"%s\"", rank, res->c_str());
45     } else {
46       auto* res = static_cast<std::string*>(my_mailbox->get());
47       XBT_INFO("Host \"%u\" received \"%s\"", rank, res->c_str());
48       XBT_INFO("Host \"%u\" send 'Token' to Host \"%s\"", rank, neighbor_mailbox->get_cname());
49       neighbor_mailbox->put(res, token_size);
50     }
51   }
52 };
53
54 int main(int argc, char** argv)
55 {
56   simgrid::s4u::Engine e(&argc, argv);
57   xbt_assert(argc > 1, "Usage: %s platform.xml\n", argv[0]);
58   e.load_platform(argv[1]);
59
60   XBT_INFO("Number of hosts '%zu'", e.get_host_count());
61   int id = 0;
62   std::vector<simgrid::s4u::Host*> list = e.get_all_hosts();
63   for (auto const& host : list) {
64     /* - Give a unique rank to each host and create a @ref relay_runner actor on each */
65     simgrid::s4u::Actor::create((std::to_string(id)).c_str(), host, RelayRunner());
66     id++;
67   }
68   e.run();
69   XBT_INFO("Simulation time %g", e.get_clock());
70
71   return 0;
72 }