Skip to content

Use fewer threads in threaded syrk instead of falling back to one - #3

Open
cakedev0 wants to merge 2 commits into
developfrom
syrk-fewer-threads
Open

cakedev0 wants to merge 2 commits into
developfrom
syrk-fewer-threads

Conversation

@cakedev0

@cakedev0 cakedev0 commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Threaded syrk falls back to the single-threaded kernel when n < nthreads * switch_ratio. With many threads that turns a parallel call into a one-core call: X.T @ X with 990 features on one socket of a Xeon 6787P (86 cores) runs at ~980 GFLOP/s with 60 threads and at 81 GFLOP/s from 62 threads up.

In that case this PR uses min(n / switch_ratio, n * n * k / 3.5e6) threads instead, like the gemm driver lowers its thread count, and keeps one thread when that leaves fewer than 4 threads (8 in single precision). A few threads are slower than one on small calls (dsyrk 64 x 2000: 46 GFLOP/s on one thread, 21 on four), more so in single precision, where one thread is about twice as fast. If another caller holds the level 3 lock, it also keeps one thread (see "Concurrent callers").

Calls that develop already runs threaded are unchanged. This driver also serves complex syrk/herk and the trailing updates of potrf, so those change too, and they gain.

Impact

Median GFLOP/s, threads pinned to cores (except on macOS), conda-forge build flags:

machine syrk n x k threads develop this PR
Xeon 6787P, 1 socket d 990 x 463810 86 82 895
s 990 x 463810 86 166 1519
d 300 x 200000 43 65 283
d 128 x 1000 86 62 129
s 128 x 5000 86 110 217
Core Ultra X7 358H (16 cores) d 96 x 5000 16 40 148
s 96 x 20000 16 84 200
d 32 x 20000 8 26 44
Core Ultra 9 285 (8P + 16E) d 160 x 20000 24 59 299
s 160 x 5000 24 122 520
d 64 x 20000 12 48 111
s 96 x 20000 16 105 338
EPYC 9555P, 48 vCPU cloud VM d 700 x 5000 48 102 872
s 700 x 5000 48 236 1909
d 256 x 20000 48 86 412
s 256 x 5000 48 206 922
d 128 x 1000 48 79 191

The laptop and EPYC rows are from short runs of the final code (2 rounds, best time) on develop c2fc6cf. Laptop cells that run develop's unchanged code moved 0.86-1.38x in that run, so read them as rough, and EPYC cells on that path moved 0.82-1.08x. The other rows are from the previous version on develop b8d14a6. The concurrency check below is the only change on these paths since then, and it doesn't apply to a single caller.

