Commit 73d7199a authored by Sonja Happ's avatar Sonja Happ

. add time measurements for result saving

- remove obsolete time measurements
parent 22b6aff5
......@@ -50,6 +50,12 @@ private:
protected:
Agent_behavior *behavior; //!< Object of Agent_behavior class; defines the behavior of the agent
// time measurements for result saving procedure (pointers to time measurements of model class)
Time_measurement * tm_csv;
Time_measurement * tm_db;
Time_measurement * tm_db_serialize;
Time_measurement * tm_db_add;
public:
/*Variables that describe properties of the agent*/
repast::AgentId id_; //!< RepastHPC Agent ID [id, start rank, type, current rank]
......@@ -143,6 +149,9 @@ public:
/* Method to advance a simulation step */
virtual void step(); // virtual function, to be re-implemented in all derived agent classes
/*Method to set pointers to time measurements*/
void set_tm(Time_measurement *_tm_csv, Time_measurement * _tm_db, Time_measurement * tm_db_serialize, Time_measurement * tm_db_add);
//Exit function for agent
void do_exit(int code);
......
......@@ -25,6 +25,7 @@
#include "model/config.h"
#include "model/io_object.h"
#include "model/time_measurement.h"
#include <repast_hpc/AgentId.h>
#include <fstream>
......@@ -35,7 +36,7 @@ public:
void init_logging();
void save_meta(void *_model_data);
void save_result(void *_model_data, bool first_step);
void save_result(void *_model_data, bool first_step, Time_measurement* tm_csv, Time_measurement* tm_db, Time_measurement* tm_db_serialize, Time_measurement* tm_db_add);
private:
repast::AgentId id; //!< RepastHPC Agent ID [id, start rank, type, current rank]
......@@ -45,7 +46,7 @@ private:
void set_result_types(); //!< This method adds all result types of an agent to the database depending on the agent type
void save_result_csv_prosumer(void *_model_data); //!< Write prosumer data to csv result stream
void save_result_csv(void *_model_data); //!< This method saves results in the csv result file
void save_result_db(void *_model_data, bool first_step); //!< This method saves results in the database
void save_result_db(void *_model_data, bool first_step, Time_measurement* tm_db_serialize, Time_measurement* tm_db_add); //!< This method saves results in the database
};
#endif //AGENT_IO_H
......@@ -112,6 +112,12 @@ public:
Time_measurement* tm_bcast_forward_sweep;
Time_measurement* tm_bcast_backward_sweep;
/*Time measurements for result saving*/
Time_measurement * tm_csv;
Time_measurement * tm_db;
Time_measurement * tm_db_serialize;
Time_measurement * tm_db_add;
unsigned int number_of_fbs_sweeps; //!< counting the number of forward-backward sweeps
//!< in each iteration
unsigned int forward_loop_counter; //!< counting number of iterations through forward sweep loop
......
......@@ -365,6 +365,22 @@ void Agent::villas_interface_disconnect() {
}
/*! \brief Set pointers of time measurements (called by model class for each agent)
* \param _tm_csv pointer to time measurement for csv saving from model class
* \param _tm_db pointer to time measurement for DB saving from model class
* \param _tm_db_serialize pointer to time measurement for serializing data for DB from model class
* \param _tm_db_add pointer to time measurement for adding data to DB from model class
*/
void Agent::set_tm(Time_measurement *_tm_csv, Time_measurement *_tm_db, Time_measurement *_tm_db_serialize,
Time_measurement *_tm_db_add) {
tm_csv = _tm_csv;
tm_db = _tm_db;
tm_db_serialize = _tm_db_serialize;
tm_db_add = _tm_db_add;
}
/*! \brief Abort the simulation (called in case of an error)
* \param code Error code identifying the error
*
......
......@@ -64,10 +64,17 @@ void AgentIO::init_logging() {
* \param _model_data [in] Datastruct containing specific data for the agent's model
* \param first_step [in] true if this method is called for the first time, false if not
* */
void AgentIO::save_result(void *_model_data, bool first_step) {
void AgentIO::save_result(void *_model_data, bool first_step,
Time_measurement* tm_csv, Time_measurement* tm_db, Time_measurement* tm_db_serialize, Time_measurement* tm_db_add) {
tm_csv->start();
this->save_result_csv(_model_data);
this->save_result_db(_model_data, first_step);
this->flush_result();
tm_csv->stop();
tm_db->start();
this->save_result_db(_model_data, first_step, tm_db_serialize, tm_db_add);
tm_db->stop();
}
void AgentIO::save_result_csv_prosumer(void *_model_data){
......@@ -268,14 +275,16 @@ void AgentIO::save_result_csv(void *_model_data) {
* \param _model_data [in] Datastruct containing specific data for the agent's model
* \param first_step [in] true if this method is called for the first time, false if not
* */
void AgentIO::save_result_db(void *_model_data, bool first_step) {
void AgentIO::save_result_db(void *_model_data, bool first_step, Time_measurement* tm_db_serialize, Time_measurement* tm_db_add) {
#ifdef USE_DB
int aid = id.id();
int at = id.agentType();
int size = 0;
void* buffer = nullptr;
if(d_props.result.db_results){ // save results of Prosumer Agent
tm_db_serialize->start();
int type = id.agentType();
if(type== TYPE_BATTERY_INT ||
type == TYPE_CHP_INT ||
......@@ -437,8 +446,10 @@ void AgentIO::save_result_db(void *_model_data, bool first_step) {
proto_res.SerializeToArray(buffer, size);
}
}
tm_db_serialize->stop();
if(buffer != nullptr) {
tm_db_add->start();
try {
d_props.result.dbconn->addResultSerialized(aid, at, t_next, buffer, size, first_step);
}
......@@ -449,7 +460,9 @@ void AgentIO::save_result_db(void *_model_data, bool first_step) {
//free buffer
free(buffer);
tm_db_add->stop();
}
}
#endif
}
......
......@@ -122,7 +122,7 @@ void Directoryfacilitator_agent::step(){
model_data.msg_sent = behavior->get_msg_sent();
model_data.msg_received = behavior->get_msg_received();
IO->save_result(&model_data, first_step);
IO->save_result(&model_data, first_step, tm_csv, tm_db, tm_db_serialize, tm_db_add);
#ifdef USE_DB
if(first_step){
IO->save_meta(&model_data);
......
......@@ -209,7 +209,7 @@ void Messagerouter_agent::step(){
//proceed one time step
if (is_local_instance) {
IO->save_result(&model_data, first_step);
IO->save_result(&model_data, first_step, tm_csv, tm_db, tm_db_serialize, tm_db_add);
#ifdef USE_DB
if (first_step) {
IO->save_meta(&model_data);
......
......@@ -96,7 +96,7 @@ void Node_agent::step() {
model_data.v_im = voltage.imag();
model_data.v_norm = std::abs(voltage) / (model_data.Vnom / sqrt(3.0));
IO->save_result(&model_data, first_step);
IO->save_result(&model_data, first_step, tm_csv, tm_db, tm_db_serialize, tm_db_add);
#ifdef USE_DB
if(first_step){
IO->save_meta(&model_data);
......
......@@ -239,7 +239,7 @@ void Prosumer_agent::step() {
}
/* save results */
IO->save_result(prosumer_data, first_step);
IO->save_result(prosumer_data, first_step, tm_csv, tm_db, tm_db_serialize, tm_db_add);
#ifdef USE_DB
if(first_step){
IO->save_meta(prosumer_data);
......
......@@ -114,7 +114,7 @@ void Slack_agent::step() {
model_data.msg_sent = behavior->get_msg_sent();
model_data.msg_received = behavior->get_msg_received();
IO->save_result(&model_data, first_step);
IO->save_result(&model_data, first_step, tm_csv, tm_db, tm_db_serialize, tm_db_add);
#ifdef USE_DB
if(first_step){
IO->save_meta(&model_data);
......
......@@ -164,7 +164,7 @@ void Transformer_agent::step() {
substation_data.msg_received = behavior->get_msg_received();
transformer_model->post_processing();
IO->save_result(&substation_data, first_step);
IO->save_result(&substation_data, first_step, tm_csv, tm_db, tm_db_serialize, tm_db_add);
#ifdef USE_DB
if(first_step){
IO->save_meta(&substation_data);
......
......@@ -1057,6 +1057,9 @@ void Model::init_agent_behaviors() {
/* each agent determines electrically adjacent agents and saves them locally*/
i->save_adjacent_agents(agent_network_elec);
/* set pointers to time measurements*/
i->set_tm(IO->tm_csv, IO->tm_db, IO->tm_db_serialize, IO->tm_db_add);
}
//wait here for fixed amount of time to be sure that all VILLAS connections are ready to be used
......
......@@ -76,6 +76,11 @@ props(_props), rank(_rank), world_size(_world_size), profile_dir(_profile_dir)
tm_bcast_FBS = new Time_measurement("bcast_fbs", rank);
tm_bcast_forward_sweep = new Time_measurement("bcast_forward_sweep", rank);
tm_bcast_backward_sweep = new Time_measurement("bcast_backward_sweep", rank);
tm_csv = new Time_measurement("results_csv", rank);
tm_db = new Time_measurement("results_db", rank);
tm_db_serialize = new Time_measurement("results_db_serialize", rank);
tm_db_add = new Time_measurement("results_db_add", rank);
}
/*! \brief Initialize message and result logging on rank level
......@@ -198,6 +203,11 @@ void ModelIO::init_db() {
d_props.result.dbconn->addTimeMeasurementType("bcast_forward_sweep","Time for MPI broadcast for forward sweep completion in us");
d_props.result.dbconn->addTimeMeasurementType("bcast_backward_sweep","Time for MPI broadcast for backward sweep completion in us");
d_props.result.dbconn->addTimeMeasurementType("results_csv","Time for saving results to csv in us");
d_props.result.dbconn->addTimeMeasurementType("results_db","Time for saving results to db in us");
d_props.result.dbconn->addTimeMeasurementType("results_db_serialize","Time for serializing results before saving to DB in us");
d_props.result.dbconn->addTimeMeasurementType("results_db_add","Time for adding results to DB in us");
// initialize metadata
addMetaEntry(rank, "simulationmeta", "stop.at", props->getProperty("stop.at"));
addMetaEntry(rank, "simulationmeta", "step.size", props->getProperty("step.size"));
......@@ -304,6 +314,11 @@ void ModelIO::finish_time_measurements(unsigned long &stop_at){
delete tm_bcast_forward_sweep;
delete tm_bcast_backward_sweep;
delete tm_csv;
delete tm_db;
delete tm_db_serialize;
delete tm_db_add;
}
......@@ -345,23 +360,27 @@ void ModelIO::log_results_for_tick() {
"," + std::to_string(tm_behaviors->get_duration() / 1000000.0) +
"," + std::to_string(number_of_fbs_sweeps) +
"," + std::to_string(tm_rt_tick->get_duration() / 1000000.0) +
"," + std::to_string(tm_load->get_duration() / 1000000.0) +
"," + std::to_string(tm_pv->get_duration() / 1000000.0) +
"," + std::to_string(tm_ev->get_duration() / 1000000.0) +
"," + std::to_string(tm_hp->get_duration() / 1000000.0) +
"," + std::to_string(tm_chp->get_duration() / 1000000.0) +
"," + std::to_string(tm_wec->get_duration() / 1000000.0) +
"," + std::to_string(tm_bio->get_duration() / 1000000.0) +
"," + std::to_string(tm_bat->get_duration() / 1000000.0) +
"," + std::to_string(tm_comp->get_duration() / 1000000.0) +
"," + std::to_string(tm_substation->get_duration() / 1000000.0) +
"," + std::to_string(tm_slack->get_duration() / 1000000.0) +
"," + std::to_string(tm_df->get_duration() / 1000000.0)+
"," + std::to_string(tm_bcast_FBS->get_duration() / 1000000.0)+
"," + std::to_string(tm_bcast_forward_sweep->get_duration() / 1000000.0)+
"," + std::to_string(tm_bcast_backward_sweep->get_duration() / 1000000.0)+
"," + std::to_string(forward_loop_counter)+
"," + std::to_string(backward_loop_counter)
//"," + std::to_string(tm_load->get_duration() / 1000000.0) +
//"," + std::to_string(tm_pv->get_duration() / 1000000.0) +
//"," + std::to_string(tm_ev->get_duration() / 1000000.0) +
//"," + std::to_string(tm_hp->get_duration() / 1000000.0) +
//"," + std::to_string(tm_chp->get_duration() / 1000000.0) +
//"," + std::to_string(tm_wec->get_duration() / 1000000.0) +
//"," + std::to_string(tm_bio->get_duration() / 1000000.0) +
//"," + std::to_string(tm_bat->get_duration() / 1000000.0) +
//"," + std::to_string(tm_comp->get_duration() / 1000000.0) +
//"," + std::to_string(tm_substation->get_duration() / 1000000.0) +
//"," + std::to_string(tm_slack->get_duration() / 1000000.0) +
//"," + std::to_string(tm_df->get_duration() / 1000000.0)+
//"," + std::to_string(tm_bcast_FBS->get_duration() / 1000000.0)+
//"," + std::to_string(tm_bcast_forward_sweep->get_duration() / 1000000.0)+
//"," + std::to_string(tm_bcast_backward_sweep->get_duration() / 1000000.0)+
//"," + std::to_string(forward_loop_counter)+
//"," + std::to_string(backward_loop_counter)
"," + std::to_string(tm_csv->get_duration() / 1000000.0)+
"," + std::to_string(tm_db->get_duration() / 1000000.0)+
"," + std::to_string(tm_db_serialize->get_duration() / 1000000.0)+
"," + std::to_string(tm_db_add->get_duration() / 1000000.0)
);
......@@ -397,6 +416,7 @@ void ModelIO::log_results_for_tick() {
tm_rt_tick->get_duration(), tick);
// Agent behavior time measurements
/*
d_props.result.dbconn->addTimeMeasurement(tm_load->get_time_measurement_type(), tm_load->getId(), tm_load->get_duration(), tick);
d_props.result.dbconn->addTimeMeasurement(tm_pv->get_time_measurement_type(), tm_pv->getId(), tm_pv->get_duration(), tick);
d_props.result.dbconn->addTimeMeasurement(tm_ev->get_time_measurement_type(), tm_ev->getId(), tm_ev->get_duration(), tick);
......@@ -409,18 +429,24 @@ void ModelIO::log_results_for_tick() {
d_props.result.dbconn->addTimeMeasurement(tm_substation->get_time_measurement_type(), tm_substation->getId(), tm_substation->get_duration(), tick);
d_props.result.dbconn->addTimeMeasurement(tm_slack->get_time_measurement_type(), tm_slack->getId(), tm_slack->get_duration(), tick);
d_props.result.dbconn->addTimeMeasurement(tm_df->get_time_measurement_type(), tm_df->getId(), tm_df->get_duration(), tick);
*/
// MPI broadcast time measurements
/*
d_props.result.dbconn->addTimeMeasurement(tm_bcast_FBS->get_time_measurement_type(), tm_bcast_FBS->getId(), tm_bcast_FBS->get_duration(), tick);
d_props.result.dbconn->addTimeMeasurement(tm_bcast_forward_sweep->get_time_measurement_type(), tm_bcast_forward_sweep->getId(), tm_bcast_forward_sweep->get_duration(), tick);
d_props.result.dbconn->addTimeMeasurement(tm_bcast_backward_sweep->get_time_measurement_type(), tm_bcast_backward_sweep->getId(), tm_bcast_backward_sweep->get_duration(), tick);
*/
d_props.result.dbconn->addTimeMeasurement(tm_csv->get_time_measurement_type(), tm_csv->getId(), tm_csv->get_duration(), tick);
d_props.result.dbconn->addTimeMeasurement(tm_db->get_time_measurement_type(), tm_db->getId(), tm_db->get_duration(), tick);
d_props.result.dbconn->addTimeMeasurement(tm_db_serialize->get_time_measurement_type(), tm_db_serialize->getId(), tm_db_serialize->get_duration(), tick);
d_props.result.dbconn->addTimeMeasurement(tm_db_add->get_time_measurement_type(), tm_db_add->getId(), tm_db_add->get_duration(), tick);
}
catch (const DBException &ex){
std::cout << "[DBEXCEPTION ON RANK " << rank << "] Error: " << ex.what() << std::endl;
std::cerr << "[DBEXCEPTION ON RANK " << rank << "] Error: " << ex.what() << std::endl;
}
if(tick == 1){
//send time measurements of init phase as meta info to db
if(d_props.result.db_results) {
......@@ -470,6 +496,11 @@ void ModelIO::reset_time_measurements() {
tm_bcast_forward_sweep->reset();
tm_bcast_backward_sweep->reset();
tm_csv->reset();
tm_db->reset();
tm_db_serialize->reset();
tm_db_add->reset();
}
void ModelIO::set_result_file_header() {
......@@ -487,23 +518,27 @@ void ModelIO::set_result_file_header() {
"behavior calculation time [s],"<<
"number of FBS sweeps,"<<
"real time tick time [s]," <<
"Load behavior [s]," <<
"PV behavior [s]," <<
"EV behavior [s]," <<
"HP behavior [s]," <<
"CHP behavior [s]," <<
"WEC behavior [s]," <<
"Biofuel behavior [s]," <<
"Battery behavior [s]," <<
"Compensator behavior [s]," <<
"Substation behavior [s]," <<
"Slack behavior [s]," <<
"DF behavior [s]," <<
"MPI bcast FBS [s]," <<
"MPI bcast forward sweep [s]," <<
"MPI bcast backward sweep [s]," <<
"Forward loop counter," <<
"Backward loop counter" <<
//"Load behavior [s]," <<
//"PV behavior [s]," <<
//"EV behavior [s]," <<
//"HP behavior [s]," <<
//"CHP behavior [s]," <<
//"WEC behavior [s]," <<
//"Biofuel behavior [s]," <<
//"Battery behavior [s]," <<
//"Compensator behavior [s]," <<
//"Substation behavior [s]," <<
//"Slack behavior [s]," <<
//"DF behavior [s]," <<
//"MPI bcast FBS [s]," <<
//"MPI bcast forward sweep [s]," <<
//"MPI bcast backward sweep [s]," <<
//"Forward loop counter," <<
//"Backward loop counter" <<
"CSV saving," <<
"DB saving," <<
"DB serializing," <<
"DB adding" <<
std::endl;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment