Skip to content

Commit 1121ca6

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

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

tasks/all/runner.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#define _CRT_SECURE_NO_WARNINGS
12
#include <gtest/gtest.h>
3+
#include <tbb/global_control.h>
24

35
#include <boost/mpi/communicator.hpp>
46
#include <boost/mpi/environment.hpp>
@@ -56,6 +58,12 @@ int main(int argc, char** argv) {
5658
boost::mpi::environment env(argc, argv);
5759
boost::mpi::communicator world;
5860

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

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

tasks/tbb/runner.cpp

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

3-
int main(int argc, char **argv) {
412
::testing::InitGoogleTest(&argc, argv);
513
return RUN_ALL_TESTS();
614
}

0 commit comments

Comments
 (0)