Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Whenever possible, use std::move() for parameters (mostly std::string).
[simgrid.git] / include / simgrid / smpi / replay.hpp
1 /* Copyright (c) 2009-2019. 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 #ifndef SMPI_REPLAY_HPP_
6 #define SMPI_REPLAY_HPP_
7
8 #include "src/smpi/include/smpi_actor.hpp"
9
10 #include <boost/algorithm/string/join.hpp>
11 #include <xbt/replay.hpp>
12 #include <xbt/ex.h>
13
14 #include <memory>
15 #include <sstream>
16
17 #define CHECK_ACTION_PARAMS(action, mandatory, optional)                                                               \
18   {                                                                                                                    \
19     if (action.size() < static_cast<unsigned long>(mandatory + 2)) {                                                   \
20       std::stringstream ss;                                                                                            \
21       ss << __func__ << " replay failed.\n"                                                                            \
22          << action.size() << " items were given on the line. First two should be process_id and action.  "             \
23          << "This action needs after them " << mandatory << " mandatory arguments, and accepts " << optional           \
24          << " optional ones. \n"                                                                                       \
25          << "The full line that was given is:\n   ";                                                                   \
26       for (const auto& elem : action) {                                                                                \
27         ss << elem << " ";                                                                                             \
28       }                                                                                                                \
29       ss << "\nPlease contact the Simgrid team if support is needed";                                                  \
30       throw std::invalid_argument(ss.str());                                                                           \
31     }                                                                                                                  \
32   }
33
34 XBT_PRIVATE void* smpi_get_tmp_sendbuffer(int size);
35 XBT_PRIVATE void* smpi_get_tmp_recvbuffer(int size);
36 XBT_PRIVATE void smpi_free_tmp_buffer(void* buf);
37 XBT_PRIVATE void smpi_free_replay_tmp_buffers();
38
39 XBT_PRIVATE void log_timed_action(simgrid::xbt::ReplayAction& action, double clock);
40
41 namespace simgrid {
42 namespace smpi {
43 namespace replay {
44 extern MPI_Datatype MPI_DEFAULT_TYPE;
45
46 class RequestStorage; // Forward decl
47
48 /**
49  * Base class for all parsers.
50  */
51 class ActionArgParser {
52 public:
53   virtual ~ActionArgParser() = default;
54   virtual void parse(simgrid::xbt::ReplayAction& action, std::string name) { CHECK_ACTION_PARAMS(action, 0, 0) }
55 };
56
57 class WaitTestParser : public ActionArgParser {
58 public:
59   int src;
60   int dst;
61   int tag;
62
63   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
64 };
65
66 class SendRecvParser : public ActionArgParser {
67 public:
68   /* communication partner; if we send, this is the receiver and vice versa */
69   int partner;
70   double size;
71   int tag;
72   MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
73
74   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
75 };
76
77 class ComputeParser : public ActionArgParser {
78 public:
79   /* communication partner; if we send, this is the receiver and vice versa */
80   double flops;
81
82   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
83 };
84
85 class CollCommParser : public ActionArgParser {
86 public:
87   double size;
88   double comm_size;
89   double comp_size;
90   int send_size;
91   int recv_size;
92   int root = 0;
93   MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
94   MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
95 };
96
97 class BcastArgParser : public CollCommParser {
98 public:
99   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
100 };
101
102 class ReduceArgParser : public CollCommParser {
103 public:
104   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
105 };
106
107 class AllReduceArgParser : public CollCommParser {
108 public:
109   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
110 };
111
112 class AllToAllArgParser : public CollCommParser {
113 public:
114   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
115 };
116
117 class GatherArgParser : public CollCommParser {
118 public:
119   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
120 };
121
122 class GatherVArgParser : public CollCommParser {
123 public:
124   int recv_size_sum;
125   std::shared_ptr<std::vector<int>> recvcounts;
126   std::vector<int> disps;
127   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
128 };
129
130 class ScatterArgParser : public CollCommParser {
131 public:
132   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
133 };
134
135 class ScatterVArgParser : public CollCommParser {
136 public:
137   int recv_size_sum;
138   int send_size_sum;
139   std::shared_ptr<std::vector<int>> sendcounts;
140   std::vector<int> disps;
141   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
142 };
143
144 class ReduceScatterArgParser : public CollCommParser {
145 public:
146   int recv_size_sum;
147   std::shared_ptr<std::vector<int>> recvcounts;
148   std::vector<int> disps;
149   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
150 };
151
152 class AllToAllVArgParser : public CollCommParser {
153 public:
154   int recv_size_sum;
155   int send_size_sum;
156   std::shared_ptr<std::vector<int>> recvcounts;
157   std::shared_ptr<std::vector<int>> sendcounts;
158   std::vector<int> senddisps;
159   std::vector<int> recvdisps;
160   int send_buf_size;
161   int recv_buf_size;
162   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
163 };
164
165 /**
166  * Base class for all ReplayActions.
167  * Note that this class actually implements the behavior of each action
168  * while the parsing of the replay arguments is done in the @ref ActionArgParser class.
169  * In other words: The logic goes here, the setup is done by the ActionArgParser.
170  */
171 template <class T> class ReplayAction {
172 protected:
173   const std::string name;
174   const aid_t my_proc_id;
175   T args;
176
177 public:
178   explicit ReplayAction(std::string name) : name(std::move(name)), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
179   virtual ~ReplayAction() = default;
180
181   void execute(simgrid::xbt::ReplayAction& action)
182   {
183     // Needs to be re-initialized for every action, hence here
184     double start_time = smpi_process()->simulated_elapsed();
185     args.parse(action, name);
186     kernel(action);
187     if (name != "Init")
188       log_timed_action(action, start_time);
189   }
190
191   virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
192   void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
193   void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
194 };
195
196 class WaitAction : public ReplayAction<WaitTestParser> {
197 private:
198   RequestStorage& req_storage;
199
200 public:
201   explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
202   void kernel(simgrid::xbt::ReplayAction& action) override;
203 };
204
205 class SendAction : public ReplayAction<SendRecvParser> {
206 private:
207   RequestStorage& req_storage;
208
209 public:
210   explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(std::move(name)), req_storage(storage)
211   {
212   }
213   void kernel(simgrid::xbt::ReplayAction& action) override;
214 };
215
216 class RecvAction : public ReplayAction<SendRecvParser> {
217 private:
218   RequestStorage& req_storage;
219
220 public:
221   explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(std::move(name)), req_storage(storage)
222   {
223   }
224   void kernel(simgrid::xbt::ReplayAction& action) override;
225 };
226
227 class ComputeAction : public ReplayAction<ComputeParser> {
228 public:
229   explicit ComputeAction() : ReplayAction("compute") {}
230   void kernel(simgrid::xbt::ReplayAction& action) override;
231 };
232
233 class TestAction : public ReplayAction<WaitTestParser> {
234 private:
235   RequestStorage& req_storage;
236
237 public:
238   explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
239   void kernel(simgrid::xbt::ReplayAction& action) override;
240 };
241
242 class InitAction : public ReplayAction<ActionArgParser> {
243 public:
244   explicit InitAction() : ReplayAction("Init") {}
245   void kernel(simgrid::xbt::ReplayAction& action) override;
246 };
247
248 class CommunicatorAction : public ReplayAction<ActionArgParser> {
249 public:
250   explicit CommunicatorAction() : ReplayAction("Comm") {}
251   void kernel(simgrid::xbt::ReplayAction& action) override;
252 };
253
254 class WaitAllAction : public ReplayAction<ActionArgParser> {
255 private:
256   RequestStorage& req_storage;
257
258 public:
259   explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
260   void kernel(simgrid::xbt::ReplayAction& action) override;
261 };
262
263 class BarrierAction : public ReplayAction<ActionArgParser> {
264 public:
265   explicit BarrierAction() : ReplayAction("barrier") {}
266   void kernel(simgrid::xbt::ReplayAction& action) override;
267 };
268
269 class BcastAction : public ReplayAction<BcastArgParser> {
270 public:
271   explicit BcastAction() : ReplayAction("bcast") {}
272   void kernel(simgrid::xbt::ReplayAction& action) override;
273 };
274
275 class ReduceAction : public ReplayAction<ReduceArgParser> {
276 public:
277   explicit ReduceAction() : ReplayAction("reduce") {}
278   void kernel(simgrid::xbt::ReplayAction& action) override;
279 };
280
281 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
282 public:
283   explicit AllReduceAction() : ReplayAction("allreduce") {}
284   void kernel(simgrid::xbt::ReplayAction& action) override;
285 };
286
287 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
288 public:
289   explicit AllToAllAction() : ReplayAction("alltoall") {}
290   void kernel(simgrid::xbt::ReplayAction& action) override;
291 };
292
293 class GatherAction : public ReplayAction<GatherArgParser> {
294 public:
295   explicit GatherAction(std::string name) : ReplayAction(std::move(name)) {}
296   void kernel(simgrid::xbt::ReplayAction& action) override;
297 };
298
299 class GatherVAction : public ReplayAction<GatherVArgParser> {
300 public:
301   explicit GatherVAction(std::string name) : ReplayAction(std::move(name)) {}
302   void kernel(simgrid::xbt::ReplayAction& action) override;
303 };
304
305 class ScatterAction : public ReplayAction<ScatterArgParser> {
306 public:
307   explicit ScatterAction() : ReplayAction("scatter") {}
308   void kernel(simgrid::xbt::ReplayAction& action) override;
309 };
310
311 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
312 public:
313   explicit ScatterVAction() : ReplayAction("scatterv") {}
314   void kernel(simgrid::xbt::ReplayAction& action) override;
315 };
316
317 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
318 public:
319   explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
320   void kernel(simgrid::xbt::ReplayAction& action) override;
321 };
322
323 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
324 public:
325   explicit AllToAllVAction() : ReplayAction("alltoallv") {}
326   void kernel(simgrid::xbt::ReplayAction& action) override;
327 };
328 }
329 }
330 }
331
332 #endif