Logo AND Algorithmique Numérique Distribuée

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