Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
303529be929f7e2329fda29c2af062b0d14b6df8
[simgrid.git] / examples / s4u / app-token-ring / s4u_app-token-ring.cpp
1 /* Copyright (c) 2017. 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 "xbt/str.h"
7 #include <simgrid/s4u.hpp>
8 #include <algorithm>
9 #include <string>
10 #include <map>
11 #include <vector>
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_token_ring, "Messages specific for this s4u example");
14 // FIXME: The following duplicates the content of s4u::Host
15 extern std::map<std::string, simgrid::s4u::Host*> host_list;
16
17 class RelayRunner {
18   size_t task_comm_size = 1000000; /* The token is 1MB long*/
19   simgrid::s4u::MailboxPtr my_mailbox;
20   simgrid::s4u::MailboxPtr neighbor_mailbox;
21   unsigned int rank      = 0;
22
23 public:
24   explicit RelayRunner() = default;
25
26   void operator()()
27   {
28     rank = xbt_str_parse_int(simgrid::s4u::this_actor::name().c_str(),
29                              "Any process of this example must have a numerical name, not %s");
30     my_mailbox = simgrid::s4u::Mailbox::byName(std::to_string(rank));
31     if (rank + 1 == host_list.size())
32       /* The last process, which sends the token back to rank 0 */
33       neighbor_mailbox = simgrid::s4u::Mailbox::byName("0");
34     else
35       /* The others processes send to their right neighbor (rank+1) */
36       neighbor_mailbox = simgrid::s4u::Mailbox::byName(std::to_string(rank + 1));
37
38     if (rank == 0) {
39       /* The root process (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->name());
41       simgrid::s4u::this_actor::send(neighbor_mailbox, xbt_strdup("Token"), task_comm_size);
42       char* res = static_cast<char*>(simgrid::s4u::this_actor::recv(my_mailbox));
43       XBT_INFO("Host \"%u\" received \"%s\"", rank, res);
44       xbt_free(res);
45     } else {
46       char* res = static_cast<char*>(simgrid::s4u::this_actor::recv(my_mailbox));
47       XBT_INFO("Host \"%u\" received \"%s\"", rank, res);
48       XBT_INFO("Host \"%u\" send 'Token' to Host \"%s\"", rank, neighbor_mailbox->name());
49       simgrid::s4u::this_actor::send(neighbor_mailbox, res, task_comm_size);
50     }
51   }
52 };
53
54 int main(int argc, char** argv)
55 {
56   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
57   xbt_assert(argc > 1, "Usage: %s platform.xml\n", argv[0]);
58   e->loadPlatform(argv[1]);
59
60   XBT_INFO("Number of hosts '%zu'", host_list.size());
61   int id = 0;
62   for (auto h : host_list) {
63     simgrid::s4u::Host* host = h.second;
64     /* - Give a unique rank to each host and create a @ref relay_runner process on each */
65     simgrid::s4u::Actor::createActor((std::to_string(id)).c_str(), host, RelayRunner());
66     id++;
67   }
68   e->run();
69   XBT_INFO("Simulation time %g", e->getClock());
70   return 0;
71 }