Skip to content

madmatrix GPU: size wavefunction arrays by slots used, stop big fast-math compiles - #511

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

stloufra merged 6 commits into
feat-madmatrix-shared-wffrom
feat-madmatrix-wf-slots

Conversation

@stloufra

@stloufra stloufra commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #491 (shared-memory wavefunctions), which is stacked on #486 (FASTMATH=auto).

calculate_jamps of g g > t t~ g g g (1240 diagrams, 1890 amplitudes) spills ~40 KB per thread and takes 20–40 min to compile with #491. This PR makes it faster at run time and cheaper to compile. The one exception is FP64 on GPUs that give it no shared-memory slots (L40S), see below.

Changes

  • Wavefunction arrays sized by the slots used. nwf counts wavefunction calls, but EvaluateDiagrams.inc reuses slots: ttggg indexes 121 of 391. The generator now emits nWfSlots (highest aloha_obj index + 1) in SharedWavefunctions.inc, and the GPU calculate_jamps sizes pvec_sv/w_sv/aloha_obj by it. Unused slots still cost local memory and stores.

  • SHWF_RELOAD only where a shared slot is read again. The compiler barrier after each amplitude is needed only when wavefunctions live in shared memory, and then only between two accesses of a shared slot. The generator now gives each barrier a mask, bit n−1 set when it is needed with nSharedWf = n (the one closest to the read is kept), and the macro is a constexpr no-op otherwise. Without shared slots none is left; ttggg f keeps 819 of 1890 on L40S and 1245 on H100. Each barrier costs cicc time: on L40S the kept ttggg f compilation goes from 858 to 259 s of cicc; on H100 cicc 1436 → 1302 s. |M|² is bitwise identical. The dropped barriers also no longer fence the spill code, so the scheduler places it differently: on L40S ttggg runs 1.010× in f and 0.958× in v (20 repetitions, 0.953 at 512×256), on H100 nothing changes.

  • Fast-math compile stopped for big FP32 kernels. With FASTMATH=auto, once a side's -fmad build spills more than FASTMATH_MAD_STACK (4096 B) for FPTYPE in FASTMATH_CUTOFF_FPTYPES (f v), its -use_fast_math nvcc is killed and -fmad is kept. Fast math only ever won below 1.2 KB (96 picks, L40S and H100), and with the smaller arrays its approximate division takes ptxas from minutes to over an hour on ttggg.

  • Variants predicted from the amplitude count. With SHAREDWF=auto/FASTMATH=auto and FPTYPE d/f/v, the picks follow the number of amplitudes in EvaluateDiagrams.inc and are the same on every GPU outside a band (234 picks on L40S, H100 and sm_70/80/100 builds, none missed): d without shared memory below 10, with it above 30, always -use_fast_math; f/v without shared memory and with fast math below 21, with shared memory above 130, -fmad above 300. Only the predicted variant is compiled there; inside the band the gate compiles and measures as before. AUTO_PREDICT=0 compiles all of them.

Run time

check_sa perf, ME call + sync, 10 interleaved repetitions, paired median, SHAREDWF=auto FASTMATH=auto on both sides. Throughput of this PR over #491:

H100 d H100 f H100 v L40S d L40S f L40S v
g g > t t~ g g g 1.15 1.23 1.28 0.89 † 1.24 ‡ 1.24 ‡
g g > t t~ + 0–2g 0.999–1.008 0.994–1.002 0.999–1.022 0.998–1.000 0.998–1.000 0.998–1.017
l+l- + 3j 1.002 1.002 1.001 1.000 1.001 1.000
W+W- + 3j (FD) 1.019 1.000 1.000 1.014 1.000 1.000

Correctness. Same SHAREDWF/FASTMATH picks as #491 in every subprocess. |M|² bitwise identical except ttggg (≤ 2e-6 f/v, ≤ 1.3e-14 d, reordered spills). Helicity and colour choices identical (uniform --rnd-hel/--rnd-col).

Compile time

Clean builds of g g > t t~ + 0–3g (make -j8, CUDA 13.1), the three builds of each FPTYPE side by side, wall / CPU seconds. #491 and this PR with SHAREDWF=auto FASTMATH=auto, main with its defaults.

main #491 this PR faster than #491 faster than main
H100 f 577 / 860 1650 / 5863 652 / 947 2.5× / 6.2× 0.88× / 0.91×
H100 v 606 / 882 1583 / 6038 570 / 872 2.8× / 6.9× 1.06× / 1.01×
H100 d 400 / 670 1301 / 5106 436 / 712 3.0× / 7.2× 0.92× / 0.94×
L40S f 493 / 717 1350 / 5023 386 / 625 3.5× / 8.0× 1.28× / 1.15×
L40S v 523 / 766 1323 / 4962 262 / 522 5.0× / 9.5× 2.0× / 1.47×
L40S d 387 / 608 1113 / 4498 200 / 432 5.6× / 10.4× 1.94× / 1.41×

