Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use CRTP to factor refcounting across activity types
[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 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(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(simgrid::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(simgrid::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(simgrid::xbt::ReplayAction& action, const std::string& name) override;
73 };
74
75 class ComputeParser : public ActionArgParser {
76 public:
77   double flops;
78
79   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
80 };
81
82 class SleepParser : public ActionArgParser {
83 public:
84   double time;
85
86   void parse(simgrid::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(simgrid::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(simgrid::xbt::ReplayAction& action, const std::string& name) override;
112 };
113
114 class ReduceArgParser : public CollCommParser {
115 public:
116   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
117 };
118
119 class AllReduceArgParser : public CollCommParser {
120 public:
121   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
122 };
123
124 class AllToAllArgParser : public CollCommParser {
125 public:
126   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
127 };
128
129 class GatherArgParser : public CollCommParser {
130 public:
131   void parse(simgrid::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(simgrid::xbt::ReplayAction& action, const std::string& name) override;
140 };
141
142 class ScatterArgParser : public CollCommParser {
143 public:
144   void parse(simgrid::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(simgrid::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(simgrid::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(simgrid::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 protected:
185   const std::string name;
186   const aid_t my_proc_id;
187   T args;
188
189 public:
190   explicit ReplayAction(const std::string& name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
191   virtual ~ReplayAction() = default;
192
193   void execute(simgrid::xbt::ReplayAction& action)
194   {
195     // Needs to be re-initialized for every action, hence here
196     double start_time = smpi_process()->simulated_elapsed();
197     args.parse(action, name);
198     kernel(action);
199     if (name != "Init")
200       log_timed_action(action, start_time);
201   }
202
203   virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
204   unsigned char* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
205   unsigned char* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
206 };
207
208 class WaitAction : public ReplayAction<WaitTestParser> {
209 private:
210   RequestStorage& req_storage;
211
212 public:
213   explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
214   void kernel(simgrid::xbt::ReplayAction& action) override;
215 };
216
217 class SendAction : public ReplayAction<SendRecvParser> {
218 private:
219   RequestStorage& req_storage;
220
221 public:
222   explicit SendAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
223   void kernel(simgrid::xbt::ReplayAction& action) override;
224 };
225
226 class RecvAction : public ReplayAction<SendRecvParser> {
227 private:
228   RequestStorage& req_storage;
229
230 public:
231   explicit RecvAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
232   void kernel(simgrid::xbt::ReplayAction& action) override;
233 };
234
235 class ComputeAction : public ReplayAction<ComputeParser> {
236 public:
237   explicit ComputeAction() : ReplayAction("compute") {}
238   void kernel(simgrid::xbt::ReplayAction& action) override;
239 };
240
241 class SleepAction : public ReplayAction<SleepParser> {
242 public:
243   explicit SleepAction() : ReplayAction("sleep") {}
244   void kernel(simgrid::xbt::ReplayAction& action) override;
245 };
246
247 class LocationAction : public ReplayAction<LocationParser> {
248 public:
249   explicit LocationAction() : ReplayAction("location") {}
250   void kernel(simgrid::xbt::ReplayAction& action) override;
251 };
252
253 class TestAction : public ReplayAction<WaitTestParser> {
254 private:
255   RequestStorage& req_storage;
256
257 public:
258   explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
259   void kernel(simgrid::xbt::ReplayAction& action) override;
260 };
261
262 class InitAction : public ReplayAction<ActionArgParser> {
263 public:
264   explicit InitAction() : ReplayAction("Init") {}
265   void kernel(simgrid::xbt::ReplayAction& action) override;
266 };
267
268 class CommunicatorAction : public ReplayAction<ActionArgParser> {
269 public:
270   explicit CommunicatorAction() : ReplayAction("Comm") {}
271   void kernel(simgrid::xbt::ReplayAction& action) override;
272 };
273
274 class WaitAllAction : public ReplayAction<ActionArgParser> {
275 private:
276   RequestStorage& req_storage;
277
278 public:
279   explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
280   void kernel(simgrid::xbt::ReplayAction& action) override;
281 };
282
283 class BarrierAction : public ReplayAction<ActionArgParser> {
284 public:
285   explicit BarrierAction() : ReplayAction("barrier") {}
286   void kernel(simgrid::xbt::ReplayAction& action) override;
287 };
288
289 class BcastAction : public ReplayAction<BcastArgParser> {
290 public:
291   explicit BcastAction() : ReplayAction("bcast") {}
292   void kernel(simgrid::xbt::ReplayAction& action) override;
293 };
294
295 class ReduceAction : public ReplayAction<ReduceArgParser> {
296 public:
297   explicit ReduceAction() : ReplayAction("reduce") {}
298   void kernel(simgrid::xbt::ReplayAction& action) override;
299 };
300
301 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
302 public:
303   explicit AllReduceAction() : ReplayAction("allreduce") {}
304   void kernel(simgrid::xbt::ReplayAction& action) override;
305 };
306
307 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
308 public:
309   explicit AllToAllAction() : ReplayAction("alltoall") {}
310   void kernel(simgrid::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(simgrid::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(simgrid::xbt::ReplayAction& action) override;
323 };
324
325 class ScatterAction : public ReplayAction<ScatterArgParser> {
326 public:
327   explicit ScatterAction() : ReplayAction("scatter") {}
328   void kernel(simgrid::xbt::ReplayAction& action) override;
329 };
330
331 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
332 public:
333   explicit ScatterVAction() : ReplayAction("scatterv") {}
334   void kernel(simgrid::xbt::ReplayAction& action) override;
335 };
336
337 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
338 public:
339   explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
340   void kernel(simgrid::xbt::ReplayAction& action) override;
341 };
342
343 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
344 public:
345   explicit AllToAllVAction() : ReplayAction("alltoallv") {}
346   void kernel(simgrid::xbt::ReplayAction& action) override;
347 };
348 }
349 }
350 }
351
352 #endif