Skip to content

win_amd64: concurrent dgemm + dgemv from two threads crashes (0xC0000005) or hangs with scipy-openblas64 0.3.26–0.3.34, but not with the official OpenBLAS binaries #283

Closed

Description

@yadonpapa

Summary

With the scipy-openblas64 Windows x64 wheels (the OpenBLAS that NumPy bundles), a dgemm in one thread running at the same time as a dgemv in another thread usually crashes the process within a second or two (0xC0000005, no message). Sometimes it deadlocks instead.

  • dgemm alone or dgemv alone in both threads is fine.
  • Small problems that OpenBLAS does not split across its own threads are fine.
  • OPENBLAS_NUM_THREADS=1 is fine.

The official release zips from OpenMathLib/OpenBLAS do not crash with the same reproducer from 0.3.27 on, and 0.3.34 is fine. This made me think the build configuration is involved.

This is probably what numpy/numpy#27989 reports. In NumPy terms the trigger is two threads doing (N, 3) @ (3, 3) and (N, 3) @ (3,) at the same time. That is a common pattern, e.g. a color transform in a worker pool next to a luminance computation in another thread. It took down a PyQt application of ours without any message.

Reproducer (C)

The DLL is loaded at run time, so the same binary runs against the official zips (LP64, cblas_*) and the wheel's libscipy_openblas64_.dll (ILP64, scipy_cblas_*64_). Built with cl /O2 repro.c (MSVC 19.51).

repro.c
/* Two threads call dgemm ((n,3) x (3,3)) and dgemv ((n,3) x (3,)) concurrently through CBLAS.
 * The OpenBLAS DLL is loaded at run time, so one binary tests both the official release zips (LP64,
 * cblas_*) and the scipy-openblas64 wheels (ILP64, scipy_cblas_*64_).
 *
 *   repro.exe <path to the DLL> [seconds=30] [both|gemm|gemv] [max_rows=270000]
 *
 * Prints "no crash" if it survives. Built with: cl /O2 repro.c
 */
#include <windows.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef void (*dgemm32_t)(int, int, int, int, int, int, double, const double *, int,
                          const double *, int, double, double *, int);
typedef void (*dgemv32_t)(int, int, int, int, double, const double *, int, const double *, int,
                          double, double *, int);
typedef void (*dgemm64_t)(int, int, int, long long, long long, long long, double, const double *, long long,
                          const double *, long long, double, double *, long long);
typedef void (*dgemv64_t)(int, int, long long, long long, double, const double *, long long, const double *,
                          long long, double, double *, long long);
typedef char *(*config_t)(void);
typedef int (*threads_t)(void);

enum { RowMajor = 101, NoTrans = 111 };
static dgemm32_t dgemm32;
static dgemv32_t dgemv32;
static dgemm64_t dgemm64;
static dgemv64_t dgemv64;
static volatile LONG stop_flag;
static int op;                       /* 0 = both, 1 = dgemm only, 2 = dgemv only */
static unsigned max_rows = 270000u;

static void gemm(int n, const double *a, const double *b, double *c)
{
    if (dgemm64) dgemm64(RowMajor, NoTrans, NoTrans, n, 3, 3, 1.0, a, 3, b, 3, 0.0, c, 3);
    else dgemm32(RowMajor, NoTrans, NoTrans, n, 3, 3, 1.0, a, 3, b, 3, 0.0, c, 3);
}

static void gemv(int n, const double *a, const double *x, double *y)
{
    if (dgemv64) dgemv64(RowMajor, NoTrans, n, 3, 1.0, a, 3, x, 1, 0.0, y, 1);
    else dgemv32(RowMajor, NoTrans, n, 3, 1.0, a, 3, x, 1, 0.0, y, 1);
}

static DWORD WINAPI work(LPVOID arg)
{
    unsigned s = (unsigned)(uintptr_t)arg * 2654435761u + 1u;
    const double B[9] = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8};
    const double X[3] = {0.2627, 0.6780, 0.0593};
    while (!stop_flag) {
        s = s * 1103515245u + 12345u;
        int n = 1000 + (int)((s >> 8) % (max_rows - 999u));    /* 1,000 .. max_rows rows */
        double *a = calloc((size_t)n * 3, sizeof(double));
        double *c = malloc((size_t)n * 3 * sizeof(double));
        double *y = malloc((size_t)n * sizeof(double));
        if (op != 2) gemm(n, a, B, c);
        if (op != 1) gemv(n, a, X, y);
        free(a);
        free(c);
        free(y);
    }
    return 0;
}

