Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Make member variables "private".
[simgrid.git] / include / simgrid / smpi / replay.hpp
1 /* Copyright (c) 2009-2020. 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 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   double size;
69   int tag;
70   MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
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   double size;
100   double comm_size;
101   double comp_size;
102   int send_size;
103   int recv_size;
104   int root = 0;
105   MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
106   MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
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 AllToAllVArgParser : public CollCommParser {
165 public:
166   int recv_size_sum;
167   int send_size_sum;
168   std::shared_ptr<std::vector<int>> recvcounts;
169   std::shared_ptr<std::vector<int>> sendcounts;
170   std::vector<int> senddisps;
171   std::vector<int> recvdisps;
172   int send_buf_size;
173   int recv_buf_size;
174   void parse(xbt::ReplayAction& action, const std::string& name) override;
175 };
176
177 /**
178  * Base class for all ReplayActions.
179  * Note that this class actually implements the behavior of each action
180  * while the parsing of the replay arguments is done in the @ref ActionArgParser class.
181  * In other words: The logic goes here, the setup is done by the ActionArgParser.
182  */
183 template <class T> class ReplayAction {
184   const std::string name_;
185   const aid_t my_proc_id_ = s4u::this_actor::get_pid();
186   T args_;
187
188 protected:
189   const std::string& get_name() const { return name_; }
190   aid_t get_pid() const { return my_proc_id_; }
191   const T& get_args() const { return args_; }
192
193 public:
194   explicit ReplayAction(const std::string& name) : name_(name) {}
195   virtual ~ReplayAction() = default;
196
197   void execute(xbt::ReplayAction& action)
198   {
199     // Needs to be re-initialized for every action, hence here
200     double start_time = smpi_process()->simulated_elapsed();
201     args_.parse(action, name_);
202     kernel(action);
203     if (name_ != "Init")
204       log_timed_action(action, start_time);
205   }
206
207   virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
208   unsigned char* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
209   unsigned char* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
210 };
211
212 class WaitAction : public ReplayAction<WaitTestParser> {
213   RequestStorage& req_storage;
214
215 public:
216   explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
217   void kernel(xbt::ReplayAction& action) override;
218 };
219
220 class SendAction : public ReplayAction<SendRecvParser> {
221   RequestStorage& req_storage;
222
223 public:
224   explicit SendAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
225   void kernel(xbt::ReplayAction& action) override;
226 };
227
228 class RecvAction : public ReplayAction<SendRecvParser> {
229   RequestStorage& req_storage;
230
231 public:
232   explicit RecvAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
233   void kernel(xbt::ReplayAction& action) override;
234 };
235
236 class ComputeAction : public ReplayAction<ComputeParser> {
237 public:
238   explicit ComputeAction() : ReplayAction("compute") {}
239   void kernel(xbt::ReplayAction& action) override;
240 };
241
242 class SleepAction : public ReplayAction<SleepParser> {
243 public:
244   explicit SleepAction() : ReplayAction("sleep") {}
245   void kernel(xbt::ReplayAction& action) override;
246 };
247
248 class LocationAction : public ReplayAction<LocationParser> {
249 public:
250   explicit LocationAction() : ReplayAction("location") {}
251   void kernel(xbt::ReplayAction& action) override;
252 };
253
254 class TestAction : public ReplayAction<WaitTestParser> {
255 private:
256   RequestStorage& req_storage;
257
258 public:
259   explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
260   void kernel(xbt::ReplayAction& action) override;
261 };
262
263 class InitAction : public ReplayAction<ActionArgParser> {
264 public:
265   explicit InitAction() : ReplayAction("Init") {}
266   void kernel(xbt::ReplayAction& action) override;
267 };
268
269 class CommunicatorAction : public ReplayAction<ActionArgParser> {
270 public:
271   explicit CommunicatorAction() : ReplayAction("Comm") {}
272   void kernel(xbt::ReplayAction& action) override;
273 };
274
275 class WaitAllAction : public ReplayAction<ActionArgParser> {
276   RequestStorage& req_storage;
277
278 public:
279   explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
280   void kernel(xbt::ReplayAction& action) override;
281 };
282
283 class BarrierAction : public ReplayAction<ActionArgParser> {
284 public:
285   explicit BarrierAction() : ReplayAction("barrier") {}
286   void kernel(xbt::ReplayAction& action) override;
287 };
288
289 class BcastAction : public ReplayAction<BcastArgParser> {
290 public:
291   explicit BcastAction() : ReplayAction("bcast") {}
292   void kernel(xbt::ReplayAction& action) override;
293 };
294
295 class ReduceAction : public ReplayAction<ReduceArgParser> {
296 public:
297   explicit ReduceAction() : ReplayAction("reduce") {}
298   void kernel(xbt::ReplayAction& action) override;
299 };
300
301 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
302 public:
303   explicit AllReduceAction() : ReplayAction("allreduce") {}
304   void kernel(xbt::ReplayAction& action) override;
305 };
306
307 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
308 public:
309   explicit AllToAllAction() : ReplayAction("alltoall") {}
310   void kernel(xbt::ReplayAction& action) override;
311 };
312
313 class GatherAction : public ReplayAction<GatherArgParser> {
314 public:
315   explicit GatherAction(const std::string& name) : ReplayAction(name) {}
316   void kernel(xbt::ReplayAction& action) override;
317 };
318
319 class GatherVAction : public ReplayAction<GatherVArgParser> {
320 public:
321   explicit GatherVAction(const std::string& name) : ReplayAction(name) {}
322   void kernel(xbt::ReplayAction& action) override;
323 };
324
325 class ScatterAction : public ReplayAction<ScatterArgParser> {
326 public:
327   explicit ScatterAction() : ReplayAction("scatter") {}
328   void kernel(xbt::ReplayAction& action) override;
329 };
330
331 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
332 public:
333   explicit ScatterVAction() : ReplayAction("scatterv") {}
334   void kernel(xbt::ReplayAction& action) override;
335 };
336
337 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
338 public:
339   explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
340   void kernel(xbt::ReplayAction& action) override;
341 };
342
343 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
344 public:
345   explicit AllToAllVAction() : ReplayAction("alltoallv") {}
346   void kernel(xbt::ReplayAction& action) override;
347 };
348
349 } // namespace replay
350 } // namespace smpi
351 } // namespace simgrid
352
353 #endif