Skip to content

Disable dynamic thread adjustment for the OpenMP server's team - #6292

Open
ilgrad wants to merge 1 commit into
OpenMathLib:developfrom
ilgrad:fix/omp-dynamic-hang
Open

ilgrad wants to merge 1 commit into
OpenMathLib:developfrom
ilgrad:fix/omp-dynamic-hang

Conversation

@ilgrad

@ilgrad ilgrad commented Nov 30, 2026

Copy link
Copy Markdown

Problem

In an OpenMP build with dynamic adjustment of the number of threads (OMP_DYNAMIC=true or omp_set_dynamic(1)), threaded level-3 calls can hang forever. The runtime may give the team in exec_blas() fewer threads than there are jobs; libgomp subtracts the load average. The jobs of a threaded level-3 driver wait for each other (inner_thread in driver/level3/level3_thread.c), so the jobs that no thread picks up leave the running ones spinning. utest/test_syrk_thread.c already avoids threaded cases when omp_get_dynamic() is set for this reason.

This is the same class of hang as #6291 (OMP_THREAD_LIMIT), but it needs a different fix: the team size under dynamic adjustment is not known before the region starts.

Reproducer

/* cc t.c -lopenblas */
#include <stdio.h>
#include <stdlib.h>
#include <cblas.h>
int main(void) {
  int n = 2000;
  double *a = malloc(sizeof(double) * n * n), *b = malloc(sizeof(double) * n * n), *c = malloc(sizeof(double) * n * n);
  for (long i = 0; i < (long)n * n; i++) { a[i] = 1; b[i] = 1; c[i] = 0; }
  cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, n, n, n, 1.0, a, n, b, n, 0.0, c, n);
  printf("%g\n", c[0]);
}
$ cat /proc/loadavg
2.10 1.49 1.61 1/1451 74295
$ OMP_DYNAMIC=true timeout 15 ./a.out; echo $?
124

I ran it on a 16-thread Ryzen 7 5800HS (GCC 16.2.1 / libgomp, Linux) under a normal desktop load. It hangs with both the current develop (93a2969, make USE_OPENMP=1) and the Fedora 45 openblas-openmp 0.3.34 package. A plain #pragma omp parallel num_threads(16) on the same machine got a team of 7 threads with OMP_DYNAMIC=true, and 16 after omp_set_dynamic(0).

Fix

exec_blas() in driver/others/blas_server_omp.c turns dynamic adjustment off for its own parallel region and restores the caller's setting afterwards. omp_set_dynamic() changes only the dyn-var of the calling task, so other OpenMP code in the process is not affected. The caller-managed path (openblas_threads_callback_) is unchanged.

Test

utest/test_omp_dynamic.c calls a 512×512 dgemm with omp_set_dynamic(1). It checks the result and that omp_get_dynamic() is still set afterwards. Whether the runtime shrinks the team depends on the machine's load, so without the fix the test hangs only on a busy machine; on an idle CI runner it passes either way.

  • Without the fix: ./openblas_utest omp_dynamic hung in 3 of 3 runs (killed by timeout 60; load average 1.5–2).
  • With the fix: 3 of 3 runs pass, and the reproducer completes.
  • make -C utest run_test (USE_OPENMP=1 NOFORTRAN=1): 179 + 1563 tests ok.
  • OMP_DYNAMIC=true ./openblas_utest: 179 ok.
  • make -C ctest: all pass.
  • Not run: the Fortran BLAS tests (no gfortran here) and any non-Linux platform.

This PR and #6291 both add a test to the same lines of utest/Makefile and utest/CMakeLists.txt. I will rebase whichever is merged second.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant