Skip to content

Commit 62e31e8

Browse files
committed
Bind TBB max threads num to OMP_NUM_THREADS environment variable value
1 parent 4ffdd85 commit 62e31e8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

tasks/all/runner.cpp

Lines changed: 7 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,12 @@ int main(int argc, char** argv) {
5657
boost::mpi::environment env(argc, argv);
5758
boost::mpi::communicator world;
5859

60+
const char* omp_env = std::getenv("OMP_NUM_THREADS");
61+
int num_threads = omp_env ? std::atoi(omp_env) : 1;
62+
63+
// Limit the number of threads in TBB
64+
tbb::global_control control(tbb::global_control::max_allowed_parallelism, num_threads);
65+
5966
::testing::InitGoogleTest(&argc, argv);
6067

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

tasks/tbb/runner.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#include <gtest/gtest.h>
2+
#include <tbb/global_control.h>
3+
4+
int main(int argc, char** argv) {
5+
const char* omp_env = std::getenv("OMP_NUM_THREADS");
6+
int num_threads = omp_env ? std::atoi(omp_env) : 1;
7+
8+
// Limit the number of threads in TBB
9+
tbb::global_control control(tbb::global_control::max_allowed_parallelism, num_threads);
210

3-
int main(int argc, char **argv) {
411
::testing::InitGoogleTest(&argc, argv);
512
return RUN_ALL_TESTS();
613
}

0 commit comments

Comments
 (0)