Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c15f27363d51e72baa932aa2c8a370ebc170736b
[simgrid.git] / include / simgrid / smpi / smpi_replay.hpp
1 /* Copyright (c) 2009-2021. 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/ex.h>
12 #include <xbt/replay.hpp>
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 unsigned char* smpi_get_tmp_sendbuffer(size_t size);
35 XBT_PRIVATE unsigned char* smpi_get_tmp_recvbuffer(size_t size);
36
37 XBT_PRIVATE void log_timed_action(const simgrid::xbt::ReplayAction& action, double clock);
38
39 namespace simgrid {
40 namespace smpi {
41 namespace replay {
42 extern MPI_Datatype MPI_DEFAULT_TYPE;
43
44 class RequestStorage; // Forward decl
45
46 /**
47  * Base class for all parsers.
48  */
49 class ActionArgParser {
50 public:
51   virtual ~ActionArgParser() = default;
52   virtual void parse(xbt::ReplayAction& action, const std::string& name) { CHECK_ACTION_PARAMS(action, 0, 0) }
53 };
54
55 class WaitTestParser : public ActionArgParser {
56 public:
57   int src;
58   int dst;
59   int tag;
60
61   void parse(xbt::ReplayAction& action, const std::string& name) override;
62 };
63
64 class SendRecvParser : public ActionArgParser {
65 public:
66   /* communication partner; if we send, this is the receiver and vice versa */
67   int partner;
68   size_t size;
69   int tag;
70   MPI_Datatype datatype1;
71
72   void parse(xbt::ReplayAction& action, const std::string& name) override;
73 };
74
75 class ComputeParser : public ActionArgParser {
76 public:
77   double flops;
78
79   void parse(xbt::ReplayAction& action, const std::string& name) override;
80 };
81
82 class SleepParser : public ActionArgParser {
83 public:
84   double time;
85
86   void parse(xbt::ReplayAction& action, const std::string& name) override;
87 };
88
89 class LocationParser : public ActionArgParser {
90 public:
91   std::string filename;
92   int line;
93
94   void parse(xbt::ReplayAction& action, const std::string& name) override;
95 };
96
97 class CollCommParser : public ActionArgParser {
98 public:
99   size_t size;
100   double comp_size;
101   int send_size;
102   int recv_size;
103   unsigned comm_size; // size of communicator
104   int root;
105   MPI_Datatype datatype1;
106   MPI_Datatype datatype2;
107 };
108
109 class BcastArgParser : public CollCommParser {
110 public:
111   void parse(xbt::ReplayAction& action, const std::string& name) override;
112 };
113
114 class ReduceArgParser : public CollCommParser {
115 public:
116   void parse(xbt::ReplayAction& action, const std::string& name) override;
117 };
118
119 class AllReduceArgParser : public CollCommParser {
120 public:
121   void parse(xbt::ReplayAction& action, const std::string& name) override;
122 };
123
124 class AllToAllArgParser : public CollCommParser {
125 public:
126   void parse(xbt::ReplayAction& action, const std::string& name) override;
127 };
128
129 class GatherArgParser : public CollCommParser {
130 public:
131   void parse(xbt::ReplayAction& action, const std::string& name) override;
132 };
133
134 class GatherVArgParser : public CollCommParser {
135 public:
136   int recv_size_sum;
137   std::shared_ptr<std::vector<int>> recvcounts;
138   std::vector<int> disps;
139   void parse(xbt::ReplayAction& action, const std::string& name) override;
140 };
141
142 class ScatterArgParser : public CollCommParser {
143 public:
144   void parse(xbt::ReplayAction& action, const std::string& name) override;
145 };
146
147 class ScatterVArgParser : public CollCommParser {
148 public:
149   int recv_size_sum;
150   int send_size_sum;
151   std::shared_ptr<std::vector<int>> sendcounts;
152   std::vector<int> disps;
153   void parse(xbt::ReplayAction& action, const std::string& name) override;
154 };
155
156 class ReduceScatterArgParser : public CollCommParser {
157 public:
158   int recv_size_sum;
159   std::shared_ptr<std::vector<int>> recvcounts;
160   std::vector<int> disps;
161   void parse(xbt::ReplayAction& action, const std::string& name) override;
162 };
163
164 class ScanArgParser : public CollCommParser {
165 public:
166   void parse(xbt::ReplayAction& action, const std::string& name) override;
167 };
168
169 class AllToAllVArgParser : public CollCommParser {
170 public:
171   int recv_size_sum;
172   int send_size_sum;
173   std::shared_ptr<std::vector<int>> recvcounts;
174   std::shared_ptr<std::vector<int>> sendcounts;
175   std::vector<int> senddisps;
176   std::vector<int> recvdisps;
177   int send_buf_size;
178   int recv_buf_size;
179   void parse(xbt::ReplayAction& action, const std::string& name) override;
180 };
181
182 /**
183  * Base class for all ReplayActions.
184  * Note that this class actually implements the behavior of each action
185  * while the parsing of the replay arguments is done in the @ref ActionArgParser class.
186  * In other words: The logic goes here, the setup is done by the ActionArgParser.
187  */
188 template <class T> class ReplayAction {
189   const std::string name_;
190   const aid_t my_proc_id_ = s4u::this_actor::get_pid();
191   T args_;
192
193 protected:
194   const std::string& get_name() const { return name_; }
195   aid_t get_pid() const { return my_proc_id_; }
196   const T& get_args() const { return args_; }
197
198 public:
199   explicit ReplayAction(const std::string& name) : name_(name) {}
200   virtual ~ReplayAction() = default;
201
202   void execute(xbt::ReplayAction& action)
203   {
204     // Needs to be re-initialized for every action, hence here
205     double start_time = smpi_process()->simulated_elapsed();
206     args_.parse(action, name_);
207     kernel(action);
208     if (name_ != "Init")
209       log_timed_action(action, start_time);
210   }
211
212   virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
213   unsigned char* send_buffer(size_t size) { return smpi_get_tmp_sendbuffer(size); }
214   unsigned char* recv_buffer(size_t size) { return smpi_get_tmp_recvbuffer(size); }
215 };
216
217 class WaitAction : public ReplayAction<WaitTestParser> {
218   RequestStorage& req_storage;
219
220 public:
221   explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
222   void kernel(xbt::ReplayAction& action) override;
223 };
224
225 class SendAction : public ReplayAction<SendRecvParser> {
226   RequestStorage& req_storage;
227
228 public:
229   explicit SendAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
230   void kernel(xbt::ReplayAction& action) override;
231 };
232
233 class RecvAction : public ReplayAction<SendRecvParser> {
234   RequestStorage& req_storage;
235
236 public:
237   explicit RecvAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
238   void kernel(xbt::ReplayAction& action) override;
239 };
240
241 class ComputeAction : public ReplayAction<ComputeParser> {
242 public:
243   explicit ComputeAction() : ReplayAction("compute") {}
244   void kernel(xbt::ReplayAction& action) override;
245 };
246
247 class SleepAction : public ReplayAction<SleepParser> {
248 public:
249   explicit SleepAction() : ReplayAction("sleep") {}
250   void kernel(xbt::ReplayAction& action) override;
251 };
252
253 class LocationAction : public ReplayAction<LocationParser> {
254 public:
255   explicit LocationAction() : ReplayAction("location") {}
256   void kernel(xbt::ReplayAction& action) override;
257 };
258
259 class TestAction : public ReplayAction<WaitTestParser> {
260 private:
261   RequestStorage& req_storage;
262
263 public:
264   explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
265   void kernel(xbt::ReplayAction& action) override;
266 };
267
268 class InitAction : public ReplayAction<ActionArgParser> {
269 public:
270   explicit InitAction() : ReplayAction("Init") {}
271   void kernel(xbt::ReplayAction& action) override;
272 };
273
274 class CommunicatorAction : public ReplayAction<ActionArgParser> {
275 public:
276   explicit CommunicatorAction() : ReplayAction("Comm") {}
277   void kernel(xbt::ReplayAction& action) override;
278 };
279
280 class WaitAllAction : public ReplayAction<ActionArgParser> {
281   RequestStorage& req_storage;
282
283 public:
284   explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
285   void kernel(xbt::ReplayAction& action) override;
286 };
287
288 class BarrierAction : public ReplayAction<ActionArgParser> {
289 public:
290   explicit BarrierAction() : ReplayAction("barrier") {}
291   void kernel(xbt::ReplayAction& action) override;
292 };
293
294 class BcastAction : public ReplayAction<BcastArgParser> {
295 public:
296   explicit BcastAction() : ReplayAction("bcast") {}
297   void kernel(xbt::ReplayAction& action) override;
298 };
299
300 class ReduceAction : public ReplayAction<ReduceArgParser> {
301 public:
302   explicit ReduceAction() : ReplayAction("reduce") {}
303   void kernel(xbt::ReplayAction& action) override;
304 };
305
306 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
307 public:
308   explicit AllReduceAction() : ReplayAction("allreduce") {}
309   void kernel(xbt::ReplayAction& action) override;
310 };
311
312 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
313 public:
314   explicit AllToAllAction() : ReplayAction("alltoall") {}
315   void kernel(xbt::ReplayAction& action) override;
316 };
317
318 class GatherAction : public ReplayAction<GatherArgParser> {
319 public:
320   using ReplayAction::ReplayAction;
321   void kernel(xbt::ReplayAction& action) override;
322 };
323
324 class GatherVAction : public ReplayAction<GatherVArgParser> {
325 public:
326   using ReplayAction::ReplayAction;
327   void kernel(xbt::ReplayAction& action) override;
328 };
329
330 class ScatterAction : public ReplayAction<ScatterArgParser> {
331 public:
332   explicit ScatterAction() : ReplayAction("scatter") {}
333   void kernel(xbt::ReplayAction& action) override;
334 };
335
336 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
337 public:
338   explicit ScatterVAction() : ReplayAction("scatterv") {}
339   void kernel(xbt::ReplayAction& action) override;
340 };
341
342 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
343 public:
344   explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
345   void kernel(xbt::ReplayAction& action) override;
346 };
347
348 class ScanAction : public ReplayAction<ScanArgParser> {
349 public:
350   using ReplayAction::ReplayAction;
351   void kernel(xbt::ReplayAction& action) override;
352 };
353
354 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
355 public:
356   explicit AllToAllVAction() : ReplayAction("alltoallv") {}
357   void kernel(xbt::ReplayAction& action) override;
358 };
359
360 } // namespace replay
361 } // namespace smpi
362 } // namespace simgrid
363
364 #endif