From 938704f4400783e38e91b5853057c769fa26ffc1 Mon Sep 17 00:00:00 2001 From: KyleKlenk <kyle.c.klenk@gmail.com> Date: Tue, 28 Jun 2022 13:47:03 -0600 Subject: [PATCH] can start distributed SummaActors from file more easily --- build/includes/global/global.hpp | 39 ++++- build/includes/main.h | 7 - build/makefile | 17 +- build/source/actors/global/global.cpp | 33 +++- build/source/actors/main.cpp | 154 ++++++++++++------ .../actors/summa_actor/summa_server.cpp | 6 +- 6 files changed, 187 insertions(+), 69 deletions(-) delete mode 100644 build/includes/main.h diff --git a/build/includes/global/global.hpp b/build/includes/global/global.hpp index c4d4b8d..4e09056 100644 --- a/build/includes/global/global.hpp +++ b/build/includes/global/global.hpp @@ -1,6 +1,13 @@ #pragma once #include <chrono> +#include <optional> +#include <iostream> +#include <bits/stdc++.h> +#include <unistd.h> +#include "json.hpp" + +using json = nlohmann::json; extern bool debug; @@ -8,4 +15,34 @@ extern bool debug; * Return the time between to time points */ double calculateTime(std::chrono::time_point<std::chrono::system_clock> start, - std::chrono::time_point<std::chrono::system_clock> end); \ No newline at end of file + std::chrono::time_point<std::chrono::system_clock> end); + +template <typename T> +std::optional<T> getSettings(std::string settings_file_path, std::string key_1, std::string key_2, + T return_value) { + json settings; + std::string summa_actors_settings = "/Summa_Actors_Settings.json"; + std::ifstream settings_file(settings_file_path + summa_actors_settings); + settings_file >> settings; + settings_file.close(); + + // find first key + try { + if (settings.find(key_1) != settings.end()) { + json key_1_settings = settings[key_1]; + + // find value behind second key + if (key_1_settings.find(key_2) != key_1_settings.end()) { + return key_1_settings[key_2]; + } else + return {}; + + } else { + return {}; // return none in the optional (error value) + } + } catch (json::exception& e) { + std::cout << e.what() << "\n"; + return {}; + } + +} \ No newline at end of file diff --git a/build/includes/main.h b/build/includes/main.h deleted file mode 100644 index 8c4b0b9..0000000 --- a/build/includes/main.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef MAIN_H_ -#define MAIN_H_ - - - - -#endif \ No newline at end of file diff --git a/build/makefile b/build/makefile index f8b31cd..6f1d8a6 100644 --- a/build/makefile +++ b/build/makefile @@ -16,16 +16,17 @@ ACTORS_LIBRARIES = -L$(EBROOTCAF)/lib -L$(EBROOTCAF)/lib64 -L$(EBROOTNETCDFMINFO # Production runs -FLAGS_NOAH = -O3 -ffree-form -ffree-line-length-none -fmax-errors=0 -fPIC -Wfatal-errors -FLAGS_COMM = -O3 -ffree-line-length-none -fmax-errors=0 -fPIC -Wfatal-errors -FLAGS_SUMMA = -O3 -ffree-line-length-none -fmax-errors=0 -fPIC -Wfatal-errors -FLAGS_ACTORS = -O3 -Wfatal-errors -std=c++17 +# FLAGS_NOAH = -O3 -ffree-form -ffree-line-length-none -fmax-errors=0 -fPIC -Wfatal-errors +# FLAGS_COMM = -O3 -ffree-line-length-none -fmax-errors=0 -fPIC -Wfatal-errors +# FLAGS_SUMMA = -O3 -ffree-line-length-none -fmax-errors=0 -fPIC -Wfatal-errors +# FLAGS_ACTORS = -O3 -Wfatal-errors -std=c++17 # # Debug runs -# FLAGS_NOAH = -pg -g -O0 -ffree-form -ffree-line-length-none -fmax-errors=0 -fbacktrace -Wno-unused -Wno-unused-dummy-argument -fPIC -# FLAGS_COMM = -pg -g -O0 -Wall -ffree-line-length-none -fmax-errors=0 -fbacktrace -fcheck=bounds -fPIC -# FLAGS_SUMMA = -pg -g -O0 -Wall -ffree-line-length-none -fmax-errors=0 -fbacktrace -fcheck=bounds -fPIC -# FLAGS_ACTORS = -pg -g -O0 -Wall +FLAGS_NOAH = -g -O0 -ffree-form -ffree-line-length-none -fmax-errors=0 -fbacktrace -Wno-unused -Wno-unused-dummy-argument -fPIC +FLAGS_COMM = -g -O0 -Wall -ffree-line-length-none -fmax-errors=0 -fbacktrace -fcheck=bounds -fPIC +FLAGS_SUMMA = -g -O0 -Wall -ffree-line-length-none -fmax-errors=0 -fbacktrace -fcheck=bounds -fPIC +FLAGS_ACTORS = -g -O0 -Wall -std=c++17 + #======================================================================== diff --git a/build/source/actors/global/global.cpp b/build/source/actors/global/global.cpp index d1911da..fff197e 100644 --- a/build/source/actors/global/global.cpp +++ b/build/source/actors/global/global.cpp @@ -1,8 +1,39 @@ #include "global.hpp" #include <chrono> + double calculateTime(std::chrono::time_point<std::chrono::system_clock> start, std::chrono::time_point<std::chrono::system_clock> end) { return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); -} \ No newline at end of file +} + +// template <typename T> +// std::optional<T> getSettings(std::string settings_file_path, std::string key_1, std::string key_2, +// T return_value) { +// json settings; +// std::string summa_actors_settings = "/Summa_Actors_Settings.json"; +// std::ifstream settings_file(settings_file_path + summa_actors_settings); +// settings_file >> settings; +// settings_file.close(); + +// // find first key +// try { +// if (settings.find(key_1) != settings.end()) { +// json key_1_settings = settings[key_1]; + +// // find value behind second key +// if (key_1_settings.find(key_2) != key_1_settings.end()) { +// return key_1_settings[key_2]; +// } else +// return {}; + +// } else { +// return {}; // return none in the optional (error value) +// } +// } catch (json::exception& e) { +// std::cout << e.what() << "\n"; +// return {}; +// } + +// } \ No newline at end of file diff --git a/build/source/actors/main.cpp b/build/source/actors/main.cpp index 9aca725..bffef41 100644 --- a/build/source/actors/main.cpp +++ b/build/source/actors/main.cpp @@ -9,6 +9,8 @@ #include <bits/stdc++.h> #include <unistd.h> #include <iostream> +#include "json.hpp" + using namespace caf; @@ -19,50 +21,59 @@ class config : public actor_system_config { public: int startGRU = -1; int countGRU = -1; - std::string configPath = ""; + std::string config_path = ""; bool debugMode = false; - uint16_t port = 4444; - std::string host = "cnic-giws-cpu-19001-02"; bool server_mode = false; - bool distributed = false; config() { opt_group{custom_options_, "global"} .add(startGRU, "gru,g", "Starting GRU Index") .add(countGRU, "numGRU,n", "Total Number of GRUs") - .add(configPath, "config,c", "Path name of the config directory") + .add(config_path, "config,c", "Path name of the config directory") .add(debugMode, "debug-mode,b", "enable debug mode") - .add(distributed, "distributed-mode,d", "enable distributed mode") - .add(port, "port,p", "set port") - .add(host, "host,h", "set Host (ignored in server mode)") .add(server_mode, "server-mode,s", "enable server mode"); } }; void run_client(actor_system& system, const config& cfg) { scoped_actor self{system}; - if (cfg.distributed) { - aout(self) << "Starting SUMMA-Client in Distributed Mode\n"; - auto c = system.spawn(summa_client); - if (!cfg.host.empty() && cfg.port > 0) { - anon_send(c, connect_atom_v, cfg.host, cfg.port); - } else { - aout(self) << "No Server Config" << std::endl; - } + std::string key_1 = "DistributedSettings"; + std::string key_host = "host"; + std::string key_port = "port"; + std::string host; + int port; + + aout(self) << "Starting SUMMA-Client in Distributed Mode\n"; + host = getSettings(cfg.config_path, "DistributedSettings", "host", host).value_or(""); + port = getSettings(cfg.config_path, "DistributedSettings", "port", port).value_or(-1); + + if (host == "" || port == -1) { + aout(self) << "ERROR: run_client() host and port - CHECK SETTINGS FILE\n"; + return; + } + + auto c = system.spawn(summa_client); + if (!host.empty() && port > 0) { + anon_send(c, connect_atom_v, host, port); } else { - aout(self) << "Starting SUMMA in non-distributed mode \n"; - auto summa = system.spawn(summa_actor, cfg.startGRU, cfg.countGRU, cfg.configPath, self); + aout(self) << "No Server Config" << std::endl; } } - void run_server(actor_system& system, const config& cfg) { scoped_actor self{system}; - auto server = system.spawn(summa_server, cfg.configPath); - aout(self) << "Attempting to publish summa_server_actor" << cfg.port << std::endl; - auto is_port = io::publish(server, cfg.port); + int port; + + port = getSettings(cfg.config_path, "DistributedSettings", "port", port).value_or(-1); + if (port == -1) { + aout(self) << "ERROR: run_server() port - CHECK SETTINGS FILE\n"; + return; + } + auto server = system.spawn(summa_server, cfg.config_path); + aout(self) << "Attempting to publish summa_server_actor on port " << port << std::endl; + auto is_port = io::publish(server, port); if (!is_port) { std::cerr << "********PUBLISH FAILED*******" << to_string(is_port.error()) << "\n"; return; @@ -74,42 +85,87 @@ void run_server(actor_system& system, const config& cfg) { anon_send_exit(server, exit_reason::user_shutdown); } + + + void caf_main(actor_system& sys, const config& cfg) { scoped_actor self{sys}; - if (cfg.startGRU == -1) { - aout(self) << "Starting GRU was not defined!! " << - "startGRU is set with the \"-g\" option\n"; - aout(self) << "EXAMPLE: ./summaMain -g 1 -n 10 -c location/of/config \n"; - return; - } - if (cfg.countGRU == -1) { - aout(self) << "Number of GRUs was not defined!! " << - "countGRU is set with the \"-n\" option\n"; - aout(self) << "EXAMPLE: ./summaMain -g 1 -n 10 -c location/of/config \n"; - return; - } - if (cfg.configPath == "") { - aout(self) << "File Manager was not defined!! " << - "fileManger is set with the \"-c\" option\n"; - aout(self) << "EXAMPLE: ./summaMain -g 1 -n 10 -c location/of/config \n"; - return; - } - if (cfg.debugMode) { - aout(self) << "Starting SUMMA-Actors in DebugMode\n"; - bool debug = true; - } + std::string key_1 = "DistributedSettings"; + std::string key_2 = "distributed-mode"; + bool distributed_mode = false; - // Start the Actors - if (cfg.distributed) { - aout(self) << "Starting SUMMA-Actors in Distributed Mode \n"; + distributed_mode = getSettings(cfg.config_path, key_1, key_2, distributed_mode).value_or(false); + if (distributed_mode) { + // only command line arguments needed are config_path and server-mode auto system = cfg.server_mode ? run_server : run_client; system(sys, cfg); + } else { - auto summa = sys.spawn(summa_actor, cfg.startGRU, cfg.countGRU, cfg.configPath, self); + // Configure command line arguments + if (cfg.startGRU == -1) { + aout(self) << "Starting GRU was not defined!! " << + "startGRU is set with the \"-g\" option\n"; + aout(self) << "EXAMPLE: ./summaMain -g 1 -n 10 -c location/of/config \n"; + return; + } + if (cfg.countGRU == -1) { + aout(self) << "Number of GRUs was not defined!! " << + "countGRU is set with the \"-n\" option\n"; + aout(self) << "EXAMPLE: ./summaMain -g 1 -n 10 -c location/of/config \n"; + return; + } + if (cfg.config_path == "") { + aout(self) << "File Manager was not defined!! " << + "fileManger is set with the \"-c\" option\n"; + aout(self) << "EXAMPLE: ./summaMain -g 1 -n 10 -c location/of/config \n"; + return; + } + + auto summa = sys.spawn(summa_actor, cfg.startGRU, cfg.countGRU, cfg.config_path, self); } + + // // Start the Actors + // if (cfg.distributed) { + // aout(self) << "Starting SUMMA-Actors in Distributed Mode \n"; + // auto system = cfg.server_mode ? run_server : run_client; + // system(sys, cfg); + // } else { + // auto summa = sys.spawn(summa_actor, cfg.startGRU, cfg.countGRU, cfg.configPath, self); + // } // start SUMMA // auto system = cfg.server_mode ? run_server : run_client; // system(sys, cfg); } -CAF_MAIN(id_block::summa, io::middleman) \ No newline at end of file +CAF_MAIN(id_block::summa, io::middleman) +// void parseSettings(actor_system& sys, std::string config_path, const config& cfg) { +// scoped_actor self{sys}; + +// json settings; +// std::string summa_actors_settings = "/Summa_Actors_Settings.json"; +// std::ifstream settings_file(config_path + summa_actors_settings); +// settings_file >> settings; +// settings_file.close(); + +// if (settings.find("DistributedSettings") != settings.end()) { +// json distributed_settings = settings["DistributedSettings"]; + +// if (distributed_settings.find("distributed-mode") != distributed_settings.end()) { +// cfg.setDistributed(distributed_settings["distributed-mode"]); +// } else { +// aout(self) << "ERROR: Cannot find distributed-mode in settings file\n"; +// } +// if (distributed_settings.find("host") != distributed_settings.end()) { +// cfg.host = distributed_settings["host"]; +// } else { +// aout(self) << "ERROR: Cannot find host\n"; +// } +// if (distributed_settings.find("port") != distributed_settings.end()) { +// cfg.port = distributed_settings["port"]; +// } else { +// aout(self) << "ERROR: Cannot find port\n"; +// } +// } else { +// aout(self) << "ERROR: Cannot find Distributed Settings \n"; +// } +// } \ No newline at end of file diff --git a/build/source/actors/summa_actor/summa_server.cpp b/build/source/actors/summa_actor/summa_server.cpp index 9542b92..da7c2b8 100644 --- a/build/source/actors/summa_actor/summa_server.cpp +++ b/build/source/actors/summa_actor/summa_server.cpp @@ -33,7 +33,7 @@ behavior summa_server(stateful_actor<summa_server_state>* self, std::string conf } else { aout(self) << "HRU Batches Assembled, Ready For Clients to Connect \n"; - for (int i = 0; i < self->state.batch_list.size(); i++) { + for (std::vector<int>::size_type i = 0; i < self->state.batch_list.size(); i++) { self->state.batch_list[i]->printBatchInfo(); } } @@ -59,7 +59,7 @@ behavior summa_server(stateful_actor<summa_server_state>* self, std::string conf if (batch_to_send == NULL) { aout(self) << "We Are Done - Telling Clients to exit \n"; - for (int i = 0; i < self->state.client_list.size(); i++) { + for (std::vector<int>::size_type i = 0; i < self->state.client_list.size(); i++) { self->send(self->state.client_list[i]->getActor(), time_to_exit_v); } @@ -132,7 +132,7 @@ int assembleBatches(stateful_actor<summa_server_state>* self) { Batch* getUnsolvedBatch(stateful_actor<summa_server_state>* self) { // Find the first unassigned batch - for (int i = 0; i < self->state.batch_list.size(); i++) { + for (std::vector<int>::size_type i = 0; i < self->state.batch_list.size(); i++) { if (self->state.batch_list[i]->getBatchStatus() == unassigned) { return self->state.batch_list[i]; } -- GitLab