5 #ifndef ROCKY_ZAGROS_LOG_STRATEGY
6 #define ROCKY_ZAGROS_LOG_STRATEGY
8 #include<rocky/zagros/strategies/strategy.h>
9 #include<rocky/zagros/strategies/communication.h>
10 #include<nlohmann/json.hpp>
24 template<
typename T_e,
int T_dim>
30 std::fstream log_output;
33 this->filename = filename;
35 this->log_groups = log_groups;
44 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
45 std::string process_spc_filename = fmt::format(
"proc_{}_{}", mpi_rank, filename);
47 log_output.open(process_spc_filename, std::fstream::out);
49 std::fstream& stream(){
54 this->stream() <<
"step,group,best" << std::endl;
56 this->stream() <<
"step,best" << std::endl;
59 if(this->log_output.is_open())
60 this->log_output.close();
71 template<
typename T_e,
int T_dim>
80 this->problem_ = problem;
81 this->container_ = container;
82 this->handler_ = handler;
85 virtual void write_header(){
86 if (this->handler_->log_groups)
87 this->handler_->stream() <<
"step,group,best" << std::endl;
89 this->handler_->stream() <<
"step,best" << std::endl;
94 this->handler_->stream() << fmt::format(
"{},{}", this->handler_->step, best) << std::endl;
95 this->handler_->step++;
100 std::string comet_api_key_;
101 std::string workspace_;
102 std::string project_;
103 std::string metric_name_;
104 std::string experiment_name_;
105 std::string experiment_link_;
106 std::string experiment_key_;
108 comet_log_handler(std::string comet_api_key, std::string workspace, std::string project, std::string metric_name){
109 this->comet_api_key_ = comet_api_key;
110 this->workspace_ = workspace;
111 this->project_ = project;
112 this->metric_name_ = metric_name;
114 this->create_experiment();
116 void create_experiment(){
117 spdlog::info(
"creating comet experiment...");
119 nlohmann::json experiment_data = {{
"workspaceName", this->workspace_},
120 {
"projectName", this->project_}};
122 cpr::Response r = cpr::Post(cpr::Url{
"https://www.comet.com/api/rest/v2/write/experiment/create"},
123 cpr::Header{{
"Authorization", this->comet_api_key_},
124 {
"Content-type",
"application/json"},
125 {
"Accept",
"application/json"}},
126 cpr::Body{experiment_data.dump()});
128 this->connection_warning(r.status_code);
129 nlohmann::json rdata = nlohmann::json::parse(r.text);
131 this->experiment_name_ = rdata[
"name"];
132 this->experiment_link_ = rdata[
"link"];
134 this->experiment_key_ = rdata[
"experimentKey"];
135 std::string proc_metric_name = get_metric_name();
136 spdlog::info(
"{} experiment name : {}", proc_metric_name, this->experiment_name_);
137 spdlog::info(
"{} experiment link : {}", proc_metric_name, this->experiment_link_);
138 spdlog::info(
"{} experiment key : {}", proc_metric_name, this->experiment_key_);
140 void broadcast_experiment(){
143 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
147 sprintf(key_buffer,
"%.32s", experiment_key_.c_str());
148 spdlog::info(
"broadcasting experiment information to all processes...");
149 MPI_Bcast(key_buffer, 32, MPI_CHAR, 0, MPI_COMM_WORLD);
151 experiment_key_ = std::string(key_buffer);
152 spdlog::info(
"process {} has the key : {}", mpi_rank, experiment_key_);
155 std::string get_metric_name(){
157 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
158 std::string name = fmt::format(
"proc_{}_{}", mpi_rank, metric_name_);
161 void connection_warning(
int status_code){
162 if(status_code != 200)
163 spdlog::warn(
"Error while connecting Comet server! request status code : {}", status_code);
171 template<
typename T_e,
int T_dim>
178 template<
typename T_e,
int T_dim>
187 this->problem_ = problem;
188 this->container_ = container;
189 this->handler_ = handler;
191 virtual void apply(){
195 nlohmann::json metric_data = {{
"experimentKey", handler_->experiment_key_},
196 {
"metricName", handler_->get_metric_name()},
197 {
"metricValue", best}};
199 cpr::Response r = cpr::Post(cpr::Url{
"https://www.comet.com/api/rest/v2/write/experiment/metric"},
200 cpr::Header{{
"Authorization", this->handler_->comet_api_key_},
201 {
"Content-type",
"application/json"},
202 {
"Accept",
"application/json"}},
203 cpr::Body{metric_data.dump()});
204 this->handler_->connection_warning(r.status_code);