Logo AND Algorithmique Numérique Distribuée

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