Logo AND Algorithmique Numérique Distribuée

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