Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions driver/others/blas_server_omp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();

Expand Down
6 changes: 6 additions & 0 deletions utest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
5 changes: 4 additions & 1 deletion utest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
93 changes: 93 additions & 0 deletions utest/test_omp_thread_limit.c
Original file line number Diff line number Diff line change
@@ -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 <cblas.h>
#ifdef USE_OPENMP
#include <omp.h>
#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
Loading