From 0ba773d913450f88ff7bcf54a39e0c8a8c4d7d64 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Thu, 6 May 2021 17:46:34 +0200 Subject: Refactoring and start extended version --- src/Inputs.cc | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Inputs.hpp | 81 +++++++++++++++++++++++++++ src/inputs.cc | 162 ----------------------------------------------------- src/inputs.hpp | 82 --------------------------- src/simulator.cc | 15 +++-- 5 files changed, 256 insertions(+), 250 deletions(-) create mode 100644 src/Inputs.cc create mode 100644 src/Inputs.hpp delete mode 100644 src/inputs.cc delete mode 100644 src/inputs.hpp (limited to 'src') diff --git a/src/Inputs.cc b/src/Inputs.cc new file mode 100644 index 0000000..de81aec --- /dev/null +++ b/src/Inputs.cc @@ -0,0 +1,166 @@ +#include "Inputs.hpp" + +#include +#include +#include + +Inputs::Inputs(std::string node_name){ + // Here we doing all the boring stuff + FILE* input_file = fopen(INPUTS_FILE, "rb"); + char input_file_buffer[JSON_BUFFER_SIZE]; + rapidjson::FileReadStream is(input_file, input_file_buffer, sizeof(input_file_buffer)); + d.ParseStream(is); + fclose(input_file); + + // Init all variables + is_sender=d["nodes"][node_name.c_str()]["is_sender"].GetBool(); + use_hint=d["nodes"][node_name.c_str()]["use_hint"].GetBool(); + data_size=d["nodes"][node_name.c_str()]["data_size"].GetInt(); + extended=d["extended"].GetBool(); + + // Instantiate wake_ts + for(auto& v:d["nodes"][node_name.c_str()]["wake_ts"].GetArray()){ + wake_ts.push_back(v.GetDouble()); + } + + // Instantiate wake_duration + for(auto& v:d["nodes"][node_name.c_str()]["wake_duration"].GetArray()){ + wake_duration.push_back(v.GetDouble()); + } + + // Identity check + if(wake_ts.size()!=wake_duration.size()){ + std::cerr << "Invalid node configuration: wake_ts.size() != wake_duration.size()" <=next_ts){ + // Create variable for convenience + double start=cur_ts; + double end=std::max(cur_ts+cur_duration,next_ts+next_duration); + wake_duration[i]=end-start; + // Now remove next event + wake_ts.erase(wake_ts.begin() + i + 1); + wake_duration.erase(wake_duration.begin() + i +1); + // This is not optimal. Yet it is simple :D + MergeEvents(); + } + } +} + +double Inputs::GetNextTS(){ + // Ensure the caller is smart + if(wake_duration.size()<2){ + std::cerr << "You are trying to access to the next timestamp but it does not exists" <=ts){ + wake_ts.insert(it,ts); + break; + } + pos++; + } + + // Ensure that ts and duration should not go to the end + if(pos==wake_ts.size()){ + wake_ts.push_back(ts); + wake_duration.push_back(duration); + } + else { + // Handle durations here + int pos2=0; + for(auto it = std::begin(wake_duration); it != std::end(wake_duration); ++it) { + if(pos==pos2){ + wake_duration.insert(it,duration); + break; + } + else if (it+1==std::end(wake_duration)) { + wake_duration.push_back(duration); + } + pos2++; + } + } + // Don't forget + MergeEvents(); +} + +void Inputs::GeneratePlatform(std::string p){ + + // The boring stuff + FILE* input_file = fopen(INPUTS_FILE, "rb"); + char input_file_buffer[JSON_BUFFER_SIZE]; + rapidjson::FileReadStream is(input_file, input_file_buffer, sizeof(input_file_buffer)); + rapidjson::Document d; + d.ParseStream(is); + fclose(input_file); + + // Write platform file + std::ofstream pf; + pf.open (p); + pf << "\n"; + pf << "\n"; + pf << "\n \n"; + pf << " \n"; + for (Value::ConstMemberIterator itr = d["nodes"].MemberBegin(); itr != d["nodes"].MemberEnd(); ++itr) + { + std::string name=itr->name.GetString(); + double power_on=d["nodes"][itr->name.GetString()]["power_on"].GetDouble(); + double power_off=d["nodes"][itr->name.GetString()]["power_off"].GetDouble(); + // Create node + pf << " \n"; + pf << " \n"; + pf << " \n \n"; + pf << " \n"; + } + pf << " \n\n"; + pf.close(); +} \ No newline at end of file diff --git a/src/Inputs.hpp b/src/Inputs.hpp new file mode 100644 index 0000000..e709d89 --- /dev/null +++ b/src/Inputs.hpp @@ -0,0 +1,81 @@ +#include +#include + +#include +#include +#include +#include + +#define INPUTS_FILE "inputs.json" +/// @brief Pay attention to this strange number, you could tear your hairs out +#define JSON_BUFFER_SIZE 65536 + +using namespace rapidjson; + +class Inputs { + /// @brief RapidJSON + Document d; + /// @brief Current node associated with the Inputs + std::string node_name; + /// @brief Timestamps (at which time the nodes should wake up) + std::vector wake_ts; + /// @brief Wake up time durations + std::vector wake_duration; + /** + * Recursively merge overlapping events + */ + void MergeEvents(); +public: + /** + * Load node_name configuration + */ + Inputs(std::string node_name); + /** + * Generate a SimGrid platform file from the json configuration + */ + static void GeneratePlatform(std::string p); + /** + * Is there any event that remains in the queue ? + */ + bool ShouldContinue(){return wake_ts.size()!=0;} + /** + * Is there another event to process ? + */ + bool HasNext(){return(wake_ts.size()>1);} + /** + * Get current event timestamp + */ + double GetTS(){return wake_ts.front();} + /** + * Get current event duration + */ + double GetDuration(){return wake_duration.front();} + /** + * Get next event timestamp + */ + double GetNextTS(); + /** + * Get next event duration + */ + double GetNextDuration(); + /** + * Time travel machine (note that this function is following the second principle + * of thermodynamics) + */ + void GotoNextEvent(); + /** + * Allows to add a *FUTURE* event and merge overlapping events + */ + void AddEvent(double ts, double duration); + /** + * This is the timeline + */ + void DumpEvents(); + + /// @brief These are public attributes, please take care they are fragile + bool is_sender; + bool use_hint; + bool extended; + int data_size; + +}; \ No newline at end of file diff --git a/src/inputs.cc b/src/inputs.cc deleted file mode 100644 index 84acdb7..0000000 --- a/src/inputs.cc +++ /dev/null @@ -1,162 +0,0 @@ -#include "inputs.hpp" -#include "xbt/log.h" -#include -#include -#include - - -Inputs::Inputs(std::string node_name){ - // Here we doing all the boring stuff - FILE* input_file = fopen(INPUTS_FILE, "rb"); - char input_file_buffer[JSON_BUFFER_SIZE]; - rapidjson::FileReadStream is(input_file, input_file_buffer, sizeof(input_file_buffer)); - d.ParseStream(is); - fclose(input_file); - - // Init all variables - is_sender=d[node_name.c_str()]["is_sender"].GetBool(); - use_hint=d[node_name.c_str()]["use_hint"].GetBool(); - data_size=d[node_name.c_str()]["data_size"].GetInt(); - for(auto& v:d[node_name.c_str()]["wake_ts"].GetArray()){ - wake_ts.push_back(v.GetDouble()); - } - for(auto& v:d[node_name.c_str()]["wake_duration"].GetArray()){ - wake_duration.push_back(v.GetDouble()); - } - - // Identity check - if(wake_ts.size()!=wake_duration.size()){ - std::cerr << "Invalid node configuration: wake_ts.size() != wake_duration.size()" <=next_ts){ - // Create variable for convenience - double start=cur_ts; - double end=std::max(cur_ts+cur_duration,next_ts+next_duration); - wake_duration[i]=end-start; - // Now remove next event - wake_ts.erase(wake_ts.begin() + i + 1); - wake_duration.erase(wake_duration.begin() + i +1); - // This is not optimal. Yet it is simple :D - MergeEvents(); - } - } -} - -double Inputs::GetNextTS(){ - // Ensure the caller is smart - if(wake_duration.size()<2){ - std::cerr << "You are trying to access to the next timestamp but it does not exists" <=ts){ - wake_ts.insert(it,ts); - break; - } - pos++; - } - - // Ensure that ts and duration should not go to the end - if(pos==wake_ts.size()){ - wake_ts.push_back(ts); - wake_duration.push_back(duration); - } - else { - // Handle durations here - int pos2=0; - for(auto it = std::begin(wake_duration); it != std::end(wake_duration); ++it) { - if(pos==pos2){ - wake_duration.insert(it,duration); - break; - } - else if (it+1==std::end(wake_duration)) { - wake_duration.push_back(duration); - } - pos2++; - } - } - // Don't forget - MergeEvents(); -} - -void Inputs::GeneratePlatform(std::string p){ - // The boring stuff - FILE* input_file = fopen(INPUTS_FILE, "rb"); - char input_file_buffer[JSON_BUFFER_SIZE]; - rapidjson::FileReadStream is(input_file, input_file_buffer, sizeof(input_file_buffer)); - rapidjson::Document d; - d.ParseStream(is); - fclose(input_file); - - // Write platform file - std::ofstream pf; - pf.open (p); - pf << "\n"; - pf << "\n"; - pf << "\n \n"; - pf << " \n"; - for (Value::ConstMemberIterator itr = d.MemberBegin(); itr != d.MemberEnd(); ++itr) - { - std::string name=itr->name.GetString(); - double power_on=d[itr->name.GetString()]["power_on"].GetDouble(); - double power_off=d[itr->name.GetString()]["power_off"].GetDouble(); - - //db=d[itr->name.GetString()]["wake_interval"].GetDouble(); - pf << " \n"; - pf << " \n"; - pf << " \n \n"; - pf << " \n"; - } - pf << " \n\n"; - pf.close(); -} \ No newline at end of file diff --git a/src/inputs.hpp b/src/inputs.hpp deleted file mode 100644 index 23ced49..0000000 --- a/src/inputs.hpp +++ /dev/null @@ -1,82 +0,0 @@ -#include "rapidjson/document.h" -#include "rapidjson/filereadstream.h" -#include -#include -#include -#include -#include -#include "xbt/log.h" -#include - -#define INPUTS_FILE "inputs.json" -/// @brief Pay attention to this strange number, you could tear your hairs out -#define JSON_BUFFER_SIZE 65536 - -using namespace rapidjson; - -class Inputs { - /// @brief RapidJSON - Document d; - /// @brief Current node associated with the Inputs - std::string node_name; - /// @brief Timestamps (at which time the nodes should wake up) - std::vector wake_ts; - /// @brief Wake up time durations - std::vector wake_duration; - /** - * Recursively merge overlapping events - */ - void MergeEvents(); -public: - /** - * Load node_name configuration - */ - Inputs(std::string node_name); - /** - * Generate a SimGrid platform file from the json configuration - */ - static void GeneratePlatform(std::string p); - /** - * Is there any event that remains in the queue ? - */ - bool ShouldContinue(){return wake_ts.size()!=0;} - /** - * Is there another event to process ? - */ - bool HasNext(){return(wake_ts.size()>1);} - /** - * Get current event timestamp - */ - double GetTS(){return wake_ts.front();} - /** - * Get current event duration - */ - double GetDuration(){return wake_duration.front();} - /** - * Get next event timestamp - */ - double GetNextTS(); - /** - * Get next event duration - */ - double GetNextDuration(); - /** - * Time travel machine (note that this function is following the second principle - * of thermodynamics) - */ - void GotoNextEvent(); - /** - * Allows to add a *FUTURE* event and merge overlapping events - */ - void AddEvent(double ts, double duration); - /** - * This is the timeline - */ - void DumpEvents(); - - /// @brief These are public attributes, please take care they are fragile - bool is_sender; - bool use_hint; - int data_size; - -}; \ No newline at end of file diff --git a/src/simulator.cc b/src/simulator.cc index 0bf2d2f..15d7f0d 100644 --- a/src/simulator.cc +++ b/src/simulator.cc @@ -1,11 +1,14 @@ -#include "simgrid/s4u.hpp" +#include #include +#include +#include +#include + #include #include -#include "inputs.hpp" -#include "simgrid/s4u/Host.hpp" -#include "simgrid/plugins/energy.h" -#include "xbt/log.h" + +#include "Inputs.hpp" + #define PLATFORM_FILE "platform.xml" #define TURN_OFF() simgrid::s4u::this_actor::get_host()->set_pstate(0); @@ -74,7 +77,7 @@ static void obs_node(std::vector args) { Inputs i(selfName); simgrid::s4u::Mailbox *m = simgrid::s4u::Mailbox::by_name("medium"); XBT_INFO("Deploying observation node %s",selfName.c_str()); - + // Init convenient variables bool isSender=i.is_sender; bool useHint=i.use_hint; -- cgit v1.2.3