From 9114a1427e622ed3bcf5ab9b6bf7519a853653fa Mon Sep 17 00:00:00 2001 From: Valentin Seitz Date: Fri, 21 Jun 2024 15:08:24 +0200 Subject: [PATCH 1/3] move parallelism helper into utils --- src/CMakeLists.txt | 3 ++- src/backends/detection/detection.hpp | 2 +- src/backends/dlb/dlb_talp_tree/dlb_talp_tree.cpp | 2 +- src/backends/dlb/dlb_talp_tree/dlb_talp_tree.hpp | 2 +- src/{ => utils}/parallelism_helper.cpp | 2 +- src/{ => utils}/parallelism_helper.hpp | 0 6 files changed, 6 insertions(+), 5 deletions(-) rename src/{ => utils}/parallelism_helper.cpp (96%) rename src/{ => utils}/parallelism_helper.hpp (100%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bdc267e..7297b4d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,6 @@ add_subdirectory(backends) -target_sources(nesmik PRIVATE delegator.cpp nesmik.cpp parallelism_helper.cpp) +add_subdirectory(utils) +target_sources(nesmik PRIVATE delegator.cpp nesmik.cpp) if(BUILD_C_FORTRAN) add_subdirectory(bindings) diff --git a/src/backends/detection/detection.hpp b/src/backends/detection/detection.hpp index 1660b38..8ba46af 100644 --- a/src/backends/detection/detection.hpp +++ b/src/backends/detection/detection.hpp @@ -2,7 +2,7 @@ #define NESMIK_DETECTION_H #include -#include +#include #include #include diff --git a/src/backends/dlb/dlb_talp_tree/dlb_talp_tree.cpp b/src/backends/dlb/dlb_talp_tree/dlb_talp_tree.cpp index 4a39a40..62c3e2d 100644 --- a/src/backends/dlb/dlb_talp_tree/dlb_talp_tree.cpp +++ b/src/backends/dlb/dlb_talp_tree/dlb_talp_tree.cpp @@ -13,7 +13,7 @@ #endif -#include +#include DLBTalpTreeStrategy::DLBTalpTreeStrategy():mpi_helper_{}{ parallelism_descriptor_={ diff --git a/src/backends/dlb/dlb_talp_tree/dlb_talp_tree.hpp b/src/backends/dlb/dlb_talp_tree/dlb_talp_tree.hpp index 9e62b3d..83e51f7 100644 --- a/src/backends/dlb/dlb_talp_tree/dlb_talp_tree.hpp +++ b/src/backends/dlb/dlb_talp_tree/dlb_talp_tree.hpp @@ -8,7 +8,7 @@ #include #include -#include +#include struct TalpRegionNode { std::string name; diff --git a/src/parallelism_helper.cpp b/src/utils/parallelism_helper.cpp similarity index 96% rename from src/parallelism_helper.cpp rename to src/utils/parallelism_helper.cpp index 52f4378..38141d2 100644 --- a/src/parallelism_helper.cpp +++ b/src/utils/parallelism_helper.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/src/parallelism_helper.hpp b/src/utils/parallelism_helper.hpp similarity index 100% rename from src/parallelism_helper.hpp rename to src/utils/parallelism_helper.hpp -- GitLab From 5f7ca63b685fad0d75c5a81ed8c04f0eab9aae14 Mon Sep 17 00:00:00 2001 From: Valentin Seitz Date: Fri, 21 Jun 2024 15:08:39 +0200 Subject: [PATCH 2/3] add enviroment variable --- src/utils/CMakeLists.txt | 1 + src/utils/environment_variable.cpp | 29 +++++++++++++++++++++++++++++ src/utils/environment_variable.hpp | 23 +++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 src/utils/CMakeLists.txt create mode 100644 src/utils/environment_variable.cpp create mode 100644 src/utils/environment_variable.hpp diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt new file mode 100644 index 0000000..369cedf --- /dev/null +++ b/src/utils/CMakeLists.txt @@ -0,0 +1 @@ +target_sources(nesmik PRIVATE environment_variable.cpp parallelism_helper.cpp) \ No newline at end of file diff --git a/src/utils/environment_variable.cpp b/src/utils/environment_variable.cpp new file mode 100644 index 0000000..6a6e07b --- /dev/null +++ b/src/utils/environment_variable.cpp @@ -0,0 +1,29 @@ +#include "environment_variable.hpp" +#include + +EnvironmentVariable::EnvironmentVariable(const std::string &variable_name, const std::string &description) +: +variable_name_(variable_name),description_(description),value_("") +{ + const std::string variable_to_query = EnvironmentVariable::PREFIX + variable_name_; + + const char* env_string = std::getenv(variable_to_query.c_str()); + + if(env_string != nullptr){ + value_ = std::string(env_string); + } +} + + +bool EnvironmentVariable::isSet() const { + return value_.length() > 0; +} + +std::optional EnvironmentVariable::getValue() const { + if(isSet()){ + return std::optional{value_}; + } + else{ + return std::nullopt; + } +} \ No newline at end of file diff --git a/src/utils/environment_variable.hpp b/src/utils/environment_variable.hpp new file mode 100644 index 0000000..b213cc4 --- /dev/null +++ b/src/utils/environment_variable.hpp @@ -0,0 +1,23 @@ +#include +#include +class EnvironmentVariable{ + + inline static std::string PREFIX= "NESMIK_"; + public: + /* + Upon construction it will do the getenv + */ + EnvironmentVariable(const std::string &variable_name, const std::string &description); + EnvironmentVariable() = delete; + + + private: + std::string variable_name_; + std::string description_; + std::string value_; + + public: + std::optional getValue() const ; + bool isSet() const ; + +}; \ No newline at end of file -- GitLab From c603169409fb5f4ce313c0f5955bccfea3d718b4 Mon Sep 17 00:00:00 2001 From: Valentin Seitz Date: Fri, 21 Jun 2024 18:56:57 +0200 Subject: [PATCH 3/3] - Clang format - added enviroment variables --- src/backends/detection/detection.cpp | 7 ++- src/backends/detection/detection.hpp | 3 ++ src/utils/environment_variable.cpp | 44 ++++++++++--------- src/utils/environment_variable.hpp | 66 +++++++++++++++++++++------- src/utils/parallelism_helper.cpp | 63 +++++++++----------------- src/utils/parallelism_helper.hpp | 38 ++++++++-------- 6 files changed, 120 insertions(+), 101 deletions(-) diff --git a/src/backends/detection/detection.cpp b/src/backends/detection/detection.cpp index 4217fbc..627ed18 100644 --- a/src/backends/detection/detection.cpp +++ b/src/backends/detection/detection.cpp @@ -106,13 +106,18 @@ void DetectionStrategy::RegionStopLast( // two error checks are following if(region.stack_.empty()){ - std::cout << "neSmiK WARNING: The stack is already empty, so no more regions to close, but it tried to close " << region.name << std::endl; + if(verbose_mode_.getValue().value_or(false)) + { + std::cout << "neSmiK WARNING: The stack is already empty, so no more regions to close, but it tried to close " << region.name << std::endl; + } has_errors_ = true; return; } // now check if we actually pop the right name if(region.name.compare(region.stack_.top())!= 0){ + if(verbose_mode_.getValue().value_or(false)){ std::cout << "neSmiK WARNING: The stack says the next region to close is " << region.stack_.top() << " but tried closing " << region.name<< std::endl; + } has_errors_ = true; return; } diff --git a/src/backends/detection/detection.hpp b/src/backends/detection/detection.hpp index 8ba46af..1b9ca7d 100644 --- a/src/backends/detection/detection.hpp +++ b/src/backends/detection/detection.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -45,6 +46,8 @@ private: std::mutex starts_mutex_; std::mutex ends_mutex_; + EnvironmentVariable verbose_mode_ = + EnvironmentVariable("DETECTION_VERBOSE", "Enables the verbose mode in the detection backend",true); bool has_errors_ = false; diff --git a/src/utils/environment_variable.cpp b/src/utils/environment_variable.cpp index 6a6e07b..5472e51 100644 --- a/src/utils/environment_variable.cpp +++ b/src/utils/environment_variable.cpp @@ -1,29 +1,31 @@ -#include "environment_variable.hpp" +#include +#include #include +#include +#include -EnvironmentVariable::EnvironmentVariable(const std::string &variable_name, const std::string &description) -: -variable_name_(variable_name),description_(description),value_("") -{ - const std::string variable_to_query = EnvironmentVariable::PREFIX + variable_name_; - - const char* env_string = std::getenv(variable_to_query.c_str()); +#include "environment_variable.hpp" - if(env_string != nullptr){ - value_ = std::string(env_string); - } +// some helper function to compare case insenstive strings +bool charToLowerEquals(char a, char b) { + return std::tolower(static_cast(a)) == + std::tolower(static_cast(b)); } - -bool EnvironmentVariable::isSet() const { - return value_.length() > 0; +bool compareCaseInsenstive(const std::string &a, const std::string &b) { + return std::equal(a.begin(), a.end(), b.begin(), b.end(), charToLowerEquals); } -std::optional EnvironmentVariable::getValue() const { - if(isSet()){ - return std::optional{value_}; - } - else{ - return std::nullopt; - } +template <> std::optional fromEnvString(const std::string &env_string) { + if (compareCaseInsenstive(env_string, "0") || + compareCaseInsenstive(env_string, "false") || + compareCaseInsenstive(env_string, "off")) { + return std::optional{false}; + } else if (compareCaseInsenstive(env_string, "1") || + compareCaseInsenstive(env_string, "true") || + compareCaseInsenstive(env_string, "on")) { + return std::optional{true}; + } else { + return std::nullopt; + } } \ No newline at end of file diff --git a/src/utils/environment_variable.hpp b/src/utils/environment_variable.hpp index b213cc4..2e87a74 100644 --- a/src/utils/environment_variable.hpp +++ b/src/utils/environment_variable.hpp @@ -1,23 +1,55 @@ -#include +#ifndef NESMIK_ENVIROMENT_VARIABLE_HPP +#define NESMIK_ENVIROMENT_VARIABLE_HPP +#include +#include +#include +#include #include -class EnvironmentVariable{ +#include + +// some helper function to compare case insenstive strings +bool charToLowerEquals(char a, char b); + +bool compareCaseInsenstive(const std::string &a, const std::string &b); + +// dont allow the compiler to guess, but force user to implement conversion +template +std::optional fromEnvString(const std::string &env_string) = delete; + +template <> std::optional fromEnvString(const std::string &env_string); + +template class EnvironmentVariable { + inline static std::string PREFIX = "NESMIK_"; +public: + /* + Upon construction it will do the getenv + */ + EnvironmentVariable(const std::string &variable_name, + const std::string &description, bool required = false) + : variable_name_(variable_name), description_(description) { + const std::string variable_to_query = + EnvironmentVariable::PREFIX + variable_name_; - inline static std::string PREFIX= "NESMIK_"; - public: - /* - Upon construction it will do the getenv - */ - EnvironmentVariable(const std::string &variable_name, const std::string &description); - EnvironmentVariable() = delete; + const char *env_string = std::getenv(variable_to_query.c_str()); + if (env_string != nullptr) { + value_ = fromEnvString(std::string(env_string)); + } + if(required && !value_.has_value()){ + std::cout << "neSmiK Error: " << variable_to_query << " not set, restart application and set it" << std::endl; + exit(1); + } + } + EnvironmentVariable() = delete; - private: - std::string variable_name_; - std::string description_; - std::string value_; +private: + std::string variable_name_; + std::string description_; + std::optional value_; - public: - std::optional getValue() const ; - bool isSet() const ; +public: + std::optional getValue() const { return value_; } + bool isSet() const { return value_.has_value(); } +}; -}; \ No newline at end of file +#endif // NESMIK_ENVIROMENT_VARIABLE_HPP \ No newline at end of file diff --git a/src/utils/parallelism_helper.cpp b/src/utils/parallelism_helper.cpp index 38141d2..20c2014 100644 --- a/src/utils/parallelism_helper.cpp +++ b/src/utils/parallelism_helper.cpp @@ -1,64 +1,43 @@ -#include -#include #include +#include +#include #ifdef WITH_MPI +bool MPIHelper::IsUsingMPI() const { return is_using_mpi_; } -bool MPIHelper::IsUsingMPI()const -{ - return is_using_mpi_; - -} - -bool MPIHelper::CompiledWithMPI() const { - return true; -} +bool MPIHelper::CompiledWithMPI() const { return true; } bool MPIHelper::IsRankNumber(int asked_rank) const { - return asked_rank== mpi_comm_rank; + return asked_rank == mpi_comm_rank; } -int MPIHelper::getRankNumber() const{ - return mpi_comm_rank; -} +int MPIHelper::getRankNumber() const { return mpi_comm_rank; } -MPIHelper::MPIHelper(){ - // First check if MPI is initialized - int is_initialized; - MPI_Initialized(&is_initialized); - mpi_is_initialized_ = static_cast(is_initialized); +MPIHelper::MPIHelper() { + // First check if MPI is initialized + int is_initialized; + MPI_Initialized(&is_initialized); + mpi_is_initialized_ = static_cast(is_initialized); - if(mpi_is_initialized_){ - MPI_Comm_size(MPI_COMM_WORLD,&mpi_comm_size); - MPI_Comm_rank(MPI_COMM_WORLD,&mpi_comm_rank); + if (mpi_is_initialized_) { + MPI_Comm_size(MPI_COMM_WORLD, &mpi_comm_size); + MPI_Comm_rank(MPI_COMM_WORLD, &mpi_comm_rank); - is_using_mpi_ = mpi_comm_size > 1; - } + is_using_mpi_ = mpi_comm_size > 1; + } } #else +bool MPIHelper::IsUsingMPI() const { return false; } -bool MPIHelper::IsUsingMPI() const -{ - return false; -} - -bool MPIHelper::CompiledWithMPI() const { - return false; -} - -bool MPIHelper::IsRankNumber(int asked_rank) const { - return asked_rank==0; -} +bool MPIHelper::CompiledWithMPI() const { return false; } +bool MPIHelper::IsRankNumber(int asked_rank) const { return asked_rank == 0; } -int MPIHelper::getRankNumber() const{ - return mpi_comm_rank; -} - -MPIHelper::MPIHelper(){} +int MPIHelper::getRankNumber() const { return mpi_comm_rank; } +MPIHelper::MPIHelper() {} #endif \ No newline at end of file diff --git a/src/utils/parallelism_helper.hpp b/src/utils/parallelism_helper.hpp index 5aebb05..e873bb3 100644 --- a/src/utils/parallelism_helper.hpp +++ b/src/utils/parallelism_helper.hpp @@ -1,29 +1,27 @@ #ifndef NESMIK_PARALLELISM_HELPER_HPP #define NESMIK_PARALLELISM_HELPER_HPP /* - Small helper class that can be used by Backends to Check MPI status in the application beeing run. + Small helper class that can be used by Backends to Check MPI status in the + application beeing run. */ -class MPIHelper{ - private: +class MPIHelper { +private: + // Default values for non-MPI execution + bool is_using_mpi_ = false; + bool mpi_is_initialized_ = false; + int mpi_comm_size = 0; + int mpi_comm_rank = 0; - // Default values for non-MPI execution - bool is_using_mpi_ = false; - bool mpi_is_initialized_ = false; - int mpi_comm_size = 0; - int mpi_comm_rank = 0; +public: + MPIHelper(); + // returns if the library is compiled with MPI support + bool CompiledWithMPI() const; + // Check is the application is actually using MPI with more than one process + bool IsUsingMPI() const; + // Checks if rank is a certain one, and 0 if MPI is not active + bool IsRankNumber(int asked_rank) const; - public: - - MPIHelper(); - // returns if the library is compiled with MPI support - bool CompiledWithMPI() const ; - // Check is the application is actually using MPI with more than one process - bool IsUsingMPI() const; - // Checks if rank is a certain one, and 0 if MPI is not active - bool IsRankNumber(int asked_rank) const; - - int getRankNumber() const; - + int getRankNumber() const; }; #endif // NESMIK_PARALLELISM_HELPER_HPP \ No newline at end of file -- GitLab