Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d295586b745d8ce94dc1d9cee20b310aa7bfaea1
[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(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 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(s4u::this_actor::get_pid()) {}
191   virtual ~ReplayAction() = default;
192
193   void execute(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   RequestStorage& req_storage;
210
211 public:
212   explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
213   void kernel(xbt::ReplayAction& action) override;
214 };
215
216 class SendAction : public ReplayAction<SendRecvParser> {
217   RequestStorage& req_storage;
218
219 public:
220   explicit SendAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
221   void kernel(xbt::ReplayAction& action) override;
222 };
223
224 class RecvAction : public ReplayAction<SendRecvParser> {
225   RequestStorage& req_storage;
226
227 public:
228   explicit RecvAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
229   void kernel(xbt::ReplayAction& action) override;
230 };
231
232 class ComputeAction : public ReplayAction<ComputeParser> {
233 public:
234   explicit ComputeAction() : ReplayAction("compute") {}
235   void kernel(xbt::ReplayAction& action) override;
236 };
237
238 class SleepAction : public ReplayAction<SleepParser> {
239 public:
240   explicit SleepAction() : ReplayAction("sleep") {}
241   void kernel(xbt::ReplayAction& action) override;
242 };
243
244 class LocationAction : public ReplayAction<LocationParser> {
245 public:
246   explicit LocationAction() : ReplayAction("location") {}
247   void kernel(xbt::ReplayAction& action) override;
248 };
249
250 class TestAction : public ReplayAction<WaitTestParser> {
251 private:
252   RequestStorage& req_storage;
253
254 public:
255   explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
256   void kernel(xbt::ReplayAction& action) override;
257 };
258
259 class InitAction : public ReplayAction<ActionArgParser> {
260 public:
261   explicit InitAction() : ReplayAction("Init") {}
262   void kernel(xbt::ReplayAction& action) override;
263 };
264
265 class CommunicatorAction : public ReplayAction<ActionArgParser> {
266 public:
267   explicit CommunicatorAction() : ReplayAction("Comm") {}
268   void kernel(xbt::ReplayAction& action) override;
269 };
270
271 class WaitAllAction : public ReplayAction<ActionArgParser> {
272   RequestStorage& req_storage;
273
274 public:
275   explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
276   void kernel(xbt::ReplayAction& action) override;
277 };
278
279 class BarrierAction : public ReplayAction<ActionArgParser> {
280 public:
281   explicit BarrierAction() : ReplayAction("barrier") {}
282   void kernel(xbt::ReplayAction& action) override;
283 };
284
285 class BcastAction : public ReplayAction<BcastArgParser> {
286 public:
287   explicit BcastAction() : ReplayAction("bcast") {}
288   void kernel(xbt::ReplayAction& action) override;
289 };
290
291 class ReduceAction : public ReplayAction<ReduceArgParser> {
292 public:
293   explicit ReduceAction() : ReplayAction("reduce") {}
294   void kernel(xbt::ReplayAction& action) override;
295 };
296
297 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
298 public:
299   explicit AllReduceAction() : ReplayAction("allreduce") {}
300   void kernel(xbt::ReplayAction& action) override;
301 };
302
303 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
304 public:
305   explicit AllToAllAction() : ReplayAction("alltoall") {}
306   void kernel(xbt::ReplayAction& action) override;
307 };
308
309 class GatherAction : public ReplayAction<GatherArgParser> {
310 public:
311   explicit GatherAction(const std::string& name) : ReplayAction(name) {}
312   void kernel(xbt::ReplayAction& action) override;
313 };
314
315 class GatherVAction : public ReplayAction<GatherVArgParser> {
316 public:
317   explicit GatherVAction(const std::string& name) : ReplayAction(name) {}
318   void kernel(xbt::ReplayAction& action) override;
319 };
320
321 class ScatterAction : public ReplayAction<ScatterArgParser> {
322 public:
323   explicit ScatterAction() : ReplayAction("scatter") {}
324   void kernel(xbt::ReplayAction& action) override;
325 };
326
327 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
328 public:
329   explicit ScatterVAction() : ReplayAction("scatterv") {}
330   void kernel(xbt::ReplayAction& action) override;
331 };
332
333 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
334 public:
335   explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
336   void kernel(xbt::ReplayAction& action) override;
337 };
338
339 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
340 public:
341   explicit AllToAllVAction() : ReplayAction("alltoallv") {}
342   void kernel(xbt::ReplayAction& action) override;
343 };
344
345 } // namespace replay
346 } // namespace smpi
347 } // namespace simgrid
348
349 #endif