diff --git a/src/delegator.cpp b/src/delegator.cpp index 60f68f55374260eb8eba5af1cfc61f344c4d57ac..51ff0839e7d4f82f712e461a5a968d383e88f925 100644 --- a/src/delegator.cpp +++ b/src/delegator.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -19,8 +20,18 @@ #include "backends/extrae/extrae_type_stack.hpp" #endif -void Delegator::InitDetectionBackend(const std::string &backend) { - pn_annotation_strategy_ = std::make_unique(); +std::optional getNestingMode(const std::string &nesting_mode) { + // detect nesting mode specified by the user + if (stringsAreCaseInsensitiveEqual(nesting_mode, "?")) { + return NestingMode::Detection; + } else if (stringsAreCaseInsensitiveEqual(nesting_mode, "ProperlyNested")) { + return NestingMode::ProperlyNested; + } else if (stringsAreCaseInsensitiveEqual(nesting_mode, + "NotProperlyNested")) { + return NestingMode::NotProperlyNested; + } else { + return std::nullopt; + } } void Delegator::InitNonProperlyNestedBackends(const std::string &backend) { @@ -37,12 +48,13 @@ void Delegator::InitNonProperlyNestedBackends(const std::string &backend) { else { npn_annotation_strategy_ = std::make_unique(); - std::cout << "Could not find a matching NotProperlyNested backend with name" - << backend << " now selecting the Default" << std::endl; + std::cout + << "Could not find a matching NotProperlyNested backend with name " + << backend << " now selecting the Default" << std::endl; } } -void Delegator::InitProperlyNestedBackends(const std::string &backend) { +void Delegator::TryInitProperlyNestedBackend(const std::string &backend) { if (false) { // a bit ugly, but worky } @@ -58,28 +70,28 @@ void Delegator::InitProperlyNestedBackends(const std::string &backend) { #endif } -void Delegator::Init(std::string_view nesting_mode, +void Delegator::Init(std::string_view nesting_mode_in, std::string_view backend_in) { mpi_helper_ = std::make_unique(); + std::string backend = std::string(backend_in); if (mpi_helper_->IsRankNumber(0)) { - std::cout << "NESMIK init called with: " << nesting_mode - << " and Backend: " << backend << std::endl; + std::cout << "neSmiK init called with program nesting: " << nesting_mode_in + << " and backend: " << backend << std::endl; } - // detect nesting mode specified by the user - if (nesting_mode.compare("?") == 0) { - nesting_mode_ = NestingMode::Detection; - } else if (nesting_mode.compare("ProperlyNested") == 0) { - nesting_mode_ = NestingMode::ProperlyNested; - } else if (nesting_mode.compare("NotProperlyNested") == 0) { - nesting_mode_ = NestingMode::NotProperlyNested; + // now determine nesting mode + auto nesting_mode = getNestingMode(std::string(nesting_mode_in)); + if (!nesting_mode.has_value()) { + std::cout << "Something went wrong when selecting the nesting mode, " + "please make sure to spell it correctly" + << std::endl; + exit(1); } - if (mpi_helper_->IsRankNumber(0)) { // Some debug output - switch (nesting_mode_) { + switch (nesting_mode.value()) { case NestingMode::Detection: std::cout << "nesting mode set to: " << "Detection (?)" << std::endl; @@ -92,11 +104,6 @@ void Delegator::Init(std::string_view nesting_mode, std::cout << "nesting mode set to: " << "ProperlyNested" << std::endl; break; - case NestingMode::ProperlyNestedButOtherBackend: - std::cout << "Something went wrong when selecting the nesting mode" - << std::endl; - exit(1); - break; } } @@ -117,41 +124,50 @@ void Delegator::Init(std::string_view nesting_mode, } } - // next choose backend - switch (nesting_mode_) { - case NestingMode::ProperlyNestedButOtherBackend: - break; + // next do backend selection: + + switch (nesting_mode.value()) { case NestingMode::Detection: - InitDetectionBackend(backend); + pn_annotation_strategy_ = std::make_unique(); + backend_selection_ = BackendSelection::ProperlyNested; break; case NestingMode::ProperlyNested: - InitProperlyNestedBackends(backend); - // we preferably use a Properly nested backend, + TryInitProperlyNestedBackend(backend); + // we preferably use a Properly nested backend, // but if this is not possible, we will select the not properly nested // one - [[fallthrough]]; - case NestingMode::NotProperlyNested: - if (pn_annotation_strategy_ == nullptr && mpi_helper_->IsRankNumber(0)) { + if (pn_annotation_strategy_ != nullptr) { + backend_selection_ = BackendSelection::ProperlyNested; + break; // we break here if we already inited the backend + } + + // we inform the user that ProperlyNested initialization failed, and + // fallthrough + if (mpi_helper_->IsRankNumber(0)) { std::cout << "Could not find a matching ProperlyNested backend with name " << backend << std::endl; std::cout << "Now looking for a NotProperlyNested backend, as they " "support ProperlyNested ones" << std::endl; - nesting_mode_ = NestingMode::ProperlyNestedButOtherBackend; } + [[fallthrough]]; + case NestingMode::NotProperlyNested: + // two cases lead us here: either we previously tried to init a properly + // nested backend, which failed, or we are in not properly nested mode. InitNonProperlyNestedBackends(backend); + if (npn_annotation_strategy_ != nullptr) { + backend_selection_ = BackendSelection::NotProperlyNested; + } break; } - switch (nesting_mode_) { - case NestingMode::NotProperlyNested: - case NestingMode::ProperlyNestedButOtherBackend: + switch (backend_selection_) { + case BackendSelection::NotProperlyNested: return npn_annotation_strategy_->Init(); break; - case NestingMode::Detection: - case NestingMode::ProperlyNested: + case BackendSelection::ProperlyNested: return pn_annotation_strategy_->Init(); break; } @@ -159,72 +175,60 @@ void Delegator::Init(std::string_view nesting_mode, // should be thread safe to call? void Delegator::RegionStart(std::string_view name) { - switch (nesting_mode_) { - case NestingMode::NotProperlyNested: - return npn_annotation_strategy_->RegionStart({name}); - case NestingMode::ProperlyNestedButOtherBackend: - name_stack_.push(std::string(name)); - return npn_annotation_strategy_->RegionStart({name}); - case NestingMode::Detection: - case NestingMode::ProperlyNested: - // First push to stack - name_stack_.push(std::string(name)); - return pn_annotation_strategy_->RegionStart({name, name_stack_}); + name_stack_.push(std::string(name)); + + switch (backend_selection_) { + case BackendSelection::NotProperlyNested: + npn_annotation_strategy_->RegionStart({name}); + break; + case BackendSelection::ProperlyNested: + pn_annotation_strategy_->RegionStart({name, name_stack_}); break; } } + void Delegator::RegionStop(std::string_view name) { - switch (nesting_mode_) { - case NestingMode::NotProperlyNested: - case NestingMode::ProperlyNestedButOtherBackend: - return npn_annotation_strategy_->RegionStop({name}); + switch (backend_selection_) { + case BackendSelection::NotProperlyNested: + npn_annotation_strategy_->RegionStop({name}); break; - case NestingMode::Detection: - case NestingMode::ProperlyNested: + case BackendSelection::ProperlyNested: pn_annotation_strategy_->RegionStopLast({name, name_stack_}); - if (!name_stack_.empty()) { - name_stack_.pop(); - } break; } + if (!name_stack_.empty()) { + if (name.compare(name_stack_.top()) != 0) { + std::cout << "neSmiK region_close() expected " << name_stack_.top() + << "but got " << name << std::endl; + } + name_stack_.pop(); + } }; void Delegator::RegionStopLast() { - switch (nesting_mode_) { - case NestingMode::NotProperlyNested: - std::cout << "Calling region_stop_last() is not supported in " - "NotProperlyNested Codes" - << std::endl; + const std::string stack_empty = "NESMIK: STACK EMPTPY"; + const std::string &last_region_name = + name_stack_.empty() ? stack_empty : name_stack_.top(); + switch (backend_selection_) { + case BackendSelection::ProperlyNested: + pn_annotation_strategy_->RegionStopLast({last_region_name, name_stack_}); break; - case NestingMode::Detection: - case NestingMode::ProperlyNested: - case NestingMode::ProperlyNestedButOtherBackend: - const std::string stack_empty = "NESMIK: STACK EMPTPY"; - const std::string &last_region_name = - name_stack_.empty() ? stack_empty : name_stack_.top(); - - if (nesting_mode_ == NestingMode::Detection || - nesting_mode_ == NestingMode::ProperlyNested) { - pn_annotation_strategy_->RegionStopLast( - {last_region_name, name_stack_}); - } else if (nesting_mode_ == NestingMode::ProperlyNestedButOtherBackend) { - npn_annotation_strategy_->RegionStop({last_region_name}); - } - if (!name_stack_.empty()) { - name_stack_.pop(); - } + case BackendSelection::NotProperlyNested: + npn_annotation_strategy_->RegionStop({last_region_name}); break; } + + if (!name_stack_.empty()) { + name_stack_.pop(); + } } void Delegator::Finalize() { - switch (nesting_mode_) { - case NestingMode::NotProperlyNested: - case NestingMode::ProperlyNestedButOtherBackend: + switch (backend_selection_) { + case BackendSelection::NotProperlyNested: return npn_annotation_strategy_->Finalize(); break; - case NestingMode::Detection: - case NestingMode::ProperlyNested: + case BackendSelection::ProperlyNested: return pn_annotation_strategy_->Finalize(); break; } diff --git a/src/delegator.hpp b/src/delegator.hpp index 021ea81a7fd44f7eddbd248fb505115a554c4690..deb18e65803e1ced170bcbfefc7712637decde1a 100644 --- a/src/delegator.hpp +++ b/src/delegator.hpp @@ -8,13 +8,13 @@ #include enum class NestingMode { - Detection, // When selecting the very special Detection backend - ProperlyNested, // Application is properly nested - NotProperlyNested, // Application annotation is not properly nested - ProperlyNestedButOtherBackend // Application is properly nested, but were - // using a not properly nested backend + Detection, // When selecting the very special Detection backend + ProperlyNested, // Application is properly nested + NotProperlyNested // Application annotation is not properly nested }; +enum class BackendSelection { ProperlyNested, NotProperlyNested }; + class Delegator { private: inline static std::unique_ptr @@ -24,7 +24,7 @@ class Delegator { inline static thread_local std::stack name_stack_; - inline static NestingMode nesting_mode_ = NestingMode::Detection; + inline static BackendSelection backend_selection_; inline static std::unique_ptr mpi_helper_; @@ -36,8 +36,7 @@ class Delegator { static void Finalize(); private: - static void InitDetectionBackend(const std::string &backend); static void InitNonProperlyNestedBackends(const std::string &backend); - static void InitProperlyNestedBackends(const std::string &backend); + static void TryInitProperlyNestedBackend(const std::string &backend); }; #endif // NESMIK_DELEGATOR_HPP diff --git a/tests/cpp/TestRegionStopLast.cpp b/tests/cpp/TestRegionStopLast.cpp index 85c3e496bfec7de7fd057591b253b11bce8a8172..6bc07cb88f7a8c2cce97a9a7ee31a32cd25c67da 100644 --- a/tests/cpp/TestRegionStopLast.cpp +++ b/tests/cpp/TestRegionStopLast.cpp @@ -4,7 +4,7 @@ int main() { nesmik::init("ProperlyNested", "Default"); nesmik::region_start("Test0-1"); nesmik::region_start("Test1-1"); - nesmik::region_stop_last(); + nesmik::region_stop("Test1-1"); nesmik::region_start("Test1-2"); nesmik::region_stop_last(); nesmik::region_stop_last();