Skip to content

Split k between threads in gemm and syrk when C is small - #10

Open
cakedev0 wants to merge 3 commits into
developfrom
splitk
Open

cakedev0 wants to merge 3 commits into
developfrom
splitk

Conversation

@cakedev0

@cakedev0 cakedev0 commented Nov 4, 2026 •

Copy link
Copy Markdown
Owner

X.T @ X and X.T @ Y with few columns and many rows are up to ~60x faster on many threads. OpenBLAS only splits gemm and syrk over the tiles of C. When C is small and k is long, most threads have nothing to do. This PR splits k instead in that case.

Impact

Intel Xeon 6787P, one socket (86 cores, threads pinned one per core), f64, GF/s, best of 2 rounds on the final code, develop c2fc6cf:

call shape (C, k) 16 threads: develop / this PR 86 threads: develop / this PR
syrk, X.T @ X 32x32, k = 1e6 22 / 377 23 / 1279
syrk 64x64, k = 2e5 37 / 621 37 / 2277
syrk 100x100, k = 1e6 44 / 724 44 / 2112
gemm NT 64x64, k = 2e5 117 / 673 110 / 2497
gemm NT 128x128, k = 5e4 345 / 877 312 / 3053
gemm TN 200x200, k = 5e4 615 / 1090 638 / 3202
gemm TN 100x100, k = 1e6 233 / 806 200 / 1931
gemm NT 10x200, k = 1e6 95 / 133 88 / 380
gemm NT 1000x1000, k = 1e5 1195 / 1195 1846 / 1795
gemm NN 2000x2000x2000 1387 / 1379 2720 / 2731

EPYC 9555P, 48 vCPU cloud VM, same conditions:

call shape (C, k) 16 threads: develop / this PR 48 threads: develop / this PR
syrk, X.T @ X 32x32, k = 1e6 35 / 530 35 / 893
syrk 64x64, k = 2e5 53 / 846 54 / 1146
syrk 100x100, k = 1e6 62 / 968 63 / 1483
gemm NT 64x64, k = 2e5 150 / 907 150 / 1360
gemm NT 128x128, k = 5e4 392 / 1191 432 / 1518
gemm TN 200x200, k = 5e4 672 / 1402 737 / 1722
gemm TN 100x100, k = 1e6 315 / 1123 313 / 1731
gemm NT 10x200, k = 1e6 183 / 302 183 / 463
gemm NT 1000x1000, k = 1e5 1449 / 1451 2064 / 2008
gemm NN 2000x2000x2000 1610 / 1623 2195 / 2231

f32 gains there are similar or larger (syrk up to 27x). Cells on develop's path (one chunk) moved 0.84-1.14x in this short run.

On an Apple M2 (4 P + 4 E cores, threads not pinned), final code, f64: syrk with 32-100 columns is 1.8-3.8x faster at 8 threads and 1.4-1.7x at 4, gemm with a 64x64 to 200x200 C 1.3-1.8x at 8 threads and 1.05-1.25x at 4.

On an Intel laptop (Core Ultra X7 358H, 16 cores: 4 P, 8 E, 4 LP-E), 16 threads, a shorter run: syrk with 32-100 columns 7.6-8.2x, gemm with a 64x64 to 200x200 C 1.4-3.1x.

On the previous version, f32 gains were similar or larger (up to ~70x), and 32x32 to 128x128 C with k = 5000 was 2-34x faster at 16-86 threads.

The syrk rows overlap with #3: develop's syrk runs on one thread when n is small. That PR gives it more threads, and this one still splits k, which is what helps when n is tiny. Both PRs start with the same commit, which adds blas_level3_thread_try_enter.

Shapes where C has enough tiles for every thread (the last two rows, and 512-1100 square C on the full grid) don't change: the split only triggers when each thread would get less than 128 x 128 of C.

How

