Skip to content

Commit af7a027

Browse files
authored
Add GetNumProc util helper (#533)
- add `GetNumProc` helper to util library - test `GetNumProc` - document new helper in env variables guide
1 parent c679373 commit af7a027

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

docs/user_guide/environment_variables.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The following environment variables can be used to configure the project's runti
55

66
- ``PPC_NUM_PROC``: Specifies the number of processes to launch.
77
Default: ``1``
8+
Can be queried from C++ with ``ppc::util::GetNumProc()``.
89

910
- ``PPC_NUM_THREADS``: Specifies the number of threads to use.
1011
Default: ``1``

modules/util/include/util.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ enum GTestParamIndex : uint8_t { kTaskGetter, kNameTest, kTestParams };
5252

5353
std::string GetAbsoluteTaskPath(const std::string& id_path, const std::string& relative_path);
5454
int GetNumThreads();
55+
int GetNumProc();
5556
double GetTaskMaxTime();
5657
double GetPerfMaxTime();
5758

modules/util/src/util.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ int ppc::util::GetNumThreads() {
2828
return 1;
2929
}
3030

31+
int ppc::util::GetNumProc() {
32+
const auto num_proc = env::get<int>("PPC_NUM_PROC");
33+
if (num_proc.has_value()) {
34+
return num_proc.value();
35+
}
36+
return 1;
37+
}
38+
3139
double ppc::util::GetTaskMaxTime() {
3240
const auto val = env::get<double>("PPC_TASK_MAX_TIME");
3341
if (val.has_value()) {

modules/util/tests/util.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,19 @@ TEST(GetPerfMaxTime, ReadsFromEnvironment) {
108108
env::detail::set_scoped_environment_variable scoped("PPC_PERF_MAX_TIME", "12.5");
109109
EXPECT_DOUBLE_EQ(ppc::util::GetPerfMaxTime(), 12.5);
110110
}
111+
112+
TEST(GetNumProc, ReturnsDefaultWhenUnset) {
113+
const auto old = env::get<int>("PPC_NUM_PROC");
114+
if (old.has_value()) {
115+
env::detail::delete_environment_variable("PPC_NUM_PROC");
116+
}
117+
EXPECT_EQ(ppc::util::GetNumProc(), 1);
118+
if (old.has_value()) {
119+
env::detail::set_environment_variable("PPC_NUM_PROC", std::to_string(*old));
120+
}
121+
}
122+
123+
TEST(GetNumProc, ReadsFromEnvironment) {
124+
env::detail::set_scoped_environment_variable scoped("PPC_NUM_PROC", "4");
125+
EXPECT_EQ(ppc::util::GetNumProc(), 4);
126+
}

0 commit comments

Comments
 (0)