Repository navigation
Conversation
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]>
|
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:
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. |
…ev0/OpenBLAS#8, cakedev0/OpenBLAS#11, cakedev0/OpenBLAS#10 (he's interested) Co-Authored-By: Claude Opus 5.5 <[email protected]>
| } | ||
| queue[0].sa = sa; | ||
| queue[0].sb = sb; | ||
| exec_blas(count, queue); |
There was a problem hiding this comment.
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 makelevel3.cuse them when they are set instead of starting its own. - B: leave it, and say in the
openblas_cancelcomment ofcblas.hthat a gemm with a small C and a long k may not stop early.
I'd go for A. WDYT?
X.T @ XandX.T @ Ywith 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:
X.T @ XEPYC 9555P, 48 vCPU cloud VM, same conditions:
X.T @ Xf32 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.candinterface/syrk.crun one single-threaded driver call per k chunk throughexec_blas. Chunk 0 writes C with the user's beta, the others write private buffers with beta = 0. A secondexec_blasthen adds the buffers to C with the axpy kernel, split over columns. The shared code is ininterface/splitk.h, and all its state is in one heap block. Real types only, not withUSE_SIMPLE_THREADED_LEVEL3.Chunk count:
mallocmaps fresh pages on every call and the page faults cost more than the split gains,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 / callerschunks. 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):
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_blasruns 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:
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.ccompares 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
XDOUBLE, the bf16/f16 gemms, gemm3m.Benchmarks and scripts: LOG.md, exp 24-29 in probabl-ai/scikit-learn-benchmarks,
scripts/run_splitk.sh.🤖 Generated with Claude Code