Repository navigation
madmatrix: wavefunctions in GPU shared memory, SHAREDWF=auto by spill - #491
Conversation
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>
2733c7f to
8935706
Compare
Qubitol
left a comment
There was a problem hiding this comment.
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_writertests andtest_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>
|
@Qubitol Thanks, good catch. Fixed in c6b083e.
On sm75, |
|
Good job, thanks. |
Smaller arrays, no barrier there: L40S FP64 ttggg 0.89x, ttgQQ 0.985x. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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=autobuild gate, benchmarks and this summary by Claude (Claude Code, Opus 5.5).Why
In the large subprocesses
calculate_jampssits 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_jampspoints 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()inmodel_handling.py). While writingEvaluateDiagrams.incthe 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 addsA 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 forg g > t t~ g g).How many, at compile time (
sharedWfCount()inbackend/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 isnp4momenta +nw6complex amplitudes; the record is padded to an odd multiple of 16 B so neighbouring threads start in different banks.dfvFP64 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 emptyasm volatilememory 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:Without shared slots (
SHAREDWF=0, the registers-only side ofauto, FP64 on L40S) there is nothing to reload, and the clobber would only block scheduling.g g > t t~ g g ghas 1890 of them, which made cicc 3× slower for nothing.The local wavefunction arrays of
calculate_jampsare sized bynWfSlotsinstead ofnwf. The generator setsnWfSlotsto the highestaloha_objindex inEvaluateDiagrams.inc+ 1. Slots are reused, sonwfcounts more wavefunctions than there are slots, and the unused ones still cost local memory and stores.Gate at compile time:
SHAREDWF=0|1|autoShared memory pays only where the kernel spills without it. Per subprocess, without shared memory:
(throughput shared / registers only, L40S
dleft out since it has 0 slots.)So
SHAREDWF=auto(default; CUDA only,0definesMADMATRIX_NO_SHARED_WF) builds on theFASTMATH=autocheck of #486. Per subprocess it:SigmaKin.ccfour times in parallel: shared memory off/on ×-fmad=true/-use_fast_math-use_fast_mathif thecalculate_jampsstack grows by ≤FASTMATH_STACK_GROWTH= 128 B)SHAREDWF_STACK= 128 BThe choice is printed and saved in
build.cuda/SigmaKin.sharedwf(fast math inSigmaKin.fastmath). As in #486 it measures the kernel just built, so it follows changes in code generation, nvcc or GPU architecture. WithFASTMATHorSHAREDWFfixed it needs 2 compilations, with both fixed 1.Benchmarks
pp > t t~ + 0-3jandpp > l+ l- + 0-3j,d/f/v, L40S and H100, all withFASTMATH=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:dfvdfvg q > l+ l- q q q~, H100f: 1.71×.tt~ +3jdcovers 2 of 3 subprocesses:g g > t t~ g g gin FP64 runs out of GPU memory at 1024×256.l+l- +3jv), and FP64 |M|² agrees to ≤ 1.6e-14.SHWF_RELOAD()was limited to objects with shared slots and before the arrays were sized bynWfSlots.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=autothe two sides may pick different fast-math modes; thenf/vdiffer 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=autoagainstSHAREDWF=1, bothFASTMATH=auto:SHAREDWF=1SHAREDWF=autoSigmaKin.ccis compiled 4× instead of 2×. The compilations run in parallel, so the wall time grows little while free cores are available; CPU time forSigmaKin.ccdoubles, 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
SHWF_RELOAD()is empty there).-DMADMATRIX_NO_SHARED_WF.sharedWfOptinBytes()(e.g. Turing 64 KB, Blackwell 227 KB) come from the CUDA programming guide and are untested. Unknown architectures fall back to 48 KB.dthe log says it keeps shared memory where the stack spills, but with 0 slots that object is identical to the one without.