Logo AND Algorithmique Numérique Distribuée

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