Skip to content

One BLAS thread while the runtime runs, LAPACK no longer serialized by size - #6

Merged
hokb merged 1 commit into
mainfrom
claude/project-thread-7lq3h4
Nov 12, 2026
Merged

hokb merged 1 commit into
mainfrom
claude/project-thread-7lq3h4

Conversation

@hokb

@hokb hokb commented Nov 12, 2026

Copy link
Copy Markdown
Member

What

Same change as viproc (hokb/viproc#67): numpy2x runs BLAS (OpenBLAS) on one thread while its runtime runs; its two workers parallelize across ops instead.

  • _blas.py (new, from viproc): apply() at runtime start sets OpenBLAS's process-wide count to 1, restore() at shutdown() gives back the previous count; single() tells whether it is in effect.
  • _linalg._call: LAPACK calls of >= 64 x 64 are serialized (NX_GUFUNC_SERIAL) only without control over the BLAS threads (no OpenBLAS found). Before, they were always serialized by size.
  • python/tests/test_blas.py: one thread and restored, no serialization, serialization without BLAS control.
  • docs/testing.md, known differences: 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; a single large product is slower.

Measured

viproc's opt_levels.py adapted to numpy2x, 4 vCPUs (cloud VM), p = 8, speedup vs. NumPy with its default BLAS threads, two full runs (ranges):

case n before after
8 × inv 64 0.73-0.78x 2.3-2.75x
8 × inv 128-1000 0.67-1.09x 0.70-0.95x
8 × solve 64-500 0.59-1.02x 1.09-1.86x
8 × solve 1000 0.79-1.12x 0.89-0.98x
8 product chains 64-128 0.98-2.05x 0.97-1.68x
8 product chains 200-1000 0.83-1.19x 0.63-1.03x
one product 128-1000 0.29-1.02x 0.23-0.39x

FPE solver rebuild (71 × 71 complex solves and products, i7-8550U): numpy2x 3018 ms → 1829 ms (NumPy 3394 ms).

With its fixed two workers numpy2x uses fewer cores than OpenBLAS's default on large matrices: many small or medium linalg calls gain, large products and inverses lose. The per-op C path is unchanged; the Python side adds one _blas.single() call per linalg call.

Tests

pytest python/tests: 6950 passed, 6 skipped, 6 xfailed (Linux, Release).


Generated by Claude Code

…y 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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JT2yHjSFdbWf5ym7Dc5MAU
@hokb
hokb merged commit d167ad4 into main Nov 12, 2026
5 checks passed
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.

2 participants