1 /* Copyright (c) 2009-2018. 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 <boost/algorithm/string/join.hpp>
9 #include <src/smpi/include/smpi_process.hpp>
10 #include <xbt/replay.hpp>
16 #define CHECK_ACTION_PARAMS(action, mandatory, optional) \
18 if (action.size() < static_cast<unsigned long>(mandatory + 2)) { \
19 std::stringstream ss; \
20 for (const auto& elem : action) { \
23 THROWF(arg_error, 0, "%s replay failed.\n" \
24 "%zu items were given on the line. First two should be process_id and action. " \
25 "This action needs after them %lu mandatory arguments, and accepts %lu optional ones. \n" \
26 "The full line that was given is:\n %s\n" \
27 "Please contact the Simgrid team if support is needed", \
28 __func__, action.size(), static_cast<unsigned long>(mandatory), static_cast<unsigned long>(optional), \
33 XBT_PRIVATE void* smpi_get_tmp_sendbuffer(int size);
34 XBT_PRIVATE void* smpi_get_tmp_recvbuffer(int size);
35 XBT_PRIVATE void smpi_free_tmp_buffer(void* buf);
36 XBT_PRIVATE void smpi_free_replay_tmp_buffers();
38 XBT_PRIVATE void log_timed_action(simgrid::xbt::ReplayAction& action, double clock);
43 extern MPI_Datatype MPI_DEFAULT_TYPE;
45 class RequestStorage; // Forward decl
48 * Base class for all parsers.
50 class ActionArgParser {
52 virtual ~ActionArgParser() = default;
53 virtual void parse(simgrid::xbt::ReplayAction& action, std::string name) { CHECK_ACTION_PARAMS(action, 0, 0) }
56 class WaitTestParser : public ActionArgParser {
62 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
65 class SendRecvParser : public ActionArgParser {
67 /* communication partner; if we send, this is the receiver and vice versa */
71 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
73 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
76 class ComputeParser : public ActionArgParser {
78 /* communication partner; if we send, this is the receiver and vice versa */
81 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
84 class CollCommParser : public ActionArgParser {
92 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
93 MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
96 class BcastArgParser : public CollCommParser {
98 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
101 class ReduceArgParser : public CollCommParser {
103 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
106 class AllReduceArgParser : public CollCommParser {
108 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
111 class AllToAllArgParser : public CollCommParser {
113 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
116 class GatherArgParser : public CollCommParser {
118 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
121 class GatherVArgParser : public CollCommParser {
124 std::shared_ptr<std::vector<int>> recvcounts;
125 std::vector<int> disps;
126 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
129 class ScatterArgParser : public CollCommParser {
131 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
134 class ScatterVArgParser : public CollCommParser {
138 std::shared_ptr<std::vector<int>> sendcounts;
139 std::vector<int> disps;
140 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
143 class ReduceScatterArgParser : public CollCommParser {
146 std::shared_ptr<std::vector<int>> recvcounts;
147 std::vector<int> disps;
148 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
151 class AllToAllVArgParser : public CollCommParser {
155 std::shared_ptr<std::vector<int>> recvcounts;
156 std::shared_ptr<std::vector<int>> sendcounts;
157 std::vector<int> senddisps;
158 std::vector<int> recvdisps;
161 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
165 * Base class for all ReplayActions.
166 * Note that this class actually implements the behavior of each action
167 * while the parsing of the replay arguments is done in the @ActionArgParser class.
168 * In other words: The logic goes here, the setup is done by the ActionArgParser.
170 template <class T> class ReplayAction {
172 const std::string name;
173 const int my_proc_id;
177 explicit ReplayAction(std::string name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
178 virtual ~ReplayAction() = default;
180 void execute(simgrid::xbt::ReplayAction& action)
182 // Needs to be re-initialized for every action, hence here
183 double start_time = smpi_process()->simulated_elapsed();
184 args.parse(action, name);
187 log_timed_action(action, start_time);
190 virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
191 void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
192 void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
195 class WaitAction : public ReplayAction<WaitTestParser> {
197 RequestStorage& req_storage;
200 explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
201 void kernel(simgrid::xbt::ReplayAction& action) override;
204 class SendAction : public ReplayAction<SendRecvParser> {
206 RequestStorage& req_storage;
209 explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
210 void kernel(simgrid::xbt::ReplayAction& action) override;
213 class RecvAction : public ReplayAction<SendRecvParser> {
215 RequestStorage& req_storage;
218 explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
219 void kernel(simgrid::xbt::ReplayAction& action) override;
222 class ComputeAction : public ReplayAction<ComputeParser> {
224 explicit ComputeAction() : ReplayAction("compute") {}
225 void kernel(simgrid::xbt::ReplayAction& action) override;
228 class TestAction : public ReplayAction<WaitTestParser> {
230 RequestStorage& req_storage;
233 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
234 void kernel(simgrid::xbt::ReplayAction& action) override;
237 class InitAction : public ReplayAction<ActionArgParser> {
239 explicit InitAction() : ReplayAction("Init") {}
240 void kernel(simgrid::xbt::ReplayAction& action) override;
243 class CommunicatorAction : public ReplayAction<ActionArgParser> {
245 explicit CommunicatorAction() : ReplayAction("Comm") {}
246 void kernel(simgrid::xbt::ReplayAction& action) override;
249 class WaitAllAction : public ReplayAction<ActionArgParser> {
251 RequestStorage& req_storage;
254 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
255 void kernel(simgrid::xbt::ReplayAction& action) override;
258 class BarrierAction : public ReplayAction<ActionArgParser> {
260 explicit BarrierAction() : ReplayAction("barrier") {}
261 void kernel(simgrid::xbt::ReplayAction& action) override;
264 class BcastAction : public ReplayAction<BcastArgParser> {
266 explicit BcastAction() : ReplayAction("bcast") {}
267 void kernel(simgrid::xbt::ReplayAction& action) override;
270 class ReduceAction : public ReplayAction<ReduceArgParser> {
272 explicit ReduceAction() : ReplayAction("reduce") {}
273 void kernel(simgrid::xbt::ReplayAction& action) override;
276 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
278 explicit AllReduceAction() : ReplayAction("allreduce") {}
279 void kernel(simgrid::xbt::ReplayAction& action) override;
282 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
284 explicit AllToAllAction() : ReplayAction("alltoall") {}
285 void kernel(simgrid::xbt::ReplayAction& action) override;
288 class GatherAction : public ReplayAction<GatherArgParser> {
290 explicit GatherAction(std::string name) : ReplayAction(name) {}
291 void kernel(simgrid::xbt::ReplayAction& action) override;
294 class GatherVAction : public ReplayAction<GatherVArgParser> {
296 explicit GatherVAction(std::string name) : ReplayAction(name) {}
297 void kernel(simgrid::xbt::ReplayAction& action) override;
300 class ScatterAction : public ReplayAction<ScatterArgParser> {
302 explicit ScatterAction() : ReplayAction("scatter") {}
303 void kernel(simgrid::xbt::ReplayAction& action) override;
306 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
308 explicit ScatterVAction() : ReplayAction("scatterv") {}
309 void kernel(simgrid::xbt::ReplayAction& action) override;
312 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
314 explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
315 void kernel(simgrid::xbt::ReplayAction& action) override;
318 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
320 explicit AllToAllVAction() : ReplayAction("alltoallv") {}
321 void kernel(simgrid::xbt::ReplayAction& action) override;