Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3 more smells
[simgrid.git] / examples / s4u / actions-comm / s4u_actions-comm.cpp
1 /* Copyright (c) 2009-2016. 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
10 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
11
12 #define ACT_DEBUG(...) \
13   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {  \
14     char *NAME = xbt_str_join_array(action, " ");              \
15     XBT_DEBUG(__VA_ARGS__);                                    \
16     xbt_free(NAME);                                            \
17   } else ((void)0)
18
19 static void log_action(const char *const *action, double date)
20 {
21   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
22     char *name = xbt_str_join_array(action, " ");
23     XBT_VERB("%s %f", name, date);
24     xbt_free(name);
25   }
26 }
27
28 class Replayer {
29 public:
30   explicit Replayer(std::vector<std::string> args)
31   {
32     int argc;
33     char* argv[2];
34     argv[0] = &args.at(0)[0];
35     if (args.size() == 1) {
36       argc = 1;
37     } else {
38       argc    = 2;
39       argv[1] = &args.at(1)[0];
40     }
41     xbt_replay_action_runner(argc, argv);
42   }
43
44   void operator()()
45   {
46     // Nothing to do here
47   }
48
49   /* My actions */
50   static void compute(const char* const* action)
51   {
52     double amount = std::stod(action[2]);
53     double clock  = simgrid::s4u::Engine::getClock();
54     ACT_DEBUG("Entering %s", NAME);
55     simgrid::s4u::this_actor::execute(amount);
56     log_action(action, simgrid::s4u::Engine::getClock() - clock);
57   }
58
59   static void send(const char* const* action)
60   {
61     double size                 = std::stod(action[3]);
62     char* payload               = xbt_strdup(action[3]);
63     double clock                = simgrid::s4u::Engine::getClock();
64     simgrid::s4u::MailboxPtr to = simgrid::s4u::Mailbox::byName(simgrid::s4u::this_actor::name() + "_" + action[2]);
65     ACT_DEBUG("Entering Send: %s (size: %g) -- Actor %s on mailbox %s", NAME, size,
66               simgrid::s4u::this_actor::name().c_str(), to->name());
67     simgrid::s4u::this_actor::send(to, payload, size);
68     xbt_free(payload);
69
70     log_action(action, simgrid::s4u::Engine::getClock() - clock);
71   }
72
73   static void recv(const char* const* action)
74   {
75     double clock = simgrid::s4u::Engine::getClock();
76     simgrid::s4u::MailboxPtr from =
77         simgrid::s4u::Mailbox::byName(std::string(action[2]) + "_" + simgrid::s4u::this_actor::name());
78
79     ACT_DEBUG("Receiving: %s -- Actor %s on mailbox %s", NAME, simgrid::s4u::this_actor::name().c_str(), from->name());
80     simgrid::s4u::this_actor::recv(from);
81     log_action(action, simgrid::s4u::Engine::getClock() - clock);
82   }
83 };
84
85 int main(int argc, char *argv[])
86 {
87   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
88   /* Explicit initialization of the action module is required now*/
89   _xbt_replay_action_init();
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(&xbt_replay_action_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   /* Actually do the simulation using MSG_action_trace_run */
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->getClock());
122
123   _xbt_replay_action_exit(); /* Explicit finalization of the action module */
124
125   return 0;
126 }