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 <simgrid/s4u/Actor.hpp>
11 #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 extern XBT_PRIVATE void* smpi_get_tmp_sendbuffer(int size);
34 extern XBT_PRIVATE void* smpi_get_tmp_recvbuffer(int size);
35 extern XBT_PRIVATE void smpi_free_tmp_buffer(void* buf);
36 extern XBT_PRIVATE void smpi_free_replay_tmp_buffers();
41 extern MPI_Datatype MPI_DEFAULT_TYPE;
43 class RequestStorage; // Forward decl
46 * Base class for all parsers.
48 class ActionArgParser {
50 virtual ~ActionArgParser() = default;
51 virtual void parse(simgrid::xbt::ReplayAction& action, std::string name) { CHECK_ACTION_PARAMS(action, 0, 0) }
54 class WaitTestParser : public ActionArgParser {
60 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
63 class SendRecvParser : public ActionArgParser {
65 /* communication partner; if we send, this is the receiver and vice versa */
69 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
71 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
74 class ComputeParser : public ActionArgParser {
76 /* communication partner; if we send, this is the receiver and vice versa */
79 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
82 class CollCommParser : public ActionArgParser {
90 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
91 MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
94 class BcastArgParser : public CollCommParser {
96 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
99 class ReduceArgParser : public CollCommParser {
101 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
104 class AllReduceArgParser : public CollCommParser {
106 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
109 class AllToAllArgParser : public CollCommParser {
111 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
114 class GatherArgParser : public CollCommParser {
116 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
119 class GatherVArgParser : public CollCommParser {
122 std::shared_ptr<std::vector<int>> recvcounts;
123 std::vector<int> disps;
124 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
127 class ScatterArgParser : public CollCommParser {
129 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
132 class ScatterVArgParser : public CollCommParser {
136 std::shared_ptr<std::vector<int>> sendcounts;
137 std::vector<int> disps;
138 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
141 class ReduceScatterArgParser : public CollCommParser {
144 std::shared_ptr<std::vector<int>> recvcounts;
145 std::vector<int> disps;
146 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
149 class AllToAllVArgParser : public CollCommParser {
153 std::shared_ptr<std::vector<int>> recvcounts;
154 std::shared_ptr<std::vector<int>> sendcounts;
155 std::vector<int> senddisps;
156 std::vector<int> recvdisps;
159 void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
163 * Base class for all ReplayActions.
164 * Note that this class actually implements the behavior of each action
165 * while the parsing of the replay arguments is done in the ActionArgParser class.
166 * In other words: The logic goes here, the setup is done by the ActionArgParser.
168 template <class T> class ReplayAction {
170 const std::string name;
171 const int my_proc_id;
175 explicit ReplayAction(std::string name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
176 virtual ~ReplayAction() = default;
178 virtual void execute(simgrid::xbt::ReplayAction& action);
179 virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
180 void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
181 void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
184 class WaitAction : public ReplayAction<WaitTestParser> {
186 RequestStorage& req_storage;
189 explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
190 void kernel(simgrid::xbt::ReplayAction& action) override;
193 class SendAction : public ReplayAction<SendRecvParser> {
195 RequestStorage& req_storage;
198 explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
199 void kernel(simgrid::xbt::ReplayAction& action) override;
202 class RecvAction : public ReplayAction<SendRecvParser> {
204 RequestStorage& req_storage;
207 explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
208 void kernel(simgrid::xbt::ReplayAction& action) override;
211 class ComputeAction : public ReplayAction<ComputeParser> {
213 explicit ComputeAction() : ReplayAction("compute") {}
214 void kernel(simgrid::xbt::ReplayAction& action) override;
217 class TestAction : public ReplayAction<WaitTestParser> {
219 RequestStorage& req_storage;
222 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
223 void kernel(simgrid::xbt::ReplayAction& action) override;
226 class InitAction : public ReplayAction<ActionArgParser> {
228 explicit InitAction() : ReplayAction("Init") {}
229 void kernel(simgrid::xbt::ReplayAction& action) override;
232 class CommunicatorAction : public ReplayAction<ActionArgParser> {
234 explicit CommunicatorAction() : ReplayAction("Comm") {}
235 void kernel(simgrid::xbt::ReplayAction& action) override;
238 class WaitAllAction : public ReplayAction<ActionArgParser> {
240 RequestStorage& req_storage;
243 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitAll"), req_storage(storage) {}
244 void kernel(simgrid::xbt::ReplayAction& action) override;
247 class BarrierAction : public ReplayAction<ActionArgParser> {
249 explicit BarrierAction() : ReplayAction("barrier") {}
250 void kernel(simgrid::xbt::ReplayAction& action) override;
253 class BcastAction : public ReplayAction<BcastArgParser> {
255 explicit BcastAction() : ReplayAction("bcast") {}
256 void kernel(simgrid::xbt::ReplayAction& action) override;
259 class ReduceAction : public ReplayAction<ReduceArgParser> {
261 explicit ReduceAction() : ReplayAction("reduce") {}
262 void kernel(simgrid::xbt::ReplayAction& action) override;
265 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
267 explicit AllReduceAction() : ReplayAction("allReduce") {}
268 void kernel(simgrid::xbt::ReplayAction& action) override;
271 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
273 explicit AllToAllAction() : ReplayAction("allToAll") {}
274 void kernel(simgrid::xbt::ReplayAction& action) override;
277 class GatherAction : public ReplayAction<GatherArgParser> {
279 explicit GatherAction(std::string name) : ReplayAction(name) {}
280 void kernel(simgrid::xbt::ReplayAction& action) override;
283 class GatherVAction : public ReplayAction<GatherVArgParser> {
285 explicit GatherVAction(std::string name) : ReplayAction(name) {}
286 void kernel(simgrid::xbt::ReplayAction& action) override;
289 class ScatterAction : public ReplayAction<ScatterArgParser> {
291 explicit ScatterAction() : ReplayAction("scatter") {}
292 void kernel(simgrid::xbt::ReplayAction& action) override;
295 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
297 explicit ScatterVAction() : ReplayAction("scatterV") {}
298 void kernel(simgrid::xbt::ReplayAction& action) override;
301 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
303 explicit ReduceScatterAction() : ReplayAction("reduceScatter") {}
304 void kernel(simgrid::xbt::ReplayAction& action) override;
307 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
309 explicit AllToAllVAction() : ReplayAction("allToAllV") {}
310 void kernel(simgrid::xbt::ReplayAction& action) override;