diff --git a/docs/user_guide/environment_variables.rst b/docs/user_guide/environment_variables.rst index fe147e016..6a896dcb8 100644 --- a/docs/user_guide/environment_variables.rst +++ b/docs/user_guide/environment_variables.rst @@ -5,6 +5,7 @@ The following environment variables can be used to configure the project's runti - ``PPC_NUM_PROC``: Specifies the number of processes to launch. Default: ``1`` + Can be queried from C++ with ``ppc::util::GetNumProc()``. - ``PPC_NUM_THREADS``: Specifies the number of threads to use. Default: ``1`` diff --git a/modules/util/include/util.hpp b/modules/util/include/util.hpp index d7ab449ba..648b40650 100644 --- a/modules/util/include/util.hpp +++ b/modules/util/include/util.hpp @@ -52,6 +52,7 @@ enum GTestParamIndex : uint8_t { kTaskGetter, kNameTest, kTestParams }; std::string GetAbsoluteTaskPath(const std::string& id_path, const std::string& relative_path); int GetNumThreads(); +int GetNumProc(); double GetTaskMaxTime(); double GetPerfMaxTime(); diff --git a/modules/util/src/util.cpp b/modules/util/src/util.cpp index 7fe8d6506..97a777281 100644 --- a/modules/util/src/util.cpp +++ b/modules/util/src/util.cpp @@ -28,6 +28,14 @@ int ppc::util::GetNumThreads() { return 1; } +int ppc::util::GetNumProc() { + const auto num_proc = env::get("PPC_NUM_PROC"); + if (num_proc.has_value()) { + return num_proc.value(); + } + return 1; +} + double ppc::util::GetTaskMaxTime() { const auto val = env::get("PPC_TASK_MAX_TIME"); if (val.has_value()) { diff --git a/modules/util/tests/util.cpp b/modules/util/tests/util.cpp index 748aa5988..d2bd6c96d 100644 --- a/modules/util/tests/util.cpp +++ b/modules/util/tests/util.cpp @@ -108,3 +108,19 @@ TEST(GetPerfMaxTime, ReadsFromEnvironment) { env::detail::set_scoped_environment_variable scoped("PPC_PERF_MAX_TIME", "12.5"); EXPECT_DOUBLE_EQ(ppc::util::GetPerfMaxTime(), 12.5); } + +TEST(GetNumProc, ReturnsDefaultWhenUnset) { + const auto old = env::get("PPC_NUM_PROC"); + if (old.has_value()) { + env::detail::delete_environment_variable("PPC_NUM_PROC"); + } + EXPECT_EQ(ppc::util::GetNumProc(), 1); + if (old.has_value()) { + env::detail::set_environment_variable("PPC_NUM_PROC", std::to_string(*old)); + } +} + +TEST(GetNumProc, ReadsFromEnvironment) { + env::detail::set_scoped_environment_variable scoped("PPC_NUM_PROC", "4"); + EXPECT_EQ(ppc::util::GetNumProc(), 4); +}