From: Matthias Maier Date: Sun, 22 May 2022 20:18:52 +0000 (-0500) Subject: TBB oneAPI: make global_control object static X-Git-Tag: v9.4.0-rc1~178^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c7f714ea564046650ea16709b4ed53cfd7499144;p=dealii.git 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. --- 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);