When C is small and k is long, interface/gemm.c and interface/syrk.c run one single-threaded driver call per k chunk through exec_blas. Chunk 0 writes C with the user's beta, the others write private buffers with beta = 0. A second exec_blas then adds the buffers to C with the axpy kernel, split over columns. The shared code is in interface/splitk.h, and all its state is in one heap block. Real types only, not with USE_SIMPLE_THREADED_LEVEL3.

Chunk count:

  • one chunk per thread, chunks of at least 512 (256 when C is tiny, i.e. the M x N split would leave each thread under 64 x 64),
  • partial products under 24 MiB: above glibc's 32 MiB mmap threshold, malloc maps fresh pages on every call and the page faults cost more than the split gains,
  • with fewer chunks than threads, split only when C is tiny and there are at least half as many chunks as threads; otherwise keep the M x N split.

Concurrent callers

Calls that arrive together share the threads. gemm and syrk calls that may use threads are counted, and with pthreads a call splits k into nthreads / callers chunks. The split doesn't take the level 3 lock: the chunks run on the single-threaded drivers. The rounding of the result depends on the chunk count, so with pthreads the last bits can change with the number of concurrent callers: on an EPYC 7R13 at 32 threads, 350 calls of the same gemm (64x64, k = 2e5), 2 to 8 at a time, returned 7 different results, against 1 on develop.

One socket of the Xeon, each caller asking for 16 threads, all pinned to the same 16 cores, f64, throughput against develop (median of 3 rounds):

call shape (C, k) 1 caller 2 callers 4 callers 8 callers
gemm NT 64x64, k = 2e4 5.6x 4.1x 4.1x 3.5x
gemm NT 128x128, k = 5e3 2.2x 1.8x 2.0x 2.0x
syrk 100x100, k = 2e4 13.4x 5.1x 2.2x 1.25x
syrk 64x64, k = 5e3 9.3x 4.2x 2.3x 1.41x
syrk 32x32, k = 2e4 13.0x 5.6x 2.4x 1.39x

f32 is similar (syrk with 8 callers: 1.18-1.47x). With 86 threads on 86 cores and 8 callers, the same syrk shapes are at 2.6-3.8x and the two gemm shapes at 11x and 7.7x. On an EPYC 7543 (32 cores), syrk with 8 callers is at 1.43-1.51x at 16 threads and 1.11-1.24x at 32.

The first version of this PR took the level 3 lock without waiting, and fell back to the usual path when another caller held it. One caller then split over every thread while the others ran next to it, and the syrk rows above were at 0.43-0.44x with 8 callers.

OpenMP builds only split when a single gemm or syrk call is running, and take the lock as before. exec_blas runs one threaded call at a time there (MAX_PARALLEL_NUMBER = 1), so concurrent splits would take turns: sharing the threads without the lock was 0.03-0.2x on small syrk with 8 callers. With this rule, 2 to 8 callers are at 0.85-1.42x on the Xeon (GNU and LLVM runtimes, most cells 0.93-1.05x) and 0.93-1.10x on the EPYC 7543. One caller gains as with pthreads.

What is slower than develop, with pthreads, 16 threads on 16 cores of the Xeon:

  • One large gemm next to small syrk calls. With one 1000x1000x2000 gemm and 7 syrk 100x100, k = 2e4, syrk is at 0.88x (0.65-0.89 over rounds) and the gemm at 0.85x (0.59-1.26). The gemm takes all 16 threads whatever its share, and each syrk now uses 2. With 4 + 4 callers, syrk is at 0.80x and the gemm at 1.52x. With 1 + 1, syrk is at 2.4x and the gemm at 1.0x.
  • Callers that start at the same instant: the first ones see few callers and take too many chunks for one call. syrk 200x200, k = 1e5 with 8 callers is at 0.87x over a 0.5 s window and 0.98x over 4 s. When the 8 callers start 0.2 s apart (syrk 100x100, k = 2e4), it's 1.96x.
  • gemm TN 200x200, k = 5e4 with 8 callers: 0.95x (0.91-0.99) over 4 s windows.

Compared with the first version, long calls gain less with several callers at 86 threads, since they no longer get every thread: syrk 200x200, k = 1e5 with 8 callers goes from 7.0x to 4.2x.

