Reference documentation for deal.II version 9.3.3
\(\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 - 2020 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
18
19#include <algorithm>
20#include <cstdlib> // for std::getenv
21#include <thread>
22
23#ifdef DEAL_II_WITH_TBB
24# ifdef DEAL_II_TBB_WITH_ONEAPI
25# include <tbb/global_control.h>
26# else
27# include <tbb/task_scheduler_init.h>
28# endif
29#endif
30
31
33
34
35unsigned int
37{
38 // There is a slight semantic change between our n_cores() call and the
39 // std::thread alternative: in case of an error the latter one returns 0
40 // in contrast to a 1 that n_cores() used to do. For compatibility, let's
41 // translate to our numbering scheme:
42 const unsigned int n_cores = std::thread::hardware_concurrency();
43 return n_cores == 0 ? 1 : n_cores;
44}
45
46
47void
48MultithreadInfo::set_thread_limit(const unsigned int max_threads)
49{
50 // set the maximal number of threads to the given value as specified
51 n_max_threads = max_threads;
52
53 // then also see if something was given in the environment
54 {
55 if (const char *penv = std::getenv("DEAL_II_NUM_THREADS"))
56 {
57 unsigned int max_threads_env = numbers::invalid_unsigned_int;
58 try
59 {
60 max_threads_env = Utilities::string_to_int(std::string(penv));
61 }
62 catch (...)
63 {
65 false,
67 std::string(
68 "When specifying the <DEAL_II_NUM_THREADS> environment "
69 "variable, it needs to be something that can be interpreted "
70 "as an integer. The text you have in the environment "
71 "variable is <") +
72 penv + ">"));
73 }
74
75 AssertThrow(max_threads_env > 0,
77 "When specifying the <DEAL_II_NUM_THREADS> environment "
78 "variable, it needs to be a positive number."));
79
81 n_max_threads = std::min(n_max_threads, max_threads_env);
82 else
83 n_max_threads = max_threads_env;
84 }
85 }
86
87 // If we have not set the number of allowed threads yet, just default to
88 // the number of available cores
91
92#ifdef DEAL_II_WITH_TBB
93# ifdef DEAL_II_TBB_WITH_ONEAPI
94 tbb::global_control(tbb::global_control::max_allowed_parallelism,
96# else
97 // Initialize the scheduler and destroy the old one before doing so
98 static tbb::task_scheduler_init dummy(tbb::task_scheduler_init::deferred);
99 if (dummy.is_active())
100 dummy.terminate();
101 dummy.initialize(n_max_threads);
102# endif
103#endif
104}
105
106
107
108unsigned int
110{
112 return n_max_threads;
113}
114
115
116
117bool
119{
120 return n_threads() == 1;
121}
122
123
124
125std::size_t
127{
128 // only simple data elements, so use sizeof operator
129 return sizeof(MultithreadInfo);
130}
131
132
133
134void
136{
137 static bool done = false;
138 if (done)
139 return;
140
142 done = true;
143}
144
146
147namespace
148{
149 // Force the first call to set_thread_limit happen before any tasks in TBB are
150 // used. This is necessary as tbb::task_scheduler_init has no effect if TBB
151 // got automatically initialized (which happens the first time we use it).
152 struct DoOnce
153 {
154 DoOnce()
155 {
157 }
158 } do_once;
159} // namespace
160
static unsigned int n_max_threads
MultithreadInfo()=delete
static bool is_running_single_threaded()
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()
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:402
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:403
#define Assert(cond, exc)
Definition: exceptions.h:1465
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
Definition: exceptions.h:1575
int string_to_int(const std::string &s)
Definition: utilities.cc:608
static const unsigned int invalid_unsigned_int
Definition: types.h:196
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)