Logo AND Algorithmique Numérique Distribuée

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