1 /* Copyright (c) 2009-2019. The SimGrid Team. All rights reserved. */
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_
8 #include "src/smpi/include/smpi_actor.hpp"
10 #include <boost/algorithm/string/join.hpp>
11 #include <xbt/replay.hpp>
17 #define CHECK_ACTION_PARAMS(action, mandatory, optional) \
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)) { \
29 ss << "\nPlease contact the Simgrid team if support is needed"; \
30 throw std::invalid_argument(ss.str()); \
34 XBT_PRIVATE unsigned char* smpi_get_tmp_sendbuffer(size_t size);
35 XBT_PRIVATE unsigned char* smpi_get_tmp_recvbuffer(size_t size);
37 XBT_PRIVATE void log_timed_action(simgrid::xbt::ReplayAction& action, double clock);
42 extern MPI_Datatype MPI_DEFAULT_TYPE;
44 class RequestStorage; // Forward decl
47 * Base class for all parsers.
49 class ActionArgParser {
51 virtual ~ActionArgParser() = default;
52 virtual void parse(simgrid::xbt::ReplayAction& action, const std::string& name) { CHECK_ACTION_PARAMS(action, 0, 0) }
55 class WaitTestParser : public ActionArgParser {
61 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
64 class SendRecvParser : public ActionArgParser {
66 /* communication partner; if we send, this is the receiver and vice versa */
70 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
72 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
75 class ComputeParser : public ActionArgParser {
79 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
82 class LocationParser : public ActionArgParser {
87 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
90 class CollCommParser : public ActionArgParser {
98 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
99 MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
102 class BcastArgParser : public CollCommParser {
104 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
107 class ReduceArgParser : public CollCommParser {
109 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
112 class AllReduceArgParser : public CollCommParser {
114 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
117 class AllToAllArgParser : public CollCommParser {
119 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
122 class GatherArgParser : public CollCommParser {
124 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
127 class GatherVArgParser : public CollCommParser {
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;
135 class ScatterArgParser : public CollCommParser {
137 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
140 class ScatterVArgParser : public CollCommParser {
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;
149 class ReduceScatterArgParser : public CollCommParser {
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;
157 class AllToAllVArgParser : public CollCommParser {
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;
167 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
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.
176 template <class T> class ReplayAction {
178 const std::string name;
179 const aid_t my_proc_id;
183 explicit ReplayAction(const std::string& name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
184 virtual ~ReplayAction() = default;
186 void execute(simgrid::xbt::ReplayAction& action)
188 // Needs to be re-initialized for every action, hence here
189 double start_time = smpi_process()->simulated_elapsed();
190 args.parse(action, name);
193 log_timed_action(action, start_time);
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); }
201 class WaitAction : public ReplayAction<WaitTestParser> {
203 RequestStorage& req_storage;
206 explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
207 void kernel(simgrid::xbt::ReplayAction& action) override;
210 class SendAction : public ReplayAction<SendRecvParser> {
212 RequestStorage& req_storage;
215 explicit SendAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
216 void kernel(simgrid::xbt::ReplayAction& action) override;
219 class RecvAction : public ReplayAction<SendRecvParser> {
221 RequestStorage& req_storage;
224 explicit RecvAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
225 void kernel(simgrid::xbt::ReplayAction& action) override;
228 class ComputeAction : public ReplayAction<ComputeParser> {
230 explicit ComputeAction() : ReplayAction("compute") {}
231 void kernel(simgrid::xbt::ReplayAction& action) override;
234 class LocationAction : public ReplayAction<LocationParser> {
236 explicit LocationAction() : ReplayAction("location") {}
237 void kernel(simgrid::xbt::ReplayAction& action) override;
240 class TestAction : public ReplayAction<WaitTestParser> {
242 RequestStorage& req_storage;
245 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
246 void kernel(simgrid::xbt::ReplayAction& action) override;
249 class InitAction : public ReplayAction<ActionArgParser> {
251 explicit InitAction() : ReplayAction("Init") {}
252 void kernel(simgrid::xbt::ReplayAction& action) override;
255 class CommunicatorAction : public ReplayAction<ActionArgParser> {
257 explicit CommunicatorAction() : ReplayAction("Comm") {}
258 void kernel(simgrid::xbt::ReplayAction& action) override;
261 class WaitAllAction : public ReplayAction<ActionArgParser> {
263 RequestStorage& req_storage;
266 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
267 void kernel(simgrid::xbt::ReplayAction& action) override;
270 class BarrierAction : public ReplayAction<ActionArgParser> {
272 explicit BarrierAction() : ReplayAction("barrier") {}
273 void kernel(simgrid::xbt::ReplayAction& action) override;
276 class BcastAction : public ReplayAction<BcastArgParser> {
278 explicit BcastAction() : ReplayAction("bcast") {}
279 void kernel(simgrid::xbt::ReplayAction& action) override;
282 class ReduceAction : public ReplayAction<ReduceArgParser> {
284 explicit ReduceAction() : ReplayAction("reduce") {}
285 void kernel(simgrid::xbt::ReplayAction& action) override;
288 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
290 explicit AllReduceAction() : ReplayAction("allreduce") {}
291 void kernel(simgrid::xbt::ReplayAction& action) override;
294 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
296 explicit AllToAllAction() : ReplayAction("alltoall") {}
297 void kernel(simgrid::xbt::ReplayAction& action) override;
300 class GatherAction : public ReplayAction<GatherArgParser> {
302 explicit GatherAction(const std::string& name) : ReplayAction(name) {}
303 void kernel(simgrid::xbt::ReplayAction& action) override;
306 class GatherVAction : public ReplayAction<GatherVArgParser> {
308 explicit GatherVAction(const std::string& name) : ReplayAction(name) {}
309 void kernel(simgrid::xbt::ReplayAction& action) override;
312 class ScatterAction : public ReplayAction<ScatterArgParser> {
314 explicit ScatterAction() : ReplayAction("scatter") {}
315 void kernel(simgrid::xbt::ReplayAction& action) override;
318 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
320 explicit ScatterVAction() : ReplayAction("scatterv") {}
321 void kernel(simgrid::xbt::ReplayAction& action) override;
324 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
326 explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
327 void kernel(simgrid::xbt::ReplayAction& action) override;
330 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
332 explicit AllToAllVAction() : ReplayAction("alltoallv") {}
333 void kernel(simgrid::xbt::ReplayAction& action) override;