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 void* smpi_get_tmp_sendbuffer(int size);
35 XBT_PRIVATE void* smpi_get_tmp_recvbuffer(int 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 {
77 /* communication partner; if we send, this is the receiver and vice versa */
80 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
83 class CollCommParser : public ActionArgParser {
91 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
92 MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
95 class BcastArgParser : public CollCommParser {
97 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
100 class ReduceArgParser : public CollCommParser {
102 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
105 class AllReduceArgParser : public CollCommParser {
107 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
110 class AllToAllArgParser : public CollCommParser {
112 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
115 class GatherArgParser : public CollCommParser {
117 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
120 class GatherVArgParser : public CollCommParser {
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;
128 class ScatterArgParser : public CollCommParser {
130 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
133 class ScatterVArgParser : public CollCommParser {
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;
142 class ReduceScatterArgParser : public CollCommParser {
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;
150 class AllToAllVArgParser : public CollCommParser {
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;
160 void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
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.
169 template <class T> class ReplayAction {
171 const std::string name;
172 const aid_t my_proc_id;
176 explicit ReplayAction(std::string name) : name(std::move(name)), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
177 virtual ~ReplayAction() = default;
179 void execute(simgrid::xbt::ReplayAction& action)
181 // Needs to be re-initialized for every action, hence here
182 double start_time = smpi_process()->simulated_elapsed();
183 args.parse(action, name);
186 log_timed_action(action, start_time);
189 virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
190 void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
191 void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
194 class WaitAction : public ReplayAction<WaitTestParser> {
196 RequestStorage& req_storage;
199 explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
200 void kernel(simgrid::xbt::ReplayAction& action) override;
203 class SendAction : public ReplayAction<SendRecvParser> {
205 RequestStorage& req_storage;
208 explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(std::move(name)), req_storage(storage)
211 void kernel(simgrid::xbt::ReplayAction& action) override;
214 class RecvAction : public ReplayAction<SendRecvParser> {
216 RequestStorage& req_storage;
219 explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(std::move(name)), req_storage(storage)
222 void kernel(simgrid::xbt::ReplayAction& action) override;
225 class ComputeAction : public ReplayAction<ComputeParser> {
227 explicit ComputeAction() : ReplayAction("compute") {}
228 void kernel(simgrid::xbt::ReplayAction& action) override;
231 class TestAction : public ReplayAction<WaitTestParser> {
233 RequestStorage& req_storage;
236 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
237 void kernel(simgrid::xbt::ReplayAction& action) override;
240 class InitAction : public ReplayAction<ActionArgParser> {
242 explicit InitAction() : ReplayAction("Init") {}
243 void kernel(simgrid::xbt::ReplayAction& action) override;
246 class CommunicatorAction : public ReplayAction<ActionArgParser> {
248 explicit CommunicatorAction() : ReplayAction("Comm") {}
249 void kernel(simgrid::xbt::ReplayAction& action) override;
252 class WaitAllAction : public ReplayAction<ActionArgParser> {
254 RequestStorage& req_storage;
257 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
258 void kernel(simgrid::xbt::ReplayAction& action) override;
261 class BarrierAction : public ReplayAction<ActionArgParser> {
263 explicit BarrierAction() : ReplayAction("barrier") {}
264 void kernel(simgrid::xbt::ReplayAction& action) override;
267 class BcastAction : public ReplayAction<BcastArgParser> {
269 explicit BcastAction() : ReplayAction("bcast") {}
270 void kernel(simgrid::xbt::ReplayAction& action) override;
273 class ReduceAction : public ReplayAction<ReduceArgParser> {
275 explicit ReduceAction() : ReplayAction("reduce") {}
276 void kernel(simgrid::xbt::ReplayAction& action) override;
279 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
281 explicit AllReduceAction() : ReplayAction("allreduce") {}
282 void kernel(simgrid::xbt::ReplayAction& action) override;
285 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
287 explicit AllToAllAction() : ReplayAction("alltoall") {}
288 void kernel(simgrid::xbt::ReplayAction& action) override;
291 class GatherAction : public ReplayAction<GatherArgParser> {
293 explicit GatherAction(std::string name) : ReplayAction(std::move(name)) {}
294 void kernel(simgrid::xbt::ReplayAction& action) override;
297 class GatherVAction : public ReplayAction<GatherVArgParser> {
299 explicit GatherVAction(std::string name) : ReplayAction(std::move(name)) {}
300 void kernel(simgrid::xbt::ReplayAction& action) override;
303 class ScatterAction : public ReplayAction<ScatterArgParser> {
305 explicit ScatterAction() : ReplayAction("scatter") {}
306 void kernel(simgrid::xbt::ReplayAction& action) override;
309 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
311 explicit ScatterVAction() : ReplayAction("scatterv") {}
312 void kernel(simgrid::xbt::ReplayAction& action) override;
315 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
317 explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
318 void kernel(simgrid::xbt::ReplayAction& action) override;
321 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
323 explicit AllToAllVAction() : ReplayAction("alltoallv") {}
324 void kernel(simgrid::xbt::ReplayAction& action) override;