Logo AND Algorithmique Numérique Distribuée

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