Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SMPI] Replay: Apply clang-format to replay.cpp
[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
212 public:
213   explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
214   void kernel(simgrid::xbt::ReplayAction& action) override;
215 };
216
217 class InitAction : public ReplayAction<ActionArgParser> {
218 public:
219   explicit InitAction() : ReplayAction("Init") {}
220   void kernel(simgrid::xbt::ReplayAction& action) override;
221 };
222
223 class CommunicatorAction : public ReplayAction<ActionArgParser> {
224 public:
225   explicit CommunicatorAction() : ReplayAction("Comm") {}
226   void kernel(simgrid::xbt::ReplayAction& action) override;
227 };
228
229 class WaitAllAction : public ReplayAction<ActionArgParser> {
230 private:
231   RequestStorage& req_storage;
232
233 public:
234   explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitAll"), req_storage(storage) {}
235   void kernel(simgrid::xbt::ReplayAction& action) override;
236 };
237
238 class BarrierAction : public ReplayAction<ActionArgParser> {
239 public:
240   explicit BarrierAction() : ReplayAction("barrier") {}
241   void kernel(simgrid::xbt::ReplayAction& action) override;
242 };
243
244 class BcastAction : public ReplayAction<BcastArgParser> {
245 public:
246   explicit BcastAction() : ReplayAction("bcast") {}
247   void kernel(simgrid::xbt::ReplayAction& action) override;
248 };
249
250 class ReduceAction : public ReplayAction<ReduceArgParser> {
251 public:
252   explicit ReduceAction() : ReplayAction("reduce") {}
253   void kernel(simgrid::xbt::ReplayAction& action) override;
254 };
255
256 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
257 public:
258   explicit AllReduceAction() : ReplayAction("allReduce") {}
259   void kernel(simgrid::xbt::ReplayAction& action) override;
260 };
261
262 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
263 public:
264   explicit AllToAllAction() : ReplayAction("allToAll") {}
265   void kernel(simgrid::xbt::ReplayAction& action) override;
266 };
267
268 class GatherAction : public ReplayAction<GatherArgParser> {
269 public:
270   explicit GatherAction(std::string name) : ReplayAction(name) {}
271   void kernel(simgrid::xbt::ReplayAction& action) override;
272 };
273
274 class GatherVAction : public ReplayAction<GatherVArgParser> {
275 public:
276   explicit GatherVAction(std::string name) : ReplayAction(name) {}
277   void kernel(simgrid::xbt::ReplayAction& action) override;
278 };
279
280 class ScatterAction : public ReplayAction<ScatterArgParser> {
281 public:
282   explicit ScatterAction() : ReplayAction("scatter") {}
283   void kernel(simgrid::xbt::ReplayAction& action) override;
284 };
285
286 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
287 public:
288   explicit ScatterVAction() : ReplayAction("scatterV") {}
289   void kernel(simgrid::xbt::ReplayAction& action) override;
290 };
291
292 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
293 public:
294   explicit ReduceScatterAction() : ReplayAction("reduceScatter") {}
295   void kernel(simgrid::xbt::ReplayAction& action) override;
296 };
297
298 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
299 public:
300   explicit AllToAllVAction() : ReplayAction("allToAllV") {}
301   void kernel(simgrid::xbt::ReplayAction& action) override;
302 };
303 }
304 }
305 }