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. */
6 #include <xbt/replay.hpp>
10 #define CHECK_ACTION_PARAMS(action, mandatory, optional) \
12 if (action.size() < static_cast<unsigned long>(mandatory + 2)) { \
13 std::stringstream ss; \
14 for (const auto& elem : action) { \
17 THROWF(arg_error, 0, "%s replay failed.\n" \
18 "%zu items were given on the line. First two should be process_id and action. " \
19 "This action needs after them %lu mandatory arguments, and accepts %lu optional ones. \n" \
20 "The full line that was given is:\n %s\n" \
21 "Please contact the Simgrid team if support is needed", \
22 __func__, action.size(), static_cast<unsigned long>(mandatory), static_cast<unsigned long>(optional), \
30 extern MPI_Datatype MPI_DEFAULT_TYPE;
32 class RequestStorage; // Forward decl
35 * Base class for all parsers.
37 class ActionArgParser {
39 virtual ~ActionArgParser() = default;
40 virtual void parse(simgrid::xbt::ReplayAction& action, std::string name) { CHECK_ACTION_PARAMS(action, 0, 0) }
43 class WaitTestParser : public ActionArgParser {
49 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
52 class SendRecvParser : public ActionArgParser {
54 /* communication partner; if we send, this is the receiver and vice versa */
58 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
60 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
63 class ComputeParser : public ActionArgParser {
65 /* communication partner; if we send, this is the receiver and vice versa */
68 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
71 class CollCommParser : public ActionArgParser {
79 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
80 MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
83 class BcastArgParser : public CollCommParser {
85 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
88 class ReduceArgParser : public CollCommParser {
90 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
93 class AllReduceArgParser : public CollCommParser {
95 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
98 class AllToAllArgParser : public CollCommParser {
100 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
103 class GatherArgParser : public CollCommParser {
105 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
108 class GatherVArgParser : public CollCommParser {
111 std::shared_ptr<std::vector<int>> recvcounts;
112 std::vector<int> disps;
113 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
116 class ScatterArgParser : public CollCommParser {
118 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
121 class ScatterVArgParser : public CollCommParser {
125 std::shared_ptr<std::vector<int>> sendcounts;
126 std::vector<int> disps;
127 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
130 class ReduceScatterArgParser : public CollCommParser {
133 std::shared_ptr<std::vector<int>> recvcounts;
134 std::vector<int> disps;
135 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
138 class AllToAllVArgParser : public CollCommParser {
142 std::shared_ptr<std::vector<int>> recvcounts;
143 std::shared_ptr<std::vector<int>> sendcounts;
144 std::vector<int> senddisps;
145 std::vector<int> recvdisps;
148 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
152 * Base class for all ReplayActions.
153 * Note that this class actually implements the behavior of each action
154 * while the parsing of the replay arguments is done in the @ActionArgParser class.
155 * In other words: The logic goes here, the setup is done by the ActionArgParser.
157 template <class T> class ReplayAction {
159 const std::string name;
160 const int my_proc_id;
164 explicit ReplayAction(std::string name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
165 virtual ~ReplayAction() = default;
167 virtual void execute(simgrid::xbt::ReplayAction& action);
168 virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
169 void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
170 void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
173 class WaitAction : public ReplayAction<WaitTestParser> {
175 RequestStorage& req_storage;
178 explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
179 void kernel(simgrid::xbt::ReplayAction& action) override;
182 class SendAction : public ReplayAction<SendRecvParser> {
184 RequestStorage& req_storage;
187 explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
188 void kernel(simgrid::xbt::ReplayAction& action) override;
191 class RecvAction : public ReplayAction<SendRecvParser> {
193 RequestStorage& req_storage;
196 explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
197 void kernel(simgrid::xbt::ReplayAction& action) override;
200 class ComputeAction : public ReplayAction<ComputeParser> {
202 explicit ComputeAction() : ReplayAction("compute") {}
203 void kernel(simgrid::xbt::ReplayAction& action) override;
206 class TestAction : public ReplayAction<WaitTestParser> {
208 RequestStorage& req_storage;
211 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
212 void kernel(simgrid::xbt::ReplayAction& action) override;
215 class InitAction : public ReplayAction<ActionArgParser> {
217 explicit InitAction() : ReplayAction("Init") {}
218 void kernel(simgrid::xbt::ReplayAction& action) override;
221 class CommunicatorAction : public ReplayAction<ActionArgParser> {
223 explicit CommunicatorAction() : ReplayAction("Comm") {}
224 void kernel(simgrid::xbt::ReplayAction& action) override;
227 class WaitAllAction : public ReplayAction<ActionArgParser> {
229 RequestStorage& req_storage;
232 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitAll"), req_storage(storage) {}
233 void kernel(simgrid::xbt::ReplayAction& action) override;
236 class BarrierAction : public ReplayAction<ActionArgParser> {
238 explicit BarrierAction() : ReplayAction("barrier") {}
239 void kernel(simgrid::xbt::ReplayAction& action) override;
242 class BcastAction : public ReplayAction<BcastArgParser> {
244 explicit BcastAction() : ReplayAction("bcast") {}
245 void kernel(simgrid::xbt::ReplayAction& action) override;
248 class ReduceAction : public ReplayAction<ReduceArgParser> {
250 explicit ReduceAction() : ReplayAction("reduce") {}
251 void kernel(simgrid::xbt::ReplayAction& action) override;
254 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
256 explicit AllReduceAction() : ReplayAction("allReduce") {}
257 void kernel(simgrid::xbt::ReplayAction& action) override;
260 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
262 explicit AllToAllAction() : ReplayAction("allToAll") {}
263 void kernel(simgrid::xbt::ReplayAction& action) override;
266 class GatherAction : public ReplayAction<GatherArgParser> {
268 explicit GatherAction(std::string name) : ReplayAction(name) {}
269 void kernel(simgrid::xbt::ReplayAction& action) override;
272 class GatherVAction : public ReplayAction<GatherVArgParser> {
274 explicit GatherVAction(std::string name) : ReplayAction(name) {}
275 void kernel(simgrid::xbt::ReplayAction& action) override;
278 class ScatterAction : public ReplayAction<ScatterArgParser> {
280 explicit ScatterAction() : ReplayAction("scatter") {}
281 void kernel(simgrid::xbt::ReplayAction& action) override;
284 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
286 explicit ScatterVAction() : ReplayAction("scatterV") {}
287 void kernel(simgrid::xbt::ReplayAction& action) override;
290 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
292 explicit ReduceScatterAction() : ReplayAction("reduceScatter") {}
293 void kernel(simgrid::xbt::ReplayAction& action) override;
296 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
298 explicit AllToAllVAction() : ReplayAction("allToAllV") {}
299 void kernel(simgrid::xbt::ReplayAction& action) override;