Reference documentation for deal.II version GIT c14369f203 2023-10-01 07:40:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
multithread_info.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2023 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
17 #include <deal.II/base/utilities.h>
18 
19 #include <algorithm>
20 #include <cstdlib> // for std::getenv
21 #include <mutex>
22 #include <thread>
23 
24 #ifdef DEAL_II_WITH_TBB
25 # ifdef DEAL_II_TBB_WITH_ONEAPI
26 # include <tbb/global_control.h>
27 # else
28 # include <tbb/task_scheduler_init.h>
29 # endif
30 #endif
31 
32 
33 #ifdef DEAL_II_WITH_TASKFLOW
34 # include <taskflow/taskflow.hpp>
35 #endif
36 
38 
39 
40 unsigned int
42 {
43  // There is a slight semantic change between our n_cores() call and the
44  // std::thread alternative: in case of an error the latter one returns 0
45  // in contrast to a 1 that n_cores() used to do. For compatibility, let's
46  // translate to our numbering scheme:
47  const unsigned int n_cores = std::thread::hardware_concurrency();
48  return n_cores == 0 ? 1 : n_cores;
49 }
50 
51 
52 void
53 MultithreadInfo::set_thread_limit(const unsigned int max_threads)
54 {
55  // set the maximal number of threads to the given value as specified
56  n_max_threads = max_threads;
57 
58  // then also see if something was given in the environment
59  {
60  if (const char *penv = std::getenv("DEAL_II_NUM_THREADS"))
61  {
62  unsigned int max_threads_env = numbers::invalid_unsigned_int;
63  try
64  {
65  max_threads_env = Utilities::string_to_int(std::string(penv));
66  }
67  catch (...)
68  {
70  false,
71  ExcMessage(
72  std::string(
73  "When specifying the <DEAL_II_NUM_THREADS> environment "
74  "variable, it needs to be something that can be interpreted "
75  "as an integer. The text you have in the environment "
76  "variable is <") +
77  penv + ">"));
78  }
79 
80  AssertThrow(max_threads_env > 0,
81  ExcMessage(
82  "When specifying the <DEAL_II_NUM_THREADS> environment "
83  "variable, it needs to be a positive number."));
84 
86  n_max_threads = std::min(n_max_threads, max_threads_env);
87  else
88  n_max_threads = max_threads_env;
89  }
90  }
91 
92  // If we have not set the number of allowed threads yet, just default to
93  // the number of available cores
96 
97 #ifdef DEAL_II_WITH_TBB
98 # ifdef DEAL_II_TBB_WITH_ONEAPI
99  // tbb::global_control is a class that affects the specified behavior of
100  // tbb during its lifetime. Thus, in order to set a global thread limit
101  // for tbb we have to maintain the object throughout the execution of the
102  // program. We do this by maintaining a static std::unique_ptr.
103  //
104  // A std::unique_ptr is a good choice here because tbb::global_control
105  // does not provide a mechanism to override its setting - we can only
106  // delete the old and replace it with a new one.
107  static std::unique_ptr<tbb::global_control> tbb_global_control;
108  tbb_global_control = std::make_unique<tbb::global_control>(
109  tbb::global_control::max_allowed_parallelism, n_max_threads);
110 
111 # else
112  // Initialize the scheduler and destroy the old one before doing so
113  static tbb::task_scheduler_init dummy(tbb::task_scheduler_init::deferred);
114  if (dummy.is_active())
115  dummy.terminate();
116  dummy.initialize(n_max_threads);
117 # endif
118 #endif
119 
120 #ifdef DEAL_II_WITH_TASKFLOW
121  executor = std::make_unique<tf::Executor>(n_max_threads);
122 #endif
123 }
124 
125 
126 
127 unsigned int
129 {
131  return n_max_threads;
132 }
133 
134 
135 
136 bool
138 {
139  return n_threads() == 1;
140 }
141 
142 
143 
144 std::size_t
146 {
147  // only simple data elements, so use sizeof operator
148  return sizeof(MultithreadInfo);
149 }
150 
151 
152 
153 void
155 {
156  static std::once_flag is_initialized;
157  std::call_once(is_initialized, []() {
159  });
160 }
161 
162 
163 
164 #ifdef DEAL_II_WITH_TASKFLOW
165 tf::Executor &
167 {
168  // This should not trigger in normal user code, because we initialize the
169  // Executor in the static DoOnce struct at the end of this file unless you
170  // ask for the Executor before this static object gets constructed.
171  Assert(
172  executor.get() != nullptr,
173  ExcMessage(
174  "Please initialize multithreading using MultithreadInfo::set_thread_limit() first."));
175  return *(executor.get());
176 }
177 
178 std::unique_ptr<tf::Executor> MultithreadInfo::executor = nullptr;
179 #endif
180 
182 
183 namespace
184 {
185  // Force the first call to set_thread_limit happen before any tasks in TBB are
186  // used. This is necessary as tbb::task_scheduler_init has no effect if TBB
187  // got automatically initialized (which happens the first time we use it).
188  struct DoOnce
189  {
190  DoOnce()
191  {
193  }
194  } do_once;
195 } // namespace
196 
static unsigned int n_max_threads
MultithreadInfo()=delete
static bool is_running_single_threaded()
static std::unique_ptr< tf::Executor > executor
static void initialize_multithreading()
static unsigned int n_cores()
static unsigned int n_threads()
static void set_thread_limit(const unsigned int max_threads=numbers::invalid_unsigned_int)
static std::size_t memory_consumption()
static tf::Executor & get_taskflow_executor()
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:477
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:478
static ::ExceptionBase & ExcInternalError()
#define Assert(cond, exc)
Definition: exceptions.h:1616
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
Definition: exceptions.h:1705
int string_to_int(const std::string &s)
Definition: utilities.cc:606
static const unsigned int invalid_unsigned_int
Definition: types.h:213