Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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
83 class BcastArgParser : public CollCommParser {
84 public:
85   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
86 };
87
88 class ReduceArgParser : public CollCommParser {
89 public:
90   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
91 };
92
93 class AllReduceArgParser : public CollCommParser {
94 public:
95   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
96 };
97
98 class AllToAllArgParser : public CollCommParser {
99 public:
100   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
101 };
102
103 class GatherArgParser : public CollCommParser {
104 public:
105   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
106 };
107
108 class GatherVArgParser : public CollCommParser {
109 public:
110   int recv_size_sum;
111   std::shared_ptr<std::vector<int>> recvcounts;
112   std::vector<int> disps;
113   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
114 };
115
116 class ScatterArgParser : public CollCommParser {
117 public:
118   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
119 };
120
121 class ScatterVArgParser : public CollCommParser {
122 public:
123   int recv_size_sum;
124   int send_size_sum;
125   std::shared_ptr<std::vector<int>> sendcounts;
126   std::vector<int> disps;
127   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
128 };
129
130 class ReduceScatterArgParser : public CollCommParser {
131 public:
132   int recv_size_sum;
133   std::shared_ptr<std::vector<int>> recvcounts;
134   std::vector<int> disps;
135   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
136 };
137
138 class AllToAllVArgParser : public CollCommParser {
139 public:
140   int recv_size_sum;
141   int send_size_sum;
142   std::shared_ptr<std::vector<int>> recvcounts;
143   std::shared_ptr<std::vector<int>> sendcounts;
144   std::vector<int> senddisps;
145   std::vector<int> recvdisps;
146   int send_buf_size;
147   int recv_buf_size;
148   void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
149 };
150
151 /**
152  * Base class for all ReplayActions.
153  * Note that this class actually implements the behavior of each action
154  * while the parsing of the replay arguments is done in the @ActionArgParser class.
155  * In other words: The logic goes here, the setup is done by the ActionArgParser.
156  */
157 template <class T> class ReplayAction {
158 protected:
159   const std::string name;
160   const int my_proc_id;
161   T args;
162
163 public:
164   explicit ReplayAction(std::string name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
165   virtual ~ReplayAction() = default;
166
167   virtual void execute(simgrid::xbt::ReplayAction& action);
168   virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
169   void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
170   void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
171 };
172
173 class WaitAction : public ReplayAction<WaitTestParser> {
174 private:
175   RequestStorage& req_storage;
176
177 public:
178   explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
179   void kernel(simgrid::xbt::ReplayAction& action) override;
180 };
181
182 class SendAction : public ReplayAction<SendRecvParser> {
183 private:
184   RequestStorage& req_storage;
185
186 public:
187   explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
188   void kernel(simgrid::xbt::ReplayAction& action) override;
189 };
190
191 class RecvAction : public ReplayAction<SendRecvParser> {
192 private:
193   RequestStorage& req_storage;
194
195 public:
196   explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
197   void kernel(simgrid::xbt::ReplayAction& action) override;
198 };
199
200 class ComputeAction : public ReplayAction<ComputeParser> {
201 public:
202   explicit ComputeAction() : ReplayAction("compute") {}
203   void kernel(simgrid::xbt::ReplayAction& action) override;
204 };
205
206 class TestAction : public ReplayAction<WaitTestParser> {
207 private:
208   RequestStorage& req_storage;
209
210 public:
211   explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
212   void kernel(simgrid::xbt::ReplayAction& action) override;
213 };
214
215 class InitAction : public ReplayAction<ActionArgParser> {
216 public:
217   explicit InitAction() : ReplayAction("Init") {}
218   void kernel(simgrid::xbt::ReplayAction& action) override;
219 };
220
221 class CommunicatorAction : public ReplayAction<ActionArgParser> {
222 public:
223   explicit CommunicatorAction() : ReplayAction("Comm") {}
224   void kernel(simgrid::xbt::ReplayAction& action) override;
225 };
226
227 class WaitAllAction : public ReplayAction<ActionArgParser> {
228 private:
229   RequestStorage& req_storage;
230
231 public:
232   explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitAll"), req_storage(storage) {}
233   void kernel(simgrid::xbt::ReplayAction& action) override;
234 };
235
236 class BarrierAction : public ReplayAction<ActionArgParser> {
237 public:
238   explicit BarrierAction() : ReplayAction("barrier") {}
239   void kernel(simgrid::xbt::ReplayAction& action) override;
240 };
241
242 class BcastAction : public ReplayAction<BcastArgParser> {
243 public:
244   explicit BcastAction() : ReplayAction("bcast") {}
245   void kernel(simgrid::xbt::ReplayAction& action) override;
246 };
247
248 class ReduceAction : public ReplayAction<ReduceArgParser> {
249 public:
250   explicit ReduceAction() : ReplayAction("reduce") {}
251   void kernel(simgrid::xbt::ReplayAction& action) override;
252 };
253
254 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
255 public:
256   explicit AllReduceAction() : ReplayAction("allReduce") {}
257   void kernel(simgrid::xbt::ReplayAction& action) override;
258 };
259
260 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
261 public:
262   explicit AllToAllAction() : ReplayAction("allToAll") {}
263   void kernel(simgrid::xbt::ReplayAction& action) override;
264 };
265
266 class GatherAction : public ReplayAction<GatherArgParser> {
267 public:
268   explicit GatherAction(std::string name) : ReplayAction(name) {}
269   void kernel(simgrid::xbt::ReplayAction& action) override;
270 };
271
272 class GatherVAction : public ReplayAction<GatherVArgParser> {
273 public:
274   explicit GatherVAction(std::string name) : ReplayAction(name) {}
275   void kernel(simgrid::xbt::ReplayAction& action) override;
276 };
277
278 class ScatterAction : public ReplayAction<ScatterArgParser> {
279 public:
280   explicit ScatterAction() : ReplayAction("scatter") {}
281   void kernel(simgrid::xbt::ReplayAction& action) override;
282 };
283
284 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
285 public:
286   explicit ScatterVAction() : ReplayAction("scatterV") {}
287   void kernel(simgrid::xbt::ReplayAction& action) override;
288 };
289
290 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
291 public:
292   explicit ReduceScatterAction() : ReplayAction("reduceScatter") {}
293   void kernel(simgrid::xbt::ReplayAction& action) override;
294 };
295
296 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
297 public:
298   explicit AllToAllVAction() : ReplayAction("allToAllV") {}
299   void kernel(simgrid::xbt::ReplayAction& action) override;
300 };
301 }
302 }
303 }