Skip to content

CUDA: the round after a sync is picked on the host, so its dispatches skip the device's frontier check - #1855

Closed
nuxyel wants to merge 1 commit into
bendlang:mainfrom
nuxyel:cuda-entry-check
Closed

nuxyel wants to merge 1 commit into
bendlang:mainfrom
nuxyel:cuda-entry-check

Conversation

@nuxyel

@nuxyel nuxyel commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #1854

Since 2.0.38 a GPU training loop of mine is about 3% slower on CUDA, and the cost is #1447's entry check in bend_dev (#1854 has the bisection and the variants that isolate it). This keeps #1447's device-chained rounds and takes the check off the round the host can pick itself.

Change

bend2/comp.ts only.

  • After each sync the host can read the frontier, so the first round after it launches only the dispatches that frontier needs (turn_groups(f, at), the same rule the device applies), and those launches skip the check.
  • The rounds after it, between that sync and the next, are chained on the device as in GPU bangs chain their rounds on the device, three command buffers in flight #1447, and keep the check.
  • The kernel takes a fourth argument, chk, and checks only when it is set. gpu_run(f) takes the frontier the host read, or 0 for "launch all five, checked".
  • Metal: two lines change (gpu_kernel takes the unused chk, and its one call is gpu_run(0)), so it launches the same dispatches as before. The host CPU path doesn't change.

Measurements

The training loop from #1854, current code, main (295e996) against this branch, 5 alternated rounds on an idle machine, the same output bits:

main this PR
train 200, median 16.72 s 16.27 s
range 16.60-16.87 s 16.15-16.31 s

The 17 runtime benches in PAR-GPU mode (+RTS --gpu <the gate's MEMORY size>), built by each compiler with CUDA_HOME set, 5 alternated rounds on an idle machine, medians in ms, every output identical between the two:

bench main this PR ratio
bfs 563 564 1.00x
editdist 768 773 1.01x
gameoflife 215 210 0.98x
hashmap 775 780 1.01x
histogram 1022 1020 1.00x
kmeans 493 499 1.01x
lexer 1334 1327 0.99x
mandelbrot 200 201 1.00x
merkle 209 209 1.00x
nbody 196 195 0.99x
queens 1584 1622 1.02x
raytrace 954 965 1.01x
symreg 503 504 1.00x
terrain 572 568 0.99x
tree-bitonic 1851 1847 1.00x
tree-matmul 536 535 1.00x
tree-radix 755 757 1.00x

Geometric mean 1.00x: the benches neither gain nor lose.

Tests

The 42 tests that call a bang, built for CUDA by main and by this branch and run on the GPU: the 40 that are programs print their #| lines on both; the two in tests/printer/ aren't runnable programs and fail to build the same way on both.

Gates

bun gates/repo.ts fails only on the cap: comp.ts is 66,096 ttok, 96 over 66,000 (main is under it). I didn't trim anything to fit.

Not tested

  • Metal: I have no Mac, so I couldn't build it. The Metal diff is the two lines above.
  • gates/test.ts, gates/perf.ts and gates/safe.ts need the cluster.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks for the bisection and the careful measurements in #1854. I'm closing this for now, because we don't yet know whether it fixes the source of the slowdown.

#1854 shows that the entry check costs time, but not why, and this PR removes the check only where the host can stand in for it. The first round after each sync recovers the time. Every round chained after it still pays the check, so bangs that run more than one round keep the slowdown. That fits your training loop, which is made of one-round bangs, while the 17 benches stay flat. The PR also adds a fourth kernel argument that Metal ignores, and uses 0 to mean both "no work" and "unknown". It also puts comp.ts over its 66,000 ttok cap.

For the record, I ran the cluster gates on this branch (the minis use the Metal path): test 1707/1707, ping 80/80, and safe 1720 files with 0 false rejects, timeouts or infrastructure errors. Repo is 53/54 (the cap). Perf against its base, 3 alternated runs each, gives a geomean of 0.995x on the Metal GPU cells and 1.00x elsewhere. Its one failing cell, bfs on the GPU, fails on main too. So the Metal change is safe. We have no CUDA machine, so we couldn't check the CUDA numbers ourselves.

We'd like to find the cause first. I've asked in #1854 for a reproduction we can run. If you want to dig further meanwhile, here are three cheap experiments, any of which might give a one- or two-line fix that helps every round:

  1. Host reads the frontier without touching managed memory. Have the host read the frontier with cuMemcpyDtoH after the sync, rather than reading H[H_FRONT] from managed memory, or move the header words into a small cuMemAlloc buffer. H is managed with its preferred location on the GPU, and the host reads that word after every sync, just before every block loads it.
  2. Only thread 0 does the check. It loads the frontier and passes the result to the rest of the block through __shared__.
  3. Check H_GROUPS[at], the word Metal's indirect dispatch reads, instead of recomputing turn_groups(H[H_FRONT], at).

If one of these recovers the 3%, a PR with that change would be very welcome.

@nuxyel
nuxyel deleted the cuda-entry-check branch October 31, 2026 02:15
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.

CUDA: #1447's entry check in bend_dev makes a small training loop ~3% slower (bisected)

2 participants