Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Use unsigned char* for smpi buffers.
[simgrid.git] / include / simgrid / smpi / replay.hpp
index 681c6b3..27aceab 100644 (file)
@@ -1,31 +1,40 @@
-/* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2009-2019. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
-#ifndef SMPI_REPLAY_HPP
-#define SMPI_REPLAY_HPP
+#ifndef SMPI_REPLAY_HPP_
+#define SMPI_REPLAY_HPP_
 
-#include "private.hpp"
+#include "src/smpi/include/smpi_actor.hpp"
+
+#include <boost/algorithm/string/join.hpp>
 #include <xbt/replay.hpp>
+#include <xbt/ex.h>
 
+#include <memory>
 #include <sstream>
 
-#define CHECK_ACTION_PARAMS(action, mandatory, optional)                                                             \
-{                                                                                                                    \
-  if (action.size() < static_cast<unsigned long>(mandatory + 2)) {                                                   \
-    std::stringstream ss;                                                                                            \
-    for (const auto& elem : action) {                                                                                \
-      ss << elem << " ";                                                                                             \
-    }                                                                                                                \
-    THROWF(arg_error, 0, "%s replay failed.\n"                                                                       \
-                         "%zu items were given on the line. First two should be process_id and action.  "            \
-                         "This action needs after them %lu mandatory arguments, and accepts %lu optional ones. \n"   \
-                         "The full line that was given is:\n   %s\n"                                                 \
-                         "Please contact the Simgrid team if support is needed",                                     \
-           __func__, action.size(), static_cast<unsigned long>(mandatory), static_cast<unsigned long>(optional),     \
-           ss.str().c_str());                                                                                        \
-  }                                                                                                                  \
-}
+#define CHECK_ACTION_PARAMS(action, mandatory, optional)                                                               \
+  {                                                                                                                    \
+    if (action.size() < static_cast<unsigned long>(mandatory + 2)) {                                                   \
+      std::stringstream ss;                                                                                            \
+      ss << __func__ << " replay failed.\n"                                                                            \
+         << action.size() << " items were given on the line. First two should be process_id and action.  "             \
+         << "This action needs after them " << mandatory << " mandatory arguments, and accepts " << optional           \
+         << " optional ones. \n"                                                                                       \
+         << "The full line that was given is:\n   ";                                                                   \
+      for (const auto& elem : action) {                                                                                \
+        ss << elem << " ";                                                                                             \
+      }                                                                                                                \
+      ss << "\nPlease contact the Simgrid team if support is needed";                                                  \
+      throw std::invalid_argument(ss.str());                                                                           \
+    }                                                                                                                  \
+  }
+
+XBT_PRIVATE unsigned char* smpi_get_tmp_sendbuffer(size_t size);
+XBT_PRIVATE unsigned char* smpi_get_tmp_recvbuffer(size_t size);
+
+XBT_PRIVATE void log_timed_action(simgrid::xbt::ReplayAction& action, double clock);
 
 namespace simgrid {
 namespace smpi {
@@ -40,7 +49,7 @@ class RequestStorage; // Forward decl
 class ActionArgParser {
 public:
   virtual ~ActionArgParser() = default;
-  virtual void parse(simgrid::xbt::ReplayAction& action, std::string name) { CHECK_ACTION_PARAMS(action, 0, 0) }
+  virtual void parse(simgrid::xbt::ReplayAction& action, const std::string& name) { CHECK_ACTION_PARAMS(action, 0, 0) }
 };
 
 class WaitTestParser : public ActionArgParser {
@@ -49,7 +58,7 @@ public:
   int dst;
   int tag;
 
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 class SendRecvParser : public ActionArgParser {
@@ -60,7 +69,7 @@ public:
   int tag;
   MPI_Datatype datatype1 = MPI_DEFAULT_TYPE;
 
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 class ComputeParser : public ActionArgParser {
@@ -68,7 +77,7 @@ public:
   /* communication partner; if we send, this is the receiver and vice versa */
   double flops;
 
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 class CollCommParser : public ActionArgParser {
@@ -85,27 +94,27 @@ public:
 
 class BcastArgParser : public CollCommParser {
 public:
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 class ReduceArgParser : public CollCommParser {
 public:
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 class AllReduceArgParser : public CollCommParser {
 public:
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 class AllToAllArgParser : public CollCommParser {
 public:
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 class GatherArgParser : public CollCommParser {
 public:
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 class GatherVArgParser : public CollCommParser {
@@ -113,12 +122,12 @@ public:
   int recv_size_sum;
   std::shared_ptr<std::vector<int>> recvcounts;
   std::vector<int> disps;
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 class ScatterArgParser : public CollCommParser {
 public:
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 class ScatterVArgParser : public CollCommParser {
@@ -127,7 +136,7 @@ public:
   int send_size_sum;
   std::shared_ptr<std::vector<int>> sendcounts;
   std::vector<int> disps;
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 class ReduceScatterArgParser : public CollCommParser {
@@ -135,7 +144,7 @@ public:
   int recv_size_sum;
   std::shared_ptr<std::vector<int>> recvcounts;
   std::vector<int> disps;
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 class AllToAllVArgParser : public CollCommParser {
@@ -148,29 +157,38 @@ public:
   std::vector<int> recvdisps;
   int send_buf_size;
   int recv_buf_size;
-  void parse(simgrid::xbt::ReplayAction& action, std::string name) override;
+  void parse(simgrid::xbt::ReplayAction& action, const std::string& name) override;
 };
 
 /**
  * Base class for all ReplayActions.
  * Note that this class actually implements the behavior of each action
- * while the parsing of the replay arguments is done in the ActionArgParser class.
+ * while the parsing of the replay arguments is done in the @ref ActionArgParser class.
  * In other words: The logic goes here, the setup is done by the ActionArgParser.
  */
 template <class T> class ReplayAction {
 protected:
   const std::string name;
-  const int my_proc_id;
+  const aid_t my_proc_id;
   T args;
 
 public:
-  explicit ReplayAction(std::string name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
+  explicit ReplayAction(const std::string& name) : name(name), my_proc_id(simgrid::s4u::this_actor::get_pid()) {}
   virtual ~ReplayAction() = default;
 
-  virtual void execute(simgrid::xbt::ReplayAction& action);
+  void execute(simgrid::xbt::ReplayAction& action)
+  {
+    // Needs to be re-initialized for every action, hence here
+    double start_time = smpi_process()->simulated_elapsed();
+    args.parse(action, name);
+    kernel(action);
+    if (name != "Init")
+      log_timed_action(action, start_time);
+  }
+
   virtual void kernel(simgrid::xbt::ReplayAction& action) = 0;
-  void* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
-  void* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
+  unsigned char* send_buffer(int size) { return smpi_get_tmp_sendbuffer(size); }
+  unsigned char* recv_buffer(int size) { return smpi_get_tmp_recvbuffer(size); }
 };
 
 class WaitAction : public ReplayAction<WaitTestParser> {
@@ -187,7 +205,7 @@ private:
   RequestStorage& req_storage;
 
 public:
-  explicit SendAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
+  explicit SendAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
   void kernel(simgrid::xbt::ReplayAction& action) override;
 };
 
@@ -196,7 +214,7 @@ private:
   RequestStorage& req_storage;
 
 public:
-  explicit RecvAction(std::string name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
+  explicit RecvAction(const std::string& name, RequestStorage& storage) : ReplayAction(name), req_storage(storage) {}
   void kernel(simgrid::xbt::ReplayAction& action) override;
 };
 
@@ -232,7 +250,7 @@ private:
   RequestStorage& req_storage;
 
 public:
-  explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitAll"), req_storage(storage) {}
+  explicit WaitAllAction(RequestStorage& storage) : ReplayAction("waitall"), req_storage(storage) {}
   void kernel(simgrid::xbt::ReplayAction& action) override;
 };
 
@@ -256,25 +274,25 @@ public:
 
 class AllReduceAction : public ReplayAction<AllReduceArgParser> {
 public:
-  explicit AllReduceAction() : ReplayAction("allReduce") {}
+  explicit AllReduceAction() : ReplayAction("allreduce") {}
   void kernel(simgrid::xbt::ReplayAction& action) override;
 };
 
 class AllToAllAction : public ReplayAction<AllToAllArgParser> {
 public:
-  explicit AllToAllAction() : ReplayAction("allToAll") {}
+  explicit AllToAllAction() : ReplayAction("alltoall") {}
   void kernel(simgrid::xbt::ReplayAction& action) override;
 };
 
 class GatherAction : public ReplayAction<GatherArgParser> {
 public:
-  explicit GatherAction(std::string name) : ReplayAction(name) {}
+  explicit GatherAction(const std::string& name) : ReplayAction(name) {}
   void kernel(simgrid::xbt::ReplayAction& action) override;
 };
 
 class GatherVAction : public ReplayAction<GatherVArgParser> {
 public:
-  explicit GatherVAction(std::string name) : ReplayAction(name) {}
+  explicit GatherVAction(const std::string& name) : ReplayAction(name) {}
   void kernel(simgrid::xbt::ReplayAction& action) override;
 };
 
@@ -286,19 +304,19 @@ public:
 
 class ScatterVAction : public ReplayAction<ScatterVArgParser> {
 public:
-  explicit ScatterVAction() : ReplayAction("scatterV") {}
+  explicit ScatterVAction() : ReplayAction("scatterv") {}
   void kernel(simgrid::xbt::ReplayAction& action) override;
 };
 
 class ReduceScatterAction : public ReplayAction<ReduceScatterArgParser> {
 public:
-  explicit ReduceScatterAction() : ReplayAction("reduceScatter") {}
+  explicit ReduceScatterAction() : ReplayAction("reducescatter") {}
   void kernel(simgrid::xbt::ReplayAction& action) override;
 };
 
 class AllToAllVAction : public ReplayAction<AllToAllVArgParser> {
 public:
-  explicit AllToAllVAction() : ReplayAction("allToAllV") {}
+  explicit AllToAllVAction() : ReplayAction("alltoallv") {}
   void kernel(simgrid::xbt::ReplayAction& action) override;
 };
 }