Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / kernel / resource / profile / Profile.cpp
index 8b88886..234e2eb 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2004-2021. 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. */
@@ -13,6 +13,7 @@
 
 #include <boost/algorithm/string.hpp>
 #include <fstream>
+#include <memory>
 #include <ostream>
 #include <sstream>
 #include <unordered_map>
@@ -27,10 +28,8 @@ namespace profile {
 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);
+  event_list.emplace_back(0, -1);
+  stochastic_event_list.emplace_back(0, -1);
 }
 Profile::~Profile() = default;
 
@@ -38,7 +37,7 @@ Profile::~Profile() = default;
  * and get an iterator over the integrated trace  */
 Event* Profile::schedule(FutureEvtSet* fes, resource::Resource* resource)
 {
-  Event* event    = new Event();
+  auto* event     = new Event();
   event->profile  = this;
   event->idx      = 0;
   event->resource = resource;
@@ -48,8 +47,8 @@ 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!");
+  if (stochastic) {
+    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();
   }
 
@@ -78,15 +77,14 @@ DatedValue Profile::next(Event* event)
     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. */
+    } else if (stochasticloop) { /* 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();
+    if (not event->free_me) { // In the case there is an element, we draw the next event
+      futureDV = stochastic_event_list.at(event->idx).get_datedvalue();
       fes_->add_event(event_date + futureDV.date_, event);
     }
     return dateVal;
@@ -96,7 +94,7 @@ DatedValue Profile::next(Event* event)
 Profile* Profile::from_string(const std::string& name, const std::string& input, double periodicity)
 {
   int linecount                                    = 0;
-  simgrid::kernel::profile::Profile* profile       = new simgrid::kernel::profile::Profile();
+  auto* profile                                    = new simgrid::kernel::profile::Profile();
   simgrid::kernel::profile::DatedValue* last_event = &(profile->event_list.back());
 
   xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str());
@@ -105,6 +103,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
@@ -133,56 +132,54 @@ 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.push_back(stochevent);
+      profile->stochastic_event_list.emplace_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());
@@ -192,7 +189,7 @@ Profile* Profile::from_string(const std::string& name, const std::string& input,
                  last_event->date_, event.date_, input.c_str());
       last_event->date_ = event.date_ - last_event->date_;
 
-      profile->event_list.push_back(event);
+      profile->event_list.emplace_back(event);
       last_event = &(profile->event_list.back());
     }
   }
@@ -213,12 +210,11 @@ Profile* Profile::from_file(const std::string& path)
   xbt_assert(not path.empty(), "Cannot parse a trace from an empty filename");
   xbt_assert(trace_list.find(path) == trace_list.end(), "Refusing to define trace %s twice", path.c_str());
 
-  std::ifstream* f = surf_ifsopen(path);
+  auto f = std::unique_ptr<std::ifstream>(surf_ifsopen(path));
   xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", path.c_str(), (boost::join(surf_path, ":")).c_str());
 
   std::stringstream buffer;
   buffer << f->rdbuf();
-  delete f;
 
   return Profile::from_string(path, buffer.str(), -1);
 }