Skip to content

Commit b668c9a

Browse files
authored
Merge pull request #5808 from nh2/issue-5806-openblas_set_num_threads-openmp
Make OpenBLAS's usage of OpenMP respect `openblas_set_num_threads()`
2 parents 65b51b0 + f8674a7 commit b668c9a

2 files changed

Lines changed: 14 additions & 4 deletions

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: 5 additions & 4 deletions
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;
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

@@ -118,7 +119,7 @@ void goto_set_num_threads(int num_threads) {
118119

119120
}
120121
void openblas_set_num_threads(int num_threads) {
121-
122+
blas_is_num_threads_set_explicitly = 1;
122123
goto_set_num_threads(num_threads);
123124
}
124125

@@ -141,7 +142,7 @@ extern int openblas_omp_num_threads_env(void);
141142

142143
if(blas_omp_number_max <= 0)
143144
blas_omp_number_max= openblas_omp_num_threads_env();
144-
if (blas_omp_number_max <= 0)
145+
if (blas_omp_number_max <= 0)
145146
blas_omp_number_max=MAX_CPU_NUMBER;
146147
#else
147148
blas_omp_number_max = omp_get_max_threads();
@@ -361,14 +362,14 @@ static void exec_threads(int thread_num, blas_queue_t *queue, int buf_index){
361362
#ifdef BUILD_COMPLEX16
362363
sb = (void *)(((BLASLONG)sa + ((ZGEMM_P * ZGEMM_Q * 2 * sizeof(double)
363364
+ GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
364-
#else
365+
#else
365366
fprintf(stderr,"UNHANDLED COMPLEX16\n");
366367
#endif
367368
} else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
368369
#ifdef BUILD_COMPLEX
369370
sb = (void *)(((BLASLONG)sa + ((CGEMM_P * CGEMM_Q * 2 * sizeof(float)
370371
+ GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
371-
#else
372+
#else
372373
fprintf(stderr,"UNHANDLED COMPLEX\n");
373374
#endif
374375
} else {

0 commit comments

Comments
 (0)