I compared develop and this PR on a grid of n from 32 to 512 and k from 1000 to 100000 (plus a few large shapes), f32 and f64, at 4 to 86 threads on the Xeon, 2 to 16 on the laptop and 2 to 24 on the Core Ultra 9 285 (threads fill the P cores first, so 12 to 24 threads include E cores). Where the PR keeps one thread the result matches develop, and no case is slower beyond run-to-run noise (a rerun of the few 0.86-0.95x cells with the order reversed gave 0.97-1.21x; they all run develop's code path). On the M2 (static VORTEX build, which uses the default switch_ratio of 2), the fallback only happens for n < 2 * nthreads, so the only affected calls are X.T @ X with fewer than 16 features. They now use 4-7 threads instead of one at about the same speed (memory-bound): 8 to 15 columns at 8 threads measured 0.93-0.99x on the final code, within noise on this unpinned machine. Everything else is unchanged beyond noise.

The worst case I found is on the EPYC VM, where one thread is fast: d 100 x 20000 runs on 6 threads and moves between 0.88x and 1.14x from round to round, with the same thread count in both modes, so I think it depends on thread placement. d 64 x 5000 moves between 0.97x and 1.55x, and d 96 x 5000 between 1.10x and 1.73x.

Other callers of this driver, on one socket of the Xeon:

  • zsyrk and zherk, n = 64-120, k = 5000-20000: 2.1-3.6x faster at 16 threads and 1.6-2.5x at 86. Shapes that stay on one thread (32 x 20000, 96 x 1000) don't change. On the EPYC VM, up to 3.3x at 16 threads and 3.5x at 48.
  • dpotrf at 64-86 threads: n = 4000 is 1.32x faster, n = 2000 1.16-1.18x, n = 8000 1.03-1.11x. n = 500 stays within noise. This run also had the split-K and thread-count PRs merged, which don't touch potrf's path. On the EPYC VM with this PR alone, 48 threads, n = 500 to 8000: 0.96-1.10x.

The work threshold isn't scaled for complex types, so some complex calls stay on one thread where more would help: on the laptop, zherk 96 x 1000 takes ~0.75 ms at 16 threads and ~0.2 ms at 12, where develop's path applies. I left that out to keep this change small.

Concurrent callers

A threaded syrk holds the level 3 lock for the whole call. Develop runs these small calls on one thread per caller, so they run side by side. Simply threading them made callers take turns: with 8 callers, 64 x 5000 dropped to 0.19x of develop's throughput at 86 threads.

So this PR adds blas_level3_thread_try_enter and keeps one thread when the lock is taken. One round on one socket of the Xeon, throughput against develop:

syrk n x k threads 1 caller 8 callers 8 callers, without the check
d 64 x 5000 86 1.42x 1.08x 0.19x
d 64 x 5000 16 1.50x 1.11x 0.18x
d 100 x 20000 86 1.19x 0.95x 0.16x
d 100 x 20000 16 1.25x 1.07x 0.15x
d 32 x 20000 16 1.00x 0.98x 0.99x

On the EPYC VM, 8 callers stay at 0.98-1.04x at both 16 and 48 threads.

Thread count changes

Changing the thread count from one call to the next costs little. Cycles of syrk calls that alternate between thread counts (e.g. n = 32 and n = 160 with k = 2000: 1 and 10 threads at 86) are as fast as or faster than develop with pthreads, LLVM libomp and GNU libgomp. The worst case is that shortest cycle (0.37 ms): waking the threads after the single-threaded call adds 0.03-0.1 ms, and the cycle is still 2.4x faster than on develop, where both calls run on one thread.

Checks

utest/test_syrk_threads.c compares dsyrk and ssyrk at 16 threads to a naive reference, both triangles, N and T, beta 0 over NaN and beta 0.5. Inputs are small integers, so results must match exactly. It passes with pthreads and OpenMP, and fails when I inject a bug in the driver (k - 1).

I also ran a larger exact check against naive references, not included here: real and complex syrk/herk, Upper and Lower, row and column major, padded lda and ldc, alpha != 1, beta 0 over NaN, writes outside the triangle or the padding, at 8-86 threads on the Xeon, 8-16 on the laptop and 8-48 on the EPYC VM, including 4 concurrent callers. Zero failures. The EPYC run also passes the unit test.

This PR sends more calls through the threaded syrk driver, which is the code in OpenMathLib#6224 (corruption in threaded dsyrk). As I read that issue, it's considered fixed by OpenMathLib#6105. My exact checks show nothing with or without this PR, but I didn't run that issue's reproducer.

Not tested: the CMake build (the only CMake change is one test file in a list), and Arm servers. The Core Ultra 9 285 wasn't rerun on the final code, and the EPYC is a VM with 48 of its 64 cores.

Side note

Under DYNAMIC_ARCH the threading drivers use the single-precision SWITCH_RATIO for double too (the gotoblas table is filled in a file compiled without DOUBLE), which is why the Xeon cliff is at 16 columns per thread and not 8. I looked at fixing that separately (#4), but on this Xeon the double-precision values make small dgemm 2-2.5x slower at 64-86 threads, so it needs more work first.

Data and scripts: https://github.com/probabl-ai/scikit-learn-benchmarks/blob/gemm-experiments/gemm-experiments/LOG.md (exps 15-19).

🤖 Generated with Claude Code

cakedev0 added a commit to cakedev0/my-scikit-learn-backlog that referenced this pull request Nov 2, 2026
done: Blog: [blog#231](scikit-learn/blog#231) (match Substack's font size), merged
todo: move [blog#228](scikit-learn/blog#228) from Waiting to Now: address lorentzenchr's review and answer jjerphan on the OpenBLAS threading layers
todo: move [sklearn#35181](scikit-learn/scikit-learn#35181) from Now to Waiting, adam2392's and antoinebaker's questions answered
add: Maintenance: follow up on [sklearn#35254](scikit-learn/scikit-learn#35254): settle option B with dherrera1911 and betatim (LDA solvers disagree with custom priors)
add: Maintenance: follow up on [sklearn#35209](scikit-learn/scikit-learn#35209): newton-cg LogisticRegressionCV test fails on the macOS x86_64 wheels
add: Maintenance: re-review [sklearn#34619](scikit-learn/scikit-learn#34619) (faster PCA mle; jberg5 addressed my comments)
add: Benchmarks: review [benchmarks#142](probabl-ai/scikit-learn-benchmarks#142) (cuML config) with the results of the AWS L4 run
add: Upstream: upstream to OpenBLAS or close [cakedev0/OpenBLAS#3](cakedev0/OpenBLAS#3) and [cakedev0/OpenBLAS#4](cakedev0/OpenBLAS#4)
todo: since dates on [sklearn#35062](scikit-learn/scikit-learn#35062) and [sklearn#35053](scikit-learn/scikit-learn#35053), and virchan's question on the latter
todo: lorentzenchr's note on [sklearn#35029](scikit-learn/scikit-learn#35029) in the fork#38 line

Co-Authored-By: Claude Opus 5.5 <[email protected]>
cakedev0 and others added 2 commits November 6, 2026 21:02
For threaded paths that can do something useful instead of waiting.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
When n < nthreads * switch_ratio, level3_syrk_threaded.c ran the
single-threaded kernel, so for example dsyrk with n = 990 dropped from
~980 to ~80 GFLOP/s between 60 and 62 threads on a Xeon 6787P. Use
n / switch_ratio threads instead, capped so that each thread gets enough
work, and keep one thread when that leaves fewer than 4 threads (8 in
single precision), where a few threads are slower than one.

If another caller holds the level 3 lock, also keep one thread: waiting
for the lock would make concurrent callers take turns on calls that
develop runs side by side.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cakedev0

Copy link
Copy Markdown
Owner Author

I ran this PR against develop c2fc6cf on more machines, including Arm servers. Speed-up in GFLOP/s (median over 3 interleaved rounds, PR / develop), all cores of each machine, threads pinned, conda-forge build flags:

call Core Ultra X7 358H 16c Core Ultra 9 285 24c Graviton4 16c Graviton4 64c EPYC Milan 16c/32t EPYC Turin 64c Xeon 6787P 86c
dsyrk 64 x 20000 2.1x 2.1x 1.63x 1.48x 1.35x 1.22x 1.58x
dsyrk 128 x 20000 0.79x 4.0x 1.98x 1.96x 2.9x 1.79x 2.5x
dsyrk 256 x 20000 0.94x 1.06x 1.00x 3.8x 0.97x 4.5x 4.9x
dsyrk 512 x 20000 1.03x 1.00x 1.00x 6.5x 1.02x 7.5x 8.3x
dsyrk 990 x 100000 1.00x 1.01x 1.00x 11.8x 1.01x 11.3x 10.5x
ssyrk 128 x 20000 1.00x 3.8x 2.4x 2.8x 3.1x 1.33x 2.1x
ssyrk 512 x 20000 1.00x 0.99x 1.00x 9.9x 0.99x 6.3x 7.5x
whole grid 0.77-2.2x 0.82-4.0x 0.98-2.4x 0.96-16.1x 0.83-3.1x 0.99-11.3x 0.97-10.6x
  • Slower: dsyrk with 32 columns on the Milan VM, 0.83-0.86x at 16 and 32 threads, also alone in its process (27-29 -> 23-24 GF/s). The few threads this PR gives it are slower than one there. Every other cell on the servers is at 0.95x or more.
  • On the two Core Ultra machines, some cells on develop's path drop to 0.77-0.9x in the grid: shapes timed after the faster ones find the turbo budget spent (see the power limit note in Split k between threads in gemm and syrk when C is small #10). Timed alone in their process, the ones I rechecked match develop. For example dsyrk 128 x 20000 on the X7: 0.79x in the grid, 192-201 GF/s on both builds alone.
  • In the X7 grid, one cell per build stalled at ~0.35 s per call (develop on ssyrk 128 x 5000, this PR on ssyrk 64 x 20000), the same in all rounds. Alone in their process neither stalls, and ssyrk 64 x 20000 is 1.9-2.7x faster with this PR. I left these two cells out of the range.
  • On the 16-core machines only the smallest n (64 to 128, depending on the machine) changes: develop already threads the larger calls there.
  • The utests pass on Graviton4, including syrk_threads.

Machines: the two Intel client machines from the description, AWS Graviton4 (Neoverse V2) m8g.4xlarge and m8g.16xlarge, EPYC 7R13 Milan m6a.8xlarge (2 NUMA nodes), EPYC 9R45 Turin m8a.16xlarge (no SMT, OpenBLAS picks its Cooperlake kernels there), and one socket of the Xeon 6787P. "whole grid" is the range over every shape of the run at that thread count. Data and scripts: LOG.md, exp 31.

cakedev0 added a commit to cakedev0/my-scikit-learn-backlog that referenced this pull request Nov 25, 2026
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