Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid
authorEhsan Azimi <eazimi@ehsan.irisa.fr>
Thu, 29 Oct 2020 09:38:55 +0000 (10:38 +0100)
committerEhsan Azimi <eazimi@ehsan.irisa.fr>
Thu, 29 Oct 2020 09:38:55 +0000 (10:38 +0100)
src/kernel/resource/profile/DatedValue.cpp
src/kernel/resource/profile/DatedValue.hpp
src/kernel/resource/profile/Profile.cpp
src/kernel/resource/profile/Profile_test.cpp
src/kernel/resource/profile/StochasticDatedValue.cpp
src/kernel/resource/profile/StochasticDatedValue.hpp

index 68955de..61c386e 100644 (file)
@@ -14,11 +14,6 @@ bool DatedValue::operator==(DatedValue const& e2) const
 {
   return (fabs(date_ - e2.date_) < 0.0001) && (fabs(value_ - e2.value_) < 0.0001);
 }
-std::ostream& operator<<(std::ostream& out, const DatedValue& e)
-{
-  out << e.date_ << " " << e.value_;
-  return out;
-}
 
 } // namespace profile
 } // namespace kernel
index 6e5d32c..24b7540 100644 (file)
@@ -32,7 +32,6 @@ public:
   bool operator==(DatedValue const& e2) const;
   bool operator!=(DatedValue const& e2) const { return not(*this == e2); }
 };
-std::ostream& operator<<(std::ostream& out, const DatedValue& e);
 
 } // namespace profile
 } // namespace kernel
index 587a063..9a85416 100644 (file)
@@ -133,53 +133,51 @@ Profile* Profile::from_string(const std::string& name, const std::string& input,
       xbt_assert(splittedval.size() > 0, "Invalid profile line");
 
       if (splittedval[0] == "DET") {
-        stochevent.date_law = Dist_Det;
+        stochevent.date_law = Distribution::DET;
         i                   = 2;
       } else if (splittedval[0] == "NORM" || splittedval[0] == "NORMAL" || splittedval[0] == "GAUSS" ||
                  splittedval[0] == "GAUSSIAN") {
-        stochevent.date_law = Dist_Norm;
+        stochevent.date_law = Distribution::NORM;
         i                   = 3;
       } else if (splittedval[0] == "EXP" || splittedval[0] == "EXPONENTIAL") {
-        stochevent.date_law = Dist_Exp;
+        stochevent.date_law = Distribution::EXP;
         i                   = 2;
       } else if (splittedval[0] == "UNIF" || splittedval[0] == "UNIFORM") {
-        stochevent.date_law = Dist_Unif;
+        stochevent.date_law = Distribution::UNIF;
         i                   = 3;
       } else {
-        xbt_assert(false, "Unknown law %s", splittedval[0].c_str());
-        i = 0;
+        xbt_die("Unknown law %s", splittedval[0].c_str());
       }
 
       xbt_assert(splittedval.size() > i, "Invalid profile line");
       if (i == 2) {
-        stochevent.date_params = {std::atof(splittedval[1].c_str())};
+        stochevent.date_params = {std::stod(splittedval[1])};
       } else if (i == 3) {
-        stochevent.date_params = {std::atof(splittedval[1].c_str()), std::atof(splittedval[2].c_str())};
+        stochevent.date_params = {std::stod(splittedval[1]), std::stod(splittedval[2])};
       }
 
       if (splittedval[i] == "DET") {
-        stochevent.value_law = Dist_Det;
+        stochevent.value_law = Distribution::DET;
         j                    = 1;
       } else if (splittedval[i] == "NORM" || splittedval[i] == "NORMAL" || splittedval[i] == "GAUSS" ||
                  splittedval[i] == "GAUSSIAN") {
-        stochevent.value_law = Dist_Norm;
+        stochevent.value_law = Distribution::NORM;
         j                    = 2;
       } else if (splittedval[i] == "EXP" || splittedval[i] == "EXPONENTIAL") {
-        stochevent.value_law = Dist_Exp;
+        stochevent.value_law = Distribution::EXP;
         j                    = 1;
       } else if (splittedval[i] == "UNIF" || splittedval[i] == "UNIFORM") {
-        stochevent.value_law = Dist_Unif;
+        stochevent.value_law = Distribution::UNIF;
         j                    = 2;
       } else {
-        xbt_assert(false, "Unknown law %s", splittedval[i].c_str());
-        j = 0;
+        xbt_die("Unknown law %s", splittedval[i].c_str());
       }
 
       xbt_assert(splittedval.size() > i + j, "Invalid profile line");
       if (j == 1) {
-        stochevent.value_params = {std::atof(splittedval[i + 1].c_str())};
+        stochevent.value_params = {std::stod(splittedval[i + 1])};
       } else if (j == 2) {
-        stochevent.value_params = {std::atof(splittedval[i + 1].c_str()), std::atof(splittedval[i + 2].c_str())};
+        stochevent.value_params = {std::stod(splittedval[i + 1]), std::stod(splittedval[i + 2])};
       }
 
       profile->stochastic_event_list.emplace_back(stochevent);
index 55f4c61..f7e2abd 100644 (file)
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
 
-double thedate;
 class MockedResource : public simgrid::kernel::resource::Resource {
 public:
+  static double the_date;
+
   explicit MockedResource() : simgrid::kernel::resource::Resource(nullptr, "fake", nullptr) {}
   void apply_event(simgrid::kernel::profile::Event* event, double value) override
   {
-    XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", thedate, value, event->idx);
+    XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", the_date, value, event->idx);
     tmgr_trace_event_unref(&event);
   }
   bool is_used() const override { return true; }
 };
 
+double MockedResource::the_date;
+
 static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char* str)
 {
   std::vector<simgrid::kernel::profile::DatedValue> res;
@@ -46,18 +49,18 @@ static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char
   simgrid::kernel::profile::Event* insertedIt = trace->schedule(&fes, &daResource);
 
   while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
-    thedate = fes.next_date();
+    MockedResource::the_date = fes.next_date();
     double value;
     simgrid::kernel::resource::Resource* resource;
-    simgrid::kernel::profile::Event* it = fes.pop_leq(thedate, &value, &resource);
+    simgrid::kernel::profile::Event* it = fes.pop_leq(MockedResource::the_date, &value, &resource);
     if (it == nullptr)
       continue;
 
     REQUIRE(it == insertedIt); // Check that we find what we've put
     if (value >= 0) {
-      res.emplace_back(thedate, value);
+      res.emplace_back(MockedResource::the_date, value);
     } else {
-      XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", thedate, it->idx);
+      XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", MockedResource::the_date, it->idx);
     }
     resource->apply_event(it, value);
   }
