diff --git a/src/users/broker.cpp b/src/users/broker.cpp index dba642e3766f260a551d752831f95bf574080081..e9d62d6fed9d25bb47031444a3a59480bda2d8e2 100644 --- a/src/users/broker.cpp +++ b/src/users/broker.cpp @@ -54,6 +54,7 @@ Broker::Broker(rapidjson::Document *user_description_file) PPK_ASSERT_ERROR(red_windows_list->have_no_common_element(yellow_windows_list), "Error red_windows field and yellow_windows field have at least one common element in their interval"); } + state_automata = new DMWindowAutomata(dm_window,red_windows_list,yellow_windows_list); //seed parameter parsing bool seed_defined = false; @@ -163,8 +164,8 @@ Broker::Broker(rapidjson::Document *user_description_file) seed_defined, "No field 'seed' defined although dm_user_multi_behavior " "needs it"); - user = new DMUserMultiBehavior(name, param, dm_window, - seed_generator(), yellow_windows_list, red_windows_list, + user = new DMUserMultiBehavior(name, param, + seed_generator(), state_automata, logger_user_stat); } /* Feedback user */ diff --git a/src/users/broker.hpp b/src/users/broker.hpp index 0f54fc620998e4e1b36f9d225721f295ec4980ee..b2d61f0e759dee3f5c8d11bdff7374295924c719 100644 --- a/src/users/broker.hpp +++ b/src/users/broker.hpp @@ -66,4 +66,5 @@ private: /* (Optional) Red and Yellow windows for MultiBehavior DM users */ DMWindow_list* red_windows_list=nullptr; DMWindow_list* yellow_windows_list = nullptr; + DMWindowAutomata* state_automata = nullptr; }; \ No newline at end of file diff --git a/src/users/user_windows.cpp b/src/users/user_windows.cpp index 17b352d631876e3c69f86d72005a0ec1550914ea..8bc076eea1d6c6419cb588289e12fb19fdf12c3f 100644 --- a/src/users/user_windows.cpp +++ b/src/users/user_windows.cpp @@ -22,15 +22,44 @@ bool DMWindow_list::have_no_common_element( DMWindow_list* compare_list) const return intersect.is_empty(); } +DMWindowAutomata::DMWindowAutomata(DMWindow* dm_window,DMWindow_list* red_windows, + DMWindow_list* yellow_windows) +{ + this->dm_window = dm_window; + this->red_windows = red_windows; + this->yellow_windows = yellow_windows; +} +bool DMWindowAutomata::is_in_red_state(double date){ + // Check whether the date is in a red window + return (dm_window && dm_window->date_in_dm_window(date)) || (red_windows && red_windows->date_in_dm_window(date)); +} +bool DMWindowAutomata::is_in_yellow_state(double date){ + // Check whether the date is in yellow window + return yellow_windows && yellow_windows->date_in_dm_window(date); +} +bool DMWindowAutomata::has_red_state() +{ + return dm_window || red_windows; +} +bool DMWindowAutomata::has_yellow_state() +{ + return yellow_windows; +} +DMWindowAutomata::~DMWindowAutomata() +{ +} + +StateAutomata::~StateAutomata() +{ +} + DMUserMultiBehavior::DMUserMultiBehavior( - const std::string & name, const rapidjson::Value ¶m, DMWindow *window, - uint_fast32_t random_seed,DMWindow_list *y_windows, - DMWindow_list *r_windows, LoggerUserStat* logger) - : DMUserRenonce(name,param,window),DMUserReconfig(name,param,window), - DMUserDegrad(name,param,window) + const std::string & name, const rapidjson::Value ¶m, + uint_fast32_t random_seed,StateAutomata* state_automata, LoggerUserStat* logger) + : DMUserRenonce(name,param,nullptr),DMUserReconfig(name,param,nullptr), + DMUserDegrad(name,param,nullptr) { - yellow_windows = y_windows; - red_windows = r_windows; + this->state_automata = state_automata; this->logger = logger; random_gen = std::mt19937(random_seed); red_prob_multi_core= vector<double> (R_TOTAL_MULTI,0.0); @@ -73,7 +102,7 @@ double DMUserMultiBehavior::parse_proba_param(const rapidjson::Value ¶m, } void DMUserMultiBehavior::init_prob_mono_core(const rapidjson::Value ¶m,std::vector<double> & red_prob_array, std::vector<double> & yellow_prob_array){ //Red window probability initialization - if(dm_window || red_windows) + if(state_automata->has_red_state()) { std::vector<string> red_config(R_TOTAL_MONO, ""); red_config[R_DEGRAD_MONO] = "red_prob_degrad_mono_core"; @@ -86,7 +115,7 @@ void DMUserMultiBehavior::init_prob_mono_core(const rapidjson::Value ¶m,std: } // Yellow probability Initialization - if(yellow_windows) + if(state_automata->has_yellow_state()) { std::vector<string> yellow_config(Y_TOTAL_MONO, ""); yellow_config[Y_DEGRAD_MONO] = "yellow_prob_degrad_mono_core"; @@ -99,7 +128,7 @@ void DMUserMultiBehavior::init_prob_mono_core(const rapidjson::Value ¶m,std: void DMUserMultiBehavior::init_prob_multi_core(const rapidjson::Value ¶m,std::vector<double> & red_prob_array, std::vector<double> & yellow_prob_array){ //Red window probability initialization - if (dm_window || red_windows) + if (state_automata->has_red_state()) { std::vector<string> red_config(R_TOTAL_MULTI, ""); red_config[R_DEGRAD_MULTI] = "red_prob_degrad_multi_core"; @@ -114,7 +143,7 @@ void DMUserMultiBehavior::init_prob_multi_core(const rapidjson::Value ¶m,std } // Yellow probability Initialization - if (yellow_windows) + if (state_automata->has_yellow_state()) { std::vector<string> yellow_config(Y_TOTAL_MULTI, ""); yellow_config[Y_DEGRAD_MULTI] = "yellow_prob_degrad_multi_core"; @@ -337,23 +366,15 @@ void DMUserMultiBehavior::log_behavior(shared_ptr<Job> & job,std::string behavio } } -bool DMUserMultiBehavior::is_in_red_window(double date){ - // Check whether the date is in a red_window - return (dm_window && dm_window->date_in_dm_window(date)) || (red_windows && red_windows->date_in_dm_window(date)); -} -bool DMUserMultiBehavior::is_in_yellow_window(double date){ - // Check whether the date is in yellow_window - return yellow_windows && yellow_windows->date_in_dm_window(date); -} bool DMUserMultiBehavior::handle_job(double date, shared_ptr<Job> job, Profile *profile) { //red_windows and dm_windows check - if(is_in_red_window(date)) { + if(state_automata->is_in_red_state(date)) { return red_window_behavior(date, job,profile); } // yellow_windows check - if (is_in_yellow_window(date)){ + if (state_automata->is_in_yellow_state(date)){ /* * We decide at random the behavior (rigid,degrad, reconfig) * that will be done on this job @@ -365,4 +386,3 @@ bool DMUserMultiBehavior::handle_job(double date, shared_ptr<Job> job, Profile * return rigid_job(job,profile,1.0); } } - diff --git a/src/users/user_windows.hpp b/src/users/user_windows.hpp index a0c009576e08ed339bfd21fc48c5710db1d767fe..50c4fb3217576b6bd80a2d34fac04234fd6328a5 100644 --- a/src/users/user_windows.hpp +++ b/src/users/user_windows.hpp @@ -25,6 +25,29 @@ struct DMWindow_list }; +class StateAutomata { +public: + virtual bool is_in_red_state(double date) = 0; + virtual bool is_in_yellow_state(double date) =0; + virtual bool has_red_state()=0; + virtual bool has_yellow_state()=0; + virtual ~StateAutomata(); +}; + +class DMWindowAutomata: public StateAutomata { +public: + DMWindowAutomata(DMWindow* dm_window,DMWindow_list* red_windows, DMWindow_list* yellow_windows); + bool is_in_yellow_state(double date); + bool is_in_red_state(double date); + bool has_yellow_state(); + bool has_red_state(); + ~DMWindowAutomata(); +private: + + DMWindow* dm_window; + DMWindow_list* red_windows; + DMWindow_list* yellow_windows; +}; /** * @brief User class that adopts a different set of submission behaviors depending * on the energy state (red, yellow, green) @@ -40,9 +63,8 @@ class DMUserMultiBehavior : public DMUserRenonce,public DMUserReconfig, { public: DMUserMultiBehavior( - const std::string & name, const rapidjson::Value ¶m, DMWindow *window, - uint_fast32_t random_seed, DMWindow_list *yellow_windows, - DMWindow_list *red_windows, LoggerUserStat* logger); + const std::string & name, const rapidjson::Value ¶m, + uint_fast32_t random_seed, StateAutomata* state_automata, LoggerUserStat* logger); ~DMUserMultiBehavior(); double next_submission(); void jobs_to_submit( @@ -56,8 +78,7 @@ protected: R_RIGID_MONO,R_TOTAL_MONO}; enum yellow_behavior_multi {Y_DEGRAD_MULTI,Y_RECONFIG_MULTI,Y_RIGID_MULTI,Y_TOTAL_MULTI}; enum yellow_behavior_mono {Y_DEGRAD_MONO,Y_RIGID_MONO,Y_TOTAL_MONO}; - DMWindow_list *yellow_windows; - DMWindow_list * red_windows; + StateAutomata* state_automata; std::mt19937 random_gen; std::uniform_real_distribution<double> distribution = std::uniform_real_distribution<double>(0.0, 1.0); @@ -71,9 +92,6 @@ protected: void init_prob_multi_core(const rapidjson::Value ¶m, vector<double> &red_prob_array, vector<double> & yellow_prob_array); void check_deprecated_param(const rapidjson::Value ¶m); - - bool is_in_yellow_window(double date); - bool is_in_red_window(double date); void log_behavior(shared_ptr<Job> & job, std::string behavior_name, long delay_time,double random_value); bool rigid_job(shared_ptr<Job> job,Profile* profile,double random_number);