Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into stoprofiles
authorYann Duplouy <yann.duplouy@inria.fr>
Fri, 22 Nov 2019 13:16:21 +0000 (14:16 +0100)
committerYann Duplouy <yann.duplouy@inria.fr>
Fri, 22 Nov 2019 13:16:21 +0000 (14:16 +0100)
MANIFEST.in
src/kernel/resource/profile/Profile.cpp
src/kernel/resource/profile/Profile.hpp
src/kernel/resource/profile/Profile_test.cpp
src/kernel/resource/profile/StochasticDatedValue.cpp [new file with mode: 0644]
src/kernel/resource/profile/StochasticDatedValue.hpp [new file with mode: 0644]
tools/cmake/DefinePackages.cmake

index af8fabf..c5f8e88 100644 (file)
@@ -2165,6 +2165,8 @@ include src/kernel/resource/profile/FutureEvtSet.hpp
 include src/kernel/resource/profile/Profile.cpp
 include src/kernel/resource/profile/Profile.hpp
 include src/kernel/resource/profile/Profile_test.cpp
+include src/kernel/resource/profile/StochasticDatedValue.cpp
+include src/kernel/resource/profile/StochasticDatedValue.hpp
 include src/kernel/routing/ClusterZone.cpp
 include src/kernel/routing/DijkstraZone.cpp
 include src/kernel/routing/DragonflyZone.cpp
index 482ee82..3f2ffba 100644 (file)
@@ -8,6 +8,7 @@
 #include "src/kernel/resource/profile/DatedValue.hpp"
 #include "src/kernel/resource/profile/Event.hpp"
 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
+#include "src/kernel/resource/profile/StochasticDatedValue.hpp"
 #include "src/surf/surf_interface.hpp"
 
 #include <boost/algorithm/string.hpp>
@@ -27,7 +28,9 @@ Profile::Profile()
 {
   /* Add the first fake event storing the time at which the trace begins */
   DatedValue val(0, -1);
+  StochasticDatedValue stoval(0, -1);
   event_list.push_back(val);
+  stochastic_event_list.push_back(stoval);
 }
 Profile::~Profile() = default;
 
@@ -45,6 +48,10 @@ Event* Profile::schedule(FutureEvtSet* fes, resource::Resource* resource)
 
   fes_ = fes;
   fes_->add_event(0.0 /* start time */, event);
+  if (stochastic > 0) {
+    xbt_assert((event->idx < stochastic_event_list.size()), "Your profile should have at least one stochastic event!");
+    futureDV = stochastic_event_list.at(event->idx).get_datedvalue();
+  }
 
   return event;
 }
@@ -53,19 +60,37 @@ Event* Profile::schedule(FutureEvtSet* fes, resource::Resource* resource)
 DatedValue Profile::next(Event* event)
 {
   double event_date  = fes_->next_date();
-  DatedValue dateVal = event_list.at(event->idx);
-
-  if (event->idx < event_list.size() - 1) {
-    fes_->add_event(event_date + dateVal.date_, event);
-    event->idx++;
-  } else if (dateVal.date_ > 0) { /* Last element. Shall we loop? */
-    fes_->add_event(event_date + dateVal.date_, event);
-    event->idx = 1; /* idx=0 is a placeholder to store when events really start */
-  } else {          /* If we don't loop, we don't need this event anymore */
-    event->free_me = true;
-  }
 
-  return dateVal;
+  if (not stochastic) {
+    DatedValue dateVal = event_list.at(event->idx);
+
+    if (event->idx < event_list.size() - 1) {
+      fes_->add_event(event_date + dateVal.date_, event);
+      event->idx++;
+    } else if (dateVal.date_ > 0) { /* Last element. Shall we loop? */
+      fes_->add_event(event_date + dateVal.date_, event);
+      event->idx = 1; /* idx=0 is a placeholder to store when events really start */
+    } else {          /* If we don't loop, we don't need this event anymore */
+      event->free_me = true;
+    }
+    return dateVal;
+  } else {
+    DatedValue dateVal = futureDV;
+    if (event->idx < stochastic_event_list.size() - 1) {
+      event->idx++;
+    } else if (stochasticloop > 0) { /* We have reached the last element and we have to loop. */
+      event->idx = 1;
+    } else {
+      event->free_me = true; /* We have reached the last element, but we don't need to loop. */
+    }
+
+    if (event->free_me == false) { // In the case there is an element, we draw the next event
+      StochasticDatedValue stodateVal = stochastic_event_list.at(event->idx);
+      futureDV                        = stochastic_event_list.at(event->idx).get_datedvalue();
+      fes_->add_event(event_date + futureDV.date_, event);
+    }
+    return dateVal;
+  }
 }
 
 Profile* Profile::from_string(const std::string& name, const std::string& input, double periodicity)
