1 /* Copyright (c) 2017-2021. 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. */
6 #include <simgrid/s4u.hpp>
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_token_ring, "Messages specific for this s4u example");
16 explicit RelayRunner() = default;
18 void operator()() const
20 size_t token_size = 1000000; /* The token is 1MB long*/
21 simgrid::s4u::Mailbox* my_mailbox;
22 simgrid::s4u::Mailbox* neighbor_mailbox;
23 unsigned int rank = 0;
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());
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");
35 /* The others actors send to their right neighbor (rank+1) */
36 neighbor_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(rank + 1));
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 = my_mailbox->get<std::string>();
44 XBT_INFO("Host \"%u\" received \"%s\"", rank, res->c_str());
46 auto* res = my_mailbox->get<std::string>();
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);
54 int main(int argc, char** argv)
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]);
60 XBT_INFO("Number of hosts '%zu'", e.get_host_count());
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());
69 XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());