Skip to content

Commit ba8011a

Browse files
committed
Add libenvpp integration and improve thread/environment handling
Integrated libenvpp library to enhance environment variable parsing and validation. Refactored thread control logic with libenvpp and adjusted related tests and CMake configurations to support the new library. Removed outdated util test files and added necessary dependencies in build scripts.
1 parent 5d9109d commit ba8011a

12 files changed

Lines changed: 140 additions & 58 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
[submodule "3rdparty/json"]
1111
path = 3rdparty/json
1212
url = https://github.com/nlohmann/json
13+
[submodule "3rdparty/libenvpp"]
14+
path = 3rdparty/libenvpp
15+
url = https://github.com/ph3at/libenvpp

3rdparty/libenvpp

Submodule libenvpp added at 86db27f

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ include(cmake/configure.cmake)
3030
include(cmake/modes.cmake)
3131
include(cmake/sanitizers.cmake)
3232
include(cmake/json.cmake)
33+
include(cmake/libenvpp.cmake)
3334

3435
################# Parallel programming technologies #################
3536

cmake/libenvpp.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
include_directories(${CMAKE_SOURCE_DIR}/3rdparty/libenvpp/include)
2+
include_directories(${CMAKE_SOURCE_DIR}/3rdparty/libenvpp/external/fmt/include)
3+
4+
include(ExternalProject)
5+
ExternalProject_Add(ppc_libenvpp
6+
SOURCE_DIR "${CMAKE_SOURCE_DIR}/3rdparty/libenvpp"
7+
PREFIX "${CMAKE_CURRENT_BINARY_DIR}/ppc_libenvpp"
8+
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/ppc_libenvpp/build"
9+
INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/ppc_libenvpp/install"
10+
CONFIGURE_COMMAND "${CMAKE_COMMAND}" -S "${CMAKE_SOURCE_DIR}/3rdparty/libenvpp/" -B "${CMAKE_CURRENT_BINARY_DIR}/ppc_libenvpp/build/"
11+
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -G${CMAKE_GENERATOR} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
12+
-D CMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER} -D CMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER}
13+
BUILD_COMMAND "${CMAKE_COMMAND}" --build "${CMAKE_CURRENT_BINARY_DIR}/ppc_libenvpp/build" --config ${CMAKE_BUILD_TYPE} --parallel
14+
INSTALL_COMMAND "${CMAKE_COMMAND}" --install "${CMAKE_CURRENT_BINARY_DIR}/ppc_libenvpp/build" --prefix "${CMAKE_CURRENT_BINARY_DIR}/ppc_libenvpp/install")

modules/core/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ project(${exec_func_lib})
2323
add_library(${exec_func_lib} STATIC ${LIB_SOURCE_FILES})
2424
set_target_properties(${exec_func_lib} PROPERTIES LINKER_LANGUAGE CXX)
2525

26+
add_dependencies(${exec_func_lib} ppc_libenvpp)
27+
target_link_directories(${exec_func_lib} PUBLIC "${CMAKE_BINARY_DIR}/ppc_libenvpp/install/lib")
28+
target_link_directories(${exec_func_lib} PUBLIC "${CMAKE_BINARY_DIR}/ppc_libenvpp/build")
29+
target_link_libraries(${exec_func_lib} PUBLIC fmt envpp)
30+
2631
add_executable(${exec_func_tests} ${FUNC_TESTS_SOURCE_FILES})
32+
2733
add_dependencies(${exec_func_tests} ppc_googletest)
2834
target_link_directories(${exec_func_tests} PUBLIC ${CMAKE_BINARY_DIR}/ppc_googletest/install/lib)
2935
target_link_libraries(${exec_func_tests} PUBLIC gtest gtest_main)
36+
3037
if( MPI_COMPILE_FLAGS )
3138
set_target_properties(${exec_func_tests} PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
3239
endif( MPI_COMPILE_FLAGS )

modules/core/util/func_tests/util_tests.cpp

Lines changed: 0 additions & 36 deletions
This file was deleted.

modules/core/util/include/util.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,9 @@ static_assert(false, "Unsupported compiler");
6161
return kFunc.substr(start, end - start);
6262
}
6363

64+
#include <stdbool.h>
65+
#include <stdlib.h>
66+
67+
bool IsUnderMpirun();
68+
6469
} // namespace ppc::util

