From b633a2963ad850008a3250ba24427c2c6797bf6b Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 18:13:00 +0000 Subject: [PATCH] One BLAS thread while the runtime runs, LAPACK no longer serialized by size numpy2x sets OpenBLAS's process-wide thread count to 1 when its runtime starts and restores the previous count at shutdown (_blas.py, as viproc): the workers parallelize across ops instead of OpenBLAS's own threads. LAPACK calls of >= 64 x 64 are serialized (NX_GUFUNC_SERIAL) only when numpy2x has no control over the BLAS threads (no OpenBLAS found). Results are bit-identical to NumPy with OPENBLAS_NUM_THREADS=1; large products may differ in the last bits from NumPy's default thread count (docs/testing.md, known differences). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JT2yHjSFdbWf5ym7Dc5MAU --- docs/testing.md | 6 +++ python/numpy2x/_blas.py | 101 +++++++++++++++++++++++++++++++++++++ python/numpy2x/_linalg.py | 12 +++-- python/numpy2x/_ndarray.py | 9 +++- python/tests/test_blas.py | 57 +++++++++++++++++++++ 5 files changed, 178 insertions(+), 7 deletions(-) create mode 100644 python/numpy2x/_blas.py create mode 100644 python/tests/test_blas.py diff --git a/docs/testing.md b/docs/testing.md index 4f536bf..1646f70 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -169,6 +169,12 @@ is a sync point, where the program waits for that result. Never measure times in - `np.array(a)` and `np.asarray(a)` always copy a NumPy array into numpy2x, even with `copy=False`. - Data types numpy2x does not run (object, str, float16, …) give plain NumPy arrays. - On very small data, numpy2x can be slightly slower than NumPy. +- BLAS (OpenBLAS in NumPy's wheels) runs on one thread while numpy2x runs; numpy2x's workers + parallelize instead. OpenBLAS splits large matrix products by its thread count, so their results + are bit-identical to NumPy run with `OPENBLAS_NUM_THREADS=1` and may differ from NumPy's default in + the last bits. A single large product with nothing to overlap is slower than NumPy's multi-threaded + one; independent linear algebra calls get faster. The setting is process-wide (plain NumPy calls in + the same process run single-threaded too) and is restored by `numpy2x.shutdown()`. ## 8. Feedback diff --git a/python/numpy2x/_blas.py b/python/numpy2x/_blas.py new file mode 100644 index 0000000..d905cec --- /dev/null +++ b/python/numpy2x/_blas.py @@ -0,0 +1,101 @@ +"""Thread count of the BLAS library NumPy uses (OpenBLAS in NumPy's wheels). + +numpy2x runs every BLAS call on one thread while its runtime runs: the two +workers parallelize across ops, and OpenBLAS's own threads on top of them +oversubscribe the cores (8 concurrent 300 x 300 `inv` took 20 ms to 1.1 s, +NumPy ~22 ms). With one BLAS thread, LAPACK calls need no serialization +either (`_linalg._call`). + +OpenBLAS splits a product by its thread count, and the values depend on +that split. Results are therefore those of NumPy with one BLAS thread +(`OPENBLAS_NUM_THREADS=1`). OpenBLAS has no per-thread setting, so the +count is process-wide: plain NumPy calls in the same process run +single-threaded too while the runtime runs, and give the same values. The +count found before is restored when the runtime shuts down. No BLAS library +found: nothing changes, and large LAPACK calls stay serialized. +""" + +import ctypes +import glob +import os +import sys + +import numpy as np + +_funcs = None # (set, get) once looked up; False: no BLAS library found +_saved = None # thread count before numpy2x set one thread, while that is in effect + + +def _candidates(): + here = os.path.dirname(os.path.abspath(np.__file__)) + for d in (os.path.join(here, os.pardir, "numpy.libs"), os.path.join(here, ".dylibs")): + yield from sorted(glob.glob(os.path.join(d, "*openblas*"))) + if sys.platform.startswith("linux"): # NumPy built against a system OpenBLAS + try: + with open("/proc/self/maps") as f: + seen = set() + for line in f: + path = line.split()[-1] + if "openblas" in os.path.basename(path) and path not in seen: + seen.add(path) + yield path + except OSError: + pass + + +def _lookup(): + for path in _candidates(): + try: + lib = ctypes.CDLL(path) + except OSError: + continue + for prefix in ("scipy_openblas_", "openblas_"): + for suffix in ("64_", "_64_", ""): + try: + set_ = getattr(lib, f"{prefix}set_num_threads{suffix}") + get = getattr(lib, f"{prefix}get_num_threads{suffix}") + except AttributeError: + continue + set_.argtypes, set_.restype = [ctypes.c_int], None + get.argtypes, get.restype = [], ctypes.c_int + return set_, get + return False + + +def _get_funcs(): + global _funcs + if _funcs is None: + _funcs = _lookup() + return _funcs + + +def threads(): + """The BLAS library's current thread count, or None without one.""" + funcs = _get_funcs() + return funcs[1]() if funcs else None + + +def single(): + """True while numpy2x runs BLAS on one thread (`apply`), no call.""" + return _saved is not None + + +def apply(): + """One BLAS thread while the runtime runs (every optimization level).""" + global _saved + funcs = _get_funcs() + if not funcs: + return + set_, get = funcs + if _saved is None: + _saved = get() + set_(1) + + +def restore(): + """The thread count from before `apply` (runtime shut down).""" + global _saved + funcs = _get_funcs() + if funcs and _saved is not None: + funcs[0](_saved) + _saved = None diff --git a/python/numpy2x/_linalg.py b/python/numpy2x/_linalg.py index 8547ff4..48712eb 100644 --- a/python/numpy2x/_linalg.py +++ b/python/numpy2x/_linalg.py @@ -24,7 +24,7 @@ from numpy.linalg._linalg import EighResult, EigResult, SlogdetResult, SVDResult from . import _ndarray as _nd -from . import _ufuncs, _numpy2x +from . import _blas, _ufuncs, _numpy2x _NOPE = _nd._NOPE ndarray = _nd.ndarray @@ -86,15 +86,17 @@ def _assert_stacked_square(a): _CHECK_FINITE = 2 # NX_GUFUNC_CHECK_FINITE _SERIAL = 4 # NX_GUFUNC_SERIAL -# Matrices of at least this many elements run one LAPACK call at a time: -# OpenBLAS threads them itself, and several threaded calls at once -# oversubscribe its threads (nx_gufunc, NX_GUFUNC_SERIAL). +# Without control over the BLAS threads (no OpenBLAS found, `_blas.single()` +# false), matrices of at least this many elements run one LAPACK call at a +# time: OpenBLAS threads them itself, and several threaded calls at once +# oversubscribe its threads (nx_gufunc, NX_GUFUNC_SERIAL). With one BLAS +# thread (the normal case, `_blas.apply`) the workers parallelize. SERIAL_FROM = 64 * 64 def _call(gufunc, inputs, signature, invalid=None, label_sizes=None, check_finite=False): flags = _CHECK_FINITE if check_finite else 0 - if max(math.prod(x.shape[-2:]) for x in inputs) >= SERIAL_FROM: + if not _blas.single() and max(math.prod(x.shape[-2:]) for x in inputs) >= SERIAL_FROM: flags |= _SERIAL return _ufuncs.gufunc(gufunc, inputs, {"signature": signature}, label_sizes, invalid, flags, _work(inputs[0])) diff --git a/python/numpy2x/_ndarray.py b/python/numpy2x/_ndarray.py index b7f4602..9d46924 100644 --- a/python/numpy2x/_ndarray.py +++ b/python/numpy2x/_ndarray.py @@ -20,7 +20,7 @@ import numpy as np # imports the real NumPy, not numpy2x from numpy.lib.array_utils import normalize_axis_index, normalize_axis_tuple -from . import _numpy2x +from . import _blas, _numpy2x AsyncError = _numpy2x.AsyncError AsyncIndexError = _numpy2x.AsyncIndexError @@ -54,6 +54,7 @@ def _ensure_runtime(): if not _numpy2x.initialized(): sync_below, adaptive = _env_policy() _numpy2x.init(0, sync_below, adaptive) + _blas.apply() _small_ops[0] = 2048 if sync_below < 0 else sync_below _scale_budget(adaptive) @@ -1986,6 +1987,7 @@ def init(max_active_tasks=0, sync_below=None, adaptive=None): finally: adaptive = env_adaptive if adaptive is None else adaptive _numpy2x.init(max_active_tasks, sync_below, adaptive) + _blas.apply() _small_ops[0] = 2048 if sync_below < 0 else sync_below _scale_budget(adaptive) @@ -2026,7 +2028,10 @@ def shutdown(): """Waits for all ops and stops the runtime. Raises the error of a failed op that was not reported yet (at exit Python prints it).""" if _numpy2x.initialized(): - _numpy2x.shutdown() + try: + _numpy2x.shutdown() + finally: + _blas.restore() # the BLAS thread count from before the runtime def workers(): diff --git a/python/tests/test_blas.py b/python/tests/test_blas.py new file mode 100644 index 0000000..ecfcdd7 --- /dev/null +++ b/python/tests/test_blas.py @@ -0,0 +1,57 @@ +"""BLAS on one thread while the runtime runs (`_blas.py`), restored at +shutdown; LAPACK calls serialized only without control over the BLAS +threads.""" + +import numpy as np +import pytest + +import numpy2x +from numpy2x import _blas, _linalg, _ufuncs + + +def test_blas_one_thread_and_restored(): + numpy2x.shutdown() + before = _blas.threads() + if before is None: + pytest.skip("no BLAS library with a thread setting found") + try: + numpy2x.init() + assert _blas.threads() == 1 and _blas.single() + numpy2x.shutdown() + assert _blas.threads() == before and not _blas.single() + finally: + numpy2x.init() + + +def _flags_seen(monkeypatch): + seen = [] + real = _ufuncs.gufunc + + def spy(*args): + seen.append(args[5]) + return real(*args) + + monkeypatch.setattr(_ufuncs, "gufunc", spy) + return seen + + +def _inv80(): + a = np.random.default_rng(0).random((80, 80)) + 80 * np.eye(80) + np.linalg.inv(numpy2x.asarray(a)).wait() + + +def test_lapack_not_serialized(monkeypatch): + numpy2x.init() + if not _blas.single(): + pytest.skip("no BLAS library with a thread setting found") + seen = _flags_seen(monkeypatch) + _inv80() + assert not seen[-1] & _linalg._SERIAL + + +def test_lapack_serialized_without_blas_control(monkeypatch): + numpy2x.init() + seen = _flags_seen(monkeypatch) + monkeypatch.setattr(_blas, "single", lambda: False) + _inv80() + assert seen[-1] & _linalg._SERIAL