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(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(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(name), req_storage(storage) {}
220 void kernel(simgrid::xbt::ReplayAction& action) override;
223 class ComputeAction : public ReplayAction<ComputeParser> {
225 explicit ComputeAction() : ReplayAction("compute") {}
226 void kernel(simgrid::xbt::ReplayAction& action) override;
229 class TestAction : public ReplayAction<WaitTestParser> {
231 RequestStorage& req_storage;
234 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
235 void kernel(simgrid::xbt::ReplayAction& action) override;
238 class InitAction : public ReplayAction<ActionArgParser> {
240 explicit InitAction() : ReplayAction("Init") {}
241 void kernel(simgrid::xbt::ReplayAction& action) override;
244 class CommunicatorAction : public ReplayAction<ActionArgParser> {
246 explicit CommunicatorAction() : ReplayAction("Comm") {}
247 void kernel(simgrid::xbt::ReplayAction& action) override;
250 class WaitAllAction : public ReplayAction<ActionArgParser> {
252 RequestStorage& req_storage;
255 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
256 void kernel(simgrid::xbt::ReplayAction& action) override;
259 class BarrierAction : public ReplayAction<ActionArgParser> {
261 explicit BarrierAction() : ReplayAction("barrier") {}
262 void kernel(simgrid::xbt::ReplayAction& action) override;
265 class BcastAction : public ReplayAction<BcastArgParser> {
267 explicit BcastAction() : ReplayAction("bcast") {}
268 void kernel(simgrid::xbt::ReplayAction& action) override;
271 class ReduceAction : public ReplayAction<ReduceArgParser> {
273 explicit ReduceAction() : ReplayAction("reduce") {}
274 void kernel(simgrid::xbt::ReplayAction& action) override;
277 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
279 explicit AllReduceAction() : ReplayAction("allreduce") {}
280 void kernel(simgrid::xbt::ReplayAction& action) override;
283 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
285 explicit AllToAllAction() : ReplayAction("alltoall") {}
286 void kernel(simgrid::xbt::ReplayAction& action) override;
289 class GatherAction : public ReplayAction<GatherArgParser> {
291 explicit GatherAction(std::string name) : ReplayAction(name) {}
292 void kernel(simgrid::xbt::ReplayAction& action) override;
295 class GatherVAction : public ReplayAction<GatherVArgParser> {
297 explicit GatherVAction(std::string name) : ReplayAction(name) {}
298 void kernel(simgrid::xbt::ReplayAction& action) override;
301 class ScatterAction : public ReplayAction<ScatterArgParser> {
303 explicit ScatterAction() : ReplayAction("scatter") {}
304 void kernel(simgrid::xbt::ReplayAction& action) override;
307 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
309 explicit ScatterVAction() : ReplayAction("scatterv") {}
310 void kernel(simgrid::xbt::ReplayAction& action) override;
313 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
315 explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
316 void kernel(simgrid::xbt::ReplayAction& action) override;
319 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
321 explicit AllToAllVAction() : ReplayAction("alltoallv") {}
322 void kernel(simgrid::xbt::ReplayAction& action) override;