modules/core/util/src/util.cpp

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,77 @@
11
#include "core/util/include/util.hpp"
22

33
#include <cstdlib>
4-
#ifdef _WIN32
54
#include <cstdint>
65
#include <iostream>
76
#include <memory>
87
#include <vector>
9-
#endif
10-
8+
#include <algorithm>
9+
#include <array>
1110
#include <filesystem>
11+
#include <ranges>
1212
#include <string>
13+
#include <iostream>
14+
#include <libenvpp/env.hpp>
15+
16+
//namespace {
17+
//
18+
//struct MpiEnvVar {
19+
// std::string_view name;
20+
// const char* description;
21+
//};
22+
//
23+
//} // namespace
1324

14-
std::string ppc::util::GetAbsolutePath(const std::string &relative_path) {
25+
std::string ppc::util::GetAbsolutePath(const std::string& relative_path) {
1526
const std::filesystem::path path = std::string(PPC_PATH_TO_PROJECT) + "/tasks/" + relative_path;
1627
return path.string();
1728
}
1829

1930
int ppc::util::GetNumThreads() {
20-
#ifdef _WIN32
21-
size_t len;
22-
char omp_env[100];
23-
errno_t err = getenv_s(&len, omp_env, sizeof(omp_env), "PPC_NUM_THREADS");
24-
if (err != 0 || len == 0) {
25-
omp_env[0] = '\0';
31+
env::prefix pre("PPC");
32+
33+
const auto num_threads_id = pre.register_variable<unsigned>("NUM_THREADS");
34+
35+
auto validated = std::move(pre).parse_and_validate();
36+
37+
if (!validated.ok()) {
38+
std::cerr << validated.warning_message() << "\n";
39+
// throw validated.error_message();
2640
}
27-
int num_threads = std::atoi(omp_env);
28-
#else
29-
const char *omp_env = std::getenv("PPC_NUM_THREADS"); // NOLINT(concurrency-mt-unsafe)
30-
int num_threads = (omp_env != nullptr) ? std::atoi(omp_env) : 1;
31-
#endif
32-
return num_threads;
41+
42+
const auto num_threads = validated.get(num_threads_id);
43+
return num_threads ? static_cast<int>(*num_threads) : 1;
3344
}
45+
46+
//const std::array<MpiEnvVar, 13> kMpiEnvVars = {{{.name = "OMPI_COMM_WORLD_SIZE", .description = "OpenMPI"},
47+
// {.name = "OMPI_UNIVERSE_SIZE", .description = "OpenMPI"},
48+
// {.name = "PMI_SIZE", .description = "MPICH/Intel MPI"},
49+
// {.name = "PMI_RANK", .description = "MPICH/Intel MPI"},
50+
// {.name = "PMI_FD", .description = "MPICH/Intel MPI"},
51+
// {.name = "HYDRA_CONTROL_FD", .description = "Hydra"},
52+
// {.name = "PMIX_RANK", .description = "Cray PMI/PMIx"},
53+
// {.name = "PMIX_NAMESPACE", .description = "Cray PMI/PMIx"},
54+
// {.name = "SLURM_PROCID", .description = "SLURM"},
55+
// {.name = "MPI_LOCALRANKID", .description = "IBM Spectrum MPI"},
56+
// {.name = "MSMPI_RANK", .description = "Microsoft MPI"},
57+
// {.name = "MSMPI_NODE_ID", .description = "Microsoft MPI"},
58+
// {.name = "MSMPI_LOCALRANK", .description = "Microsoft MPI"}}};
59+
60+
//bool ppc::util::IsUnderMpirun() {
61+
// auto pre = env::prefix("");
62+
// const auto parsed_and_validated_pre = pre.parse_and_validate();
63+
// return std::ranges::any_of(kMpiEnvVars, [&](const auto& env_var) {
64+
// const auto mpi_env_id = pre.register_variable<std::string>(env_var.name);
65+
//
66+
// if (!parsed_and_validated_pre.ok()) {
67+
// std::cerr << parsed_and_validated_pre.warning_message();
68+
// throw parsed_and_validated_pre.error_message();
69+
// }
70+
//
71+
// if (parsed_and_validated_pre.get(mpi_env_id)) {
72+
// return true;
73+
// } else {
74+
// return false;
75+
// }
76+
// });
77+
//}

scripts/run_tests.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ def run_threads(self):
8787
self.__run_exec(f"{self.work_dir / 'ppc_func_tests'} {self.__get_gtest_settings(3, '_' + task_type + '_')}")
8888

8989
def run_core(self):
90+
self.__run_exec(f"{self.work_dir / 'ppc_func_tests'} {self.__get_gtest_settings(1, '_common')}")
91+
9092
if platform.system() == "Linux" and not self.__ppc_env.get("PPC_ASAN_RUN"):
9193
self.__run_exec(f"{self.valgrind_cmd} {self.work_dir / 'core_func_tests'} "
9294
f"{self.__get_gtest_settings(1, '*')} --gtest_filter=*:-*_death_test")
@@ -123,8 +125,8 @@ def run_performance_list(self):
123125
ppc_runner = PPCRunner()
124126
ppc_runner.setup_env(os.environ.copy())
125127

126-
# if args_dict["running_type"] in ["threads", "processes"]:
127-
# ppc_runner.run_core()
128+
if args_dict["running_type"] in ["threads", "processes"]:
129+
ppc_runner.run_core()
128130

129131
if args_dict["running_type"] == "threads":
130132
ppc_runner.run_threads()

tasks/CMakeLists.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ set(exec_perf_tests "ppc_perf_tests")
77
# Init func tests executable files
88
set(list_of_exec_tests "")
99
if (USE_FUNC_TESTS)
10-
add_executable(${exec_func_tests} "${CMAKE_CURRENT_SOURCE_DIR}/common/runners/functional.cpp")
10+
add_executable(${exec_func_tests} "${CMAKE_CURRENT_SOURCE_DIR}/common/runners/functional.cpp"
11+
"${CMAKE_CURRENT_SOURCE_DIR}/common/tests/util.cpp")
1112
list(APPEND list_of_exec_tests ${exec_func_tests})
1213
endif (USE_FUNC_TESTS)
1314

@@ -38,7 +39,10 @@ add_library(stb_image INTERFACE)
3839
3940
foreach (exec_func ${list_of_exec_tests})
4041
target_link_libraries(${exec_func} PUBLIC Threads::Threads)
41-
target_link_libraries(${exec_func} PUBLIC ${OpenMP_libomp_LIBRARY})
42+
43+
find_package(OpenMP REQUIRED)
44+
target_link_libraries(${exec_func} PUBLIC ${OpenMP_libomp_LIBRARY} OpenMP::OpenMP_CXX)
45+
4246
if( MPI_COMPILE_FLAGS )
4347
set_target_properties(${exec_func} PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
4448
endif( MPI_COMPILE_FLAGS )
@@ -51,7 +55,11 @@ foreach (exec_func ${list_of_exec_tests})
5155
add_dependencies(${exec_func} ppc_onetbb)
5256
target_link_directories(${exec_func} PUBLIC ${CMAKE_BINARY_DIR}/ppc_onetbb/install/lib)
5357
if(NOT MSVC)
54-
target_link_libraries(${exec_func} PUBLIC tbb)
58+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
59+
target_link_libraries(${exec_func} PUBLIC tbb_debug)
60+
else()
61+
target_link_libraries(${exec_func} PUBLIC tbb)
62+
endif()
5563
endif()
5664
5765
target_link_directories(stb_image INTERFACE ${CMAKE_SOURCE_DIR}/3rdparty/stb)
@@ -64,6 +72,11 @@ foreach (exec_func ${list_of_exec_tests})
6472
add_dependencies(${exec_func} ppc_json)
6573
target_link_directories(${exec_func} INTERFACE "${CMAKE_BINARY_DIR}/ppc_json/install/include")
6674
75+
add_dependencies(${exec_func} ppc_libenvpp)
76+
target_link_directories(${exec_func} PUBLIC "${CMAKE_BINARY_DIR}/ppc_libenvpp/install/lib")
77+
target_link_directories(${exec_func} PUBLIC "${CMAKE_BINARY_DIR}/ppc_libenvpp/build")
78+
target_link_libraries(${exec_func} PUBLIC fmt envpp)
79+
6780
enable_testing()
6881
add_test(NAME ${exec_func} COMMAND ${exec_func})
6982

0 commit comments

Comments
 (0)