Logo AND Algorithmique Numérique Distribuée

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