Logo AND Algorithmique Numérique Distribuée

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