@@ -80,6 +105,7 @@ Profile* Profile::from_string(const std::string& name, const std::string& input,
   boost::split(list, input, boost::is_any_of("\n\r"));
   for (auto val : list) {
     simgrid::kernel::profile::DatedValue event;
+    simgrid::kernel::profile::StochasticDatedValue stochevent;
     linecount++;
     boost::trim(val);
     if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
@@ -88,17 +114,88 @@ Profile* Profile::from_string(const std::string& name, const std::string& input,
       continue;
     if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
       continue;
+    if (val == "STOCHASTIC LOOP") {
+      profile->stochastic     = true;
+      profile->stochasticloop = true;
+      continue;
+    }
+    if (val == "STOCHASTIC") {
+      profile->stochastic = true;
+      continue;
+    }
 
-    XBT_ATTRIB_UNUSED int res = sscanf(val.c_str(), "%lg  %lg\n", &event.date_, &event.value_);
-    xbt_assert(res == 2, "%s:%d: Syntax error in trace\n%s", name.c_str(), linecount, input.c_str());
+    if (profile->stochastic) {
+      unsigned int i;
+      unsigned int j;
+      std::istringstream iss(val);
+      std::vector<std::string> splittedval((std::istream_iterator<std::string>(iss)),
+                                           std::istream_iterator<std::string>());
+
+      xbt_assert(splittedval.size() > 0, "Invalid profile line");
+
+      if (splittedval[0] == "DET") {
+        stochevent.date_law = Dist_Det;
+        i                   = 2;
+      } else if (splittedval[0] == "NORM" || splittedval[0] == "NORMAL" || splittedval[0] == "GAUSS" ||
+                 splittedval[0] == "GAUSSIAN") {
+        stochevent.date_law = Dist_Norm;
+        i                   = 3;
+      } else if (splittedval[0] == "EXP" || splittedval[0] == "EXPONENTIAL") {
+        stochevent.date_law = Dist_Exp;
+        i                   = 2;
+      } else if (splittedval[0] == "UNIF" || splittedval[0] == "UNIFORM") {
+        stochevent.date_law = Dist_Unif;
+        i                   = 3;
+      } else {
+        xbt_assert(false, "Unknown law %s", splittedval[0].c_str());
+        i = 0;
+      }
+
+      xbt_assert(splittedval.size() > i, "Invalid profile line");
+      if (i == 2) {
+        stochevent.date_params = {std::atof(splittedval[1].c_str())};
+      } else if (i == 3) {
+        stochevent.date_params = {std::atof(splittedval[1].c_str()), std::atof(splittedval[2].c_str())};
+      }
+
+      if (splittedval[i] == "DET") {
+        stochevent.value_law = Dist_Det;
+        j                    = 1;
+      } else if (splittedval[i] == "NORM" || splittedval[i] == "NORMAL" || splittedval[i] == "GAUSS" ||
+                 splittedval[i] == "GAUSSIAN") {
+        stochevent.value_law = Dist_Norm;
+        j                    = 2;
+      } else if (splittedval[i] == "EXP" || splittedval[i] == "EXPONENTIAL") {
+        stochevent.value_law = Dist_Exp;
+        j                    = 1;
+      } else if (splittedval[i] == "UNIF" || splittedval[i] == "UNIFORM") {
+        stochevent.value_law = Dist_Unif;
+        j                    = 2;
+      } else {
+        xbt_assert(false, "Unknown law %s", splittedval[i].c_str());
+        j = 0;
+      }
+
+      xbt_assert(splittedval.size() > i + j, "Invalid profile line");
+      if (j == 1) {
+        stochevent.value_params = {std::atof(splittedval[i + 1].c_str())};
+      } else if (j == 2) {
+        stochevent.value_params = {std::atof(splittedval[i + 1].c_str()), std::atof(splittedval[i + 2].c_str())};
+      }
+
+      profile->stochastic_event_list.push_back(stochevent);
+    } else {
+      XBT_ATTRIB_UNUSED int res = sscanf(val.c_str(), "%lg  %lg\n", &event.date_, &event.value_);
+      xbt_assert(res == 2, "%s:%d: Syntax error in trace\n%s", name.c_str(), linecount, input.c_str());
 
-    xbt_assert(last_event->date_ <= event.date_,
-               "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name.c_str(), linecount,
-               last_event->date_, event.date_, input.c_str());
-    last_event->date_ = event.date_ - last_event->date_;
+      xbt_assert(last_event->date_ <= event.date_,
+                 "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name.c_str(), linecount,
+                 last_event->date_, event.date_, input.c_str());
+      last_event->date_ = event.date_ - last_event->date_;
 
-    profile->event_list.push_back(event);
-    last_event = &(profile->event_list.back());
+      profile->event_list.push_back(event);
+      last_event = &(profile->event_list.back());
+    }
   }
   if (last_event) {
     if (periodicity > 0) {
index 3c173db..263a630 100644 (file)
@@ -9,6 +9,7 @@
 #include "simgrid/forward.h"
 #include "src/kernel/resource/profile/DatedValue.hpp"
 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
+#include "src/kernel/resource/profile/StochasticDatedValue.hpp"
 
 #include <queue>
 #include <vector>
@@ -35,9 +36,13 @@ public:
   static Profile* from_string(const std::string& name, const std::string& input, double periodicity);
   // private:
   std::vector<DatedValue> event_list;
+  std::vector<StochasticDatedValue> stochastic_event_list;
 
 private:
-  FutureEvtSet* fes_ = nullptr;
+  FutureEvtSet* fes_  = nullptr;
+  bool stochastic     = false;
+  bool stochasticloop = false;
+  DatedValue futureDV;
 };
 
 } // namespace profile
index c995ef8..7d7648e 100644 (file)
@@ -9,10 +9,12 @@
 #include "src/kernel/resource/profile/DatedValue.hpp"
 #include "src/kernel/resource/profile/Event.hpp"
 #include "src/kernel/resource/profile/Profile.hpp"
+#include "src/kernel/resource/profile/StochasticDatedValue.hpp"
 #include "src/surf/surf_interface.hpp"
 
 #include "xbt/log.h"
 #include "xbt/misc.h"
+#include "xbt/random.hpp"
 
 #include <cmath>
 
@@ -63,6 +65,14 @@ static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char
   return res;
 }
 
+static std::vector<simgrid::kernel::profile::StochasticDatedValue> trace2selist(const char* str)
+{
+  simgrid::kernel::profile::Profile* trace = simgrid::kernel::profile::Profile::from_string("TheName", str, 0);
+  std::vector<simgrid::kernel::profile::StochasticDatedValue> stocevlist = trace->stochastic_event_list;
+  tmgr_finalize();
+  return stocevlist;
+}
+
 TEST_CASE("kernel::profile: Resource profiles, defining the external load", "kernel::profile")
 {
 
@@ -143,4 +153,73 @@ TEST_CASE("kernel::profile: Resource profiles, defining the external load", "ker
 
     REQUIRE(want == got);
   }
+
+  SECTION("One stochastic event (parsing)")
+  {
+    std::vector<simgrid::kernel::profile::StochasticDatedValue> got = trace2selist("STOCHASTIC\n"
+                                                                                   "DET 0 UNIF 10 20");
+
+    std::vector<simgrid::kernel::profile::StochasticDatedValue> want;
+    want.push_back(simgrid::kernel::profile::StochasticDatedValue(0, -1)); // The initial fake event
+    want.push_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Det, {0},
+                                                                  simgrid::kernel::profile::Dist_Unif, {10, 20}));
+
+    REQUIRE(want == got);
+  }
+
+  SECTION("Several stochastic events (all possible parsing forms)")
+  {
+    std::vector<simgrid::kernel::profile::StochasticDatedValue> got = trace2selist("STOCHASTIC\n"
+                                                                                   "DET 0 DET 4\n"
+                                                                                   "NORMAL 25 10 DET 3\n"
+                                                                                   "UNIF 10 20 NORMAL 25 10\n"
+                                                                                   "DET 5 UNIF 5 25");
+
+    std::vector<simgrid::kernel::profile::StochasticDatedValue> want;
+    want.push_back(simgrid::kernel::profile::StochasticDatedValue(0, -1));
+    want.push_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Det, {0},
+                                                                  simgrid::kernel::profile::Dist_Det, {4}));
+    want.push_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Norm, {25, 10},
+                                                                  simgrid::kernel::profile::Dist_Det, {3}));
+    want.push_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Unif, {10, 20},
+                                                                  simgrid::kernel::profile::Dist_Norm, {25, 10}));
+    want.push_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Det, {5},
+                                                                  simgrid::kernel::profile::Dist_Unif, {5, 25}));
+
+    REQUIRE(want == got);
+  }
+
+  SECTION("Two stochastic events (drawing each distribution)")
+  {
+    simgrid::xbt::random::set_mersenne_seed(12345);
+    std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("STOCHASTIC\n"
+                                                                         "DET 0 UNIF 10 20\n"
+                                                                         "EXP 0.05 NORMAL 15 5");
+
+    std::vector<simgrid::kernel::profile::DatedValue> want;
+    // The following values were drawn using the XBT_RNG_xbt method /outside/ the testcase.
+    want.push_back(simgrid::kernel::profile::DatedValue(0, 19.29616086867082813683));
+    want.push_back(simgrid::kernel::profile::DatedValue(2.32719992449416279712, 20.16807234800742065772));
+
+    REQUIRE(want == got);
+  }
+
+  SECTION("Two stochastic events, with a loop")
+  {
+    simgrid::xbt::random::set_mersenne_seed(12345);
+    std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("STOCHASTIC LOOP\n"
+                                                                         "DET 0 UNIF 10 20\n"
+                                                                         "EXP 0.05 NORMAL 15 5\n"
+                                                                         "UNIF 1 2 DET 0");
+
+    // In this case, the main use of the last stochastic event is to set when the first event takes place.
+
+    std::vector<simgrid::kernel::profile::DatedValue> want;
+    want.push_back(simgrid::kernel::profile::DatedValue(0, 19.29616086867082813683));
+    want.push_back(simgrid::kernel::profile::DatedValue(2.32719992449416279712, 20.16807234800742065772));
+    want.push_back(simgrid::kernel::profile::DatedValue(3.51111873684917075167, 0));
+    want.push_back(simgrid::kernel::profile::DatedValue(3.51111873684917075167, 10.39759496468994726115));
+
+    REQUIRE(want == got);
+  }
 }
