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(const std::string& name) : name(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(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
209 void kernel(simgrid::xbt::ReplayAction& action) override;
212 class RecvAction : public ReplayAction<SendRecvParser> {
214 RequestStorage& req_storage;
217 explicit RecvAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
218 void kernel(simgrid::xbt::ReplayAction& action) override;
221 class ComputeAction : public ReplayAction<ComputeParser> {
223 explicit ComputeAction() : ReplayAction("compute") {}
224 void kernel(simgrid::xbt::ReplayAction& action) override;
227 class TestAction : public ReplayAction<WaitTestParser> {
229 RequestStorage& req_storage;
232 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
233 void kernel(simgrid::xbt::ReplayAction& action) override;
236 class InitAction : public ReplayAction<ActionArgParser> {
238 explicit InitAction() : ReplayAction("Init") {}
239 void kernel(simgrid::xbt::ReplayAction& action) override;
242 class CommunicatorAction : public ReplayAction<ActionArgParser> {
244 explicit CommunicatorAction() : ReplayAction("Comm") {}
245 void kernel(simgrid::xbt::ReplayAction& action) override;
248 class WaitAllAction : public ReplayAction<ActionArgParser> {
250 RequestStorage& req_storage;
253 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
254 void kernel(simgrid::xbt::ReplayAction& action) override;
257 class BarrierAction : public ReplayAction<ActionArgParser> {
259 explicit BarrierAction() : ReplayAction("barrier") {}
260 void kernel(simgrid::xbt::ReplayAction& action) override;
263 class BcastAction : public ReplayAction<BcastArgParser> {
265 explicit BcastAction() : ReplayAction("bcast") {}
266 void kernel(simgrid::xbt::ReplayAction& action) override;
269 class ReduceAction : public ReplayAction<ReduceArgParser> {
271 explicit ReduceAction() : ReplayAction("reduce") {}
272 void kernel(simgrid::xbt::ReplayAction& action) override;
275 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
277 explicit AllReduceAction() : ReplayAction("allreduce") {}
278 void kernel(simgrid::xbt::ReplayAction& action) override;
281 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
283 explicit AllToAllAction() : ReplayAction("alltoall") {}
284 void kernel(simgrid::xbt::ReplayAction& action) override;
287 class GatherAction : public ReplayAction<GatherArgParser> {
289 explicit GatherAction(const std::string& name) : ReplayAction(name) {}
290 void kernel(simgrid::xbt::ReplayAction& action) override;
293 class GatherVAction : public ReplayAction<GatherVArgParser> {
295 explicit GatherVAction(const std::string& name) : ReplayAction(name) {}
296 void kernel(simgrid::xbt::ReplayAction& action) override;
299 class ScatterAction : public ReplayAction<ScatterArgParser> {
301 explicit ScatterAction() : ReplayAction("scatter") {}
302 void kernel(simgrid::xbt::ReplayAction& action) override;
305 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
307 explicit ScatterVAction() : ReplayAction("scatterv") {}
308 void kernel(simgrid::xbt::ReplayAction& action) override;
311 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
313 explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
314 void kernel(simgrid::xbt::ReplayAction& action) override;
317 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
319 explicit AllToAllVAction() : ReplayAction("alltoallv") {}
320 void kernel(simgrid::xbt::ReplayAction& action) override;