]> https://gitweb.dealii.org/ - dealii.git/commitdiff
TBB oneAPI: make global_control object static
authorMatthias Maier <tamiko@43-1.org>
Sun, 22 May 2022 20:18:52 +0000 (15:18 -0500)
committerMatthias Maier <tamiko@43-1.org>
Sun, 22 May 2022 20:19:37 +0000 (15:19 -0500)
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

index 946d8c91c61e86a119cd16e09857857db6c63fed..7655e3cc0a72324704034694a95f80252ea23260 100644 (file)
@@ -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;
+  tbb_global_control = std::make_unique<tbb::global_control>(
+    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);

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.