@@ -154,19 +157,21 @@ TEST_CASE("kernel::profile: Resource profiles, defining the external load", "ker
 
   SECTION("One stochastic event (parsing)")
   {
+    using simgrid::kernel::profile::Distribution;
     std::vector<simgrid::kernel::profile::StochasticDatedValue> got = trace2selist("STOCHASTIC\n"
                                                                                    "DET 0 UNIF 10 20");
 
     std::vector<simgrid::kernel::profile::StochasticDatedValue> want;
     want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(0, -1)); // The initial fake event
-    want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Det, {0},
-                                                                     simgrid::kernel::profile::Dist_Unif, {10, 20}));
+    want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(Distribution::DET, {0},
+                                                                     Distribution::UNIF, {10, 20}));
 
     REQUIRE(want == got);
   }
 
   SECTION("Several stochastic events (all possible parsing forms)")
   {
+    using simgrid::kernel::profile::Distribution;
     std::vector<simgrid::kernel::profile::StochasticDatedValue> got = trace2selist("STOCHASTIC\n"
                                                                                    "DET 0 DET 4\n"
                                                                                    "NORMAL 25 10 DET 3\n"
@@ -175,14 +180,14 @@ TEST_CASE("kernel::profile: Resource profiles, defining the external load", "ker
 
     std::vector<simgrid::kernel::profile::StochasticDatedValue> want;
     want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(0, -1));
-    want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Det, {0},
-                                                                     simgrid::kernel::profile::Dist_Det, {4}));
-    want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Norm, {25, 10},
-                                                                     simgrid::kernel::profile::Dist_Det, {3}));
-    want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Unif, {10, 20},
-                                                                     simgrid::kernel::profile::Dist_Norm, {25, 10}));
-    want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(simgrid::kernel::profile::Dist_Det, {5},
-                                                                     simgrid::kernel::profile::Dist_Unif, {5, 25}));
+    want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(Distribution::DET, {0},
+                                                                     Distribution::DET, {4}));
+    want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(Distribution::NORM, {25, 10},
+                                                                     Distribution::DET, {3}));
+    want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(Distribution::UNIF, {10, 20},
+                                                                     Distribution::NORM, {25, 10}));
+    want.emplace_back(simgrid::kernel::profile::StochasticDatedValue(Distribution::DET, {5},
+                                                                     Distribution::UNIF, {5, 25}));
 
     REQUIRE(want == got);
   }
index a9e0d00..6830b75 100644 (file)
@@ -15,17 +15,16 @@ namespace profile {
 double StochasticDatedValue::draw(Distribution law, std::vector<double> params)
 {
   switch (law) {
-    case Dist_Det:
+    case Distribution::DET:
       return params[0];
-    case Dist_Exp:
+    case Distribution::EXP:
       return simgrid::xbt::random::exponential(params[0]);
-    case Dist_Unif:
+    case Distribution::UNIF:
       return simgrid::xbt::random::uniform_real(params[0], params[1]);
-    case Dist_Norm:
+    case Distribution::NORM:
       return simgrid::xbt::random::normal(params[0], params[1]);
     default:
-      xbt_assert(false, "Unimplemented distribution");
-      return 0;
+      xbt_die("Unimplemented distribution");
   }
 }
 double StochasticDatedValue::get_value()
@@ -50,26 +49,6 @@ bool StochasticDatedValue::operator==(StochasticDatedValue const& e2) const
          (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
index 29bc989..f432039 100644 (file)
@@ -14,22 +14,19 @@ namespace simgrid {
 namespace kernel {
 namespace profile {
 
-enum Distribution { Dist_Exp, Dist_Norm, Dist_Unif, Dist_Det };
+enum class Distribution { EXP, NORM, UNIF, DET };
 
 class XBT_PUBLIC StochasticDatedValue {
 public:
-  Distribution date_law;
+  Distribution date_law = Distribution::DET;
   std::vector<double> date_params;
-  Distribution value_law;
+  Distribution value_law = Distribution::DET;
   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(double d, double v) : date_params({d}), value_params({v}) {}
   explicit StochasticDatedValue(Distribution dl, const std::vector<double>& dp, Distribution vl,
                                 const std::vector<double>& vp)
       : date_law(dl), date_params(dp), value_law(vl), value_params(vp)
@@ -38,11 +35,9 @@ public:
   bool operator==(StochasticDatedValue const& e2) const;
 
 private:
-  double draw(Distribution law, std::vector<double> params);
+  static double draw(Distribution law, std::vector<double> params);
 };
 
-std::ostream& operator<<(std::ostream& out, const StochasticDatedValue& e);
-
 } // namespace profile
 } // namespace kernel
 } // namespace simgrid