Skip to content

Commit 7e4e244

Browse files
committed
Make OpenBLAS's usage of OpenMP respect openblas_set_num_threads(). Fixes #5806.
Until now, the code in `num_cpu_avail()`, if (blas_cpu_number != openmp_nthreads) { goto_set_num_threads(openmp_nthreads); } would just always set the threads back to OpenMP's thread count.
1 parent b691cb1 commit 7e4e244

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

common_thread.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,19 @@ typedef struct blas_queue {
138138
extern int blas_server_avail;
139139
extern int blas_omp_number_max;
140140
extern int blas_omp_threads_local;
141+
extern int blas_is_num_threads_set_explicitly;
141142

142143
static __inline int num_cpu_avail(int level) {
143144

144145
#ifdef USE_OPENMP
146+
/* If the user explicitly called openblas_set_num_threads(),
147+
respect that setting instead of overriding it with
148+
`omp_get_max_threads()` below (which is to get a default
149+
in case the user hasn't made an explicit choice). */
150+
if (blas_is_num_threads_set_explicitly) {
151+
return blas_cpu_number;
152+
}
153+
145154
int openmp_nthreads;
146155
openmp_nthreads=omp_get_max_threads();
147156
if (omp_in_parallel()) openmp_nthreads = blas_omp_threads_local;

driver/others/blas_server_omp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
int blas_server_avail = 0;
7171
int blas_omp_number_max = 0;
7272
int blas_omp_threads_local = 1; // num threads to use when already inside omp_in_parallel()
73+
int blas_is_num_threads_set_explicitly = 0; // tracks whether the user called openblas_set_num_threads()
7374

7475
extern int openblas_omp_adaptive_env(void);
7576

@@ -122,7 +123,7 @@ void goto_set_num_threads(int num_threads) {
122123

123124
}
124125
void openblas_set_num_threads(int num_threads) {
125-
126+
blas_is_num_threads_set_explicitly = 1;
126127
goto_set_num_threads(num_threads);
127128
}
128129

0 commit comments

Comments
 (0)