Logo AND Algorithmique Numérique Distribuée

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