1 /* Copyright (c) 2009-2020. 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 "src/smpi/include/smpi_actor.hpp"
10 #include <boost/algorithm/string/join.hpp>
11 #include <xbt/replay.hpp>
17 #define CHECK_ACTION_PARAMS(action, mandatory, optional) \
19 if ((action).size() < static_cast<unsigned long>((mandatory) + 2)) { \
20 std::stringstream ss; \
21 ss << __func__ << " replay failed.\n" \
22 << (action).size() << " items were given on the line. First two should be process_id and action. " \
23 << "This action needs after them " << (mandatory) << " mandatory arguments, and accepts " << (optional) \
24 << " optional ones. \n" \
25 << "The full line that was given is:\n "; \
26 for (const auto& elem : (action)) { \
29 ss << "\nPlease contact the Simgrid team if support is needed"; \
30 throw std::invalid_argument(ss.str()); \
34 XBT_PRIVATE unsigned char* smpi_get_tmp_sendbuffer(size_t size);
35 XBT_PRIVATE unsigned char* smpi_get_tmp_recvbuffer(size_t size);
37 XBT_PRIVATE void log_timed_action(const simgrid::xbt::ReplayAction& action, double clock);
42 extern MPI_Datatype MPI_DEFAULT_TYPE;
44 class RequestStorage; // Forward decl
47 * Base class for all parsers.
49 class ActionArgParser {
51 virtual ~ActionArgParser() = default;
52 virtual void parse(xbt::ReplayAction& action, const std::string& name) { CHECK_ACTION_PARAMS(action, 0, 0) }
55 class WaitTestParser : public ActionArgParser {
61 void parse(xbt::ReplayAction& action, const std::string& name) override;
64 class SendRecvParser : public ActionArgParser {
66 /* communication partner; if we send, this is the receiver and vice versa */
70 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
72 void parse(xbt::ReplayAction& action, const std::string& name) override;
75 class ComputeParser : public ActionArgParser {
79 void parse(xbt::ReplayAction& action, const std::string& name) override;
82 class SleepParser : public ActionArgParser {
86 void parse(xbt::ReplayAction& action, const std::string& name) override;
89 class LocationParser : public ActionArgParser {
94 void parse(xbt::ReplayAction& action, const std::string& name) override;
97 class CollCommParser : public ActionArgParser {
105 MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
106 MPI_Datatype datatype2 = MPI_DEFAULT_TYPE;
109 class BcastArgParser : public CollCommParser {
111 void parse(xbt::ReplayAction& action, const std::string& name) override;
114 class ReduceArgParser : public CollCommParser {
116 void parse(xbt::ReplayAction& action, const std::string& name) override;
119 class AllReduceArgParser : public CollCommParser {
121 void parse(xbt::ReplayAction& action, const std::string& name) override;
124 class AllToAllArgParser : public CollCommParser {
126 void parse(xbt::ReplayAction& action, const std::string& name) override;
129 class GatherArgParser : public CollCommParser {
131 void parse(xbt::ReplayAction& action, const std::string& name) override;
134 class GatherVArgParser : public CollCommParser {
137 std::shared_ptr<std::vector<int>> recvcounts;
138 std::vector<int> disps;
139 void parse(xbt::ReplayAction& action, const std::string& name) override;
142 class ScatterArgParser : public CollCommParser {
144 void parse(xbt::ReplayAction& action, const std::string& name) override;
147 class ScatterVArgParser : public CollCommParser {
151 std::shared_ptr<std::vector<int>> sendcounts;
152 std::vector<int> disps;
153 void parse(xbt::ReplayAction& action, const std::string& name) override;
156 class ReduceScatterArgParser : public CollCommParser {
159 std::shared_ptr<std::vector<int>> recvcounts;
160 std::vector<int> disps;
161 void parse(xbt::ReplayAction& action, const std::string& name) override;
164 class AllToAllVArgParser : public CollCommParser {
168 std::shared_ptr<std::vector<int>> recvcounts;
169 std::shared_ptr<std::vector<int>> sendcounts;
170 std::vector<int> senddisps;
171 std::vector<int> recvdisps;
174 void parse(xbt::ReplayAction& action, const std::string& name) override;
178 * Base class for all ReplayActions.
179 * Note that this class actually implements the behavior of each action
180 * while the parsing of the replay arguments is done in the @ref ActionArgParser class.
181 * In other words: The logic goes here, the setup is done by the ActionArgParser.
183 template <class T> class ReplayAction {
184 const std::string name_;
185 const aid_t my_proc_id_ = s4u::this_actor::get_pid();
189 const std::string& get_name() const { return name_; }
190 aid_t get_pid() const { return my_proc_id_; }
191 const T& get_args() const { return args_; }
194 explicit ReplayAction(const std::string& name) : name_(name) {}
195 virtual ~ReplayAction() = default;
197 void execute(xbt::ReplayAction& action)
199 // Needs to be re-initialized for every action, hence here
200 double start_time = smpi_process()->simulated_elapsed();
201 args_.parse(action, name_);
204 log_timed_action(action, start_time);
207 virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
208 unsigned char* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
209 unsigned char* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
212 class WaitAction : public ReplayAction<WaitTestParser> {
213 RequestStorage& req_storage;
216 explicit WaitAction(RequestStorage& storage) : ReplayAction("Wait"), req_storage(storage) {}
217 void kernel(xbt::ReplayAction& action) override;
220 class SendAction : public ReplayAction<SendRecvParser> {
221 RequestStorage& req_storage;
224 explicit SendAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
225 void kernel(xbt::ReplayAction& action) override;
228 class RecvAction : public ReplayAction<SendRecvParser> {
229 RequestStorage& req_storage;
232 explicit RecvAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
233 void kernel(xbt::ReplayAction& action) override;
236 class ComputeAction : public ReplayAction<ComputeParser> {
238 explicit ComputeAction() : ReplayAction("compute") {}
239 void kernel(xbt::ReplayAction& action) override;
242 class SleepAction : public ReplayAction<SleepParser> {
244 explicit SleepAction() : ReplayAction("sleep") {}
245 void kernel(xbt::ReplayAction& action) override;
248 class LocationAction : public ReplayAction<LocationParser> {
250 explicit LocationAction() : ReplayAction("location") {}
251 void kernel(xbt::ReplayAction& action) override;
254 class TestAction : public ReplayAction<WaitTestParser> {
256 RequestStorage& req_storage;
259 explicit TestAction(RequestStorage& storage) : ReplayAction("Test"), req_storage(storage) {}
260 void kernel(xbt::ReplayAction& action) override;
263 class InitAction : public ReplayAction<ActionArgParser> {
265 explicit InitAction() : ReplayAction("Init") {}
266 void kernel(xbt::ReplayAction& action) override;
269 class CommunicatorAction : public ReplayAction<ActionArgParser> {
271 explicit CommunicatorAction() : ReplayAction("Comm") {}
272 void kernel(xbt::ReplayAction& action) override;
275 class WaitAllAction : public ReplayAction<ActionArgParser> {
276 RequestStorage& req_storage;
279 explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
280 void kernel(xbt::ReplayAction& action) override;
283 class BarrierAction : public ReplayAction<ActionArgParser> {
285 explicit BarrierAction() : ReplayAction("barrier") {}
286 void kernel(xbt::ReplayAction& action) override;
289 class BcastAction : public ReplayAction<BcastArgParser> {
291 explicit BcastAction() : ReplayAction("bcast") {}
292 void kernel(xbt::ReplayAction& action) override;
295 class ReduceAction : public ReplayAction<ReduceArgParser> {
297 explicit ReduceAction() : ReplayAction("reduce") {}
298 void kernel(xbt::ReplayAction& action) override;
301 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
303 explicit AllReduceAction() : ReplayAction("allreduce") {}
304 void kernel(xbt::ReplayAction& action) override;
307 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
309 explicit AllToAllAction() : ReplayAction("alltoall") {}
310 void kernel(xbt::ReplayAction& action) override;
313 class GatherAction : public ReplayAction<GatherArgParser> {
315 explicit GatherAction(const std::string& name) : ReplayAction(name) {}
316 void kernel(xbt::ReplayAction& action) override;
319 class GatherVAction : public ReplayAction<GatherVArgParser> {
321 explicit GatherVAction(const std::string& name) : ReplayAction(name) {}
322 void kernel(xbt::ReplayAction& action) override;
325 class ScatterAction : public ReplayAction<ScatterArgParser> {
327 explicit ScatterAction() : ReplayAction("scatter") {}
328 void kernel(xbt::ReplayAction& action) override;
331 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
333 explicit ScatterVAction() : ReplayAction("scatterv") {}
334 void kernel(xbt::ReplayAction& action) override;
337 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
339 explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
340 void kernel(xbt::ReplayAction& action) override;
343 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
345 explicit AllToAllVAction() : ReplayAction("alltoallv") {}
346 void kernel(xbt::ReplayAction& action) override;
349 } // namespace replay
351 } // namespace simgrid