Skip to content

madmatrix GPU: FASTMATH=auto and SHAREDWF=auto, per-subprocess gates against calculate_jamps spills - #486

Open
stloufra wants to merge 16 commits into
mainfrom
feat-madmatrix-fast_math
Open

stloufra wants to merge 16 commits into
mainfrom
feat-madmatrix-fast_math

Conversation

@stloufra

@stloufra stloufra commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Big calculate_jamps kernels sit at the 255-register cap and spill to local memory. Two opt-in, CUDA-only gates fight the spills per subprocess. Both measure the kernel just built, so nothing is tabulated per process, card or nvcc version. Defaults unchanged (FASTMATH=1, SHAREDWF=0).

Includes #491 and #511 (merged here).

Mechanisms

FASTMATH=auto. -use_fast_math can be slower than strict: approximate div/sqrt remove the scheduling barriers of the precise calls, more values stay live and the spill stack grows (g g > t t~ g g f: 1312 → 2080 B). The gain is all from -fmad, the losses all from approximate div/sqrt.

  • SigmaKin.cc built twice, -fmad=true and -use_fast_math.
  • Keep fast math if the calculate_jamps stack (cuobjdump -res-usage) grows by ≤ FASTMATH_STACK_GROWTH (128 B), else -fmad.

SHAREDWF=auto. Wavefunctions of calculate_jamps in shared memory instead of spilled local memory.

  • nSharedWf slots per thread, sized from the arch's shared-memory limit at compile time. Slots go to the wavefunctions with the most reads × lifetime.
  • Built without (sw0) and with (sw1) shared slots, each with its own fast-math pick. sw1 kept if the sw0 stack exceeds SHAREDWF_STACK (128 B).
  • Compiler barrier (SHWF_RELOAD) after an amplitude only where a shared slot is read again, else values stay in registers too. Per-barrier mask, constexpr no-op elsewhere: ttggg keeps 819–1245 of 1890.

Compile cost kept down.

  • Wavefunction arrays sized by slots used (ttggg: 121, not 391).
  • f/v: -use_fast_math build killed once -fmad spills > FASTMATH_MAD_STACK (4 KB). Fast math never won there.
  • AUTO_PREDICT=1: picks follow the amplitude count, the same on every GPU outside a band (234 picks on L40S, H100, sm_70/80/100, none missed). Outside it only the predicted variant is compiled: d no shared memory below 10 amplitudes, shared above 30, always fast math. f/v no shared memory and fast math below 21, shared above 130, -fmad above 300.

Picks printed and saved in build.cuda/SigmaKin.{fastmath,sharedwf}.

Run time

ME wall throughput over main before #484 (fabaad5; #484 since merged, included here). Time summed over subprocesses, 10 interleaved repetitions, paired median. Launch 2048×256, tt~ +3j 256×256, W+W- +3j 1024×256.

L40S d L40S f L40S v H100 d H100 f H100 v A100 d A100 f A100 v
tt~ +2j 1.01 1.48 1.78 1.48 1.58 1.31 1.42 1.40 1.27
tt~ +3j 0.95 1.40 1.47 1.23 1.33 1.46 1.06 1.11 1.16
l+l- +2j 1.00 1.01 1.01 0.99 0.99 1.01 0.99 0.99 1.01
l+l- +3j 1.00 1.30 1.33 1.18 1.72 1.90 1.19 1.76 1.93
W+W- +2j (FD) 1.00 1.31 1.20 1.53 1.02 0.98 1.32 1.03 0.98
W+W- +3j (FD) 1.04 1.76 1.62 1.20 1.06 1.07 1.11 1.08 1.10

Correctness. #511 against #491: same picks, |M|² bitwise identical except g g > t t~ g g g (≤ 2e-6 f/v, ≤ 1.3e-14 d, reordered spills), helicity and colour choices identical.

Compile time

Clean build of g g > t t~ + 0–3g, make -j8, CUDA 13.1, three builds side by side, single run. Main 3371e16 with defaults, this PR FASTMATH=auto SHAREDWF=auto. Wall / CPU s:

main this PR PR / main
H100 d 400 / 670 436 / 712 1.09× / 1.06×
H100 f 577 / 860 652 / 947 1.13× / 1.10×
H100 v 606 / 882 570 / 872 0.94× / 0.99×
L40S d 387 / 608 200 / 432 0.52× / 0.71×
L40S f 493 / 717 386 / 625 0.78× / 0.87×
L40S v 523 / 766 262 / 522 0.50× / 0.68×

Without #511 (#486 + #491) the same build was 2.5–3.3× wall, 6.5–7.6× CPU over main.

Top: run time over main. Bottom: compile time over main, wall filled, CPU open.

🤖 Generated with Claude Code

Fast math per subprocess, kept unless approx div/sqrt grow jamps spill stack.
@@ -352,7 +373,11 @@ else ifeq ($(BACKEND),hip)
GPUFLAGS += -target x86_64-linux-gnu -DHIP_PLATFORM=amd

# Fast math (is -DHIP_FAST_MATH equivalent to -ffast-math?)

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.

Would be nice to have a reply to this question?

@stloufra stloufra Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No. On AMD -DHIP_FAST_MATH is a no-op. We have discussed that in relation to Jorgens work some time ago. Changed.

# more than FASTMATH_STACK_GROWTH bytes (fast-math benchmarks on L40S and H100): then -fmad is kept.
ifeq ($(FASTMATH),auto)
FASTMATH_STACK_GROWTH ?= 128
override JAMPS_STACK = $(CUDA_HOME)/bin/cuobjdump -res-usage $(1) | awk '/Function .*calculate_jamps/ { getline; sub(/.*STACK:/, ""); if ($$1 + 0 > max) max = $$1 + 0 } END { print max + 0 }'

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.