The gated build is back at main's compile time: ttggg is compiled once, with fewer barriers and smaller arrays. Step by step (sizing and fast-math stop, then the prediction) in the comment below.

🤖 Generated with Claude Code

stloufra and others added 2 commits November 20, 2026 15:52
nwf counts calls, slots reused: ttggg 121 not 391, barrier only with shared slots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fast math never picked there, dense arrays make its ptxas take over an hour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@stloufra
stloufra added this pull request to stack #496 November 20, 2026 14:54
@stloufra

stloufra commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Compile time

Update: the numbers in this section are for 0b95a6d. The variant prediction (695991a) and the barrier masks (f96ceb9) are now in this PR, final numbers at the end.

Clean builds of g g > t t~ + 0–3g (make -j8, CUDA 13.1), the three builds of each FPTYPE side by side on the node (same contention), wall / CPU seconds. Main is 3371e16 with its defaults (one compilation, -use_fast_math). #491 and this PR use SHAREDWF=auto FASTMATH=auto.

main #491 this PR this PR / #491
H100 f 662 / 955 1779 / 6394 1165 / 1850 1.53× / 3.46× faster
H100 v 634 / 941 1624 / 6318 1164 / 2909 1.40× / 2.17×
H100 d 417 / 709 1459 / 5649 1297 / 3234 1.12× / 1.75×
L40S f 517 / 740 1338 / 5186 1070 / 2473 1.25× / 2.10×
L40S v 513 / 750 1420 / 5132 1068 / 2588 1.33× / 1.98×
L40S d 361 / 588 1089 / 4362 218 / 1063 5.0× / 4.1×

L40S run time, g g > t t~ + 0–3g

Same method as the H100 numbers above (512×256, 10 interleaved repetitions, paired median):

d f v
g g > t t~ g g g 0.89 at 256×256 (#491 fails at 512×256) 1.22 ± 0.006 1.30 ± 0.003
g g > t t~ + 0–2g 0.998–1.000 0.998–1.000 0.998–1.017
  • ttggg d with madmatrix: wavefunctions in GPU shared memory, SHAREDWF=auto by spill #491 needs 68 KB of stack per thread and stops with a CUDA error at 512×256 on L40S (it runs at 256×256). With this PR it needs 34 KB and runs, but FP64 has no shared slots on L40S and the smaller arrays give 5× more local loads, so it is 0.89× where both run.
  • |M|² bitwise identical except ttggg (≤ 1.96e-6 f/v, ≤ 1.25e-14 d), helicity and colour choices identical, same SHAREDWF/FASTMATH picks.

With the prediction and the barrier masks

Separate side-by-side run with the same method (main and #491 differ by up to 13% from the run above), wall / CPU seconds:

main #491 this PR
H100 f 577 / 860 1650 / 5863 652 / 947
H100 v 606 / 882 1583 / 6038 570 / 872
H100 d 400 / 670 1301 / 5106 436 / 712
L40S f 493 / 717 1350 / 5023 386 / 625
L40S v 523 / 766 1323 / 4962 262 / 522
L40S d 387 / 608 1113 / 4498 200 / 432
  • Prediction: outside a band of amplitude counts (d below 10 / above 30, f/v below 21 / above 130) only the predicted variant is compiled, so ttggg is compiled once. 234 picks on L40S, H100 and sm_70/80/100 builds, none missed; the band now includes g g > t t~ g g. Alone it halves the CPU time (H100 f 2755 → 1356 s) and moves wall time little, since the single ttggg compilation is the critical path.
  • Barrier masks: the remaining barriers cost the ttggg compilation most of its cicc time; keeping only the 819 (L40S f) / 1245 (H100 f) of 1890 that a later shared read needs takes the ttggg build from 960 to 379 s wall on L40S.
  • Together the gated build is at main's compile time: 0.88–1.06× main's speed on H100 (wall), 1.3–2.0× faster than main on L40S, 2.5–5.6× wall / 6.2–10.4× CPU faster than madmatrix: wavefunctions in GPU shared memory, SHAREDWF=auto by spill #491.

🤖 Generated with Claude Code

stloufra and others added 4 commits November 20, 2026 19:10
Smaller arrays, no barrier there: L40S FP64 ttggg 0.89x, ttgQQ 0.985x.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Compile time over L40S FP64 run time, FP64 not target there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Picks follow amplitude count, GPU-independent outside band, 234 picks none missed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Generator mask per barrier, bit per nSharedWf, constexpr no-op otherwise.

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

Copy link
Copy Markdown
Contributor Author

Merged into #486 (8add3b6) together with #491.

@stloufra stloufra closed this Nov 22, 2026
@stloufra stloufra reopened this Nov 22, 2026
@stloufra
stloufra merged commit f96ceb9 into main Nov 22, 2026
87 of 89 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.

1 participant