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;
82 virtual void parse(simgrid::xbt::ReplayAction& action, std::string name) = 0;
85 class BcastArgParser : public CollCommParser {
87 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
90 class ReduceArgParser : public CollCommParser {
92 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
95 class AllReduceArgParser : public CollCommParser {
97 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
100 class AllToAllArgParser : public CollCommParser {
102 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
105 class GatherArgParser : public CollCommParser {
107 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
110 class GatherVArgParser : public CollCommParser {
113 std::shared_ptr<std::vector<int>> recvcounts;
114 std::vector<int> disps;
115 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
118 class ScatterArgParser : public CollCommParser {
120 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
123 class ScatterVArgParser : public CollCommParser {
127 std::shared_ptr<std::vector<int>> sendcounts;
128 std::vector<int> disps;
129 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
132 class ReduceScatterArgParser : public CollCommParser {
135 std::shared_ptr<std::vector<int>> recvcounts;
136 std::vector<int> disps;
137 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
140 class AllToAllVArgParser : public CollCommParser {
144 std::shared_ptr<std::vector<int>> recvcounts;
145 std::shared_ptr<std::vector<int>> sendcounts;
146 std::vector<int> senddisps;
147 std::vector<int> recvdisps;
150 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
154 * Base class for all ReplayActions.
155 * Note that this class actually implements the behavior of each action
156 * while the parsing of the replay arguments is done in the @ActionArgParser class.
157 * In other words: The logic goes here, the setup is done by the ActionArgParser.
159 template <class T> class ReplayAction {
161 const std::string name;
162 const int my_proc_id;
166 explicit ReplayAction(std::string name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
167 virtual ~ReplayAction() = default;
169 virtual void execute(simgrid::xbt::ReplayAction& action);
170 virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
171 void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
172 void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
175 class WaitAction : public ReplayAction<WaitTestParser> {
177 RequestStorage& req_storage;
180 explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
181 void kernel(simgrid::xbt::ReplayAction& action) override;
184 class SendAction : public ReplayAction<SendRecvParser> {
186 RequestStorage& req_storage;
189 explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
190 void kernel(simgrid::xbt::ReplayAction& action) override;
193 class RecvAction : public ReplayAction<SendRecvParser> {
195 RequestStorage& req_storage;
198 explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
199 void kernel(simgrid::xbt::ReplayAction& action) override;
202 class ComputeAction : public ReplayAction<ComputeParser> {
204 explicit ComputeAction() : ReplayAction("compute") {}
205 void kernel(simgrid::xbt::ReplayAction& action) override;
208 class TestAction : public ReplayAction<WaitTestParser> {
210 RequestStorage& req_storage;
213 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
214 void kernel(simgrid::xbt::ReplayAction& action) override;
217 class InitAction : public ReplayAction<ActionArgParser> {
219 explicit InitAction() : ReplayAction("Init") {}
220 void kernel(simgrid::xbt::ReplayAction& action) override;
223 class CommunicatorAction : public ReplayAction<ActionArgParser> {
225 explicit CommunicatorAction() : ReplayAction("Comm") {}
226 void kernel(simgrid::xbt::ReplayAction& action) override;
229 class WaitAllAction : public ReplayAction<ActionArgParser> {
231 RequestStorage& req_storage;
234 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitAll"), req_storage(storage) {}
235 void kernel(simgrid::xbt::ReplayAction& action) override;
238 class BarrierAction : public ReplayAction<ActionArgParser> {
240 explicit BarrierAction() : ReplayAction("barrier") {}
241 void kernel(simgrid::xbt::ReplayAction& action) override;
244 class BcastAction : public ReplayAction<BcastArgParser> {
246 explicit BcastAction() : ReplayAction("bcast") {}
247 void kernel(simgrid::xbt::ReplayAction& action) override;
250 class ReduceAction : public ReplayAction<ReduceArgParser> {
252 explicit ReduceAction() : ReplayAction("reduce") {}
253 void kernel(simgrid::xbt::ReplayAction& action) override;
256 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
258 explicit AllReduceAction() : ReplayAction("allReduce") {}
259 void kernel(simgrid::xbt::ReplayAction& action) override;
262 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
264 explicit AllToAllAction() : ReplayAction("allToAll") {}
265 void kernel(simgrid::xbt::ReplayAction& action) override;
268 class GatherAction : public ReplayAction<GatherArgParser> {
270 explicit GatherAction(std::string name) : ReplayAction(name) {}
271 void kernel(simgrid::xbt::ReplayAction& action) override;
274 class GatherVAction : public ReplayAction<GatherVArgParser> {
276 explicit GatherVAction(std::string name) : ReplayAction(name) {}
277 void kernel(simgrid::xbt::ReplayAction& action) override;
280 class ScatterAction : public ReplayAction<ScatterArgParser> {
282 explicit ScatterAction() : ReplayAction("scatter") {}
283 void kernel(simgrid::xbt::ReplayAction& action) override;
286 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
288 explicit ScatterVAction() : ReplayAction("scatterV") {}
289 void kernel(simgrid::xbt::ReplayAction& action) override;
292 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
294 explicit ReduceScatterAction() : ReplayAction("reduceScatter") {}
295 void kernel(simgrid::xbt::ReplayAction& action) override;
298 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
300 explicit AllToAllVAction() : ReplayAction("allToAllV") {}
301 void kernel(simgrid::xbt::ReplayAction& action) override;