Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove a teshsuite example that is superseeded by a proper example
[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
15 class RelayRunner {
16   size_t task_comm_size = 1000000; /* The token is 1MB long*/
17   simgrid::s4u::MailboxPtr my_mailbox;
18   simgrid::s4u::MailboxPtr neighbor_mailbox;
19   unsigned int rank      = 0;
20
21 public:
22   explicit RelayRunner() = default;
23
24   void operator()()
25   {
26     try {
27       rank = std::stoi(simgrid::s4u::this_actor::getName());
28     } catch (std::invalid_argument& ia) {
29       throw std::invalid_argument(std::string("Processes of this example must have a numerical name, not ") +
30                                   ia.what());
31     }
32     my_mailbox = simgrid::s4u::Mailbox::byName(std::to_string(rank));
33     if (rank + 1 == simgrid::s4u::Engine::getInstance()->getHostCount())
34       /* The last process, which sends the token back to rank 0 */
35       neighbor_mailbox = simgrid::s4u::Mailbox::byName("0");
36     else
37       /* The others processes send to their right neighbor (rank+1) */
38       neighbor_mailbox = simgrid::s4u::Mailbox::byName(std::to_string(rank + 1));
39
40     if (rank == 0) {
41       /* The root process (rank 0) first sends the token then waits to receive it back */
42       XBT_INFO("Host \"%u\" send 'Token' to Host \"%s\"", rank, neighbor_mailbox->getName());
43       neighbor_mailbox->put(xbt_strdup("Token"), task_comm_size);
44       char* res = static_cast<char*>(my_mailbox->get());
45       XBT_INFO("Host \"%u\" received \"%s\"", rank, res);
46       xbt_free(res);
47     } else {
48       char* res = static_cast<char*>(my_mailbox->get());
49       XBT_INFO("Host \"%u\" received \"%s\"", rank, res);
50       XBT_INFO("Host \"%u\" send 'Token' to Host \"%s\"", rank, neighbor_mailbox->getName());
51       neighbor_mailbox->put(res, task_comm_size);
52     }
53   }
54 };
55
56 int main(int argc, char** argv)
57 {
58   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
59   xbt_assert(argc > 1, "Usage: %s platform.xml\n", argv[0]);
60   e->loadPlatform(argv[1]);
61
62   XBT_INFO("Number of hosts '%zu'", e->getHostCount());
63   int id = 0;
64   std::vector<simgrid::s4u::Host*> list;
65   e->getHostList(&list);
66   for (auto const& host : list) {
67     /* - Give a unique rank to each host and create a @ref relay_runner process on each */
68     simgrid::s4u::Actor::createActor((std::to_string(id)).c_str(), host, RelayRunner());
69     id++;
70   }
71   e->run();
72   XBT_INFO("Simulation time %g", e->getClock());
73
74   delete e;
75   return 0;
76 }