Skip to content

Commit c2f4bdb

Browse files
authored
Merge pull request #4163 from martin-frbg/issue4017
Rework OpenMP thread count limit handling
2 parents 09131f7 + 9ff84dc commit c2f4bdb

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

common_thread.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ extern void goto_set_num_threads(int nthreads);
5353
/* Global Parameter */
5454
extern int blas_cpu_number;
5555
extern int blas_num_threads;
56-
extern int blas_num_threads_set;
5756
extern int blas_omp_linked;
5857

5958
#define BLAS_LEGACY 0x8000U
@@ -136,15 +135,13 @@ typedef struct blas_queue {
136135
#ifdef SMP_SERVER
137136

138137
extern int blas_server_avail;
138+
extern int blas_omp_number_max;
139139

140140
static __inline int num_cpu_avail(int level) {
141141

142142
#ifdef USE_OPENMP
143143
int openmp_nthreads;
144-
if (blas_num_threads_set == 0)
145144
openmp_nthreads=omp_get_max_threads();
146-
else
147-
openmp_nthreads=blas_cpu_number;
148145
#endif
149146

150147
#ifndef USE_OPENMP
@@ -156,7 +153,13 @@ int openmp_nthreads;
156153
) return 1;
157154

158155
#ifdef USE_OPENMP
159-
if (blas_cpu_number != openmp_nthreads) {
156+
if (openmp_nthreads > blas_omp_number_max){
157+
#ifdef DEBUG
158+
fprintf(stderr,"WARNING - more OpenMP threads requested (%d) than available (%d)\n",openmp_nthreads,blas_omp_number_max);
159+
#endif
160+
openmp_nthreads = blas_omp_number_max;
161+
}
162+
if (blas_cpu_number != openmp_nthreads) {
160163
goto_set_num_threads(openmp_nthreads);
161164
}
162165
#endif

driver/others/blas_server_omp.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#endif
6969

7070
int blas_server_avail = 0;
71+
int blas_omp_number_max = 0;
7172

7273
extern int openblas_omp_adaptive_env();
7374

@@ -100,8 +101,6 @@ static void adjust_thread_buffers() {
100101

101102
void goto_set_num_threads(int num_threads) {
102103

103-
blas_num_threads_set = 1;
104-
if (num_threads < 0) blas_num_threads_set = 0;
105104
if (num_threads < 1) num_threads = blas_num_threads;
106105

107106
if (num_threads > MAX_CPU_NUMBER) num_threads = MAX_CPU_NUMBER;
@@ -125,6 +124,8 @@ void openblas_set_num_threads(int num_threads) {
125124
}
126125

127126
int blas_thread_init(void){
127+
if(blas_omp_number_max <= 0)
128+
blas_omp_number_max = omp_get_max_threads();
128129

129130
blas_get_cpu_number();
130131

driver/others/memory.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,6 @@ This value is equal or large than blas_cpu_number. This means some threads are s
422422
*/
423423
int blas_num_threads = 0;
424424

425-
int blas_num_threads_set = 0;
426-
427425
int goto_get_num_procs (void) {
428426
return blas_cpu_number;
429427
}
@@ -1996,8 +1994,6 @@ This value is equal or large than blas_cpu_number. This means some threads are s
19961994
*/
19971995
int blas_num_threads = 0;
19981996

1999-
int blas_num_threads_set = 0;
2000-
20011997
int goto_get_num_procs (void) {
20021998
return blas_cpu_number;
20031999
}

driver/others/memory_qalloc.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ The numbers of threads in the thread pool.
283283
This value is equal or large than blas_cpu_number. This means some threads are sleep.
284284
*/
285285
int blas_num_threads = 0;
286-
int blas_num_threads_set = 0;
287286

288287
int goto_get_num_procs (void) {
289288
return blas_cpu_number;

0 commit comments

Comments
 (0)