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;
95 virtual void parse(simgrid::xbt::ReplayAction& action, std::string name) = 0;
98 class BcastArgParser : public CollCommParser {
100 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
103 class ReduceArgParser : public CollCommParser {
105 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
108 class AllReduceArgParser : public CollCommParser {
110 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
113 class AllToAllArgParser : public CollCommParser {
115 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
118 class GatherArgParser : public CollCommParser {
120 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
123 class GatherVArgParser : public CollCommParser {
126 std::shared_ptr<std::vector<int>> recvcounts;
127 std::vector<int> disps;
128 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
131 class ScatterArgParser : public CollCommParser {
133 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
136 class ScatterVArgParser : public CollCommParser {
140 std::shared_ptr<std::vector<int>> sendcounts;
141 std::vector<int> disps;
142 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
145 class ReduceScatterArgParser : public CollCommParser {
148 std::shared_ptr<std::vector<int>> recvcounts;
149 std::vector<int> disps;
150 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
153 class AllToAllVArgParser : public CollCommParser {
157 std::shared_ptr<std::vector<int>> recvcounts;
158 std::shared_ptr<std::vector<int>> sendcounts;
159 std::vector<int> senddisps;
160 std::vector<int> recvdisps;
163 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
167 * Base class for all ReplayActions.
168 * Note that this class actually implements the behavior of each action
169 * while the parsing of the replay arguments is done in the @ActionArgParser class.
170 * In other words: The logic goes here, the setup is done by the ActionArgParser.
172 template <class T> class ReplayAction {
174 const std::string name;
175 const int my_proc_id;
179 explicit ReplayAction(std::string name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
180 virtual ~ReplayAction() = default;
182 void execute(simgrid::xbt::ReplayAction& action)
184 // Needs to be re-initialized for every action, hence here
185 double start_time = smpi_process()->simulated_elapsed();
186 args.parse(action, name);
189 log_timed_action(action, start_time);
192 virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
193 void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
194 void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
197 class WaitAction : public ReplayAction<WaitTestParser> {
199 RequestStorage& req_storage;
202 explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
203 void kernel(simgrid::xbt::ReplayAction& action) override;
206 class SendAction : public ReplayAction<SendRecvParser> {
208 RequestStorage& req_storage;
211 explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
212 void kernel(simgrid::xbt::ReplayAction& action) override;
215 class RecvAction : public ReplayAction<SendRecvParser> {
217 RequestStorage& req_storage;
220 explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
221 void kernel(simgrid::xbt::ReplayAction& action) override;
224 class ComputeAction : public ReplayAction<ComputeParser> {
226 explicit ComputeAction() : ReplayAction("compute") {}
227 void kernel(simgrid::xbt::ReplayAction& action) override;
230 class TestAction : public ReplayAction<WaitTestParser> {
232 RequestStorage& req_storage;
235 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
236 void kernel(simgrid::xbt::ReplayAction& action) override;
239 class InitAction : public ReplayAction<ActionArgParser> {
241 explicit InitAction() : ReplayAction("Init") {}
242 void kernel(simgrid::xbt::ReplayAction& action) override;
245 class CommunicatorAction : public ReplayAction<ActionArgParser> {
247 explicit CommunicatorAction() : ReplayAction("Comm") {}
248 void kernel(simgrid::xbt::ReplayAction& action) override;
251 class WaitAllAction : public ReplayAction<ActionArgParser> {
253 RequestStorage& req_storage;
256 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
257 void kernel(simgrid::xbt::ReplayAction& action) override;
260 class BarrierAction : public ReplayAction<ActionArgParser> {
262 explicit BarrierAction() : ReplayAction("barrier") {}
263 void kernel(simgrid::xbt::ReplayAction& action) override;
266 class BcastAction : public ReplayAction<BcastArgParser> {
268 explicit BcastAction() : ReplayAction("bcast") {}
269 void kernel(simgrid::xbt::ReplayAction& action) override;
272 class ReduceAction : public ReplayAction<ReduceArgParser> {
274 explicit ReduceAction() : ReplayAction("reduce") {}
275 void kernel(simgrid::xbt::ReplayAction& action) override;
278 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
280 explicit AllReduceAction() : ReplayAction("allreduce") {}
281 void kernel(simgrid::xbt::ReplayAction& action) override;
284 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
286 explicit AllToAllAction() : ReplayAction("allToAll") {}
287 void kernel(simgrid::xbt::ReplayAction& action) override;
290 class GatherAction : public ReplayAction<GatherArgParser> {
292 explicit GatherAction(std::string name) : ReplayAction(name) {}
293 void kernel(simgrid::xbt::ReplayAction& action) override;
296 class GatherVAction : public ReplayAction<GatherVArgParser> {
298 explicit GatherVAction(std::string name) : ReplayAction(name) {}
299 void kernel(simgrid::xbt::ReplayAction& action) override;
302 class ScatterAction : public ReplayAction<ScatterArgParser> {
304 explicit ScatterAction() : ReplayAction("scatter") {}
305 void kernel(simgrid::xbt::ReplayAction& action) override;
308 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
310 explicit ScatterVAction() : ReplayAction("scatterv") {}
311 void kernel(simgrid::xbt::ReplayAction& action) override;
314 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
316 explicit ReduceScatterAction() : ReplayAction("reduceScatter") {}
317 void kernel(simgrid::xbt::ReplayAction& action) override;
320 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
322 explicit AllToAllVAction() : ReplayAction("allToAllV") {}
323 void kernel(simgrid::xbt::ReplayAction& action) override;