int main(int argc, char **argv)
{
    if (argc < 2) {
        fprintf(stderr, "usage: repro.exe <dll> [seconds] [both|gemm|gemv] [max_rows]\n");
        return 2;
    }
    HMODULE h = LoadLibraryA(argv[1]);
    if (!h) {
        fprintf(stderr, "cannot load %s (%lu)\n", argv[1], GetLastError());
        return 2;
    }
    config_t config;
    threads_t threads;
    /* scipy-openblas64 also exports unprefixed cblas_* names, but with 64-bit integers: look for it first */
    dgemm64 = (dgemm64_t)GetProcAddress(h, "scipy_cblas_dgemm64_");
    dgemv64 = (dgemv64_t)GetProcAddress(h, "scipy_cblas_dgemv64_");
    if (dgemm64 && dgemv64) {
        config = (config_t)GetProcAddress(h, "scipy_openblas_get_config64_");
        threads = (threads_t)GetProcAddress(h, "scipy_openblas_get_num_threads64_");
    } else {
        dgemm64 = NULL;
        dgemv64 = NULL;
        dgemm32 = (dgemm32_t)GetProcAddress(h, "cblas_dgemm");
        dgemv32 = (dgemv32_t)GetProcAddress(h, "cblas_dgemv");
        config = (config_t)GetProcAddress(h, "openblas_get_config");
        threads = (threads_t)GetProcAddress(h, "openblas_get_num_threads");
        if (!dgemm32 || !dgemv32) {
            fprintf(stderr, "cblas_dgemm / cblas_dgemv not found\n");
            return 2;
        }
    }
    double seconds = argc > 2 ? atof(argv[2]) : 30.0;
    op = argc > 3 ? (strcmp(argv[3], "gemm") == 0 ? 1 : strcmp(argv[3], "gemv") == 0 ? 2 : 0) : 0;
    if (argc > 4) max_rows = (unsigned)atoi(argv[4]);
    printf("%s, %d threads\n", config ? config() : "?", threads ? threads() : -1);
    fflush(stdout);

    HANDLE t[2];
    for (int i = 0; i < 2; i++)
        t[i] = CreateThread(NULL, 0, work, (LPVOID)(uintptr_t)i, 0, NULL);
    Sleep((DWORD)(seconds * 1000.0));
    InterlockedExchange(&stop_flag, 1);
    WaitForMultipleObjects(2, t, TRUE, INFINITE);
    printf("no crash\n");
    return 0;
}

Results

Each case was run 3 times with a 30 s run time. "hang" means the process was still alive after 90 s.

DLL dgemm + dgemv in two threads
official 0.3.25 ok, ok, ok
official 0.3.26 hang, hang, hang
official 0.3.27 ok, ok, ok
official 0.3.28 ok, ok, ok
official 0.3.34 ok, ok, ok
wheel 0.3.34.106.0 crash after 0.6 s, 0.3 s, 0.3 s
wheel 0.3.34.106.0, dgemm only ok, ok, ok
wheel 0.3.34.106.0, dgemv only ok, ok, ok
wheel 0.3.34.106.0, rows ≤ 3,000 ok, ok, ok
wheel 0.3.34.106.0, OPENBLAS_NUM_THREADS=1 ok, ok, ok

The same calls from Python threads through ctypes, against the wheels:

scipy-openblas64 dgemm + dgemv
0.3.23.293.2 ok, ok, ok
0.3.24.95.1 ok, ok, ok
0.3.26.0.4 hang, hang, hang
0.3.27.0.1 crash after 11.1 s, crash after 28.9 s, hang
0.3.28.0.2 hang, hang, crash after 2.7 s
0.3.34.106.0 crash after 8.0 s, crash after 12.1 s, crash after 0.9 s

The same pattern through NumPy (fresh uv environments, Python 3.12.12):

numpy bundled OpenBLAS result
1.26.4 0.3.23.dev ok
2.0.2, 2.1.3 0.3.27 crash or hang
2.2.0, 2.2.6 0.3.28, 0.3.29 crash
2.4.6 0.3.31.188.0 crash or hang
2.5.3 0.3.34.106.0 crash

More results with numpy 2.5.3:

  • OPENBLAS_NUM_THREADS=2: crash or hang.
  • OPENBLAS_NUM_THREADS=4: hang in 3 of 3 runs.
  • OPENBLAS_NUM_THREADS=1: no failure in 2 × 60 s with up to 8 threads (about 10 million calls).

What differs between the two builds

openblas_get_config() returns these strings:

  • official 0.3.34: OpenBLAS 0.3.34 DYNAMIC_ARCH NO_AFFINITY Haswell MAX_THREADS=64
  • wheel 0.3.34.106.0: OpenBLAS 0.3.34.106.0 USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell MAX_THREADS=24

tools/build_steps_windows.sh also sets NUM_THREADS=24 BUFFERSIZE=20 NO_WARMUP=1 CONSISTENT_FPCSR=1 and INTERFACE64=1.

This machine has 24 logical CPUs, so the default thread count equals MAX_THREADS. I have not built OpenBLAS myself, so I can't say which flag matters. I understand from #97 that the small NUM_THREADS is intentional.

Both builds hang on 0.3.26. That matches the Windows thread-server change in that release (OpenMathLib/OpenBLAS#4359, partly reverted in OpenMathLib/OpenBLAS#4835 for 0.3.28). Only the wheels keep failing after that.

Minidumps from the application crashes (wheel 0.3.33.112.0, no symbols) show the fault in an OpenBLAS worker thread. Twice it was a write to address 0x24 inside ntdll.dll, and once a write fault inside ucrtbase.dll.

Environment

  • Windows 11 Pro 10.0.26300, x64
  • Intel Core Ultra 9 275HX (Arrow Lake): 24 cores, no SMT, no AVX-512
  • OpenBLAS picks the Haswell kernels and 24 threads by default.

Workaround on our side: set OPENBLAS_NUM_THREADS=1 before importing numpy. Our matrices are 3×3, so nothing is lost.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions