Skip to content

Commit 0b2150b

Browse files
authored
Bind TBB max threads num to OMP_NUM_THREADS environment variable value (#225)
close #180
1 parent 3f65cca commit 0b2150b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

tasks/all/runner.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <gtest/gtest.h>
2+
#include <tbb/global_control.h>
23

34
#include <boost/mpi/communicator.hpp>
45
#include <boost/mpi/environment.hpp>
@@ -56,6 +57,21 @@ int main(int argc, char** argv) {
5657
boost::mpi::environment env(argc, argv);
5758
boost::mpi::communicator world;
5859

60+
#ifdef _WIN32
61+
size_t len;
62+
char omp_env[100];
63+
errno_t err = getenv_s(&len, omp_env, sizeof(omp_env), "OMP_NUM_THREADS");
64+
if (err != 0 || len == 0) {
65+
omp_env[0] = '\0';
66+
}
67+
#else
68+
const char* omp_env = std::getenv("OMP_NUM_THREADS");
69+
#endif
70+
int num_threads = (omp_env != nullptr) ? std::atoi(omp_env) : 1;
71+
72+
// Limit the number of threads in TBB
73+
tbb::global_control control(tbb::global_control::max_allowed_parallelism, num_threads);
74+
5975
::testing::InitGoogleTest(&argc, argv);
6076

6177
auto& listeners = ::testing::UnitTest::GetInstance()->listeners();

tasks/tbb/runner.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
#include <gtest/gtest.h>
2+
#include <tbb/global_control.h>
3+
4+
int main(int argc, char** argv) {
5+
#ifdef _WIN32
6+
size_t len;
7+
char omp_env[100];
8+
errno_t err = getenv_s(&len, omp_env, sizeof(omp_env), "OMP_NUM_THREADS");
9+
if (err != 0 || len == 0) {
10+
omp_env[0] = '\0';
11+
}
12+
#else
13+
const char* omp_env = std::getenv("OMP_NUM_THREADS");
14+
#endif
15+
int num_threads = (omp_env != nullptr) ? std::atoi(omp_env) : 1;
16+
17+
// Limit the number of threads in TBB
18+
tbb::global_control control(tbb::global_control::max_allowed_parallelism, num_threads);
219

3-
int main(int argc, char **argv) {
420
::testing::InitGoogleTest(&argc, argv);
521
return RUN_ALL_TESTS();
622
}

0 commit comments

Comments
 (0)