Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SMPI] Replay: Move ReplayAction definitions to replay.hpp
[simgrid.git] / include / simgrid / smpi / replay.hpp
1 /* Copyright (c) 2009-2018. 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 #include "private.hpp"
6 #include <xbt/replay.hpp>
7
8 #include <sstream>
9
10 #define CHECK_ACTION_PARAMS(action, mandatory, optional)                                                             \
11 {                                                                                                                    \
12   if (action.size() < static_cast<unsigned long>(mandatory + 2)) {                                                   \
13     std::stringstream ss;                                                                                            \
14     for (const auto& elem : action) {                                                                                \
15       ss << elem << " ";                                                                                             \
16     }                                                                                                                \
17     THROWF(arg_error, 0, "%s replay failed.\n"                                                                       \
18                          "%zu items were given on the line. First two should be process_id and action.  "            \
19                          "This action needs after them %lu mandatory arguments, and accepts %lu optional ones. \n"   \
20                          "The full line that was given is:\n   %s\n"                                                 \
21                          "Please contact the Simgrid team if support is needed",                                     \
22            __func__, action.size(), static_cast<unsigned long>(mandatory), static_cast<unsigned long>(optional),     \
23            ss.str().c_str());                                                                                        \
24   }                                                                                                                  \
25 }
26
27 namespace simgrid {
28 namespace smpi {
29 namespace replay {
30 extern MPI_Datatype MPI_DEFAULT_TYPE;
31
32 class RequestStorage; // Forward decl
33
34 /**
35  * Base class for all parsers.
36  */
37 class ActionArgParser {
38 public:
39   virtual ~ActionArgParser() = default;
40   virtual void parse(simgrid::xbt::ReplayAction& action, std::string name) { CHECK_ACTION_PARAMS(action, 0, 0) }
41 };
42
43 class WaitTestParser : public ActionArgParser {
44 public:
45   int src;
46   int dst;
47   int tag;
48
49   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
50 };
51
52 class SendRecvParser : public ActionArgParser {
53 public:
54   /* communication partner; if we send, this is the receiver and vice versa */
55   int partner;
56   double size;
57   int tag;
58   MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
59
60   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
61 };
62
63 class ComputeParser : public ActionArgParser {
64 public:
65   /* communication partner; if we send, this is the receiver and vice versa */
66   double flops;
67
68   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
69 };
70
71 class CollCommParser : public ActionArgParser {
72 public:
73   double size;
74   double comm_size;
75   double comp_size;
76   int send_size;
77   int recv_size;
78   int root = 0;
79   MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
80   MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
81
82   virtual void parse(simgrid::xbt::ReplayAction& action, std::string name) = 0;
83 };
84
85 class BcastArgParser : public CollCommParser {
86 public:
87   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
88 };
89
90 class ReduceArgParser : public CollCommParser {
91 public:
92   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
93 };
94
95 class AllReduceArgParser : public CollCommParser {
96 public:
97   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
98 };
99
100 class AllToAllArgParser : public CollCommParser {
101 public:
102   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
103 };
104
105 class GatherArgParser : public CollCommParser {
106 public:
107   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
108 };
109
110 class GatherVArgParser : public CollCommParser {
111 public:
112   int recv_size_sum;
113   std::shared_ptr<std::vector<int>> recvcounts;
114   std::vector<int> disps;
115   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
116 };
117
118 class ScatterArgParser : public CollCommParser {
119 public:
120   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
121 };
122
123 class ScatterVArgParser : public CollCommParser {
124 public:
125   int recv_size_sum;
126   int send_size_sum;
127   std::shared_ptr<std::vector<int>> sendcounts;
128   std::vector<int> disps;
129   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
130 };
131
132 class ReduceScatterArgParser : public CollCommParser {
133 public:
134   int recv_size_sum;
135   std::shared_ptr<std::vector<int>> recvcounts;
136   std::vector<int> disps;
137   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
138 };
139
140 class AllToAllVArgParser : public CollCommParser {
141 public:
142   int recv_size_sum;
143   int send_size_sum;
144   std::shared_ptr<std::vector<int>> recvcounts;
145   std::shared_ptr<std::vector<int>> sendcounts;
146   std::vector<int> senddisps;
147   std::vector<int> recvdisps;
148   int send_buf_size;
149   int recv_buf_size;
150   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
151 };
152
153 /**
154  * Base class for all ReplayActions.
155  * Note that this class actually implements the behavior of each action
156  * while the parsing of the replay arguments is done in the @ActionArgParser class.
157  * In other words: The logic goes here, the setup is done by the ActionArgParser.
158  */
159 template <class T> class ReplayAction {
160 protected:
161   const std::string name;
162   const int my_proc_id;
163   T args;
164
165 public:
166   explicit ReplayAction(std::string name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
167   virtual ~ReplayAction() = default;
168
169   virtual void execute(simgrid::xbt::ReplayAction& action);
170   virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
171   void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
172   void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
173 };
174
175 class WaitAction : public ReplayAction<WaitTestParser> {
176 private:
177   RequestStorage& req_storage;
178
179 public:
180   explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
181   void kernel(simgrid::xbt::ReplayAction& action) override;
182 };
183
184 class SendAction : public ReplayAction<SendRecvParser> {
185 private:
186   RequestStorage& req_storage;
187
188 public:
189   explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
190   void kernel(simgrid::xbt::ReplayAction& action) override;
191 };
192
193 class RecvAction : public ReplayAction<SendRecvParser> {
194 private:
195   RequestStorage& req_storage;
196
197 public:
198   explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
199   void kernel(simgrid::xbt::ReplayAction& action) override;
200 };
201
202 class ComputeAction : public ReplayAction<ComputeParser> {
203 public:
204   explicit ComputeAction() : ReplayAction("compute") {}
205   void kernel(simgrid::xbt::ReplayAction& action) override;
206 };
207
208 class TestAction : public ReplayAction<WaitTestParser> {
209 private:
210   RequestStorage& req_storage;
211 public:
212   explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
213   void kernel(simgrid::xbt::ReplayAction& action) override;
214 };
215
216 class InitAction : public ReplayAction<ActionArgParser> {
217 public:
218   explicit InitAction() : ReplayAction("Init") {}
219   void kernel(simgrid::xbt::ReplayAction& action) override;
220 };
221
222 class CommunicatorAction : public ReplayAction<ActionArgParser> {
223 public:
224   explicit CommunicatorAction() : ReplayAction("Comm") {}
225   void kernel(simgrid::xbt::ReplayAction& action) override;
226 };
227
228 class WaitAllAction : public ReplayAction<ActionArgParser> {
229 private:
230   RequestStorage& req_storage;
231
232 public:
233   explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitAll"), req_storage(storage) {}
234   void kernel(simgrid::xbt::ReplayAction& action) override;
235 };
236
237 class BarrierAction : public ReplayAction<ActionArgParser> {
238 public:
239   explicit BarrierAction() : ReplayAction("barrier") {}
240   void kernel(simgrid::xbt::ReplayAction& action) override;
241 };
242
243 class BcastAction : public ReplayAction<BcastArgParser> {
244 public:
245   explicit BcastAction() : ReplayAction("bcast") {}
246   void kernel(simgrid::xbt::ReplayAction& action) override;
247 };
248
249 class ReduceAction : public ReplayAction<ReduceArgParser> {
250 public:
251   explicit ReduceAction() : ReplayAction("reduce") {}
252   void kernel(simgrid::xbt::ReplayAction& action) override;
253 };
254
255 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
256 public:
257   explicit AllReduceAction() : ReplayAction("allReduce") {}
258   void kernel(simgrid::xbt::ReplayAction& action) override;
259 };
260
261 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
262 public:
263   explicit AllToAllAction() : ReplayAction("allToAll") {}
264   void kernel(simgrid::xbt::ReplayAction& action) override;
265 };
266
267 class GatherAction : public ReplayAction<GatherArgParser> {
268 public:
269   explicit GatherAction(std::string name) : ReplayAction(name) {}
270   void kernel(simgrid::xbt::ReplayAction& action) override;
271 };
272
273 class GatherVAction : public ReplayAction<GatherVArgParser> {
274 public:
275   explicit GatherVAction(std::string name) : ReplayAction(name) {}
276   void kernel(simgrid::xbt::ReplayAction& action) override;
277 };
278
279 class ScatterAction : public ReplayAction<ScatterArgParser> {
280 public:
281   explicit ScatterAction() : ReplayAction("scatter") {}
282   void kernel(simgrid::xbt::ReplayAction& action) override;
283 };
284
285 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
286 public:
287   explicit ScatterVAction() : ReplayAction("scatterV") {}
288   void kernel(simgrid::xbt::ReplayAction& action) override;
289 };
290
291 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
292 public:
293   explicit ReduceScatterAction() : ReplayAction("reduceScatter") {}
294   void kernel(simgrid::xbt::ReplayAction& action) override;
295 };
296
297 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
298 public:
299   explicit AllToAllVAction() : ReplayAction("allToAllV") {}
300   void kernel(simgrid::xbt::ReplayAction& action) override;
301 };
302
303 }
304 }
305 }