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
6 changes: 6 additions & 0 deletions driver/others/blas_server_omp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 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_dynamic.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
2 changes: 1 addition & 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_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
Expand Down
80 changes: 80 additions & 0 deletions utest/test_omp_dynamic.c
Original file line number Diff line number Diff line change
@@ -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 <cblas.h>
#ifdef USE_OPENMP
#include <omp.h>
#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
Loading