Skip to content

madmatrix: wavefunctions in GPU shared memory, SHAREDWF=auto by spill - #491

Merged
stloufra merged 6 commits into
feat-madmatrix-fast_mathfrom
feat-madmatrix-shared-wf
Nov 22, 2026
Merged

stloufra merged 6 commits into
feat-madmatrix-fast_mathfrom
feat-madmatrix-shared-wf

Conversation

@stloufra

@stloufra stloufra commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR

Previously I tried recomputing the wavefunctions recursively from the momenta instead of keeping them, which did not pay off. @oliviermattelaer suggested putting them in shared memory, which gave me the idea to "play compiler": I designed a score that picks the hottest wavefunctions and keeps them in shared memory instead of letting them spill to local memory. We pay in compile time, since the build compares the spills with shared memory on and off, but the matrix-element kernel runs up to 1.7× faster. For gridpacks the compile cost is paid once and should not matter.

Stacked on #486 (FASTMATH=auto): the gate reuses its stack check, so review and merge that one first.

Claude summary

Prototypes, generator integration, the SHAREDWF=auto build gate, benchmarks and this summary by Claude (Claude Code, Opus 5.5).

Why

In the large subprocesses calculate_jamps sits at the 255-register cap. ptxas then spills to local memory, which is per-thread global memory: L1 caches it only while it fits, and with 256 threads per block and a 0.2–45 kB stack per thread it does not. ptxas picks the values to spill from the live ranges after scheduling, and the cache picks what stays on chip. Neither knows which wavefunctions the diagrams reuse.

Principle

Shared memory sits in the same on-chip SRAM as L1, but the program decides what lives there and nothing evicts it. So the generator decides which wavefunction slots are worth keeping on chip, and calculate_jamps points those slots (wavefunction and its momentum) at a per-thread record in dynamic shared memory instead of the local arrays.

Score, at generation time (shared_wf_order() in model_handling.py). While writing EvaluateDiagrams.inc the generator records every call: the slots it reads and the slot it writes. Slots are reused, so each slot holds a sequence of wavefunctions. Every value it holds adds

score(slot) += reads × (line of last read − line of write)

A wavefunction that is read often over a long stretch blocks registers longest, which is what spills. Slots are sorted by score into SharedWavefunctions.inc (sharedWfSlots[], e.g. 26 slots for g g > t t~ g g).

How many, at compile time (sharedWfCount() in backend/gpu/SigmaKin.cc). Per architecture, the first N slots go to shared memory while 256 threads × record fits into the opt-in shared memory. One slot is np4 momenta + nw6 complex amplitudes; the record is padded to an odd multiple of 16 B so neighbouring threads start in different banks.

bytes / slot L40S (99 KB) A100 (163 KB) H100 (227 KB)
d 96 0 6 9
f 48 7 13 18
v 64 5 9 13

FP64 on parts with < 128 KB gets 0 slots: on L40S it lost 8% (g g > t t~ g g), because the shared carve-out shrinks L1.

SHWF_RELOAD() (an empty asm volatile memory clobber) follows every amplitude. Without it the compiler keeps copies of the shared values in registers too and the spill comes back. It is emitted only when the object has shared slots:

#define SHWF_RELOAD() \
  if constexpr( nSharedWf > 0 ) asm volatile( "" ::: "memory" )

Without shared slots (SHAREDWF=0, the registers-only side of auto, FP64 on L40S) there is nothing to reload, and the clobber would only block scheduling. g g > t t~ g g g has 1890 of them, which made cicc 3× slower for nothing.

The local wavefunction arrays of calculate_jamps are sized by nWfSlots instead of nwf. The generator sets nWfSlots to the highest aloha_obj index in EvaluateDiagrams.inc + 1. Slots are reused, so nwf counts more wavefunctions than there are slots, and the unused ones still cost local memory and stores.

Gate at compile time: SHAREDWF=0|1|auto

Shared memory pays only where the kernel spills without it. Per subprocess, without shared memory:

GPU stack ≤ 128 B (no spill) stack > 128 B (spill)
L40S 0.91–1.00, median 0.98 (n 19) 0.99–1.45, median 1.10 (n 13)
H100 0.81–1.05, median 0.94 (n 26) 1.03–1.71, median 1.12 (n 21)

(throughput shared / registers only, L40S d left out since it has 0 slots.)

So SHAREDWF=auto (default; CUDA only, 0 defines MADMATRIX_NO_SHARED_WF) builds on the FASTMATH=auto check of #486. Per subprocess it:

  1. compiles SigmaKin.cc four times in parallel: shared memory off/on × -fmad=true/-use_fast_math
  2. on each side, picks the fast-math mode by the madmatrix GPU: FASTMATH=auto and SHAREDWF=auto, per-subprocess gates against calculate_jamps spills #486 rule (keep -use_fast_math if the calculate_jamps stack grows by ≤ FASTMATH_STACK_GROWTH = 128 B)
  3. keeps shared memory if the stack of the picked object without it is > SHAREDWF_STACK = 128 B

The choice is printed and saved in build.cuda/SigmaKin.sharedwf (fast math in SigmaKin.fastmath). As in #486 it measures the kernel just built, so it follows changes in code generation, nvcc or GPU architecture. With FASTMATH or SHAREDWF fixed it needs 2 compilations, with both fixed 1.

Benchmarks

pp > t t~ + 0-3j and pp > l+ l- + 0-3j, d/f/v, L40S and H100, all with FASTMATH=auto. GPU time of the ME kernels summed over subprocesses, 10 interleaved repetitions, paired median ± 1.4826 MAD. 2048×256 blocks×threads (tt~ +3j: 1024×256).

Throughput of the gated build (SHAREDWF=auto) over shared memory off; in brackets, always on (SHAREDWF=1) over off:

L40S d L40S f L40S v H100 d H100 f H100 v
tt~ 1.00 (1.00) 1.00 (0.94) 1.00 (0.95) 1.00 (0.99) 1.00 (1.00) 1.00 (0.90)
tt~ +1j 1.00 (1.00) 1.00 (0.97) 1.00 (0.98) 1.03 (1.03) 1.00 (0.93) 1.00 (0.87)
tt~ +2j 1.00 (1.00) 1.25 (1.25) 1.16 (1.16) 1.42 (1.42) 1.25 (1.23) 1.11 (1.11)
tt~ +3j 1.00 (1.00) 1.05 (1.05) 1.06 (1.06) 1.27 (1.27) 1.06 (1.06) 1.05 (1.05)
l+l- 1.00 (1.00) 1.00 (0.98) 1.00 (0.98) 1.00 (0.81) 1.00 (0.89) 1.00 (1.00)
l+l- +1j 1.00 (1.00) 1.00 (0.98) 1.00 (0.95) 1.00 (0.93) 1.00 (1.00) 1.00 (0.93)
l+l- +2j 1.00 (1.00) 1.00 (0.99) 1.00 (0.97) 1.00 (0.96) 1.00 (0.91) 1.00 (0.90)
l+l- +3j 1.00 (1.00) 1.07 (1.07) 1.03 (1.03) 1.13 (1.13) 1.54 (1.54) 1.61 (1.61)
  • Spread ≤ 0.04 on all bold entries. The small H100 kernels (< 1 ms) are noisy, up to ±0.25, so their always-on losses are only indicative.
  • Per subprocess the largest gain is g q > l+ l- q q q~, H100 f: 1.71×.
  • tt~ +3j d covers 2 of 3 subprocesses: g g > t t~ g g g in FP64 runs out of GPU memory at 1024×256.
  • Geometric mean over all subprocesses, against off: L40S gated 1.035 vs always on 1.022, H100 gated 1.093 vs always on 1.053 (n 47 each).
  • A100 was benchmarked only together with GPU: warp-page AoSoA layout for multichannel numerators #484 and madmatrix GPU: FASTMATH=auto and SHAREDWF=auto, per-subprocess gates against calculate_jamps spills #486, against main without them (comment in #486). The whole UMAMI call is up to 1.93× faster (l+l- +3j v), and FP64 |M|² agrees to ≤ 1.6e-14.
  • The benchmarks and compile times here were measured before SHWF_RELOAD() was limited to objects with shared slots and before the arrays were sized by nWfSlots.

