Logo AND Algorithmique Numérique Distribuée

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