23#if defined(GPR_SCALAR_EIGEN) && defined(EIGEN_VECTORIZE)
24#error "scalar_eigen builds Eigen with EIGEN_DONT_VECTORIZE"
27#ifdef GPR_HAVE_OPENBLAS
28extern "C" int openblas_get_num_threads();
29extern "C" void openblas_set_num_threads(
int);
34std::atomic<int> configured_threads{1};
35std::atomic<int> requested_tile_threads{1};
36std::atomic<bool> library_serial_kernels{
false};
37bool initialUnconstrainedReductions() noexcept
39 const char* raw = std::getenv(
"GPR_UNCONSTRAINED_REDUCTIONS");
40 return raw !=
nullptr && raw[0] !=
'\0' && raw[0] !=
'0';
42std::atomic<bool> unconstrained_library_reductions{
43 initialUnconstrainedReductions()};
44std::atomic<DistributedFactorization> factorization{
46std::atomic<std::uint64_t> factorization_generation{0};
48struct SerialEigenThreadState {
49 std::recursive_mutex mutex;
51 int saved_threads = 1;
58SerialEigenThreadState& serialEigenThreadState()
60 static SerialEigenThreadState& state = *
new SerialEigenThreadState;
67 auto& state = serialEigenThreadState();
69 if (state.depth == 0) {
70 state.saved_threads = Eigen::nbThreads();
71 if (state.saved_threads != 1) Eigen::setNbThreads(1);
78 auto& state = serialEigenThreadState();
80 if (state.depth == 0 && state.saved_threads != 1) {
81 Eigen::setNbThreads(state.saved_threads);
88 return factorization.load(std::memory_order_relaxed);
93 return factorization_generation.load(std::memory_order_relaxed);
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));
112 std::string_view name)
noexcept
121 return configured_threads.load(std::memory_order_relaxed);
126 if (threads < 1) threads = 1;
127 configured_threads.store(threads, std::memory_order_relaxed);
135 omp_set_max_active_levels(1);
136 omp_set_num_threads(threads);
143 if (threads < 1) threads = 1;
145 const std::lock_guard<std::recursive_mutex> lock(
146 serialEigenThreadState().mutex);
147 Eigen::setNbThreads(threads);
149#ifdef GPR_HAVE_OPENBLAS
150 openblas_set_num_threads(threads);
156 library_serial_kernels.store(enabled, std::memory_order_relaxed);
161 return library_serial_kernels.load(std::memory_order_relaxed);
166 if (unconstrained_library_reductions.exchange(
167 enabled, std::memory_order_relaxed) != enabled) {
168 factorization_generation.fetch_add(1, std::memory_order_relaxed);
174 return unconstrained_library_reductions.load(std::memory_order_relaxed);
179 requested_tile_threads.store(threads, std::memory_order_relaxed);
194 if (sched_getaffinity(0,
sizeof(set), &set) != 0)
return 0;
195 return CPU_COUNT(&set);
203#ifdef GPR_HAVE_OPENBLAS
204 return openblas_get_num_threads();
~ScopedSerialEigenThreads()
ScopedSerialEigenThreads()
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
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