diff --git a/src/kernel/resource/profile/StochasticDatedValue.cpp b/src/kernel/resource/profile/StochasticDatedValue.cpp
new file mode 100644 (file)
index 0000000..b044334
--- /dev/null
@@ -0,0 +1,75 @@
+/* Copyright (c) 2004-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. */
+
+#include "src/kernel/resource/profile/StochasticDatedValue.hpp"
+#include "xbt.h"
+#include "xbt/random.hpp"
+#include <math.h>
+
+namespace simgrid {
+namespace kernel {
+namespace profile {
+
+double StochasticDatedValue::draw(Distribution law, std::vector<double> params)
+{
+  switch (law) {
+    case Dist_Det:
+      return params[0];
+    case Dist_Exp:
+      return simgrid::xbt::random::exponential(params[0]);
+    case Dist_Unif:
+      return simgrid::xbt::random::uniform_real(params[0], params[1]);
+    case Dist_Norm:
+      return simgrid::xbt::random::normal(params[0], params[1]);
+    default:
+      xbt_assert(false, "Unimplemented distribution");
+      return 0;
+  }
+}
+double StochasticDatedValue::get_value()
+{
+  return draw(value_law, value_params);
+}
+double StochasticDatedValue::get_date()
+{
+  return draw(date_law, date_params);
+}
+DatedValue StochasticDatedValue::get_datedvalue()
+{
+  DatedValue event;
+  event.date_  = get_date();
+  event.value_ = get_value();
+  return event;
+}
+
+bool StochasticDatedValue::operator==(StochasticDatedValue const& e2) const
+{
+  return (e2.date_law == date_law) && (e2.value_law == value_law) && (e2.value_params == value_params) &&
+         (e2.date_params == date_params);
+}
+
+std::ostream& operator<<(std::ostream& out, const StochasticDatedValue& e)
+{
+  out << e.date_law << " (";
+  for (unsigned int i = 0; i < e.date_params.size(); i++) {
+    out << e.date_params[i];
+    if (i != e.date_params.size() - 1) {
+      out << ",";
+    }
+  }
+  out << ") " << e.value_law << " (";
+  for (unsigned int i = 0; i < e.value_params.size(); i++) {
+    out << e.value_params[i];
+    if (i != e.value_params.size() - 1) {
+      out << ",";
+    }
+  }
+  out << ")";
+  return out;
+}
+
+} // namespace profile
+} // namespace kernel
+} // namespace simgrid
diff --git a/src/kernel/resource/profile/StochasticDatedValue.hpp b/src/kernel/resource/profile/StochasticDatedValue.hpp
new file mode 100644 (file)
index 0000000..9528da4
--- /dev/null
@@ -0,0 +1,49 @@
+/* Copyright (c) 2004-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 SIMGRID_KERNEL_PROFILE_STOCHASTICDATEDVALUE
+#define SIMGRID_KERNEL_PROFILE_STOCHASTICDATEDVALUE
+
+#include "simgrid/forward.h"
+#include "src/kernel/resource/profile/DatedValue.hpp"
+#include <vector>
+
+namespace simgrid {
+namespace kernel {
+namespace profile {
+
+enum Distribution { Dist_Exp, Dist_Norm, Dist_Unif, Dist_Det };
+
+class XBT_PUBLIC StochasticDatedValue {
+public:
+  Distribution date_law;
+  std::vector<double> date_params;
+  Distribution value_law;
+  std::vector<double> value_params;
+  DatedValue get_datedvalue();
+  double get_date();
+  double get_value();
+  explicit StochasticDatedValue() = default;
+  explicit StochasticDatedValue(double d, double v)
+      : date_law(Dist_Det), date_params({d}), value_law(Dist_Det), value_params({v})
+  {
+  }
+  explicit StochasticDatedValue(Distribution dl, std::vector<double> dp, Distribution vl, std::vector<double> vp)
+      : date_law(dl), date_params(dp), value_law(vl), value_params(vp)
+  {
+  }
+  bool operator==(StochasticDatedValue const& e2) const;
+
+private:
+  double draw(Distribution law, std::vector<double> params);
+};
+
+std::ostream& operator<<(std::ostream& out, const StochasticDatedValue& e);
+
+} // namespace profile
+} // namespace kernel
+} // namespace simgrid
+
+#endif
index bbafaa5..4344ecb 100644 (file)
@@ -326,6 +326,8 @@ set(SURF_SRC
   src/kernel/resource/profile/FutureEvtSet.hpp
   src/kernel/resource/profile/Profile.cpp
   src/kernel/resource/profile/Profile.hpp
+  src/kernel/resource/profile/StochasticDatedValue.cpp
+  src/kernel/resource/profile/StochasticDatedValue.hpp
 
   src/kernel/routing/ClusterZone.cpp
   src/kernel/routing/DijkstraZone.cpp