From 0bfdbb488b5204be6cfca83a6148f6a930c132e8 Mon Sep 17 00:00:00 2001 From: Yann Duplouy Date: Wed, 20 Nov 2019 11:55:14 +0100 Subject: [PATCH] Now able to parse stochastic profiles --- src/kernel/resource/profile/Profile.cpp | 136 ++++++++++++++++++++---- src/kernel/resource/profile/Profile.hpp | 7 +- 2 files changed, 122 insertions(+), 21 deletions(-) diff --git a/src/kernel/resource/profile/Profile.cpp b/src/kernel/resource/profile/Profile.cpp index 482ee82d3c..8b888860dc 100644 --- a/src/kernel/resource/profile/Profile.cpp +++ b/src/kernel/resource/profile/Profile.cpp @@ -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 @@ -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) @@ -88,17 +113,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 splittedval((std::istream_iterator(iss)), + std::istream_iterator()); + + 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) { diff --git a/src/kernel/resource/profile/Profile.hpp b/src/kernel/resource/profile/Profile.hpp index 3c173db61a..263a63048a 100644 --- a/src/kernel/resource/profile/Profile.hpp +++ b/src/kernel/resource/profile/Profile.hpp @@ -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 #include @@ -35,9 +36,13 @@ public: static Profile* from_string(const std::string& name, const std::string& input, double periodicity); // private: std::vector event_list; + std::vector stochastic_event_list; private: - FutureEvtSet* fes_ = nullptr; + FutureEvtSet* fes_ = nullptr; + bool stochastic = false; + bool stochasticloop = false; + DatedValue futureDV; }; } // namespace profile -- 2.20.1