Skip to content
Merged
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 docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
101 changes: 101 additions & 0 deletions python/numpy2x/_blas.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 7 additions & 5 deletions python/numpy2x/_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]))
Expand Down
9 changes: 7 additions & 2 deletions python/numpy2x/_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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():
Expand Down
57 changes: 57 additions & 0 deletions python/tests/test_blas.py
Original file line number Diff line number Diff line change
@@ -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
Loading