From c7f714ea564046650ea16709b4ed53cfd7499144 Mon Sep 17 00:00:00 2001 From: Matthias Maier Date: Sun, 22 May 2022 15:18:52 -0500 Subject: [PATCH] TBB oneAPI: make global_control object static tbb::global_control is a class that affects the specified behavior of tbb during its lifetime. Thus, in order to set a global thread limit for tbb we have to maintain the object throughout the execution of the program. We do this by maintaining a static std::unique_ptr. A std::unique_ptr is a good choice here because tbb::global_control does not provide a mechanism to override its setting - we can only delete the old and replace it with a new one. --- source/base/multithread_info.cc | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/source/base/multithread_info.cc b/source/base/multithread_info.cc index 946d8c91c6..7655e3cc0a 100644 --- a/source/base/multithread_info.cc +++ b/source/base/multithread_info.cc @@ -98,8 +98,18 @@ MultithreadInfo::set_thread_limit(const unsigned int max_threads) #ifdef DEAL_II_WITH_TBB # ifdef DEAL_II_TBB_WITH_ONEAPI - tbb::global_control(tbb::global_control::max_allowed_parallelism, - n_max_threads); + // tbb::global_control is a class that affects the specified behavior of + // tbb during its lifetime. Thus, in order to set a global thread limit + // for tbb we have to maintain the object throughout the execution of the + // program. We do this by maintaining a static std::unique_ptr. + // + // A std::unique_ptr is a good choice here because tbb::global_control + // does not provide a mechanism to override its setting - we can only + // delete the old and replace it with a new one. + static std::unique_ptr tbb_global_control; + tbb_global_control = std::make_unique( + tbb::global_control::max_allowed_parallelism, n_max_threads); + # else // Initialize the scheduler and destroy the old one before doing so static tbb::task_scheduler_init dummy(tbb::task_scheduler_init::deferred); -- 2.39.5