This seems to be CUDA-related but it is not gated, is it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It was gated only indirectly, by the error near the top when FASTMATH=auto is used without BACKEND=cuda. Made it explicit in 0984bc7: the rule now sits under ifeq ($(FASTMATH)$(BACKEND),autocuda) and the backend check is written out plainly. auto stays opt-in, the default remains FASTMATH=1 on every backend.

stloufra and others added 6 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>
Discarded if constexpr still parsed on host HIP, __align__ undefined there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rule under autocuda, HIP_FAST_MATH comment answered (no-op on AMD).
@stloufra
stloufra force-pushed the feat-madmatrix-fast_math branch from 336a532 to 0984bc7 Compare November 19, 2026 15:31
@stloufra

stloufra commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Combined #484 + #486 + #491

Whole UMAMI call, main at fabaad5 (before #484) against #484 + #486 + #491 (FASTMATH=auto SHAREDWF=auto). Tables, including #511 on top, in the comment below.

combo_484_486_491_min_s

Compile time. Clean builds of all 18 process × precision combinations, make -j8, 2–4 builds in parallel on the node (indicative, the builds compete for cores):

L40S node H100 node A100 node
before 2272 s 2515 s 2240 s
after 5511 s 7555 s 5752 s
after / before 2.4× 3.0× 2.6×
tt~ +3j alone 2.2–4.1× 3.0–4.8× 2.9–3.1×

SigmaKin.cc is compiled 4× per subprocess instead of once (-fmad/-use_fast_math × shared memory off/on). With free cores the 4 compilations overlap. Here 2–4 builds already shared the cores, so most of the 4× shows up as wall time. Fixing FASTMATH and/or SHAREDWF brings it back to 2 or 1 compilation.

@stloufra
stloufra added this pull request to stack #496 November 19, 2026 15:51
@stloufra

Copy link
Copy Markdown
Contributor Author

@oliviermattelaer @theoheimel @Qubitol maybe we should discuss before we merge, so I am converting to draft for now.

@stloufra
stloufra marked this pull request as draft November 19, 2026 15:54
stloufra and others added 3 commits November 19, 2026 17:07
Turing 64 KB not 99 KB, unknown arch fall back to 48 KB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
Smaller arrays, no barrier there: L40S FP64 ttggg 0.89x, ttgQQ 0.985x.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
oliviermattelaer added a commit that referenced this pull request Nov 20, 2026
Merges mg5 3.8.2 57460b5 with 3.5.18 78fbcaa merged into it
(4698ffa). Merge base baa636c (previous 3.8.x merge).

Taken from upstream:
- run_card: pdlabel stays user-defined when pdlabel1/2 are set internally (#481)
- LO setcuts: smin no longer above the cut threshold (3.5.18: ihtmin, decay
  jets/leptons with ptjNmin/ptlNmin, cutuse=1, mmll/ptllmin/mmnl)
- loop-induced madevent output with group_subprocesses=False (#486)
- MadLoop (unoptimized output): loop filter keeps all amplitudes for 2>1
  and 1>2 processes
- symfact.dat symmetry factor assigned on every return path (auto_dsig)
- RunCardLO.reset_simd: clean the compiled code when vector_size/nb_warp
  change (the hook was a no-op stub on main)
- create_release.py: no macOS metadata in the release tarball (ported to
  MG7's rewritten script)
- IOTests: TIR libraries pinned for the loop TIR_output and MatchBox tests
- tests: smin below cut threshold, loop-induced ggh nogroup, ML loop filter
  across a boost, reset_simd; added to the CI bundles

Kept from MadGraph7 (upstream change not applied):
- genps.f/myamp.f: MG7 t-channel sampling (#323, msoft/tsoft); mg5 #466
  relative-t and log map of the remaining s not taken
- real-emission helicity ties: MG7 already removes the equal-helicity reuse
  at export (born_support.optimize_real_helicities); upstream's boosted
  re-check of T_IDENT and its test are superseded
- LO vector mode without grouping and for decays (export_v4.py and the
  DSIG_VEC wrapper of auto_dsig_v4.inc): upstream's code has no IFLAV_VEC
  and does not fit MG7's flavour-aware vector interface
- UpdateNotes.txt: MG7 notes only, no mg5 history
- Pythia8 merging plugin, PY8Card plugins, LHAPDF 6.2 makefiles, density
  test tolerance: main already had the equivalent
- IOTest goldens in conflict: kept at main's version

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>
@stloufra stloufra added this to the Feature freeze milestone Nov 20, 2026
@oliviermattelaer

Copy link
Copy Markdown
Contributor

Hi Frank,

This sounds cool, but I'm worried to rush on this one, and has you said we need to discuss it.
So I would be in favor to not in include it in v1.0

@stloufra

Copy link
Copy Markdown
Contributor Author

Ok. Won’t rush it, if you think it is premature.

@oliviermattelaer oliviermattelaer removed this from the Feature freeze milestone Nov 21, 2026
stloufra and others added 2 commits November 21, 2026 19:06
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>
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 changed the title madmatrix: FASTMATH=auto, fast math per subprocess by spill growth madmatrix GPU: FASTMATH=auto and SHAREDWF=auto, per-subprocess gates against calculate_jamps spills Nov 22, 2026
@stloufra

Copy link
Copy Markdown
Contributor Author
image

@stloufra
stloufra marked this pull request as ready for review November 22, 2026 19:26
@stloufra
stloufra requested a review from Qubitol November 22, 2026 21:47
Conflict in gpu SigmaKin.cc: keep shared-memory launch, main's comment.

This branch has not been deployed

No deployments
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.

3 participants