From fdae9ed2cefc031c63c19c7928694b4770c94f08 Mon Sep 17 00:00:00 2001 From: Valentin Seitz Date: Wed, 29 May 2024 21:49:43 +0200 Subject: [PATCH 1/5] Adopted Generic API --- cmake/FindExtrae.cmake | 54 +++++++++++++------------- src/backends/default/default.cpp | 8 ++-- src/backends/default/default.hpp | 8 ++-- src/backends/extrae/CMakeLists.txt | 10 ++++- src/backends/extrae/legacy/wrapper.cpp | 12 +----- src/backends/extrae/legacy/wrapper.hpp | 4 +- src/sit.cpp | 5 ++- src/sit_delegator.cpp | 6 +-- src/sit_strategy.hpp | 8 ++-- 9 files changed, 57 insertions(+), 58 deletions(-) diff --git a/cmake/FindExtrae.cmake b/cmake/FindExtrae.cmake index 051810a..6fe5ebc 100644 --- a/cmake/FindExtrae.cmake +++ b/cmake/FindExtrae.cmake @@ -15,13 +15,13 @@ Result Variables This will define the following variables: -``EXTRAE_FOUND`` +``Extrae_FOUND`` True if the system has the Extrae library. -``EXTRAE_DIR`` +``Extrae_DIR`` The root directory of the Extrae library. -``EXTRAE_INCLUDE_DIRS`` +``Extrae_INCLUDE_DIRS`` Include directories needed to use Extrae. -``EXTRAE_LIBRARIES`` +``Extrae_LIBRARIES`` Libraries needed to link to Extrae. Cache Variables @@ -29,59 +29,59 @@ Cache Variables The following cache variables may also be set: -``EXTRAE_DIR`` +``Extrae_DIR`` The Extrae installation directory (prefix). #]=======================================================================] -if(NOT EXTRAE_DIR) - if(DEFINED ENV{EXTRAE_HOME}) - message(STATUS "Find Extrae: No EXTRAE_DIR provided: Falling back on $ENV{EXTRAE_HOME}") - set(EXTRAE_DIR $ENV{EXTRAE_HOME} ) +if(NOT Extrae_DIR) + if(DEFINED ENV{Extrae_HOME}) + message(STATUS "Find Extrae: No Extrae_DIR provided: Falling back on $ENV{Extrae_HOME}") + set(Extrae_DIR $ENV{Extrae_HOME} ) else() - message(FATAL_ERROR "Find Extrae: Couldn't get from $EXTRAE_HOME, please specify installation location with -DEXTRAE_DIR") + message(STATUS "Find Extrae: Trying to find Extrae in the normal system paths as $Extrae_HOME is not specified") endif() endif() -if(DEFINED EXTRAE_LIB) - find_library(EXTRAE_LIBRARY - NAMES ${EXTRAE_LIB} - HINTS ${EXTRAE_DIR} +if(DEFINED Extrae_LIB) + find_library(Extrae_LIBRARY + NAMES ${Extrae_LIB} + HINTS ${Extrae_DIR} PATH_SUFFIXES lib lib64) else() - find_library(EXTRAE_LIBRARY + find_library(Extrae_LIBRARY NAMES seqtrace - HINTS ${EXTRAE_DIR} + HINTS ${Extrae_DIR} PATH_SUFFIXES lib lib64) endif() -find_path(EXTRAE_INCLUDE_DIR +find_path(Extrae_INCLUDE_DIR NAMES extrae.h - PATHS ${EXTRAE_DIR} + PATHS ${Extrae_DIR} PATH_SUFFIXES include ) include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(EXTRAE +find_package_handle_standard_args(Extrae REQUIRED_VARS - EXTRAE_LIBRARY - EXTRAE_INCLUDE_DIR + Extrae_LIBRARY + Extrae_INCLUDE_DIR ) -if(EXTRAE_FOUND) - set(EXTRAE_LIBRARIES ${EXTRAE_LIBRARY}) - set(EXTRAE_INCLUDE_DIRS ${EXTRAE_INCLUDE_DIR}) +if(Extrae_FOUND) + set(Extrae_LIBRARIES ${Extrae_LIBRARY}) + set(Extrae_INCLUDE_DIRS ${Extrae_INCLUDE_DIR}) endif() -if(NOT EXTRAE_FOUND) +if(NOT Extrae_FOUND) message(FATAL_ERROR "Did not find required library Extrae") else() - message(STATUS "Using Extrae in installation: ${EXTRAE_DIR}") + message(STATUS "Using Extrae in installation: ${Extrae_DIR}") endif() -mark_as_advanced(EXTRAE_DIR) +mark_as_advanced(Extrae_DIR) diff --git a/src/backends/default/default.cpp b/src/backends/default/default.cpp index 85a189d..34f199d 100644 --- a/src/backends/default/default.cpp +++ b/src/backends/default/default.cpp @@ -4,18 +4,18 @@ #include "default.hpp" void DefaultStrategy::region_start( - const SITRegionInformation ®ion) const noexcept { + const SITRegionInformation ®ion) noexcept { std::cout << "Region start: " << region.name << std::endl; } void DefaultStrategy::region_stop( - const SITRegionInformation ®ion) const noexcept { + const SITRegionInformation ®ion) noexcept { std::cout << "Region stop: " << region.name << std::endl; } -void DefaultStrategy::init() const noexcept { +void DefaultStrategy::init() noexcept { std::cout << "SIT_init() Backend: Default " << std::endl; } -void DefaultStrategy::finalize() const noexcept { +void DefaultStrategy::finalize() noexcept { std::cout << "SIT_finalize() Backend: Default " << std::endl; } diff --git a/src/backends/default/default.hpp b/src/backends/default/default.hpp index 1cb6060..1d7b57e 100644 --- a/src/backends/default/default.hpp +++ b/src/backends/default/default.hpp @@ -5,8 +5,8 @@ class DefaultStrategy : public SITProfilingStrategy { public: inline static const std::string_view name = "Default"; - void region_start(const SITRegionInformation ®ion) const noexcept override; - void region_stop(const SITRegionInformation ®ion) const noexcept override; - virtual void init() const noexcept; - virtual void finalize() const noexcept; + void region_start(const SITRegionInformation ®ion) noexcept override; + void region_stop(const SITRegionInformation ®ion) noexcept override; + virtual void init() noexcept; + virtual void finalize() noexcept; }; diff --git a/src/backends/extrae/CMakeLists.txt b/src/backends/extrae/CMakeLists.txt index 670a704..af1fc44 100644 --- a/src/backends/extrae/CMakeLists.txt +++ b/src/backends/extrae/CMakeLists.txt @@ -1,2 +1,8 @@ -#find_package(Extrae REQUIRED) -target_sources(sit PRIVATE extrae_type_stack.cpp extrae_value_stack.cpp) \ No newline at end of file +find_package(Extrae REQUIRED) +target_sources(sit PRIVATE extrae_type_stack.cpp) +if(Extrae_FOUND) +target_include_directories(sit + PRIVATE ${Extrae_INCLUDE_DIRS}) +target_link_libraries(sit PUBLIC ${Extrae_LIBRARIES}) +target_compile_definitions(sit PRIVATE "ENABLE_EXTRAE") +endif() \ No newline at end of file diff --git a/src/backends/extrae/legacy/wrapper.cpp b/src/backends/extrae/legacy/wrapper.cpp index f182a70..c0a3d7d 100644 --- a/src/backends/extrae/legacy/wrapper.cpp +++ b/src/backends/extrae/legacy/wrapper.cpp @@ -51,17 +51,7 @@ public: return routine_to_value_mapper; } - static inline extrae_value_t pop_level_stack(const int level) { - if (level_stacks[level].empty()) { - return 0; - } - level_stacks[level].pop(); - if (level_stacks[level].empty()) { - return 0; - } - const auto value = level_stacks[level].top(); - return value; - } + static inline void push_level_stack(const extrae_value_t value, int level) { level_stacks[level].push(value); diff --git a/src/backends/extrae/legacy/wrapper.hpp b/src/backends/extrae/legacy/wrapper.hpp index e706298..42bb72d 100644 --- a/src/backends/extrae/legacy/wrapper.hpp +++ b/src/backends/extrae/legacy/wrapper.hpp @@ -18,7 +18,7 @@ Extrae_Wrapper_Init_ValueStack("Name Of Type") SIMPLE INSTRUMENTATION TOOL - enum class class MultiLevelExtraeWrapper { + class MultiLevelExtraeWrapper { public : static constexpr int MAX_LEVELS = 10; private : static constexpr extrae_type_t BASE_EVENT = 81000; @@ -104,7 +104,7 @@ void event_stop(const std::string &routineName, int level, bool withCounters) { } } -void extrae_stop() { Extrae_shutdown(); } +void extrae_stop() { Extrae_shutdown(); }s void extrae_start() { Extrae_restart(); } void finalize() { diff --git a/src/sit.cpp b/src/sit.cpp index 099c542..d9ef341 100644 --- a/src/sit.cpp +++ b/src/sit.cpp @@ -2,8 +2,11 @@ #include "sit/sit.hpp" #include "sit_delegator.hpp" #include + +// provides a c-like way to call things with just a fuction compared to instanciate a class. namespace sit { -void init(const std::string &backend) { return SITDelegator::init(backend); } +void init(const std::string &backend) { return SITDelegator::init(backend); +} void region_start(const std::string &name) { return SITDelegator::region_start(name); } diff --git a/src/sit_delegator.cpp b/src/sit_delegator.cpp index 77b2372..b14998c 100644 --- a/src/sit_delegator.cpp +++ b/src/sit_delegator.cpp @@ -13,7 +13,7 @@ #endif #ifdef ENABLE_EXTRAE -#include "backends/extrae/extrae_value_stack.hpp" +#include "backends/extrae/extrae_type_stack.hpp" #endif void SITDelegator::init(std::string_view backend) { @@ -23,8 +23,8 @@ void SITDelegator::init(std::string_view backend) { profiling_strategy = std::make_unique(); } #ifdef ENABLE_EXTRAE - else if (backend.compare(ExtraeValueStackStrategy::name) == 0) { - profiling_strategy = std::make_unique(); + else if (backend.compare(ExtraeTypeStackStrategy::name) == 0) { + profiling_strategy = std::make_unique(); } #endif #ifdef ENABLE_DLB diff --git a/src/sit_strategy.hpp b/src/sit_strategy.hpp index 19749c1..34ae59d 100644 --- a/src/sit_strategy.hpp +++ b/src/sit_strategy.hpp @@ -15,11 +15,11 @@ class SITProfilingStrategy { public: virtual ~SITProfilingStrategy() = default; virtual void - region_start(const SITRegionInformation ®ion) const noexcept = 0; + region_start(const SITRegionInformation ®ion) noexcept = 0; virtual void - region_stop(const SITRegionInformation ®ion) const noexcept = 0; - virtual void init() const noexcept = 0; - virtual void finalize() const noexcept = 0; + region_stop(const SITRegionInformation ®ion) noexcept = 0; + virtual void init() noexcept = 0; + virtual void finalize() noexcept = 0; }; #endif // SIT_STRATEGY_HPP \ No newline at end of file -- GitLab From a50766aa4168c6083bd8e5e60aa8daaf8c043d22 Mon Sep 17 00:00:00 2001 From: Valentin Seitz Date: Wed, 29 May 2024 21:49:50 +0200 Subject: [PATCH 2/5] added extrae --- src/backends/extrae/extrae_type_stack.cpp | 84 +++++++++++++++++++--- src/backends/extrae/extrae_type_stack.hpp | 29 ++++++++ src/backends/extrae/extrae_value_stack.cpp | 8 +-- src/backends/extrae/extrae_value_stack.hpp | 10 +-- 4 files changed, 113 insertions(+), 18 deletions(-) create mode 100644 src/backends/extrae/extrae_type_stack.hpp diff --git a/src/backends/extrae/extrae_type_stack.cpp b/src/backends/extrae/extrae_type_stack.cpp index 6360abf..b8b5f8b 100644 --- a/src/backends/extrae/extrae_type_stack.cpp +++ b/src/backends/extrae/extrae_type_stack.cpp @@ -1,17 +1,81 @@ -#include "sit_strategy.hpp" +#include "extrae_type_stack.hpp" +#include #include +extern "C" { +#include "extrae.h" +} -class ExtraeTypeStackStrategy : public SITProfilingStrategy { +void ExtraeTypeStackStrategy::init() noexcept { -public: - inline static const std::string_view name = "Extrae::TypeStack"; + auto isInitializedVal = Extrae_is_initialized(); + if (isInitializedVal == 0) { + // Extrae is not initialized yet see + // https://github.com/bsc-performance-tools/extrae/blob/daee11a2f98e301eb146608f51df0c844e1ff381/include/extrae_types.h#L33 + Extrae_init(); + didInitialize = true; + } +} + +extrae_value +ExtraeTypeStackStrategy::get_value_by_region_name(const std::string &name) { + // maybe insert if not count up: + if (regionNameToValue.count(name) > 0) { + return regionNameToValue[name]; + } else { +#pragma omp criticial + { + numberOfDistinctRegions++; + regionNameToValue[name] = numberOfDistinctRegions; + } + return numberOfDistinctRegions; + } +} + +void ExtraeTypeStackStrategy::region_start( + const SITRegionInformation ®ion) noexcept { + + // First push to stack + const auto regionName = std::string(region.name); + // to be locked + regionNameStack.push(regionName); + auto typeOffset = regionNameStack.size(); + maxStackSize = std::max(typeOffset, maxStackSize); + // end of locked region + + // maybe requires a lock to? + auto value = get_value_by_region_name(regionName); - void - region_start(const SITRegionInformation ®ion) const noexcept override {} - void region_stop(const SITRegionInformation ®ion) const noexcept override { + auto type = baseType + typeOffset; + Extrae_eventandcounters(type, value); +} +void ExtraeTypeStackStrategy::region_stop( + const SITRegionInformation ®ion) noexcept { + + if (regionNameStack.empty()) { + // whoops + return; + } + + const auto &lastRegionName = regionNameStack.top(); + + if (lastRegionName.compare(region.name) == 0) { + // its the same -> we pop it and end that stack level with a 0 + regionNameStack.pop(); + auto typeOffset = regionNameStack.size(); + auto type = baseType + typeOffset; + Extrae_eventandcounters(type, 0); + return; + } else { + std::cout << "imbalanced region stop " << region.name << std::endl; } +} + +void ExtraeTypeStackStrategy::finalize() noexcept { - virtual void init() const noexcept {} - virtual void finalize() const noexcept {} -}; + if (didInitialize) { + // Assumption is that if we needed to initialize the lib we also need to + // finalize it + Extrae_fini(); + } +} diff --git a/src/backends/extrae/extrae_type_stack.hpp b/src/backends/extrae/extrae_type_stack.hpp new file mode 100644 index 0000000..8ec7e6a --- /dev/null +++ b/src/backends/extrae/extrae_type_stack.hpp @@ -0,0 +1,29 @@ +#include "sit_strategy.hpp" +#include +#include +#include +// #include +typedef unsigned long extrae_value; +typedef unsigned long extrae_type; + +class ExtraeTypeStackStrategy : public SITProfilingStrategy { + +private: + bool didInitialize; + static constexpr unsigned long baseType = 81000; + std::stack regionNameStack; + std::unordered_map regionNameToValue; + std::size_t numberOfDistinctRegions; + std::size_t maxStackSize; + + extrae_value get_value_by_region_name(const std::string &name); + + // std::mutex regionStackMutex; + +public: + inline static const std::string_view name = "Extrae::TypeStack"; + void region_start(const SITRegionInformation ®ion) noexcept override; + void region_stop(const SITRegionInformation ®ion) noexcept override; + virtual void init() noexcept; + virtual void finalize() noexcept; +}; diff --git a/src/backends/extrae/extrae_value_stack.cpp b/src/backends/extrae/extrae_value_stack.cpp index 90cef23..2add7bc 100644 --- a/src/backends/extrae/extrae_value_stack.cpp +++ b/src/backends/extrae/extrae_value_stack.cpp @@ -1,17 +1,17 @@ #include "extrae_value_stack.hpp" void ExtraeValueStackStrategy::region_start( - const SITRegionInformation ®ion) const noexcept { + const SITRegionInformation ®ion) noexcept { // TODO } void ExtraeValueStackStrategy::region_stop( - const SITRegionInformation ®ion) const noexcept { + const SITRegionInformation ®ion) noexcept { // TODO } -void ExtraeValueStackStrategy::init() const noexcept { +void ExtraeValueStackStrategy::init() noexcept { // TODO } -void ExtraeValueStackStrategy::finalize() const noexcept { +void ExtraeValueStackStrategy::finalize() noexcept { // TODO } diff --git a/src/backends/extrae/extrae_value_stack.hpp b/src/backends/extrae/extrae_value_stack.hpp index 039c674..4bde6fd 100644 --- a/src/backends/extrae/extrae_value_stack.hpp +++ b/src/backends/extrae/extrae_value_stack.hpp @@ -4,8 +4,10 @@ class ExtraeValueStackStrategy : public SITProfilingStrategy { public: inline static const std::string_view name = "Extrae::ValueStack"; - void region_start(const SITRegionInformation ®ion) const noexcept override; - void region_stop(const SITRegionInformation ®ion) const noexcept override; - virtual void init() const noexcept; - virtual void finalize() const noexcept; + virtual void + region_start(const SITRegionInformation ®ion) noexcept override; + virtual void + region_stop(const SITRegionInformation ®ion) noexcept override; + virtual void init() noexcept; + virtual void finalize() noexcept; }; -- GitLab From 2122ed0415b9a772bff1e63a526437ae3559b0ed Mon Sep 17 00:00:00 2001 From: Valentin Seitz Date: Thu, 30 May 2024 14:48:21 +0200 Subject: [PATCH 3/5] added Extrae Test and implented an easy way to export a paraver config suited to view the annoations --- CMakeLists.txt | 7 +- src/backends/extrae/CMakeLists.txt | 3 +- src/backends/extrae/extrae_type_stack.cpp | 229 +++++++++++++++++++--- src/backends/extrae/extrae_type_stack.hpp | 29 ++- tests/CMakeLists.txt | 13 ++ tests/cpp/TestCppExtraeTypeStack.cpp | 17 ++ 6 files changed, 262 insertions(+), 36 deletions(-) create mode 100644 tests/cpp/TestCppExtraeTypeStack.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fefaff5..02d1b5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,10 +9,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) list (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") -option(ENABLE_EXTRAE "Enable Extrae" OFF) -option(ENABLE_DLB "Enable DLB" OFF) +option(BACKEND_EXTRAE "Enable Extrae" OFF) +option(BACKEND_DLB "Enable DLB" OFF) option(BUILD_C_FORTRAN "Enable C and Fortran Interfaces" ON) option(GEN_BINDINGS "Use Shroud to Generate C/Fortran interface" OFF) + add_library(sit SHARED) set(SIT_PUBLIC_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include) @@ -25,8 +26,6 @@ target_include_directories(sit PUBLIC ) -#set_target_properties(sit PROPERTIES PUBLIC_HEADER "${SIT_PUBLIC_HEADERS}") - if(BUILD_C_FORTRAN) enable_language(Fortran) set(CMAKE_Fortran_FORMAT FREE) diff --git a/src/backends/extrae/CMakeLists.txt b/src/backends/extrae/CMakeLists.txt index af1fc44..d35754d 100644 --- a/src/backends/extrae/CMakeLists.txt +++ b/src/backends/extrae/CMakeLists.txt @@ -5,4 +5,5 @@ target_include_directories(sit PRIVATE ${Extrae_INCLUDE_DIRS}) target_link_libraries(sit PUBLIC ${Extrae_LIBRARIES}) target_compile_definitions(sit PRIVATE "ENABLE_EXTRAE") -endif() \ No newline at end of file +endif() + diff --git a/src/backends/extrae/extrae_type_stack.cpp b/src/backends/extrae/extrae_type_stack.cpp index b8b5f8b..21fb90a 100644 --- a/src/backends/extrae/extrae_type_stack.cpp +++ b/src/backends/extrae/extrae_type_stack.cpp @@ -1,11 +1,106 @@ #include "extrae_type_stack.hpp" #include #include +#include +#include +#include +#include +#include +#include + extern "C" { -#include "extrae.h" +#include } -void ExtraeTypeStackStrategy::init() noexcept { + + +const std::string ExtraeTypeStackStrategy::paraverConfigHead = R"( +#ParaverCFG +ConfigFile.Version: 3.4 +ConfigFile.NumWindows: 2 +ConfigFile.BeginDescription +Configuration to visualize the annotation done by SIT in Paraver +ConfigFile.EndDescription +)"; + + +const std::string ExtraeTypeStackStrategy::paraverConfigOverviewWindow = R"( +################################################################################ +< NEW DISPLAYING WINDOW SIT Annotation Overview > +################################################################################ +window_name SIT Annotation Overview +window_type single +window_width 600 +window_height 114 +window_comm_lines_enabled false +window_flags_enabled true +window_noncolor_mode true +window_custom_color_enabled false +window_semantic_scale_min_at_zero false +window_logical_filtered true +window_physical_filtered false +window_intracomms_enabled true +window_intercomms_enabled true +window_comm_fromto true +window_comm_tagsize true +window_comm_typeval true +window_compute_y_max false +window_level thread +window_scale_relative 1.000000000000 +window_end_time_relative 1.000000000000 +window_object appl { 1, { All } } +window_begin_time_relative 0.000000000000 +window_open true +window_drawmode draw_maximum +window_drawmode_rows draw_maximum +window_pixel_size 1 +window_labels_to_draw 1 +window_object_axis_position 0 +window_selected_functions { 14, { {cpu, Active Thd}, {appl, Adding}, {task, Adding}, {thread, Last Evt Val}, {node, Adding}, {system, Adding}, {workload, Adding}, {from_obj, All}, {to_obj, All}, {tag_msg, All}, {size_msg, All}, {bw_msg, All}, {evt_type, =}, {evt_value, All} } } +window_compose_functions { 9, { {compose_cpu, As Is}, {compose_appl, As Is}, {compose_task, As Is}, {compose_thread, Stacked Val}, {compose_node, As Is}, {compose_system, As Is}, {compose_workload, As Is}, {topcompose1, As Is}, {topcompose2, As Is} } } +window_filter_module evt_type SIT_REPLACE_NUM_LEVELS SIT_REPLACE_LEVELS +)"; + + +const std::string ExtraeTypeStackStrategy::paraverConfigLevelWindow = R"( +################################################################################ +< NEW DISPLAYING WINDOW SIT_REPLACE_WINDOW_NAME > +################################################################################ +window_name SIT_REPLACE_WINDOW_NAME +window_type single +window_width 600 +window_height 114 +window_comm_lines_enabled false +window_flags_enabled true +window_noncolor_mode true +window_custom_color_enabled false +window_semantic_scale_min_at_zero false +window_logical_filtered true +window_physical_filtered false +window_intracomms_enabled true +window_intercomms_enabled true +window_comm_fromto true +window_comm_tagsize true +window_comm_typeval true +window_compute_y_max false +window_level thread +window_scale_relative 1.000000000000 +window_end_time_relative 1.000000000000 +window_object appl { 1, { All } } +window_begin_time_relative 0.000000000000 +window_open true +window_drawmode draw_maximum +window_drawmode_rows draw_maximum +window_pixel_size 1 +window_labels_to_draw 1 +window_object_axis_position 0 +window_selected_functions { 14, { {cpu, Active Thd}, {appl, Adding}, {task, Adding}, {thread, Last Evt Val}, {node, Adding}, {system, Adding}, {workload, Adding}, {from_obj, All}, {to_obj, All}, {tag_msg, All}, {size_msg, All}, {bw_msg, All}, {evt_type, =}, {evt_value, All} } } +window_compose_functions { 9, { {compose_cpu, As Is}, {compose_appl, As Is}, {compose_task, As Is}, {compose_thread, As Is}, {compose_node, As Is}, {compose_system, As Is}, {compose_workload, As Is}, {topcompose1, As Is}, {topcompose2, As Is} } } +window_filter_module evt_type 1 SIT_REPLACE_EVENT_TYPE +)"; + + +void ExtraeTypeStackStrategy::init() noexcept { auto isInitializedVal = Extrae_is_initialized(); if (isInitializedVal == 0) { @@ -14,20 +109,37 @@ void ExtraeTypeStackStrategy::init() noexcept { Extrae_init(); didInitialize = true; } + + // Check if we wanna write a configuration file + const auto ENV_SIT_EXTRAE_WRITE_CONFIG_FILE = std::getenv("SIT_EXTRAE_WRITE_CONFIG_FILE"); + const auto ENV_SIT_EXTRAE_CONFIG_FILE_PATH = std::getenv("SIT_EXTRAE_CONFIG_FILE_PATH"); + if(ENV_SIT_EXTRAE_WRITE_CONFIG_FILE != nullptr){ + const auto configFileString= std::string(ENV_SIT_EXTRAE_WRITE_CONFIG_FILE); + if(configFileString.compare("1") == 0 || configFileString.compare("ON") == 0){ + EXTRAE_WRITE_CONFIG_FILE = true; + } + } + + if(ENV_SIT_EXTRAE_CONFIG_FILE_PATH != nullptr){ + EXTRAE_CONFIG_FILE_PATH = std::string(ENV_SIT_EXTRAE_CONFIG_FILE_PATH); + } + + + //} } extrae_value ExtraeTypeStackStrategy::get_value_by_region_name(const std::string &name) { - // maybe insert if not count up: - if (regionNameToValue.count(name) > 0) { - return regionNameToValue[name]; - } else { -#pragma omp criticial - { - numberOfDistinctRegions++; - regionNameToValue[name] = numberOfDistinctRegions; - } - return numberOfDistinctRegions; + // maybe insert + if (regionMapData.regionNameToValue.count(name) > 0) { + return regionMapData.regionNameToValue[name]; + } + else + // if not allocate new entry + { + const auto newValue= regionMapData.regionNameToValue.size()+1; + regionMapData.regionNameToValue[name] = newValue; + return newValue; } } @@ -36,13 +148,11 @@ void ExtraeTypeStackStrategy::region_start( // First push to stack const auto regionName = std::string(region.name); - // to be locked - regionNameStack.push(regionName); - auto typeOffset = regionNameStack.size(); - maxStackSize = std::max(typeOffset, maxStackSize); - // end of locked region + auto typeOffset = regionStackData.regionNameStack.size(); + regionStackData.regionNameStack.push(regionName); + + regionStackData.historicMaxStackSize = std::max(typeOffset, regionStackData.historicMaxStackSize); - // maybe requires a lock to? auto value = get_value_by_region_name(regionName); auto type = baseType + typeOffset; @@ -52,17 +162,18 @@ void ExtraeTypeStackStrategy::region_start( void ExtraeTypeStackStrategy::region_stop( const SITRegionInformation ®ion) noexcept { - if (regionNameStack.empty()) { + if (regionStackData.regionNameStack.empty()) { // whoops + std::cout << "imbalanced region stop REASON: Emtpy stack " << region.name << std::endl; return; } - const auto &lastRegionName = regionNameStack.top(); + const auto &lastRegionName = regionStackData.regionNameStack.top(); if (lastRegionName.compare(region.name) == 0) { // its the same -> we pop it and end that stack level with a 0 - regionNameStack.pop(); - auto typeOffset = regionNameStack.size(); + regionStackData.regionNameStack.pop(); + auto typeOffset = regionStackData.regionNameStack.size(); auto type = baseType + typeOffset; Extrae_eventandcounters(type, 0); return; @@ -73,6 +184,80 @@ void ExtraeTypeStackStrategy::region_stop( void ExtraeTypeStackStrategy::finalize() noexcept { + + unsigned int numberOfRegionsRegistered = regionMapData.regionNameToValue.size(); + std::vector values{}; + std::vector routine_names{}; + + for (const auto &item : regionMapData.regionNameToValue) { + const auto &routine_name = item.first; + const auto &value = item.second; + routine_names.push_back(routine_name); + values.push_back(value); + } + // leaking memory a bit, but c vs c++ strings are not worth making it not + // leaky? + char **routine_c_names = new char *[numberOfRegionsRegistered]; + for (auto i = 0; i < numberOfRegionsRegistered; i++) { + routine_c_names[i] = new char[routine_names[i].size() + 1]; + std::strcpy(routine_c_names[i], routine_names[i].c_str()); + } + + + for (auto level = 0; level <= regionStackData.historicMaxStackSize; level++) { + extrae_type_t type = baseType+level; + std::string description = "Level: " + std::to_string(level); + Extrae_define_event_type(&type, const_cast(description.c_str()), + &numberOfRegionsRegistered, values.data(), routine_c_names); + } + + + if(EXTRAE_WRITE_CONFIG_FILE){ + std::string fileName = EXTRAE_CONFIG_FILE_PATH + "SIT_Annotations.cfg"; + std::ofstream outputFile; + outputFile.open(fileName); + + // write header + outputFile << paraverConfigHead; + + // compute overview + auto numberOfEventTypes = regionStackData.historicMaxStackSize + 1 ; // 0 based so plus 1 + auto allTypes = std::vector(numberOfEventTypes); + std::iota(std::begin(allTypes), std::end(allTypes), baseType); // Fill with baseType baseType+1 .. + + // Build a small list of all types + std::string allTypesString =""; + for(const auto& type : allTypes){ + allTypesString+=std::to_string(type) + " "; + } + + auto overviewReplacedNumLevels = std::regex_replace(paraverConfigOverviewWindow, std::regex("SIT_REPLACE_NUM_LEVELS"), std::to_string(numberOfEventTypes)); + auto overviewReplacedAllTypes= std::regex_replace(overviewReplacedNumLevels, std::regex("SIT_REPLACE_LEVELS"), allTypesString); + + + // write Overview + outputFile << overviewReplacedAllTypes; + outputFile << std::endl; + + // write seperate windows + for (auto level = 0; level <= regionStackData.historicMaxStackSize; level++) { + auto type = baseType+level; + std::string windowName = "SIT Instrumentation Level: " + std::to_string(level); + + auto replacedEventType = std::regex_replace(paraverConfigLevelWindow, std::regex("SIT_REPLACE_EVENT_TYPE"), std::to_string(baseType+level)); + auto replacedWindowName = std::regex_replace(replacedEventType, std::regex("SIT_REPLACE_WINDOW_NAME"), windowName); + + // write Overview + outputFile << replacedWindowName; + outputFile << std::endl; + + + + } + + } + + if (didInitialize) { // Assumption is that if we needed to initialize the lib we also need to // finalize it diff --git a/src/backends/extrae/extrae_type_stack.hpp b/src/backends/extrae/extrae_type_stack.hpp index 8ec7e6a..a54fb24 100644 --- a/src/backends/extrae/extrae_type_stack.hpp +++ b/src/backends/extrae/extrae_type_stack.hpp @@ -6,22 +6,33 @@ typedef unsigned long extrae_value; typedef unsigned long extrae_type; -class ExtraeTypeStackStrategy : public SITProfilingStrategy { - -private: - bool didInitialize; - static constexpr unsigned long baseType = 81000; +struct RegionStackData{ std::stack regionNameStack; + std::size_t historicMaxStackSize; +}; + +struct RegionMapData{ std::unordered_map regionNameToValue; - std::size_t numberOfDistinctRegions; - std::size_t maxStackSize; +}; - extrae_value get_value_by_region_name(const std::string &name); - // std::mutex regionStackMutex; +class ExtraeTypeStackStrategy : public SITProfilingStrategy { + +private: + inline static bool EXTRAE_WRITE_CONFIG_FILE=false; + inline static std::string EXTRAE_CONFIG_FILE_PATH ="./"; + static const std::string paraverConfigHead; + static const std::string paraverConfigOverviewWindow; + static const std::string paraverConfigLevelWindow; + inline static thread_local bool didInitialize; + inline static constexpr unsigned long baseType = 81000; + inline static thread_local RegionStackData regionStackData; + inline static thread_local RegionMapData regionMapData; + static extrae_value get_value_by_region_name(const std::string &name); public: inline static const std::string_view name = "Extrae::TypeStack"; + void region_start(const SITRegionInformation ®ion) noexcept override; void region_stop(const SITRegionInformation ®ion) noexcept override; virtual void init() noexcept; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5641c32..28b6b09 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,6 +19,19 @@ add_test(NAME TestCppDLB endif() +if(ENABLE_EXTRAE) +add_executable(TestCppExtraeTypeStack cpp/TestCppExtraeTypeStack.cpp) +target_link_libraries(TestCppExtraeTypeStack sit::sit) +target_include_directories(TestCppExtraeTypeStack + PRIVATE ${SIT_PUBLIC_HEADERS}) + +add_test(NAME TestCppExtraeTypeStack + COMMAND TestCppExtraeTypeStack) + + +endif() + + if(BUILD_C_FORTRAN) add_executable(TestCLink c/TestCLink.c) target_link_libraries(TestCLink sit) diff --git a/tests/cpp/TestCppExtraeTypeStack.cpp b/tests/cpp/TestCppExtraeTypeStack.cpp new file mode 100644 index 0000000..3f72c1c --- /dev/null +++ b/tests/cpp/TestCppExtraeTypeStack.cpp @@ -0,0 +1,17 @@ +#include "sit/sit.hpp" + +int main() { + sit::init("Extrae::TypeStack"); + + sit::region_start("init"); + sit::region_start("init_ts"); + sit::region_stop("init_ts"); + + sit::region_start("init_io"); + sit::region_stop("init_io"); + sit::region_stop("init"); + + sit::region_start("init_ts"); + sit::region_stop("init_ts"); + sit::finalize(); +} \ No newline at end of file -- GitLab From 87000c710fbea468197ce56c8f68e5b357790547 Mon Sep 17 00:00:00 2001 From: Valentin Seitz Date: Thu, 30 May 2024 14:59:37 +0200 Subject: [PATCH 4/5] remove legacy wrapper --- src/backends/extrae/legacy/wrapper.cpp | 126 --------------------- src/backends/extrae/legacy/wrapper.hpp | 141 ------------------------ src/backends/extrae/legacy/wrapper.yaml | 8 -- 3 files changed, 275 deletions(-) delete mode 100644 src/backends/extrae/legacy/wrapper.cpp delete mode 100644 src/backends/extrae/legacy/wrapper.hpp delete mode 100644 src/backends/extrae/legacy/wrapper.yaml diff --git a/src/backends/extrae/legacy/wrapper.cpp b/src/backends/extrae/legacy/wrapper.cpp deleted file mode 100644 index c0a3d7d..0000000 --- a/src/backends/extrae/legacy/wrapper.cpp +++ /dev/null @@ -1,126 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -extern "C" { -#include "extrae.h" -} - -class MultiLevelExtraeWrapper { -public: - static constexpr int MAX_LEVELS = 10; - -private: - static constexpr extrae_type_t BASE_EVENT = 81000; - - static std::array, MAX_LEVELS> level_stacks; - - static std::unordered_map - routine_to_value_mapper; - - static extrae_value_t _num_routines; // where the 0 is actually the no routine - - static void _maybe_add_routine_name(const std::string &routine_name) { - if (routine_to_value_mapper.count(routine_name) > 0) { - return; - } else { -#pragma omp criticial - { - _num_routines++; - routine_to_value_mapper[routine_name] = _num_routines; - } - } - } - -public: - static extrae_value_t get_routine_value(const std::string &routine_name) { - _maybe_add_routine_name(routine_name); - return routine_to_value_mapper[routine_name]; - } - static extrae_type_t get_level_event(const int level) { - return BASE_EVENT + level; - } - - static const decltype(routine_to_value_mapper) & - get_routine_to_value_mapper() { - return routine_to_value_mapper; - } - - - - static inline void push_level_stack(const extrae_value_t value, int level) { - level_stacks[level].push(value); - } -}; - -std::unordered_map - MultiLevelExtraeWrapper::routine_to_value_mapper = - std::unordered_map(); -std::array, MultiLevelExtraeWrapper::MAX_LEVELS> - MultiLevelExtraeWrapper::level_stacks = - std::array, - MultiLevelExtraeWrapper::MAX_LEVELS>(); -extrae_value_t MultiLevelExtraeWrapper::_num_routines = 0; - -void event_start(const std::string &routineName, int level, bool withCounters) { - - extrae_type_t event = MultiLevelExtraeWrapper::get_level_event(level); - extrae_value_t value = - MultiLevelExtraeWrapper::get_routine_value(std::string(routineName)); - MultiLevelExtraeWrapper::push_level_stack(value, level); - if (withCounters) { - Extrae_eventandcounters(event, value); - } else { - Extrae_event(event, value); - } -} -void event_stop(const std::string &routineName, int level, bool withCounters) { - extrae_type_t event = MultiLevelExtraeWrapper::get_level_event(level); - extrae_value_t value = MultiLevelExtraeWrapper::pop_level_stack(level); - if (withEvents) { - Extrae_eventandcounters(event, value); - } else { - Extrae_event(event, value); - } -} - -void extrae_stop() { Extrae_shutdown(); } - -void extrae_start() { Extrae_restart(); } -void finalize() { - // now register all the routines with the different levels - unsigned n_values = - MultiLevelExtraeWrapper::get_routine_to_value_mapper().size(); - - std::vector values{}; - std::vector routine_names{}; - - for (const auto &item : - MultiLevelExtraeWrapper::get_routine_to_value_mapper()) { - const auto &routine_name = item.first; - const auto &value = item.second; - routine_names.push_back(routine_name); - values.push_back(value); - } - // leaking memory a bit, but c vs c++ strings are not worth making it not - // leaky? - char **routine_c_names = new char *[n_values]; - for (size_t i = 0; i < n_values; i++) { - routine_c_names[i] = new char[routine_names[i].size() + 1]; - std::strcpy(routine_c_names[i], routine_names[i].c_str()); - } - - for (auto level = 0; level < MultiLevelExtraeWrapper::MAX_LEVELS; level++) { - auto event = MultiLevelExtraeWrapper::get_level_event(level); - std::string description = "Level: " + std::to_string(level); - Extrae_define_event_type(&event, const_cast(description.c_str()), - &n_values, values.data(), routine_c_names); - } - - printf("\nRegistered %i regions with extrae\n", n_values); -} diff --git a/src/backends/extrae/legacy/wrapper.hpp b/src/backends/extrae/legacy/wrapper.hpp deleted file mode 100644 index 42bb72d..0000000 --- a/src/backends/extrae/legacy/wrapper.hpp +++ /dev/null @@ -1,141 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -extern "C" { -#include "extrae.h" -} - -Extrae_Wrapper_Init_ValueStack("Name Of Type") - Extrae_Wrapper_Init_TypeStack("Prefix Type") - - Extrae_Wrapper_Init("aklsdjföa", Type = ValueStack) - - SIMPLE INSTRUMENTATION TOOL - - class MultiLevelExtraeWrapper { - public : static constexpr int MAX_LEVELS = 10; - - private : static constexpr extrae_type_t BASE_EVENT = 81000; - - static std::array, MAX_LEVELS> level_stacks; - - static std::unordered_map - routine_to_value_mapper; - - static extrae_value_t - _num_routines; // where the 0 is actually the no routine - - static void _maybe_add_routine_name(const std::string &routine_name){ - if (routine_to_value_mapper.count(routine_name) > 0){return;} -else { -#pragma omp criticial - { - _num_routines++; - routine_to_value_mapper[routine_name] = _num_routines; - } -} -} - -public: -static extrae_value_t get_routine_value(const std::string &routine_name) { - _maybe_add_routine_name(routine_name); - return routine_to_value_mapper[routine_name]; -} -static extrae_type_t get_level_event(const int level) { - return BASE_EVENT + level; -} - -static const decltype(routine_to_value_mapper) &get_routine_to_value_mapper() { - return routine_to_value_mapper; -} - -static inline extrae_value_t pop_level_stack(const int level) { - if (level_stacks[level].empty()) { - return 0; - } - level_stacks[level].pop(); - if (level_stacks[level].empty()) { - return 0; - } - const auto value = level_stacks[level].top(); - return value; -} - -static inline void push_level_stack(const extrae_value_t value, int level) { - level_stacks[level].push(value); -} -} -; - -std::unordered_map - MultiLevelExtraeWrapper::routine_to_value_mapper = - std::unordered_map(); -std::array, MultiLevelExtraeWrapper::MAX_LEVELS> - MultiLevelExtraeWrapper::level_stacks = - std::array, - MultiLevelExtraeWrapper::MAX_LEVELS>(); -extrae_value_t MultiLevelExtraeWrapper::_num_routines = 0; - -void event_start(const std::string &routineName, int level, bool withCounters) { - - extrae_type_t event = MultiLevelExtraeWrapper::get_level_event(level); - extrae_value_t value = - MultiLevelExtraeWrapper::get_routine_value(std::string(routineName)); - MultiLevelExtraeWrapper::push_level_stack(value, level); - if (withCounters) { - Extrae_eventandcounters(event, value); - } else { - Extrae_event(event, value); - } -} -void event_stop(const std::string &routineName, int level, bool withCounters) { - extrae_type_t event = MultiLevelExtraeWrapper::get_level_event(level); - extrae_value_t value = MultiLevelExtraeWrapper::pop_level_stack(level); - if (withEvents) { - Extrae_eventandcounters(event, value); - } else { - Extrae_event(event, value); - } -} - -void extrae_stop() { Extrae_shutdown(); }s - -void extrae_start() { Extrae_restart(); } -void finalize() { - // now register all the routines with the different levels - unsigned n_values = - MultiLevelExtraeWrapper::get_routine_to_value_mapper().size(); - - std::vector values{}; - std::vector routine_names{}; - - for (const auto &item : - MultiLevelExtraeWrapper::get_routine_to_value_mapper()) { - const auto &routine_name = item.first; - const auto &value = item.second; - routine_names.push_back(routine_name); - values.push_back(value); - } - // leaking memory a bit, but c vs c++ strings are not worth making it not - // leaky? - char **routine_c_names = new char *[n_values]; - for (size_t i = 0; i < n_values; i++) { - routine_c_names[i] = new char[routine_names[i].size() + 1]; - std::strcpy(routine_c_names[i], routine_names[i].c_str()); - } - - for (auto level = 0; level < MultiLevelExtraeWrapper::MAX_LEVELS; level++) { - auto event = MultiLevelExtraeWrapper::get_level_event(level); - std::string description = "Level: " + std::to_string(level); - Extrae_define_event_type(&event, const_cast(description.c_str()), - &n_values, values.data(), routine_c_names); - } - - printf("\nRegistered %i regions with extrae\n", n_values); -} diff --git a/src/backends/extrae/legacy/wrapper.yaml b/src/backends/extrae/legacy/wrapper.yaml deleted file mode 100644 index 9123b2c..0000000 --- a/src/backends/extrae/legacy/wrapper.yaml +++ /dev/null @@ -1,8 +0,0 @@ -library: sit -cxx_header: api.hpp - -declarations: -- decl: void sit_init(std::string_view backend) -- decl: void sit_region_start(std::string_view name) -- decl: void sit_region_stop(std::string_view name) -- decl: void sit_finalize() \ No newline at end of file -- GitLab From 9626304515cb24dc5317156a770bf5adb2a7ecbe Mon Sep 17 00:00:00 2001 From: Valentin Seitz Date: Thu, 30 May 2024 15:41:43 +0200 Subject: [PATCH 5/5] update dlb --- src/backends/dlb/dlb.cpp | 8 ++++---- src/backends/dlb/dlb.hpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/backends/dlb/dlb.cpp b/src/backends/dlb/dlb.cpp index 4b34700..c474dad 100644 --- a/src/backends/dlb/dlb.cpp +++ b/src/backends/dlb/dlb.cpp @@ -4,7 +4,7 @@ #include void DLBTalpStrategy::region_start( - const SITRegionInformation ®ion) const noexcept { + const SITRegionInformation ®ion) noexcept { // we have to create a small std::string thing because we cannot assume that // the string_view passed is properly null terminated auto handle = DLB_MonitoringRegionRegister(std::string(region.name).c_str()); @@ -12,17 +12,17 @@ void DLBTalpStrategy::region_start( } void DLBTalpStrategy::region_stop( - const SITRegionInformation ®ion) const noexcept { + const SITRegionInformation ®ion) noexcept { auto handle = DLB_MonitoringRegionRegister(std::string(region.name).c_str()); DLB_MonitoringRegionStop(handle); } -void DLBTalpStrategy::init() const noexcept { +void DLBTalpStrategy::init() noexcept { DLB_Init(0, NULL, "--talp"); std::cout << "SIT::" << name << " init()" << std::endl; } -void DLBTalpStrategy::finalize() const noexcept { +void DLBTalpStrategy::finalize() noexcept { DLB_Finalize(); std::cout << "SIT::" << name << " finalize()" << std::endl; } diff --git a/src/backends/dlb/dlb.hpp b/src/backends/dlb/dlb.hpp index 1e48b41..99eef78 100644 --- a/src/backends/dlb/dlb.hpp +++ b/src/backends/dlb/dlb.hpp @@ -4,8 +4,8 @@ class DLBTalpStrategy : public SITProfilingStrategy { public: inline static const std::string_view name = "DLB"; - void region_start(const SITRegionInformation ®ion) const noexcept override; - void region_stop(const SITRegionInformation ®ion) const noexcept override; - virtual void init() const noexcept; - virtual void finalize() const noexcept; + void region_start(const SITRegionInformation ®ion) noexcept override; + void region_stop(const SITRegionInformation ®ion) noexcept override; + virtual void init() noexcept; + virtual void finalize() noexcept; }; -- GitLab