Skip to content

Use fewer threads for small dot, axpy, getrf and gesv - #8

Open
cakedev0 wants to merge 1 commit into
developfrom
thread-counts
Open

cakedev0 wants to merge 1 commit into
developfrom
thread-counts

Conversation

@cakedev0

@cakedev0 cakedev0 commented Nov 4, 2026 •

Copy link
Copy Markdown
Owner

ddot, daxpy, dgetrf and dgesv use every thread as soon as the problem passes a small fixed size (n > 10000 for dot/axpy). Past ~8-16 threads, waking and synchronizing them costs more than the work. On one socket of a Xeon 6 (86 cores), ddot with n = 1e5 takes 4 µs on 8 threads and 25 µs on 86. dgetrf 1000x1000 takes 43 ms on 86 threads and 8 ms on this PR's 8. This PR sizes the thread count by the problem instead:

  • ddot (x86_64 kernel) and axpy: min(T, max(8, n/25000)) threads (axpy: 200 kB of x per thread), and all T once that reaches a third of T. So nothing changes with 24 threads or fewer.
  • getrf: at most max(8, sqrt(m*n)/200) threads, but only when that is below a third of T, like dot/axpy. On hybrid CPUs, a count just above the number of P cores is slower than using all cores: on a Core Ultra 9 285, getrf 2000 at 24 threads was ~9% slower with 10 threads.
  • gesv: the same cap, for the factorization only. The solve splits over the right-hand sides, so it scales with them, and capping it made nrhs = 10n 2-2.4x slower.

It only changes thread counts, no kernels. Other architectures' dot kernels and sdot are unchanged.

Only calls above 24 threads change, so I measured two large CPUs: a Xeon 6787P and an EPYC 9555P. I'd still like to see an Arm server.

Results

Best time of 2 rounds, develop c2fc6cf vs this branch, both built with the conda-forge flags, threads pinned. One socket of a Xeon 6787P:

call threads develop this PR
ddot n=1e5 86 25 µs 4 µs
dgetrf 1000 64 50 ms 12 ms
dgetrf 4000 86 134 ms 61 ms
dgetrf 20000x500 86 50 ms 25 ms
dgesv 2000, 1 rhs 86 76 ms 18 ms
dgesv 2000, 2000 rhs 86 84 ms 27 ms
dgesv 2000, 20000 rhs 32 128 ms 124 ms
dgesv 2000, 20000 rhs 86 125 ms 68 ms
dgesv 1000, 10000 rhs 86 49 ms 14 ms

The gesv grid (n = 1000-2000, 1 to 20000 right-hand sides, 32-86 threads) is 1.03-5.68x faster, with no slower cell. saxpy n = 1e5 is 3.8x faster at 43 threads and 15x at 86, and n = 1e6 is 1.05-1.37x.

EPYC 9555P, a cloud VM with 48 of its 64 cores:

call threads develop this PR
ddot n=1e5 48 4.0 µs 2.1 µs
daxpy n=1e5 48 4.0 µs 2.3 µs
dgetrf 1000 48 17.2 ms 3.4 ms
dgetrf 2000 48 32.1 ms 11.8 ms
dgetrf 20000x500 48 30.0 ms 16.5 ms
dgesv 2000, 1 rhs 48 31.6 ms 10.2 ms
dgesv 1000, 1000 rhs 48 19.3 ms 5.4 ms
dgesv 1000, 10000 rhs 48 31.7 ms 17.8 ms
dgesv 2000, 20000 rhs 48 121 ms 98 ms

Nothing changes at 24 threads there (0.98-1.13x on these calls). Calls the cap doesn't reach, like getrf 4000 at 48 threads, stay within noise (0.97x). saxpy n = 1e5 is 1.7-2.2x faster at 32-48 threads.

