Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove duplicated but unused forward declarations.
[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 void* smpi_get_tmp_sendbuffer(int size);
35 XBT_PRIVATE void* smpi_get_tmp_recvbuffer(int 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   /* communication partner; if we send, this is the receiver and vice versa */
78   double flops;
79
80   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
81 };
82
83 class CollCommParser : public ActionArgParser {
84 public:
85   double size;
86   double comm_size;
87   double comp_size;
88   int send_size;
89   int recv_size;
90   int root = 0;
91   MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
92   MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
93 };
94
95 class BcastArgParser : public CollCommParser {
96 public:
97   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
98 };
99
100 class ReduceArgParser : public CollCommParser {
101 public:
102   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
103 };
104
105 class AllReduceArgParser : public CollCommParser {
106 public:
107   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
108 };
109
110 class AllToAllArgParser : public CollCommParser {
111 public:
112   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
113 };
114
115 class GatherArgParser : public CollCommParser {
116 public:
117   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
118 };
119
120 class GatherVArgParser : public CollCommParser {
121 public:
122   int recv_size_sum;
123   std::shared_ptr<std::vector<int>> recvcounts;
124   std::vector<int> disps;
125   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
126 };
127
128 class ScatterArgParser : public CollCommParser {
129 public:
130   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
131 };
132
133 class ScatterVArgParser : public CollCommParser {
134 public:
135   int recv_size_sum;
136   int send_size_sum;
137   std::shared_ptr<std::vector<int>> sendcounts;
138   std::vector<int> disps;
139   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
140 };
141
142 class ReduceScatterArgParser : public CollCommParser {
143 public:
144   int recv_size_sum;
145   std::shared_ptr<std::vector<int>> recvcounts;
146   std::vector<int> disps;
147   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
148 };
149
150 class AllToAllVArgParser : public CollCommParser {
151 public:
152   int recv_size_sum;
153   int send_size_sum;
154   std::shared_ptr<std::vector<int>> recvcounts;
155   std::shared_ptr<std::vector<int>> sendcounts;
156   std::vector<int> senddisps;
157   std::vector<int> recvdisps;
158   int send_buf_size;
159   int recv_buf_size;
160   void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
161 };
162
163 /**
164  * Base class for all ReplayActions.
165  * Note that this class actually implements the behavior of each action
166  * while the parsing of the replay arguments is done in the @ref ActionArgParser class.
167  * In other words: The logic goes here, the setup is done by the ActionArgParser.
168  */
169 template <class T> class ReplayAction {
170 protected:
171   const std::string name;
172   const aid_t my_proc_id;
173   T args;
174
175 public:
176   explicit ReplayAction(std::string name) : name(std::move(name)), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
177   virtual ~ReplayAction() = default;
178
179   void execute(simgrid::xbt::ReplayAction& action)
180   {
181     // Needs to be re-initialized for every action, hence here
182     double start_time = smpi_process()->simulated_elapsed();
183     args.parse(action, name);
184     kernel(action);
185     if (name != "Init")
186       log_timed_action(action, start_time);
187   }
188
189   virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
190   void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
191   void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
192 };
193
194 class WaitAction : public ReplayAction<WaitTestParser> {
195 private:
196   RequestStorage& req_storage;
197
198 public:
199   explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
200   void kernel(simgrid::xbt::ReplayAction& action) override;
201 };
202
203 class SendAction : public ReplayAction<SendRecvParser> {
204 private:
205   RequestStorage& req_storage;
206
207 public:
208   explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(std::move(name)), req_storage(storage)
209   {
210   }
211   void kernel(simgrid::xbt::ReplayAction& action) override;
212 };
213
214 class RecvAction : public ReplayAction<SendRecvParser> {
215 private:
216   RequestStorage& req_storage;
217
218 public:
219   explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(std::move(name)), req_storage(storage)
220   {
221   }
222   void kernel(simgrid::xbt::ReplayAction& action) override;
223 };
224
225 class ComputeAction : public ReplayAction<ComputeParser> {
226 public:
227   explicit ComputeAction() : ReplayAction("compute") {}
228   void kernel(simgrid::xbt::ReplayAction& action) override;
229 };
230
231 class TestAction : public ReplayAction<WaitTestParser> {
232 private:
233   RequestStorage& req_storage;
234
235 public:
236   explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
237   void kernel(simgrid::xbt::ReplayAction& action) override;
238 };
239
240 class InitAction : public ReplayAction<ActionArgParser> {
241 public:
242   explicit InitAction() : ReplayAction("Init") {}
243   void kernel(simgrid::xbt::ReplayAction& action) override;
244 };
245
246 class CommunicatorAction : public ReplayAction<ActionArgParser> {
247 public:
248   explicit CommunicatorAction() : ReplayAction("Comm") {}
249   void kernel(simgrid::xbt::ReplayAction& action) override;
250 };
251
252 class WaitAllAction : public ReplayAction<ActionArgParser> {
253 private:
254   RequestStorage& req_storage;
255
256 public:
257   explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
258   void kernel(simgrid::xbt::ReplayAction& action) override;
259 };
260
261 class BarrierAction : public ReplayAction<ActionArgParser> {
262 public:
263   explicit BarrierAction() : ReplayAction("barrier") {}
264   void kernel(simgrid::xbt::ReplayAction& action) override;
265 };
266
267 class BcastAction : public ReplayAction<BcastArgParser> {
268 public:
269   explicit BcastAction() : ReplayAction("bcast") {}
270   void kernel(simgrid::xbt::ReplayAction& action) override;
271 };
272
273 class ReduceAction : public ReplayAction<ReduceArgParser> {
274 public:
275   explicit ReduceAction() : ReplayAction("reduce") {}
276   void kernel(simgrid::xbt::ReplayAction& action) override;
277 };
278
279 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
280 public:
281   explicit AllReduceAction() : ReplayAction("allreduce") {}
282   void kernel(simgrid::xbt::ReplayAction& action) override;
283 };
284
285 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
286 public:
287   explicit AllToAllAction() : ReplayAction("alltoall") {}
288   void kernel(simgrid::xbt::ReplayAction& action) override;
289 };
290
291 class GatherAction : public ReplayAction<GatherArgParser> {
292 public:
293   explicit GatherAction(std::string name) : ReplayAction(std::move(name)) {}
294   void kernel(simgrid::xbt::ReplayAction& action) override;
295 };
296
297 class GatherVAction : public ReplayAction<GatherVArgParser> {
298 public:
299   explicit GatherVAction(std::string name) : ReplayAction(std::move(name)) {}
300   void kernel(simgrid::xbt::ReplayAction& action) override;
301 };
302
303 class ScatterAction : public ReplayAction<ScatterArgParser> {
304 public:
305   explicit ScatterAction() : ReplayAction("scatter") {}
306   void kernel(simgrid::xbt::ReplayAction& action) override;
307 };
308
309 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
310 public:
311   explicit ScatterVAction() : ReplayAction("scatterv") {}
312   void kernel(simgrid::xbt::ReplayAction& action) override;
313 };
314
315 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
316 public:
317   explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
318   void kernel(simgrid::xbt::ReplayAction& action) override;
319 };
320
321 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
322 public:
323   explicit AllToAllVAction() : ReplayAction("alltoallv") {}
324   void kernel(simgrid::xbt::ReplayAction& action) override;
325 };
326 }
327 }
328 }
329
330 #endif