Checks

utest/test_splitk.c compares gemm in all 4 transpose cases (row and column major) and syrk (both triangles, N and T) to a naive reference at 8 threads, with padded lda, ldb and ldc and beta 0 over NaN. Inputs are small integers, so results must match exactly, and the padding and the other triangle must stay untouched. It passes with pthreads and OpenMP on the laptop, and with pthreads on the EPYC VM and the M2. It fails when I inject a bug in the reduction, either dropping the last buffer or missing the diagonal.

I also ran a larger exact check, not included here, with more shapes around the trigger, alpha != 1, and 1, 4 and 8 concurrent callers: 8-86 threads on the Xeon, 8-16 on the laptop, 8-48 on the EPYC VM and 4-8 on the M2. Zero failures.

With the last commit (threads shared between callers), I reran the unit tests on the Xeon (pthreads and OpenMP), and a concurrent exact check with 1, 4 and 8 callers on random gemm and syrk calls: 8-86 threads on the Xeon (pthreads, OpenMP with the GNU and LLVM runtimes) and 8-32 on an EPYC 7543. Zero failures. The other machines only ran the previous commit.

Slowdowns

The rows above and the laptop shapes show none beyond noise on the final code. The full grid below is from the previous version, before the reduction moved to the axpy kernel, and I didn't rerun it: C = 32-1100 square, k = 5e3-1e6, gemm NT and syrk N, f32/f64, plus a few other shapes, on the Xeon (8-86 threads), the laptop (2-16 threads) and the desktop below. None beyond noise. Cells under 0.95x all take develop's path (one chunk) and move both ways between runs.

Compared with the previous commit (chunks of at least 512 in all cases), 32x32 C with k = 2e4 at 64-86 threads gains 9-11x instead of ~14x on the Xeon. I kept the simpler rule.

The first version used up to 64 MiB of buffers and was up to 2x slower than develop for 512x512 C with k = 2e4 (the mmap issue above). It also split 128x128x5000 into 9 chunks on 12-16 threads, which on the laptop's E cores made it 0.77x: a static split runs at the pace of the slowest core, which got 1/9 of the work instead of 1/12.

On an Intel desktop (Core Ultra 9 285, 8 P + 16 E cores), the split shapes are 1.3-12x faster. In a long benchmark run, the shapes that don't split looked up to 1.5x slower at 12-24 threads. That turned out to be the power limit: this CPU sustains 65 W over ~32 s, and with this PR the earlier small shapes run on all cores and spend the burst budget. Timed alone, they are the same as develop (2000^3 at 24 threads: 17-18 ms on both), and after ~40 s of all-core work develop gets just as slow (24-27 ms).

On the M2, the previous version lost at 6 threads (4 P + 2 E): syrk with 64-128 columns and k = 5e3-5e4 was 0.81-0.91x, since equal chunks run at the pace of the E cores. On the final code, a short run of those shapes gives 1.15-1.38x. I'm not sure why, and this machine is noisy unpinned, so I wouldn't count on it. The desktop numbers are also from the previous version.

Not covered

  • Complex types, XDOUBLE, the bf16/f16 gemms, gemm3m.
  • f64 syrk with 300 columns at 86 threads: the 24 MiB cap allows 35 chunks, under half the threads, so it keeps develop's path. Use fewer threads in threaded syrk instead of falling back to one #3 is what fixes that case, though 300 x 200000 still takes 100 ms at 86 threads against 10 ms at 43.
  • Not tested: Arm servers and Windows. The CMake build passes the unit tests on Linux x86 (EPYC 7R13, pthreads and OpenMP, without Fortran). The EPYC is a VM with 48 of its 64 cores.

Benchmarks and scripts: LOG.md, exp 24-29 in probabl-ai/scikit-learn-benchmarks, scripts/run_splitk.sh.

🤖 Generated with Claude Code

