diff --git a/driver/others/blas_server_omp.c b/driver/others/blas_server_omp.c index ab14d634a8..c64a6fa2bf 100644 --- a/driver/others/blas_server_omp.c +++ b/driver/others/blas_server_omp.c @@ -119,12 +119,24 @@ static void adjust_thread_buffers(void) { } } +/* OMP_THREAD_LIMIT caps every team below omp_get_max_threads(); the jobs of a threaded + level-3 driver wait for each other, so a job that no thread runs blocks the others forever */ +static int omp_thread_limit_cap(int num_threads) { +#if defined(_OPENMP) && (_OPENMP >= 200805) + int limit = omp_get_thread_limit(); + if (limit > 0 && num_threads > limit) num_threads = limit; +#endif + return num_threads; +} + void goto_set_num_threads(int num_threads) { if (num_threads < 1) num_threads = blas_num_threads; if (num_threads > MAX_CPU_NUMBER) num_threads = MAX_CPU_NUMBER; + num_threads = omp_thread_limit_cap(num_threads); + if (num_threads > blas_num_threads) { blas_num_threads = num_threads; } @@ -171,6 +183,7 @@ extern int openblas_omp_num_threads_env(void); #else blas_omp_number_max = omp_get_max_threads(); #endif + blas_omp_number_max = omp_thread_limit_cap(blas_omp_number_max); blas_get_cpu_number(); diff --git a/utest/CMakeLists.txt b/utest/CMakeLists.txt index 8db20ae5f0..4f218b8922 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_thread_limit.c ) # test_syrk_thread_workarea.c calls the threaded SYRK driver directly, which a shared libopenblas does not export if (BUILD_STATIC_LIBS) @@ -185,3 +186,8 @@ endif() add_test(${OpenBLAS_utest_bin} ${CMAKE_CURRENT_BINARY_DIR}/${OpenBLAS_utest_bin}) add_test(${OpenBLAS_utest_ext_bin} ${CMAKE_CURRENT_BINARY_DIR}/${OpenBLAS_utest_ext_bin}) +if (USE_OPENMP) + add_test(NAME ${OpenBLAS_utest_bin}_omp_thread_limit + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${OpenBLAS_utest_bin} omp_thread_limit) + set_tests_properties(${OpenBLAS_utest_bin}_omp_thread_limit PROPERTIES ENVIRONMENT "OMP_THREAD_LIMIT=2") +endif() diff --git a/utest/Makefile b/utest/Makefile index 7916c3636f..3ffe8b7c6a 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_thread_limit.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 @@ -90,6 +90,9 @@ run_test: $(UTESTBIN) $(UTESTEXTBIN) ifneq ($(CROSS), 1) ./$(UTESTBIN) ./$(UTESTEXTBIN) +ifeq ($(USE_OPENMP), 1) + OMP_THREAD_LIMIT=2 ./$(UTESTBIN) omp_thread_limit +endif endif clean: diff --git a/utest/test_omp_thread_limit.c b/utest/test_omp_thread_limit.c new file mode 100644 index 0000000000..377e268f90 --- /dev/null +++ b/utest/test_omp_thread_limit.c @@ -0,0 +1,93 @@ +/***************************************************************************** +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. + +**********************************************************************************/ + +/* + * OMP_THREAD_LIMIT below the number of CPUs: an OpenMP team then has fewer + * threads than omp_get_max_threads() reports, and the jobs of a threaded + * level-3 driver that no thread picks up leave the others waiting forever. The + * case runs only when such a limit is in effect; `make run_test` and CTest run + * this suite a second time with OMP_THREAD_LIMIT=2. + */ + +#include "openblas_utest.h" +#include +#ifdef USE_OPENMP +#include +#endif + +#if defined(USE_OPENMP) && defined(BUILD_DOUBLE) && defined(_OPENMP) && (_OPENMP >= 200805) + +#define TL_N 512 + +static void tl_dgemm(double *a, double *b, double *c) +{ + int i; + for (i = 0; i < TL_N * TL_N; i++) { a[i] = 1.0; b[i] = 2.0; c[i] = -1.0; } + cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, TL_N, TL_N, TL_N, + 1.0, a, TL_N, b, TL_N, 0.0, c, TL_N); + for (i = 0; i < TL_N * TL_N; i++) ASSERT_DBL_NEAR_TOL(2.0 * TL_N, c[i], 0.0); +} + +CTEST(omp_thread_limit, dgemm) +{ + int limit = omp_get_thread_limit(); + int procs = openblas_get_num_procs(); + double *a, *b, *c; + + if (limit >= procs || limit >= omp_get_max_threads()) { + CTEST_LOG("skipped: OMP_THREAD_LIMIT (%d) is not below the thread count", limit); + return; + } + if (omp_in_parallel()) { + CTEST_LOG("skipped: called inside a parallel region"); + return; + } + + a = (double *)malloc(sizeof(double) * TL_N * TL_N); + b = (double *)malloc(sizeof(double) * TL_N * TL_N); + c = (double *)malloc(sizeof(double) * TL_N * TL_N); + ASSERT_TRUE(a != NULL && b != NULL && c != NULL); + + /* the default thread count, taken from omp_get_max_threads() */ + tl_dgemm(a, b, c); + ASSERT_TRUE(openblas_get_num_threads() <= limit); + + /* an explicit request for more threads than the limit admits */ + openblas_set_num_threads(procs); + tl_dgemm(a, b, c); + ASSERT_TRUE(openblas_get_num_threads() <= limit); + + free(a); free(b); free(c); +} + +#endif