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 SleepParser : public ActionArgParser {
86 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
89 class LocationParser : public ActionArgParser {
94 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
97 class CollCommParser : public ActionArgParser {
105 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
106 MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
109 class BcastArgParser : public CollCommParser {
111 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
114 class ReduceArgParser : public CollCommParser {
116 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
119 class AllReduceArgParser : public CollCommParser {
121 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
124 class AllToAllArgParser : public CollCommParser {
126 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
129 class GatherArgParser : public CollCommParser {
131 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
134 class GatherVArgParser : public CollCommParser {
137 std::shared_ptr<std::vector<int>> recvcounts;
138 std::vector<int> disps;
139 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
142 class ScatterArgParser : public CollCommParser {
144 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
147 class ScatterVArgParser : public CollCommParser {
151 std::shared_ptr<std::vector<int>> sendcounts;
152 std::vector<int> disps;
153 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
156 class ReduceScatterArgParser : public CollCommParser {
159 std::shared_ptr<std::vector<int>> recvcounts;
160 std::vector<int> disps;
161 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
164 class AllToAllVArgParser : public CollCommParser {
168 std::shared_ptr<std::vector<int>> recvcounts;
169 std::shared_ptr<std::vector<int>> sendcounts;
170 std::vector<int> senddisps;
171 std::vector<int> recvdisps;
174 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
178 * Base class for all ReplayActions.
179 * Note that this class actually implements the behavior of each action
180 * while the parsing of the replay arguments is done in the @ref ActionArgParser class.
181 * In other words: The logic goes here, the setup is done by the ActionArgParser.
183 template <class T> class ReplayAction {
185 const std::string name;
186 const aid_t my_proc_id;
190 explicit ReplayAction(const std::string& name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
191 virtual ~ReplayAction() = default;
193 void execute(simgrid::xbt::ReplayAction& action)
195 // Needs to be re-initialized for every action, hence here
196 double start_time = smpi_process()->simulated_elapsed();
197 args.parse(action, name);
200 log_timed_action(action, start_time);
203 virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
204 unsigned char* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
205 unsigned char* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
208 class WaitAction : public ReplayAction<WaitTestParser> {
210 RequestStorage& req_storage;
213 explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
214 void kernel(simgrid::xbt::ReplayAction& action) override;
217 class SendAction : public ReplayAction<SendRecvParser> {
219 RequestStorage& req_storage;
222 explicit SendAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
223 void kernel(simgrid::xbt::ReplayAction& action) override;
226 class RecvAction : public ReplayAction<SendRecvParser> {
228 RequestStorage& req_storage;
231 explicit RecvAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
232 void kernel(simgrid::xbt::ReplayAction& action) override;
235 class ComputeAction : public ReplayAction<ComputeParser> {
237 explicit ComputeAction() : ReplayAction("compute") {}
238 void kernel(simgrid::xbt::ReplayAction& action) override;
241 class SleepAction : public ReplayAction<SleepParser> {
243 explicit SleepAction() : ReplayAction("sleep") {}
244 void kernel(simgrid::xbt::ReplayAction& action) override;
247 class LocationAction : public ReplayAction<LocationParser> {
249 explicit LocationAction() : ReplayAction("location") {}
250 void kernel(simgrid::xbt::ReplayAction& action) override;
253 class TestAction : public ReplayAction<WaitTestParser> {
255 RequestStorage& req_storage;
258 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
259 void kernel(simgrid::xbt::ReplayAction& action) override;
262 class InitAction : public ReplayAction<ActionArgParser> {
264 explicit InitAction() : ReplayAction("Init") {}
265 void kernel(simgrid::xbt::ReplayAction& action) override;
268 class CommunicatorAction : public ReplayAction<ActionArgParser> {
270 explicit CommunicatorAction() : ReplayAction("Comm") {}
271 void kernel(simgrid::xbt::ReplayAction& action) override;
274 class WaitAllAction : public ReplayAction<ActionArgParser> {
276 RequestStorage& req_storage;
279 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
280 void kernel(simgrid::xbt::ReplayAction& action) override;
283 class BarrierAction : public ReplayAction<ActionArgParser> {
285 explicit BarrierAction() : ReplayAction("barrier") {}
286 void kernel(simgrid::xbt::ReplayAction& action) override;
289 class BcastAction : public ReplayAction<BcastArgParser> {
291 explicit BcastAction() : ReplayAction("bcast") {}
292 void kernel(simgrid::xbt::ReplayAction& action) override;
295 class ReduceAction : public ReplayAction<ReduceArgParser> {
297 explicit ReduceAction() : ReplayAction("reduce") {}
298 void kernel(simgrid::xbt::ReplayAction& action) override;
301 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
303 explicit AllReduceAction() : ReplayAction("allreduce") {}
304 void kernel(simgrid::xbt::ReplayAction& action) override;
307 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
309 explicit AllToAllAction() : ReplayAction("alltoall") {}
310 void kernel(simgrid::xbt::ReplayAction& action) override;
313 class GatherAction : public ReplayAction<GatherArgParser> {
315 explicit GatherAction(const std::string& name) : ReplayAction(name) {}
316 void kernel(simgrid::xbt::ReplayAction& action) override;
319 class GatherVAction : public ReplayAction<GatherVArgParser> {
321 explicit GatherVAction(const std::string& name) : ReplayAction(name) {}
322 void kernel(simgrid::xbt::ReplayAction& action) override;
325 class ScatterAction : public ReplayAction<ScatterArgParser> {
327 explicit ScatterAction() : ReplayAction("scatter") {}
328 void kernel(simgrid::xbt::ReplayAction& action) override;
331 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
333 explicit ScatterVAction() : ReplayAction("scatterv") {}
334 void kernel(simgrid::xbt::ReplayAction& action) override;
337 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
339 explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
340 void kernel(simgrid::xbt::ReplayAction& action) override;
343 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
345 explicit AllToAllVAction() : ReplayAction("alltoallv") {}
346 void kernel(simgrid::xbt::ReplayAction& action) override;