@cakedev0
cakedev0 marked this pull request as ready for review November 5, 2026 00:04
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]>
OpenBLAS splits gemm and syrk over the tiles of C, so X.T @ X and X.T @ Y
with few columns and many rows leave most threads idle. When each thread's
share of C is under 128 x 128 and k is long, run one single-threaded driver
call per k chunk and add the partial products with the axpy kernel.

Real types only, with buffers under 24 MiB (glibc maps larger blocks anew on
every call). If another caller holds the level 3 lock, use the usual path:
waiting would make concurrent callers take turns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With several callers, one split-K call held the level 3 lock and used every
thread while the others ran next to it on the usual path. Small syrk calls,
which develop runs side by side on one thread each, got up to ~2.5x slower.

Count the gemm and syrk calls that may use threads, and split k into
nthreads / callers chunks, without the level 3 lock: the chunks run on the
single-threaded drivers. OpenMP builds run MAX_PARALLEL_NUMBER threaded calls
at a time, so there a call only splits when no other one is running.

Co-Authored-By: Claude Fable 5.2 <[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 32 x 1e6 6.1x 8.1x 12.4x 48.4x 12.5x 51.7x 55.0x
dsyrk 64 x 2e5 5.4x 9.5x 11.2x 45.7x 15.6x 50.2x 56.0x
ssyrk 64 x 2e5 5.1x 6.9x 11.2x 45.2x 17.8x 37.0x 54.9x
dgemm NT 64x64, k = 2e5 2.2x 2.6x 4.8x 16.5x 5.3x 21.3x 22.4x
dgemm NT 128x128, k = 2e5 1.23x 1.63x 2.8x 9.0x 3.4x 8.2x 13.3x
dgemm TN 200x200, k = 5e4 1.27x 1.41x 1.10x 3.4x 1.65x 5.6x 4.9x
dgemm NT 10x200, k = 1e6 1.69x 1.37x 1.69x 5.2x 1.26x 4.8x 4.8x
dgemm NT 1000x1000, k = 1e5 (no split) 0.94x 0.92x 1.00x 1.02x 1.03x 0.98x 0.97x
dgemm 2000^3 (no split) 0.88x 0.89x 1.00x 1.00x 0.99x 1.00x 0.98x
whole grid 0.83-6.2x 0.77-11.0x 1.00-13.4x 1.00-51.9x 0.95-20.5x 0.98-53.2x 0.86-91.0x
  • Faster on every machine, up to ~50x for X.T @ X with 32-64 columns on 64 cores. On the servers, cells that don't split stay at 0.95-1.03x.
  • 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 on the Core Ultra 9 285 at 24 threads: dgemm NT 512x512, k = 20000, 711-730 vs 728-732 GF/s, and sgemm 2000^3 1648-1712 vs 1654-1694 GF/s.
  • One grid cell on the Xeon at 86 threads is 0.86x (sgemm NT 256x256, k = 20000, develop's path). Alone in its process it matches develop (650-970 vs 720-1000 GF/s).
  • The utests pass on Graviton4, including splitk.

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
Comment thread interface/splitk.h
}
queue[0].sa = sa;
queue[0].sb = sb;
exec_blas(count, queue);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cancellation doesn't reach the chunks. Claude found this while reviewing the PR, from reading the code. I didn't try a cancel.

openblas_cancel covers gemm. develop's threaded driver starts a generation on the calling thread and passes cancel_slot / cancel_gen to its workers (level3_thread.c, in gemm_driver). Here each chunk runs the single-threaded driver, which starts its own generation on the thread that runs it (openblas_cancel_begin() in level3.c). So a cancel aimed at the calling thread only stops chunk 0, and the call returns once the other chunks are done, about as late as without the cancel.

syrk is not affected: its drivers don't poll.

Two options:

  • A: start the generation in the split path, pass the slot and the generation in args, and make level3.c use them when they are set instead of starting its own.
  • B: leave it, and say in the openblas_cancel comment of cblas.h that a gemm with a small C and a long k may not stop early.

I'd go for A. WDYT?

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