The gated numbers are composed from the off/on objects of the same night. A separate gated build of all 24 processes on both GPUs picked, in all 96 subprocesses, the side the rule says, with register count, stack and STL/LDL identical to the benchmarked object.

Correctness. With fast math fixed, |M|² of shared memory against registers only agrees to ≤ 1.3e-14 in FP64. Under FASTMATH=auto the two sides may pick different fast-math modes; then f/v differ by median 2e-7, max 3.9e-4.

Compile time

Clean build of pp > t t~ + 3j, v, the largest process (g g > t t~ g g g: 1240 diagrams, 45 kB stack without shared memory), SHAREDWF=auto against SHAREDWF=1, both FASTMATH=auto:

SHAREDWF=1 SHAREDWF=auto
L40S node 1221 s 1231 s +1%
H100 node 1396 s 1577 s +13%

SigmaKin.cc is compiled 4× instead of 2×. The compilations run in parallel, so the wall time grows little while free cores are available; CPU time for SigmaKin.cc doubles, and on a busy machine it shows up as wall time. nvcc spends the time in cicc (NVVM), so nothing is shared between the variants.

Limitations

stloufra and others added 4 commits November 19, 2026 13:43
Hot wf slots by generator score in GPU shared mem, kept only where calculate_jamps spills without.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
New macros use MADMATRIX prefix, no MGONGPU.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Shorter name, no MG prefix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Template is %-formatted, bare 15% broke output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@stloufra
stloufra force-pushed the feat-madmatrix-shared-wf branch from 2733c7f to 8935706 Compare November 19, 2026 12:43

@Qubitol Qubitol left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Recommended decision: Request changes. Reviewed 8935706db3 against 0aa4709fb5.

[P1] Use the correct shared-memory limit for Turing — backend/gpu/SigmaKin.cc:386–388

sharedWfOptinBytes() returns 99 KiB for SM 7.5, but Turing supports only 64 KiB per block. For g g > t t~ g g with FPTYPE=f, the sizing functions select seven slots and request 86,016 bytes (84 KiB). Consequently, SHAREDWF=1, or auto selecting shared memory, fails at cudaFuncSetAttribute() before launching calculate_jamps. This also affects the 16-thread helicity warm-up because the opt-in request always uses 256 threads. Please handle the supported architectures’ limits explicitly and use a conservative fallback for unknown ones.

Validation:

  • Passed all 29 test_madmatrix_writer tests and test_madmatrix_vs_fortran.
  • Confirmed the 84 KiB request using the generated process and a compiled harness containing the PR’s sizing functions.
  • CUDA/HIP execution and performance were not independently validated.

The mutex and static map in sharedWfBytes() are appropriate: they protect a persistent host-side cache of per-device initialization and compiled stride values.

AI-assisted: Codex, GPT-6.1 Sol, effort High

Discarded if constexpr still parsed on host HIP, __align__ undefined there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Turing 64 KB not 99 KB, unknown arch fall back to 48 KB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@stloufra

stloufra commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

@Qubitol Thanks, good catch. Fixed in c6b083e. sharedWfOptinBytes() now lists the opt-in limit of every supported architecture explicitly:

Architecture Shared Memory Limit per Block
sm70 / sm72 96 KB
sm75 64 KB
sm80 / sm87 163 KB
sm86 / sm89 / sm120 / sm121 99 KB
sm90 / sm100 / sm101 / sm103 / sm110 227 KB
Other architectures 48 KB (no opt-in required)

On sm75, g g > t t~ g g f now gets 5 slots (60 KB) instead of 7 (84 KB). The slot counts on the benchmarked GPUs (L40S, H100, A100) are unchanged.

@Qubitol

Qubitol commented Nov 20, 2026

Copy link
Copy Markdown
Member

Good job, thanks.
We can merge as soon as merge conflicts are fixed.

stloufra added a commit that referenced this pull request Nov 20, 2026
Smaller arrays, no barrier there: L40S FP64 ttggg 0.89x, ttgQQ 0.985x.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
stloufra added a commit that referenced this pull request Nov 22, 2026
SHAREDWF=auto, slot-sized arrays, predicted variants, barrier masks on top of FASTMATH=auto.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@stloufra
stloufra merged commit c6b083e into main Nov 22, 2026
89 of 90 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