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);
36 XBT_PRIVATE void smpi_free_tmp_buffer(void* buf);
37 XBT_PRIVATE void smpi_free_replay_tmp_buffers();
39 XBT_PRIVATE void log_timed_action(simgrid::xbt::ReplayAction& action, double clock);
44 extern MPI_Datatype MPI_DEFAULT_TYPE;
46 class RequestStorage; // Forward decl
49 * Base class for all parsers.
51 class ActionArgParser {
53 virtual ~ActionArgParser() = default;
54 virtual void parse(simgrid::xbt::ReplayAction& action, std::string name) { CHECK_ACTION_PARAMS(action, 0, 0) }
57 class WaitTestParser : public ActionArgParser {
63 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
66 class SendRecvParser : public ActionArgParser {
68 /* communication partner; if we send, this is the receiver and vice versa */
72 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
74 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
77 class ComputeParser : public ActionArgParser {
79 /* communication partner; if we send, this is the receiver and vice versa */
82 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
85 class CollCommParser : public ActionArgParser {
93 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
94 MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
97 class BcastArgParser : public CollCommParser {
99 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
102 class ReduceArgParser : public CollCommParser {
104 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
107 class AllReduceArgParser : public CollCommParser {
109 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
112 class AllToAllArgParser : public CollCommParser {
114 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
117 class GatherArgParser : public CollCommParser {
119 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
122 class GatherVArgParser : public CollCommParser {
125 std::shared_ptr<std::vector<int>> recvcounts;
126 std::vector<int> disps;
127 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
130 class ScatterArgParser : public CollCommParser {
132 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
135 class ScatterVArgParser : public CollCommParser {
139 std::shared_ptr<std::vector<int>> sendcounts;
140 std::vector<int> disps;
141 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
144 class ReduceScatterArgParser : public CollCommParser {
147 std::shared_ptr<std::vector<int>> recvcounts;
148 std::vector<int> disps;
149 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
152 class AllToAllVArgParser : public CollCommParser {
156 std::shared_ptr<std::vector<int>> recvcounts;
157 std::shared_ptr<std::vector<int>> sendcounts;
158 std::vector<int> senddisps;
159 std::vector<int> recvdisps;
162 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
166 * Base class for all ReplayActions.
167 * Note that this class actually implements the behavior of each action
168 * while the parsing of the replay arguments is done in the @ref ActionArgParser class.
169 * In other words: The logic goes here, the setup is done by the ActionArgParser.
171 template <class T> class ReplayAction {
173 const std::string name;
174 const aid_t my_proc_id;
178 explicit ReplayAction(std::string name) : name(std::move(name)), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
179 virtual ~ReplayAction() = default;
181 void execute(simgrid::xbt::ReplayAction& action)
183 // Needs to be re-initialized for every action, hence here
184 double start_time = smpi_process()->simulated_elapsed();
185 args.parse(action, name);
188 log_timed_action(action, start_time);
191 virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
192 void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
193 void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
196 class WaitAction : public ReplayAction<WaitTestParser> {
198 RequestStorage& req_storage;
201 explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
202 void kernel(simgrid::xbt::ReplayAction& action) override;
205 class SendAction : public ReplayAction<SendRecvParser> {
207 RequestStorage& req_storage;
210 explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(std::move(name)), req_storage(storage)
213 void kernel(simgrid::xbt::ReplayAction& action) override;
216 class RecvAction : public ReplayAction<SendRecvParser> {
218 RequestStorage& req_storage;
221 explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(std::move(name)), req_storage(storage)
224 void kernel(simgrid::xbt::ReplayAction& action) override;
227 class ComputeAction : public ReplayAction<ComputeParser> {
229 explicit ComputeAction() : ReplayAction("compute") {}
230 void kernel(simgrid::xbt::ReplayAction& action) override;
233 class TestAction : public ReplayAction<WaitTestParser> {
235 RequestStorage& req_storage;
238 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
239 void kernel(simgrid::xbt::ReplayAction& action) override;
242 class InitAction : public ReplayAction<ActionArgParser> {
244 explicit InitAction() : ReplayAction("Init") {}
245 void kernel(simgrid::xbt::ReplayAction& action) override;
248 class CommunicatorAction : public ReplayAction<ActionArgParser> {
250 explicit CommunicatorAction() : ReplayAction("Comm") {}
251 void kernel(simgrid::xbt::ReplayAction& action) override;
254 class WaitAllAction : public ReplayAction<ActionArgParser> {
256 RequestStorage& req_storage;
259 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
260 void kernel(simgrid::xbt::ReplayAction& action) override;
263 class BarrierAction : public ReplayAction<ActionArgParser> {
265 explicit BarrierAction() : ReplayAction("barrier") {}
266 void kernel(simgrid::xbt::ReplayAction& action) override;
269 class BcastAction : public ReplayAction<BcastArgParser> {
271 explicit BcastAction() : ReplayAction("bcast") {}
272 void kernel(simgrid::xbt::ReplayAction& action) override;
275 class ReduceAction : public ReplayAction<ReduceArgParser> {
277 explicit ReduceAction() : ReplayAction("reduce") {}
278 void kernel(simgrid::xbt::ReplayAction& action) override;
281 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
283 explicit AllReduceAction() : ReplayAction("allreduce") {}
284 void kernel(simgrid::xbt::ReplayAction& action) override;
287 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
289 explicit AllToAllAction() : ReplayAction("alltoall") {}
290 void kernel(simgrid::xbt::ReplayAction& action) override;
293 class GatherAction : public ReplayAction<GatherArgParser> {
295 explicit GatherAction(std::string name) : ReplayAction(std::move(name)) {}
296 void kernel(simgrid::xbt::ReplayAction& action) override;
299 class GatherVAction : public ReplayAction<GatherVArgParser> {
301 explicit GatherVAction(std::string name) : ReplayAction(std::move(name)) {}
302 void kernel(simgrid::xbt::ReplayAction& action) override;
305 class ScatterAction : public ReplayAction<ScatterArgParser> {
307 explicit ScatterAction() : ReplayAction("scatter") {}
308 void kernel(simgrid::xbt::ReplayAction& action) override;
311 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
313 explicit ScatterVAction() : ReplayAction("scatterv") {}
314 void kernel(simgrid::xbt::ReplayAction& action) override;
317 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
319 explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
320 void kernel(simgrid::xbt::ReplayAction& action) override;
323 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
325 explicit AllToAllVAction() : ReplayAction("alltoallv") {}
326 void kernel(simgrid::xbt::ReplayAction& action) override;