Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5bf49e8c5d35494c05cf4a5657985c5d435c3012
[simgrid.git] / examples / s4u / app-token-ring / s4u-app-token-ring.cpp
1 /* Copyright (c) 2017-2018. 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 task_comm_size = 1000000; /* The token is 1MB long*/
16   simgrid::s4u::MailboxPtr my_mailbox;
17   simgrid::s4u::MailboxPtr 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 (std::invalid_argument& ia) {
28       throw std::invalid_argument(std::string("Processes of this example must have a numerical name, not ") +
29                                   ia.what());
30     }
31     my_mailbox = simgrid::s4u::Mailbox::byName(std::to_string(rank));
32     if (rank + 1 == simgrid::s4u::Engine::getInstance()->get_host_count())
33       /* The last process, which sends the token back to rank 0 */
34       neighbor_mailbox = simgrid::s4u::Mailbox::byName("0");
35     else
36       /* The others processes send to their right neighbor (rank+1) */
37       neighbor_mailbox = simgrid::s4u::Mailbox::byName(std::to_string(rank + 1));
38
39     if (rank == 0) {
40       /* The root process (rank 0) first sends the token then waits to receive it back */
41       XBT_INFO("Host \"%u\" send 'Token' to Host \"%s\"", rank, neighbor_mailbox->get_cname());
42       std::string msg = "Token";
43       neighbor_mailbox->put(&msg, task_comm_size);
44       std::string* res = static_cast<std::string*>(my_mailbox->get());
45       XBT_INFO("Host \"%u\" received \"%s\"", rank, res->c_str());
46     } else {
47       std::string* res = static_cast<std::string*>(my_mailbox->get());
48       XBT_INFO("Host \"%u\" received \"%s\"", rank, res->c_str());
49       XBT_INFO("Host \"%u\" send 'Token' to Host \"%s\"", rank, neighbor_mailbox->get_cname());
50       neighbor_mailbox->put(res, task_comm_size);
51     }
52   }
53 };
54
55 int main(int argc, char** argv)
56 {
57   simgrid::s4u::Engine e(&argc, argv);
58   xbt_assert(argc > 1, "Usage: %s platform.xml\n", argv[0]);
59   e.load_platform(argv[1]);
60
61   XBT_INFO("Number of hosts '%zu'", e.get_host_count());
62   int id = 0;
63   std::vector<simgrid::s4u::Host*> list = e.get_all_hosts();
64   for (auto const& host : list) {
65     /* - Give a unique rank to each host and create a @ref relay_runner process on each */
66     simgrid::s4u::Actor::create((std::to_string(id)).c_str(), host, RelayRunner());
67     id++;
68   }
69   e.run();
70   XBT_INFO("Simulation time %g", e.getClock());
71
72   return 0;
73 }