Loading...
Searching...
No Matches
LinalgThreads.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#include "LinalgThreads.h"
3
4#include <Eigen/Core>
5#include <atomic>
6#include <cstdlib>
7#include <mutex>
8
9#include "LocalExecutor.h"
10#include "SerialEigenThreads.h"
11#include "TileParallel.h"
12
13#if defined(_OPENMP)
14#include <omp.h>
15#endif
16
17#ifdef __linux__
18#include <sched.h>
19#endif
20
21// The default build passes EIGEN_DONT_VECTORIZE and GPR_SCALAR_EIGEN,
22// matching develop. -Dscalar_eigen=false leaves Eigen vectorized.
23#if defined(GPR_SCALAR_EIGEN) && defined(EIGEN_VECTORIZE)
24#error "scalar_eigen builds Eigen with EIGEN_DONT_VECTORIZE"
25#endif
26
27#ifdef GPR_HAVE_OPENBLAS
28extern "C" int openblas_get_num_threads();
29extern "C" void openblas_set_num_threads(int);
30#endif
31
32namespace gpr::linalg {
33namespace {
34std::atomic<int> configured_threads{1};
35std::atomic<int> requested_tile_threads{1};
36std::atomic<bool> library_serial_kernels{false};
37bool initialUnconstrainedReductions() noexcept
38{
39 const char* raw = std::getenv("GPR_UNCONSTRAINED_REDUCTIONS");
40 return raw != nullptr && raw[0] != '\0' && raw[0] != '0';
41}
42std::atomic<bool> unconstrained_library_reductions{
43 initialUnconstrainedReductions()};
44std::atomic<DistributedFactorization> factorization{
46std::atomic<std::uint64_t> factorization_generation{0};
47
48struct SerialEigenThreadState {
49 std::recursive_mutex mutex;
50 int depth = 0;
51 int saved_threads = 1;
52};
53
54// The state is process-lifetime and is never destroyed. A function-local
55// static would run its destructor during exit, and a later static destructor
56// that fits a model or factors a matrix would then lock a destroyed mutex.
57// The allocation is reclaimed by the operating system at exit.
58SerialEigenThreadState& serialEigenThreadState()
59{
60 static SerialEigenThreadState& state = *new SerialEigenThreadState;
61 return state;
62}
63} // namespace
64
66{
67 auto& state = serialEigenThreadState();
68 state.mutex.lock();
69 if (state.depth == 0) {
70 state.saved_threads = Eigen::nbThreads();
71 if (state.saved_threads != 1) Eigen::setNbThreads(1);
72 }
73 ++state.depth;
74}
75
77{
78 auto& state = serialEigenThreadState();
79 --state.depth;
80 if (state.depth == 0 && state.saved_threads != 1) {
81 Eigen::setNbThreads(state.saved_threads);
82 }
83 state.mutex.unlock();
84}
85
87{
88 return factorization.load(std::memory_order_relaxed);
89}
90
91std::uint64_t distributedFactorizationGeneration() noexcept
92{
93 return factorization_generation.load(std::memory_order_relaxed);
94}
95
97{
98 if (factorization.exchange(mode, std::memory_order_relaxed) != mode)
99 factorization_generation.fetch_add(1, std::memory_order_relaxed);
102 requested_tile_threads.load(std::memory_order_relaxed));
103}
104
106{
107 return mode == DistributedFactorization::Library ? "library"
108 : "reproducible";
109}
110
111std::optional<DistributedFactorization> parseDistributedFactorization(
112 std::string_view name) noexcept
113{
114 if (name == "reproducible") return DistributedFactorization::Reproducible;
115 if (name == "library") return DistributedFactorization::Library;
116 return std::nullopt;
117}
118
120{
121 return configured_threads.load(std::memory_order_relaxed);
122}
123
124void configureLinalgThreads(int threads)
125{
126 if (threads < 1) threads = 1;
127 configured_threads.store(threads, std::memory_order_relaxed);
128 // Search products keep one fixed reduction order. The requested budget
129 // is spent by the canonical tile and panel executors instead.
131#if defined(_OPENMP)
132 omp_set_dynamic(0);
133 // One active level: an assembly worker must not start another team on
134 // top of the factor team. The factor itself runs outside those regions.
135 omp_set_max_active_levels(1);
136 omp_set_num_threads(threads);
137#endif
139}
140
141void configureProductThreads(int threads)
142{
143 if (threads < 1) threads = 1;
144 {
145 const std::lock_guard<std::recursive_mutex> lock(
146 serialEigenThreadState().mutex);
147 Eigen::setNbThreads(threads);
148 }
149#ifdef GPR_HAVE_OPENBLAS
150 openblas_set_num_threads(threads);
151#endif
152}
153
154void configureLibrarySerialKernels(bool enabled) noexcept
155{
156 library_serial_kernels.store(enabled, std::memory_order_relaxed);
157}
158
159bool librarySerialKernels() noexcept
160{
161 return library_serial_kernels.load(std::memory_order_relaxed);
162}
163
165{
166 if (unconstrained_library_reductions.exchange(
167 enabled, std::memory_order_relaxed) != enabled) {
168 factorization_generation.fetch_add(1, std::memory_order_relaxed);
169 }
170}
171
173{
174 return unconstrained_library_reductions.load(std::memory_order_relaxed);
175}
176
177void configureTileThreads(int threads)
178{
179 requested_tile_threads.store(threads, std::memory_order_relaxed);
180 // Products are already one thread. Reproducible mode spends the count
181 // on independent tiles. Library mode keeps tiles on the calling thread;
182 // its ScaLAPACK rounding still follows the process grid, not this count.
185 ? threads
186 : 1);
187}
188
189int affinityCpuCount() noexcept
190{
191#ifdef __linux__
192 cpu_set_t set;
193 CPU_ZERO(&set);
194 if (sched_getaffinity(0, sizeof(set), &set) != 0) return 0;
195 return CPU_COUNT(&set);
196#else
197 return 0;
198#endif
199}
200
201int blasThreadCount() noexcept
202{
203#ifdef GPR_HAVE_OPENBLAS
204 return openblas_get_num_threads();
205#else
206 return 0;
207#endif
208}
209
210} // namespace gpr::linalg
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
int blasThreadCount() noexcept
void setTileThreads(int threads) noexcept
Sets the tile thread count (at least one).
std::uint64_t distributedFactorizationGeneration() noexcept
std::optional< DistributedFactorization > parseDistributedFactorization(std::string_view name) noexcept
"reproducible" or "library"; anything else gives no value.
void configureProductThreads(int threads)
void configureLinalgThreads(int threads)
const char * distributedFactorizationName(DistributedFactorization mode) noexcept
void refreshKernelTeam()
bool unconstrainedLibraryReductions() noexcept
bool librarySerialKernels() noexcept
void configureTileThreads(int threads)
DistributedFactorization distributedFactorization() noexcept
Process-wide selection; Reproducible unless configured otherwise.
void configureLibrarySerialKernels(bool enabled) noexcept
int configuredLinalgThreads() noexcept
Requested dense numerical budget, independent of nested BLAS controls.
void configureDistributedFactorization(DistributedFactorization mode)
int affinityCpuCount() noexcept
CPUs in this process's scheduling affinity, or 0 when unknown.
void configureUnconstrainedLibraryReductions(bool enabled) noexcept