Skip to content

Commit d945541

Browse files
authored
Merge pull request #6260 from apullin/nested-threading
Do not start nested parallel work from inside a thread-server job
2 parents 949b0d5 + c94c201 commit d945541

7 files changed

Lines changed: 409 additions & 1 deletion

File tree

‎common_thread.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,29 @@ extern int blas_omp_number_max;
156156
extern int blas_omp_threads_local;
157157
extern int blas_is_num_threads_set_explicitly;
158158

159+
#if defined(_MSC_VER) && !defined(__clang__)
160+
#define BLAS_THREAD_LOCAL __declspec(thread)
161+
#elif defined(__clang__)
162+
#if __has_feature(tls)
163+
#define BLAS_THREAD_LOCAL __thread
164+
#endif
165+
#elif defined(__GNUC__) || defined(__SUNPRO_C) || defined(__xlC__)
166+
#define BLAS_THREAD_LOCAL __thread
167+
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
168+
#define BLAS_THREAD_LOCAL _Thread_local
169+
#endif
170+
171+
/* Non-zero while the current thread runs a job for the thread server: as a
172+
pool or OpenMP team thread, through the threads callback, or as the caller
173+
executing its own share in exec_blas(). num_cpu_avail() then returns 1, so
174+
that internally threaded kernels do not start nested parallel work. */
175+
#ifdef BLAS_THREAD_LOCAL
176+
extern BLAS_THREAD_LOCAL int blas_thread_in_job;
177+
static __inline int blas_in_job(void) { return blas_thread_in_job; }
178+
#else
179+
int blas_in_job(void);
180+
#endif
181+
159182
#if defined(ARCH_ARM64) && defined(OS_DARWIN) && defined(__clang__) && (defined(VORTEXM4) || defined(DYNAMIC_ARCH)) && \
160183
(!defined(BFLOAT16) || !defined(BGEMM)) && !defined(HFLOAT16)
161184
#include <sys/sysctl.h>
@@ -185,6 +208,8 @@ static __inline int sme_level3_threads(int nthreads) {
185208

186209
static __inline int num_cpu_avail(int level) {
187210

211+
if (blas_in_job()) return 1;
212+
188213
#ifdef USE_OPENMP
189214
/* If the user explicitly called openblas_set_num_threads(),
190215
respect that setting instead of overriding it with

‎driver/others/blas_server.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,27 @@ int blas_server_avail __attribute__((aligned(ATTRIBUTE_SIZE))) = 0;
115115

116116
int blas_omp_threads_local = 1;
117117

118+
#ifdef BLAS_THREAD_LOCAL
119+
BLAS_THREAD_LOCAL int blas_thread_in_job = 0;
120+
#define blas_set_in_job(v) (blas_thread_in_job = (v))
121+
#else
122+
static pthread_key_t blas_in_job_key;
123+
static pthread_once_t blas_in_job_once = PTHREAD_ONCE_INIT;
124+
static int blas_in_job_key_ok = 0;
125+
static void blas_in_job_key_init(void) {
126+
blas_in_job_key_ok = (pthread_key_create(&blas_in_job_key, NULL) == 0);
127+
}
128+
int blas_in_job(void) {
129+
pthread_once(&blas_in_job_once, blas_in_job_key_init);
130+
return blas_in_job_key_ok && pthread_getspecific(blas_in_job_key) != NULL;
131+
}
132+
static void blas_set_in_job(int v) {
133+
pthread_once(&blas_in_job_once, blas_in_job_key_init);
134+
if (blas_in_job_key_ok)
135+
pthread_setspecific(blas_in_job_key, v ? (void *)1 : NULL);
136+
}
137+
#endif
138+
118139
static void * blas_thread_buffer[MAX_CPU_NUMBER];
119140

120141
/* Local Variables */
@@ -390,6 +411,9 @@ blas_queue_t *tscq;
390411
unsigned long start, stop;
391412
#endif
392413

414+
/* this thread only ever runs jobs of the pool */
415+
blas_set_in_job(1);
416+
393417
#if defined(OS_LINUX) && !defined(NO_AFFINITY)
394418
if (!increased_threads)
395419
thread_status[cpu].node = gotoblas_set_affinity(cpu + 1);
@@ -795,6 +819,7 @@ int exec_blas(BLASLONG num, blas_queue_t *queue){
795819
if (unlikely(blas_server_avail == 0)) blas_thread_init();
796820
#endif
797821
int (*routine)(blas_arg_t *, void *, void *, double *, double *, BLASLONG);
822+
int in_job;
798823

799824
#ifdef TIMING_DEBUG
800825
BLASULONG start, stop;
@@ -839,6 +864,9 @@ if (openblas_threads_callback_) {
839864
routine = (int (*)(blas_arg_t *, void *, void *, double *, double *, BLASLONG))queue -> routine;
840865
queue->worker_sb = queue->sb;
841866

867+
in_job = blas_in_job();
868+
blas_set_in_job(1);
869+
842870
if (queue -> mode & BLAS_LEGACY) {
843871
legacy_exec(routine, queue -> mode, queue -> args, queue -> sb);
844872
} else
@@ -849,6 +877,8 @@ if (openblas_threads_callback_) {
849877
(routine)(queue -> args, queue -> range_m, queue -> range_n,
850878
queue -> sa, queue -> sb, 0);
851879

880+
blas_set_in_job(in_job);
881+
852882
#ifdef TIMING_DEBUG
853883
stop = rpcc();
854884
#endif
@@ -1059,6 +1089,7 @@ static void adjust_thread_buffers() {
10591089
static void exec_threads(int cpu, blas_queue_t *queue, int buf_index) {
10601090

10611091
int (*routine)(blas_arg_t *, void *, void *, void *, void *, BLASLONG) = (int (*)(blas_arg_t *, void *, void *, void *, void *, BLASLONG))queue -> routine;
1092+
int in_job = blas_in_job();
10621093

10631094
atomic_store_queue(&thread_status[cpu].queue, (blas_queue_t *)1);
10641095

@@ -1149,6 +1180,8 @@ if (!(queue -> mode & BLAS_COMPLEX)){
11491180
main_status[cpu] = MAIN_RUNNING2;
11501181
#endif
11511182

1183+
blas_set_in_job(1);
1184+
11521185
if (queue -> mode & BLAS_LEGACY) {
11531186
legacy_exec(routine, queue -> mode, queue -> args, sb);
11541187
} else
@@ -1158,6 +1191,8 @@ if (queue -> mode & BLAS_PTHREAD) {
11581191
} else
11591192
(routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
11601193

1194+
blas_set_in_job(in_job);
1195+
11611196
#ifdef SMP_DEBUG
11621197
fprintf(STDERR, "Server[%2ld] Calculation finished!\n", cpu);
11631198
#endif

‎driver/others/blas_server_omp.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ int blas_omp_number_max = 0;
7272
int blas_omp_threads_local = 1;
7373
int blas_is_num_threads_set_explicitly = 0; // tracks whether the user called openblas_set_num_threads()
7474

75+
#ifdef BLAS_THREAD_LOCAL
76+
BLAS_THREAD_LOCAL int blas_thread_in_job = 0;
77+
#define blas_set_in_job(v) (blas_thread_in_job = (v))
78+
#else
79+
#include <pthread.h>
80+
static pthread_key_t blas_in_job_key;
81+
static pthread_once_t blas_in_job_once = PTHREAD_ONCE_INIT;
82+
static int blas_in_job_key_ok = 0;
83+
static void blas_in_job_key_init(void) {
84+
blas_in_job_key_ok = (pthread_key_create(&blas_in_job_key, NULL) == 0);
85+
}
86+
int blas_in_job(void) {
87+
pthread_once(&blas_in_job_once, blas_in_job_key_init);
88+
return blas_in_job_key_ok && pthread_getspecific(blas_in_job_key) != NULL;
89+
}
90+
static void blas_set_in_job(int v) {
91+
pthread_once(&blas_in_job_once, blas_in_job_key_init);
92+
if (blas_in_job_key_ok)
93+
pthread_setspecific(blas_in_job_key, v ? (void *)1 : NULL);
94+
}
95+
#endif
96+
7597
extern int openblas_omp_adaptive_env(void);
7698

7799
static _Atomic(void *) blas_thread_buffer[MAX_PARALLEL_NUMBER][MAX_CPU_NUMBER];
@@ -302,6 +324,7 @@ static void exec_threads(int thread_num, blas_queue_t *queue, int buf_index){
302324

303325
void *buffer, *sa, *sb;
304326
int pos=0, release_flag=0;
327+
int in_job = blas_in_job();
305328

306329
buffer = NULL;
307330
sa = queue -> sa;
@@ -382,6 +405,8 @@ fprintf(stderr,"UNHANDLED COMPLEX\n");
382405

383406
queue->worker_sb = sb;
384407

408+
blas_set_in_job(1);
409+
385410
if (queue -> mode & BLAS_LEGACY) {
386411
legacy_exec(queue -> routine, queue -> mode, queue -> args, sb);
387412
} else
@@ -396,6 +421,8 @@ fprintf(stderr,"UNHANDLED COMPLEX\n");
396421

397422
}
398423

424+
blas_set_in_job(in_job);
425+
399426
if (release_flag) blas_memory_free(buffer);
400427

401428
}

‎driver/others/blas_server_win32.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ typedef struct{
6565
/* We need this global for checking if initialization is finished. */
6666
int blas_server_avail = 0;
6767
int blas_omp_threads_local = 1;
68+
BLAS_THREAD_LOCAL int blas_thread_in_job = 0;
6869
/* Local Variables */
6970
static BLASULONG server_lock = 0;
7071

@@ -223,6 +224,9 @@ static DWORD WINAPI blas_thread_server(void *arg){
223224
fprintf(STDERR, "Server[%2ld] Thread is started!\n", cpu);
224225
#endif
225226

227+
/* this thread only ever runs jobs of the pool */
228+
blas_thread_in_job = 1;
229+
226230
while (1){
227231

228232
/* Waiting for Queue */
@@ -493,6 +497,7 @@ int exec_blas(BLASLONG num, blas_queue_t *queue){
493497
#ifndef ALL_THREADED
494498
int (*routine)(blas_arg_t *, void *, void *, double *, double *, BLASLONG);
495499
#endif
500+
int in_job;
496501

497502
if ((num <= 0) || (queue == NULL)) return 0;
498503

@@ -513,6 +518,9 @@ int exec_blas(BLASLONG num, blas_queue_t *queue){
513518
routine = queue -> routine;
514519
queue->worker_sb = queue->sb;
515520

521+
in_job = blas_thread_in_job;
522+
blas_thread_in_job = 1;
523+
516524
if (queue -> mode & BLAS_LEGACY) {
517525
legacy_exec(routine, queue -> mode, queue -> args, queue -> sb);
518526
} else
@@ -523,6 +531,8 @@ int exec_blas(BLASLONG num, blas_queue_t *queue){
523531
(routine)(queue -> args, queue -> range_m, queue -> range_n,
524532
queue -> sa, queue -> sb, 0);
525533

534+
blas_thread_in_job = in_job;
535+
526536
if ((num > 1) && queue -> next) exec_blas_async_wait(num - 1, queue -> next);
527537

528538
return 0;
@@ -624,6 +634,7 @@ void openblas_set_num_threads(int num)
624634
static void exec_threads(int cpu, blas_queue_t *queue, int buf_index) {
625635

626636
void *buffer, *sa, *sb;
637+
int in_job = blas_thread_in_job;
627638

628639
buffer = blas_memory_alloc(2);
629640
sa = queue -> sa;
@@ -699,11 +710,13 @@ if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
699710
main_status[cpu] = MAIN_RUNNING2;
700711
#endif
701712

713+
blas_thread_in_job = 1;
702714
if (!(queue -> mode & BLAS_LEGACY)) {
703715
(routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
704716
} else {
705717
legacy_exec(routine, queue -> mode, queue -> args, sb);
706718
}
719+
blas_thread_in_job = in_job;
707720
if (buffer) blas_memory_free(buffer);
708721
}
709722

‎utest/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ else ()
2222
test_trmv.c
2323
test_gemm.c
2424
test_matrix_packing.c
25+
test_nested_thread.c
2526
test_syrk_thread.c
2627
)
2728
# test_syrk_thread_workarea.c calls the threaded SYRK driver directly, which a shared libopenblas does not export

‎utest/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ UTESTEXTBIN=openblas_utest_ext$(EXE)
1414
include $(TOPDIR)/Makefile.system
1515

1616
OBJS=utest_main.o test_min.o test_amax.o test_ismin.o test_rotmg.o test_axpy.o test_dotu.o test_dsdot.o test_swap.o test_rot.o test_dnrm2.o test_zscal.o \
17-
test_amin.o test_axpby.o test_gemv.o test_trmv.o test_gemm.o test_matrix_packing.o test_syrk_thread.o test_syrk_thread_workarea.o
17+
test_amin.o test_axpby.o test_gemv.o test_trmv.o test_gemm.o test_matrix_packing.o test_nested_thread.o test_syrk_thread.o test_syrk_thread_workarea.o
1818
#test_rot.o test_swap.o test_axpy.o test_dotu.o test_dsdot.o test_fork.o
1919
OBJS_EXT=utest_main.o $(DIR_EXT)/xerbla.o $(DIR_EXT)/common.o
2020
OBJS_EXT+=$(DIR_EXT)/test_isamin.o $(DIR_EXT)/test_idamin.o $(DIR_EXT)/test_icamin.o $(DIR_EXT)/test_izamin.o

0 commit comments

Comments
 (0)