zgesv also gets the cap, through the same file (complex getrf is a different file and doesn't). On a previous version with all right-hand sides capped, zgesv with one right-hand side was up to 9x faster at n = 500, and 1.06-1.84x at n = 2000.

On the previous version, including the symv change of #11, scikit-learn 1.9.1 / scipy / numpy from conda-forge with this library swapped in, 43-86 threads:

  • Lasso 20000x100 (20 epochs): 56-94 → 13-19 ms.
  • Lasso 100000x1000, X larger than L3: 183-268 → 113-119 ms.
  • scipy.linalg.solve 1000: 22-55 → 10 ms; solve 4000 at 64-86 threads: 401-414 → 309-316 ms.

8-24 threads are unchanged.

Checks

Checks against a 1-thread call pass (relative difference < 1e-10) on the previous version. On the final code, the exact gemm/syrk checks pass on the laptop and on the EPYC VM with this PR merged, which only shows nothing else broke: these routines have no new test here.

Known caveats

  • On the Xeon at 86 threads, a call that still uses all threads measures slower when it runs right after one that used few: ddot 1e6 16 → 36 µs after daxpy 5e5 on 20 threads. Measured alone or for 2 s, they match develop. The socket has Efficiency Latency Control on: the uncore runs at full speed only when >95% of the cores are busy. Develop's idle threads spin on every core, while with this PR most of them sleep, and the uncore takes a few hundred ms to come back up.
  • The cap only below a third of T gives up some Xeon gains that a lower threshold had, e.g. getrf 4000 at 43 threads (71 vs 51 ms) and getrf 8000 at 86 threads (434 vs 290 ms). I preferred the same rule as dot/axpy and no change on small machines.
  • On the Xeon, OpenBLAS develop sometimes stalls for seconds when T threads are pinned to exactly T cores: Linux leaves two threads on one core and every threaded call waits ~3 ms (e.g. gesv 4000x100 2 s instead of 62 ms). The EPYC VM hits it too at 48 threads on 48 vCPUs. Both builds hit it at random, so the tables take the best of the rounds. It is unrelated to this PR.

Code and data: probabl-ai/scikit-learn-benchmarks, branch gemm-experiments (LOG.md exp20-23, scripts/run_tc.sh, scripts/run_sklearn_e2e.sh).

🤖 Generated with Claude Code

These use every thread as soon as the problem passes a small fixed size. On
one socket of a Xeon 6 (86 cores), past ~8-16 threads, waking and
synchronizing them costs more than the work: ddot with n = 1e5 takes 4 us on 8
threads and 26 us on 64, dgetrf 1000 takes 5 ms on 8 threads and 44 ms on 64.

Size the thread count by the problem, and only when the result is below a third
of the threads: busying every core on Xeon 6 raises the uncore frequency, and on
hybrid CPUs a count just above the number of P cores is slower than all of them.

gesv only caps the factorization, since the solve scales with the number of
right-hand sides.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cakedev0 cakedev0 changed the title Use fewer threads for small dot, axpy, symv, getrf and gesv Use fewer threads for small dot, axpy, getrf and gesv Nov 6, 2026
@cakedev0
cakedev0 marked this pull request as ready for review November 6, 2026 23:54
@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
ddot 1e5 0.96x 1.01x 0.98x 1.04x 1.28x 2.0x 5.7x
daxpy 1e5 1.26x 0.95x 0.99x 1.95x 1.35x 2.4x 3.0x
daxpy 1e6 0.99x 1.16x 1.01x 1.00x 1.03x 1.03x 0.47x
dgetrf 1000 0.99x 0.99x 1.01x 3.8x 1.93x 7.8x 6.2x
dgetrf 4000 0.99x 1.06x 1.00x 0.81x 1.01x 2.6x 1.91x
dgetrf 20000 x 500 0.99x 1.06x 1.00x 1.04x 1.01x 2.4x 1.77x
dgesv 2000, 1 rhs 0.96x 0.99x 1.00x 1.36x 2.2x 4.8x 2.9x
dgesv 1000, 10000 rhs 1.00x 1.09x 1.00x 1.53x 1.10x 3.7x 3.6x
dgesv 2000, 20000 rhs 1.00x 1.03x 1.00x 1.05x 1.09x 1.69x 1.84x
whole grid 0.96-1.26x 0.95-1.16x 0.98-1.01x 0.81-3.8x 1.01-2.2x 0.99-7.8x 0.46-6.2x

getrf loses on Graviton4. On the 64-core one, dgetrf 2000 at 32 threads is 0.71x (340 -> 242 GF/s, also alone in its process), dgesv 2000 the same, and dgetrf 4000 at 64 threads 0.81x. getrf scales further there than on the x86 machines. Develop at different thread counts, GFLOP/s:

dgetrf 10 16 20 24 32 48 64
2000 241 315 338 350 341 269 174
4000 271 369 432 476 555 607 529

The cap gives 10 threads for n = 2000 and 20 for n = 4000, where the best counts are 24 and 48. So sqrt(m*n)/200 is tuned for x86. sqrt(m*n)/100 would fit Graviton4 better, but I haven't checked it on the x86 machines.

Other notes:

  • Nothing changes at 24 threads or fewer, as intended (0.93-1.26x, the extremes are noisy daxpy cells).
  • ddot doesn't change on Graviton4, as expected (only the x86_64 kernel is patched). daxpy 1e5 is 1.95x at 64 threads.
  • On the Xeon at 86 threads, ddot and daxpy 1e6 are 0.46x in the grid, where they run right after the small calls. Alone in their process they match develop (117-124 vs 121-124 GF/s for ddot): the uncore caveat above.

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 for reference. "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