From d6ff7cd494f381d512e2479e056c5fc809e56cb9 Mon Sep 17 00:00:00 2001 From: Ilia Gradina Date: Mon, 30 Nov 2026 14:01:43 +0200 Subject: [PATCH] Disable dynamic thread adjustment for the OpenMP server's team With OMP_DYNAMIC=true the OpenMP runtime may start fewer threads than exec_blas() queues jobs (libgomp subtracts the load average). The jobs of a threaded level-3 driver wait for each other, so a job that no thread runs blocks the others forever: dgemm hangs on a busy machine. --- driver/others/blas_server_omp.c | 6 +++ utest/CMakeLists.txt | 1 + utest/Makefile | 2 +- utest/test_omp_dynamic.c | 80 +++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 utest/test_omp_dynamic.c diff --git a/driver/others/blas_server_omp.c b/driver/others/blas_server_omp.c index ab14d634a8..70862426e0 100644 --- a/driver/others/blas_server_omp.c +++ b/driver/others/blas_server_omp.c @@ -471,6 +471,11 @@ while (true) { #endif openblas_threads_callback_(1, (openblas_dojob_callback) exec_threads, num, sizeof(blas_queue_t), (void*) queue, buf_index); } else { + /* The jobs of a threaded level-3 driver wait for each other. With dynamic adjustment + (OMP_DYNAMIC=true) the runtime may start fewer threads than there are jobs, and the + jobs that no thread runs would block the others forever. */ + int dynamic = omp_get_dynamic(); + if (dynamic) omp_set_dynamic(0); if (openblas_omp_adaptive_env() != 0) { #pragma omp parallel for num_threads(num) schedule(OMP_SCHED) @@ -491,6 +496,7 @@ while (true) { exec_threads(omp_get_thread_num(), &queue[i], buf_index); } } + if (dynamic) omp_set_dynamic(1); } #ifdef HAVE_C11 diff --git a/utest/CMakeLists.txt b/utest/CMakeLists.txt index 8db20ae5f0..a78da4a79a 100644 --- a/utest/CMakeLists.txt +++ b/utest/CMakeLists.txt @@ -24,6 +24,7 @@ else () test_matrix_packing.c test_nested_thread.c test_syrk_thread.c + test_omp_dynamic.c ) # test_syrk_thread_workarea.c calls the threaded SYRK driver directly, which a shared libopenblas does not export if (BUILD_STATIC_LIBS) diff --git a/utest/Makefile b/utest/Makefile index 7916c3636f..2612381020 100644 --- a/utest/Makefile +++ b/utest/Makefile @@ -14,7 +14,7 @@ UTESTEXTBIN=openblas_utest_ext$(EXE) include $(TOPDIR)/Makefile.system 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 \ - 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 + 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 test_omp_dynamic.o #test_rot.o test_swap.o test_axpy.o test_dotu.o test_dsdot.o test_fork.o OBJS_EXT=utest_main.o $(DIR_EXT)/xerbla.o $(DIR_EXT)/common.o OBJS_EXT+=$(DIR_EXT)/test_isamin.o $(DIR_EXT)/test_idamin.o $(DIR_EXT)/test_icamin.o $(DIR_EXT)/test_izamin.o diff --git a/utest/test_omp_dynamic.c b/utest/test_omp_dynamic.c new file mode 100644 index 0000000000..b926912b7e --- /dev/null +++ b/utest/test_omp_dynamic.c @@ -0,0 +1,80 @@ +/***************************************************************************** +Copyright (c) 2026, The OpenBLAS Project +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name of the OpenBLAS project nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE +USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +**********************************************************************************/ + +/* + * Dynamic adjustment of the number of threads (OMP_DYNAMIC=true, omp_set_dynamic(1)): the + * OpenMP runtime may give a team fewer threads than requested, e.g. libgomp subtracts the + * load average. The jobs of a threaded level-3 driver wait for each other, so a job that no + * thread picks up leaves the others waiting forever. Whether the runtime shrinks the team + * depends on the load of the machine, so without the fix this case hangs only on a busy one. + */ + +#include "openblas_utest.h" +#include +#ifdef USE_OPENMP +#include +#endif + +#if defined(USE_OPENMP) && defined(BUILD_DOUBLE) + +#define DY_N 512 + +CTEST(omp_dynamic, dgemm) +{ + double *a, *b, *c; + int i, saved; + + if (omp_in_parallel()) { + CTEST_LOG("skipped: called inside a parallel region"); + return; + } + + a = (double *)malloc(sizeof(double) * DY_N * DY_N); + b = (double *)malloc(sizeof(double) * DY_N * DY_N); + c = (double *)malloc(sizeof(double) * DY_N * DY_N); + ASSERT_TRUE(a != NULL && b != NULL && c != NULL); + for (i = 0; i < DY_N * DY_N; i++) { a[i] = 1.0; b[i] = 2.0; c[i] = -1.0; } + + saved = omp_get_dynamic(); + omp_set_dynamic(1); + cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, DY_N, DY_N, DY_N, + 1.0, a, DY_N, b, DY_N, 0.0, c, DY_N); + /* the caller's setting is left as it was */ + ASSERT_TRUE(omp_get_dynamic() != 0); + omp_set_dynamic(saved); + + for (i = 0; i < DY_N * DY_N; i++) ASSERT_DBL_NEAR_TOL(2.0 * DY_N, c[i], 0.0); + free(a); free(b); free(c); +} + +#endif