Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
snake_case s4u::Mailbox
[simgrid.git] / examples / s4u / replay-comm / s4u-replay-comm.cpp
1 /* Copyright (c) 2009-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 "xbt/replay.hpp"
8 #include "xbt/str.h"
9 #include <boost/algorithm/string/join.hpp>
10 #include <string>
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(replay_comm, "Messages specific for this msg example");
13
14 #define ACT_DEBUG(...)                                                                                                 \
15   if (XBT_LOG_ISENABLED(replay_comm, xbt_log_priority_verbose)) {                                                   \
16     std::string NAME = boost::algorithm::join(action, " ");                                                            \
17     XBT_DEBUG(__VA_ARGS__);                                                                                            \
18   } else                                                                                                               \
19   ((void)0)
20
21 static void log_action(simgrid::xbt::ReplayAction& action, double date)
22 {
23   if (XBT_LOG_ISENABLED(replay_comm, xbt_log_priority_verbose)) {
24     std::string s = boost::algorithm::join(action, " ");
25     XBT_VERB("%s %f", s.c_str(), date);
26   }
27 }
28
29 class Replayer {
30 public:
31   explicit Replayer(std::vector<std::string> args)
32   {
33     int argc;
34     char* argv[2];
35     argv[0] = &args.at(0)[0];
36     if (args.size() == 1) {
37       argc = 1;
38     } else {
39       argc    = 2;
40       argv[1] = &args.at(1)[0];
41     }
42     simgrid::xbt::replay_runner(argc, argv);
43   }
44
45   void operator()()
46   {
47     // Nothing to do here
48   }
49
50   /* My actions */
51   static void compute(simgrid::xbt::ReplayAction& action)
52   {
53     double amount = std::stod(action[2]);
54     double clock  = simgrid::s4u::Engine::get_clock();
55     ACT_DEBUG("Entering %s", NAME.c_str());
56     simgrid::s4u::this_actor::execute(amount);
57     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
58   }
59
60   static void send(simgrid::xbt::ReplayAction& action)
61   {
62     double size                 = std::stod(action[3]);
63     std::string* payload        = new std::string(action[3]);
64     double clock                = simgrid::s4u::Engine::get_clock();
65     simgrid::s4u::MailboxPtr to =
66         simgrid::s4u::Mailbox::by_name(simgrid::s4u::this_actor::get_name() + "_" + action[2]);
67     ACT_DEBUG("Entering Send: %s (size: %g) -- Actor %s on mailbox %s", NAME.c_str(), size,
68               simgrid::s4u::this_actor::get_cname(), to->get_cname());
69     to->put(payload, size);
70     delete payload;
71
72     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
73   }
74
75   static void recv(simgrid::xbt::ReplayAction& action)
76   {
77     double clock = simgrid::s4u::Engine::get_clock();
78     simgrid::s4u::MailboxPtr from =
79         simgrid::s4u::Mailbox::by_name(std::string(action[2]) + "_" + simgrid::s4u::this_actor::get_name());
80
81     ACT_DEBUG("Receiving: %s -- Actor %s on mailbox %s", NAME.c_str(), simgrid::s4u::this_actor::get_cname(),
82               from->get_cname());
83     from->get();
84     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
85   }
86 };
87
88 int main(int argc, char* argv[])
89 {
90   simgrid::s4u::Engine e(&argc, argv);
91
92   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file [action_files]\n"
93                        "\t# if all actions are in the same file\n"
94                        "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
95                        "\t# if actions are in separate files, specified in deployment\n"
96                        "\tExample: %s msg_platform.xml msg_deployment.xml ",
97              argv[0], argv[0], argv[0]);
98
99   e.load_platform(argv[1]);
100   e.register_default(&simgrid::xbt::replay_runner);
101   e.register_actor<Replayer>("p0");
102   e.register_actor<Replayer>("p1");
103   e.load_deployment(argv[2]);
104
105   /*   Action registration */
106   xbt_replay_action_register("compute", Replayer::compute);
107   xbt_replay_action_register("send", Replayer::send);
108   xbt_replay_action_register("recv", Replayer::recv);
109
110   if (argv[3]) {
111     simgrid::xbt::action_fs = new std::ifstream(argv[3], std::ifstream::in);
112   }
113
114   e.run();
115
116   if (argv[3]) {
117     delete simgrid::xbt::action_fs;
118     simgrid::xbt::action_fs = nullptr;
119   }
120
121   XBT_INFO("Simulation time %g", e.get_clock());
122
123   return 0;
124 }