Automatic Differentiation
 
Loading...
Searching...
No Matches
init_threadpool_tbb.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CORE_INIT_THREADPOOL_TBB_HPP
2#define STAN_MATH_PRIM_CORE_INIT_THREADPOOL_TBB_HPP
3
5
6#ifndef TBB_INTERFACE_NEW
7#include <tbb/tbb_stddef.h>
8
9#if TBB_VERSION_MAJOR >= 2020
10#define TBB_INTERFACE_NEW
11#endif
12#endif
13
14#ifdef TBB_INTERFACE_NEW
15#include <tbb/global_control.h>
16#include <tbb/task_arena.h>
17#else
18#include <tbb/task_scheduler_init.h>
19#endif
20
21#include <charconv>
22#include <cstdlib>
23#include <string_view>
24#include <thread>
25
26namespace stan {
27namespace math {
28namespace internal {
29
46inline int get_num_threads() {
47#ifdef STAN_THREADS
48 const char* env_stan_num_threads = std::getenv("STAN_NUM_THREADS");
49 if (env_stan_num_threads == nullptr) {
50 return 1;
51 }
52
53 const std::string_view value(env_stan_num_threads);
54 int num_threads;
55 const auto [end, error]
56 = std::from_chars(value.begin(), value.end(), num_threads);
57 if (error != std::errc() || end != value.end()
58 || (num_threads < 1 && num_threads != -1)) {
59 invalid_argument("get_num_threads(int)", "STAN_NUM_THREADS",
60 env_stan_num_threads,
61 "The STAN_NUM_THREADS environment variable is '",
62 "' but it must be a positive number or -1");
63 }
64
65 return num_threads == -1 ? std::thread::hardware_concurrency() : num_threads;
66#else
67 return 1;
68#endif
69}
70
71#ifdef TBB_INTERFACE_NEW
74 tbb::global_control control;
75 tbb::task_arena arena;
76
77 explicit threadpool_tbb_state(int num_threads)
78 : control(tbb::global_control::max_allowed_parallelism, num_threads),
79 arena(num_threads, 1) {
80 arena.initialize();
81 }
82};
83
86 return threadpool_tbb_state(num_threads);
87}
88#endif
89
90} // namespace internal
91
92#ifdef TBB_INTERFACE_NEW
114inline tbb::task_arena& init_threadpool_tbb(int n_threads = 0) {
115 int tbb_max_threads = 1;
116#ifdef STAN_THREADS
117 if (n_threads == 0) {
118 tbb_max_threads = internal::get_num_threads();
119 } else if (n_threads > 0) {
120 tbb_max_threads = n_threads;
121 } else if (n_threads == -1) {
122 tbb_max_threads = std::thread::hardware_concurrency();
123 } else {
124 invalid_argument("init_threadpool_tbb(int)", "n_threads", n_threads,
125 "The number of threads is '",
126 "' but it must be positive or -1");
127 }
128#endif
129 static auto state = internal::make_threadpool_tbb(tbb_max_threads);
130 state.arena.initialize();
131
132 return state.arena;
133}
134#else
157inline tbb::task_scheduler_init& init_threadpool_tbb(int n_threads = 0) {
158 int tbb_max_threads = 1;
159#ifdef STAN_THREADS
160 if (n_threads == 0) {
161 tbb_max_threads = internal::get_num_threads();
162 } else if (n_threads > 0) {
163 tbb_max_threads = n_threads;
164 } else if (n_threads == -1) {
165 tbb_max_threads = std::thread::hardware_concurrency();
166 } else {
167 invalid_argument("init_threadpool_tbb(int)", "n_threads", n_threads,
168 "The number of threads is '",
169 "' but it must be positive or -1");
170 }
171#endif
172 static tbb::task_scheduler_init tbb_scheduler(tbb_max_threads, 0);
173 return tbb_scheduler;
174}
175#endif
176
177} // namespace math
178} // namespace stan
179
180#endif
threadpool_tbb_state make_threadpool_tbb(int num_threads)
Construct independently owned thread-pool state for an explicit count.
int get_num_threads()
Get number of threads to use.
tbb::task_arena & init_threadpool_tbb(int n_threads=0)
Initialize the Intel TBB threadpool and global scheduler through the tbb::task_arena object.
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw an invalid_argument exception with a consistently formatted message.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Own the concurrency control before the arena, so the arena dies first.