1 /* Copyright (c) 2017. The SimGrid Team. All rights reserved. */
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. */
7 #include <simgrid/s4u.hpp>
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;
19 size_t task_comm_size = 1000000; /* The token is 1MB long*/
20 simgrid::s4u::MailboxPtr my_mailbox;
21 simgrid::s4u::MailboxPtr neighbor_mailbox;
22 unsigned int rank = 0;
23 explicit RelayRunner() = default;
27 rank = xbt_str_parse_int(simgrid::s4u::this_actor::name().c_str(),
28 "Any process of this example must have a numerical name, not %s");
29 my_mailbox = simgrid::s4u::Mailbox::byName((std::to_string(rank)).c_str());
30 if (rank + 1 == host_list.size())
31 /* The last process, which sends the token back to rank 0 */
32 neighbor_mailbox = simgrid::s4u::Mailbox::byName("0");
34 /* The others processes send to their right neighbor (rank+1) */
35 neighbor_mailbox = simgrid::s4u::Mailbox::byName((std::to_string(rank + 1)).c_str());
38 /* The root process (rank 0) first sends the token then waits to receive it back */
39 XBT_INFO("Host \"%u\" send 'Token' to Host \"%s\"", rank, neighbor_mailbox->name());
40 simgrid::s4u::this_actor::send(neighbor_mailbox, xbt_strdup("Token"), task_comm_size);
41 char* res = static_cast<char*>(simgrid::s4u::this_actor::recv(my_mailbox));
42 XBT_INFO("Host \"%u\" received \"%s\"", rank, res);
44 char* res = static_cast<char*>(simgrid::s4u::this_actor::recv(my_mailbox));
45 XBT_INFO("Host \"%u\" received \"%s\"", rank, res);
46 XBT_INFO("Host \"%u\" send 'Token' to Host \"%s\"", rank, neighbor_mailbox->name());
47 simgrid::s4u::this_actor::send(neighbor_mailbox, res, task_comm_size);
52 int main(int argc, char** argv)
54 simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
55 xbt_assert(argc > 1, "Usage: %s platform.xml\n", argv[0]);
56 e->loadPlatform(argv[1]);
58 XBT_INFO("Number of hosts '%zu'", host_list.size());
60 for (auto h : host_list) {
61 simgrid::s4u::Host* host = h.second;
62 /* - Give a unique rank to each host and create a @ref relay_runner process on each */
63 simgrid::s4u::Actor::createActor((std::to_string(id)).c_str(), host, RelayRunner());
67 XBT_INFO("Simulation time %g", e->getClock());