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
9 #include <xbt/replay.hpp>
13 #define CHECK_ACTION_PARAMS(action, mandatory, optional) \
15 if (action.size() < static_cast<unsigned long>(mandatory + 2)) { \
16 std::stringstream ss; \
17 for (const auto& elem : action) { \
20 THROWF(arg_error, 0, "%s replay failed.\n" \
21 "%zu items were given on the line. First two should be process_id and action. " \
22 "This action needs after them %lu mandatory arguments, and accepts %lu optional ones. \n" \
23 "The full line that was given is:\n %s\n" \
24 "Please contact the Simgrid team if support is needed", \
25 __func__, action.size(), static_cast<unsigned long>(mandatory), static_cast<unsigned long>(optional), \
33 extern MPI_Datatype MPI_DEFAULT_TYPE;
35 class RequestStorage; // Forward decl
38 * Base class for all parsers.
40 class ActionArgParser {
42 virtual ~ActionArgParser() = default;
43 virtual void parse(simgrid::xbt::ReplayAction& action, std::string name) { CHECK_ACTION_PARAMS(action, 0, 0) }
46 class WaitTestParser : public ActionArgParser {
52 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
55 class SendRecvParser : public ActionArgParser {
57 /* communication partner; if we send, this is the receiver and vice versa */
61 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
63 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
66 class ComputeParser : public ActionArgParser {
68 /* communication partner; if we send, this is the receiver and vice versa */
71 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
74 class CollCommParser : public ActionArgParser {
82 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
83 MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
86 class BcastArgParser : public CollCommParser {
88 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
91 class ReduceArgParser : public CollCommParser {
93 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
96 class AllReduceArgParser : public CollCommParser {
98 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
101 class AllToAllArgParser : public CollCommParser {
103 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
106 class GatherArgParser : public CollCommParser {
108 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
111 class GatherVArgParser : public CollCommParser {
114 std::shared_ptr<std::vector<int>> recvcounts;
115 std::vector<int> disps;
116 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
119 class ScatterArgParser : public CollCommParser {
121 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
124 class ScatterVArgParser : public CollCommParser {
128 std::shared_ptr<std::vector<int>> sendcounts;
129 std::vector<int> disps;
130 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
133 class ReduceScatterArgParser : public CollCommParser {
136 std::shared_ptr<std::vector<int>> recvcounts;
137 std::vector<int> disps;
138 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
141 class AllToAllVArgParser : public CollCommParser {
145 std::shared_ptr<std::vector<int>> recvcounts;
146 std::shared_ptr<std::vector<int>> sendcounts;
147 std::vector<int> senddisps;
148 std::vector<int> recvdisps;
151 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
155 * Base class for all ReplayActions.
156 * Note that this class actually implements the behavior of each action
157 * while the parsing of the replay arguments is done in the ActionArgParser class.
158 * In other words: The logic goes here, the setup is done by the ActionArgParser.
160 template <class T> class ReplayAction {
162 const std::string name;
163 const int my_proc_id;
167 explicit ReplayAction(std::string name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
168 virtual ~ReplayAction() = default;
170 virtual void execute(simgrid::xbt::ReplayAction& action);
171 virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
172 void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
173 void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
176 class WaitAction : public ReplayAction<WaitTestParser> {
178 RequestStorage& req_storage;
181 explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
182 void kernel(simgrid::xbt::ReplayAction& action) override;
185 class SendAction : public ReplayAction<SendRecvParser> {
187 RequestStorage& req_storage;
190 explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
191 void kernel(simgrid::xbt::ReplayAction& action) override;
194 class RecvAction : public ReplayAction<SendRecvParser> {
196 RequestStorage& req_storage;
199 explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
200 void kernel(simgrid::xbt::ReplayAction& action) override;
203 class ComputeAction : public ReplayAction<ComputeParser> {
205 explicit ComputeAction() : ReplayAction("compute") {}
206 void kernel(simgrid::xbt::ReplayAction& action) override;
209 class TestAction : public ReplayAction<WaitTestParser> {
211 RequestStorage& req_storage;
214 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
215 void kernel(simgrid::xbt::ReplayAction& action) override;
218 class InitAction : public ReplayAction<ActionArgParser> {
220 explicit InitAction() : ReplayAction("Init") {}
221 void kernel(simgrid::xbt::ReplayAction& action) override;
224 class CommunicatorAction : public ReplayAction<ActionArgParser> {
226 explicit CommunicatorAction() : ReplayAction("Comm") {}
227 void kernel(simgrid::xbt::ReplayAction& action) override;
230 class WaitAllAction : public ReplayAction<ActionArgParser> {
232 RequestStorage& req_storage;
235 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitAll"), req_storage(storage) {}
236 void kernel(simgrid::xbt::ReplayAction& action) override;
239 class BarrierAction : public ReplayAction<ActionArgParser> {
241 explicit BarrierAction() : ReplayAction("barrier") {}
242 void kernel(simgrid::xbt::ReplayAction& action) override;
245 class BcastAction : public ReplayAction<BcastArgParser> {
247 explicit BcastAction() : ReplayAction("bcast") {}
248 void kernel(simgrid::xbt::ReplayAction& action) override;
251 class ReduceAction : public ReplayAction<ReduceArgParser> {
253 explicit ReduceAction() : ReplayAction("reduce") {}
254 void kernel(simgrid::xbt::ReplayAction& action) override;
257 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
259 explicit AllReduceAction() : ReplayAction("allReduce") {}
260 void kernel(simgrid::xbt::ReplayAction& action) override;
263 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
265 explicit AllToAllAction() : ReplayAction("allToAll") {}
266 void kernel(simgrid::xbt::ReplayAction& action) override;
269 class GatherAction : public ReplayAction<GatherArgParser> {
271 explicit GatherAction(std::string name) : ReplayAction(name) {}
272 void kernel(simgrid::xbt::ReplayAction& action) override;
275 class GatherVAction : public ReplayAction<GatherVArgParser> {
277 explicit GatherVAction(std::string name) : ReplayAction(name) {}
278 void kernel(simgrid::xbt::ReplayAction& action) override;
281 class ScatterAction : public ReplayAction<ScatterArgParser> {
283 explicit ScatterAction() : ReplayAction("scatter") {}
284 void kernel(simgrid::xbt::ReplayAction& action) override;
287 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
289 explicit ScatterVAction() : ReplayAction("scatterV") {}
290 void kernel(simgrid::xbt::ReplayAction& action) override;
293 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
295 explicit ReduceScatterAction() : ReplayAction("reduceScatter") {}
296 void kernel(simgrid::xbt::ReplayAction& action) override;
299 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
301 explicit AllToAllVAction() : ReplayAction("allToAllV") {}
302 void kernel(simgrid::xbt::ReplayAction& action) override;