Skip to content

The work pass after a grow runs each lane's own ring, up to the put the grow left (#925) - #1132

Merged
Lorenzobattistela merged 2 commits into
bendlang:mainfrom
AdrielSantana:work-after-grow
Oct 6, 2026
Merged

Lorenzobattistela merged 2 commits into
bendlang:mainfrom
AdrielSantana:work-after-grow

Conversation

@AdrielSantana

@AdrielSantana AdrielSantana commented Sep 27, 2026 •

Copy link
Copy Markdown

Answers the last question of #925. Two commits on main (ebd9e904): Adriel's, which adds the work pass after a grow, and a follow-up that limits it to rounds grown from few roots, after review found it cost tree-matmul 11% on the gate's minis.

Why

A round of gpu_run grows the frontier (one group while f < CUBE_T, CUBE_G groups while f < LANES), then the work pass drains every ring with rg = ring_flip(me). On Metal CUBE_LOG is 7, so CUBE_G is 128 and work group row takes ring position row of every grow group: lanes 32s..32s+31 of a SIMD group run one task from each of grow groups 32s..32s+31. After a grow from one root those are 32 unrelated subtrees, and every branch any of them takes is paid by all 32. A raycaster's lanes trace 32 patches of the screen far from each other.

The obvious fix, rg = me, is a disaster on trees (2.0.29, wall time: kmeans 0.587 → 11.969 s, tree-bitonic 1.898 → 45.155 s, tree-matmul 0.727 → 3.428 s). TG_HOLD holds one group per Apple core, so the work groups run in waves and each reads its rings' put when it starts. With me a group holds a whole grow group, its joins complete in the first wave, and their forks, dealt through the cursor to ring_flip(c), land under a later group's put and run whole on one lane.

What changes

  • After a grow from under CUBE_T roots, the work pass (pass 3) drains each lane's own ring, only up to the put the ring held when the grow ended. A late group leaves deals made during the pass to the next round.
  • Grow passes store each ring's put in the upper half of its put word (ring_held). ring_put is a u32 view of a 64-bit plane word whose other half was unused, so the layout does not change at any CUBE_LOG, and CUDA's memset already zeroes it.
  • Every other round keeps pass 1: ring_flip and the live put, as on main.

Why only from few roots: when several lanes of a grow group fork together, the group's cursor puts kid i of lane L at 24i + L, so neighbouring rings hold the same kid of different parents. From one root those parents are cousins, and a SIMD group of neighbouring rings runs related work. From f >= CUBE_T roots dealt through the cursor, deal c sits in grow group c % CUBE_G and neighbouring rings hold unrelated deals, while ring_flip reads consecutive deals (a fork's kids, which in tree-matmul share their input blocks) in one SIMD group. Pass 3 on every grow measured tree-matmul at 1.11x and tree-bitonic at 1.07x on the M4 minis.

Why it cannot hang

A pass-3 lane stops when its ring's get reaches the stored put.

  • The stored put is fresh. Pass 3 runs only when f < CUBE_T, so the grow on CUBE_G groups ran right before it in the same gpu_run; its lanes cover all LANES rings and store at their end.
  • It comes after the grow's last push within the group. A grow lane pushes only within its own group, and the group's lanes leave the loop together after a barrier.
  • get never passes it. It starts at or below the stored put and advances only by draining a slot below it, which the grow wrote in an earlier dispatch.

Limits

  • Some deals made during a grow wait a round. A continuation can deal through the cursor into a group that already finished growing; the task lands above that ring's stored put and runs next round, like a task dealt during pass 3.
  • Fork-free tasks the grow leaves in place run where the cursor dealt them, as before.

Measured

Gate minis (Apple M4), gates/perf.ts over BEND_HOC, main ebd9e904 and this branch in turn, 5 rounds, median PAR-GPU time:

bench main this PR PR / main
raytrace 0.406 s 0.326 s 0.80
tree-matmul 0.944 s 0.910 s 0.96
tree-bitonic 1.794 s 1.715 s 0.96
editdist 0.509 s 0.529 s 1.03
histogram 1.483 s 1.503 s 1.01

The other benches are within ±2%; terrain lands at 0.23 or 0.32 s on both sides, run by run. Every output is the same.

On Adriel's M5 (2.0.32, before the follow-up, which leaves single-root rounds unchanged), the Bendcraft raycaster's frame was 1.7x to 2.8x faster by GPU time, and the program in #925 read A 4.6, B 15.6 and C 1.35 ms (5.8, 18.5 and 2.1 before). The first question of #925, a fill that stops at "every lane has a task" whatever the sizes, is untouched.

Tests

On ebd9e904, with this branch and with main:

  • gates/repo.ts 50 / 50 on both. comp.ts is 63,949 ttok against its cap of 64,000 (main 63,838).
  • gates/test.ts 1576 / 1576 and gates/ping.ts 47 / 47 on both.
  • gates/safe.ts tests: 1017 agree, 74 unsafe, 491 rejects, 1 false reject (agree_32), the same on both.
  • gates/perf.ts: 122 to 124 / 124 on this branch, 119 to 123 / 124 on main; the misses on both are raytrace's compiler time and mandelbrot's sub-100 ms GPU cell.
  • paper/BendRT.pdf rebuilt with Typst 0.15.1 (--root bend2/docs); main's source rebuilds to within one byte of main's PDF.
  • Not run: CUDA, which runs the same kernel and gpu_run.

After merging, re-pin apple_m4's PAR-GPU column with --pin, so a change that undoes raytrace's gain fails the gate.

🤖 Generated with Claude Code

AdrielSantana added a commit to AdrielSantana/bendcraft that referenced this pull request Sep 28, 2026
…endlang/bend#1132 and #1140, from the bendcraft branch of AdrielSantana/bend at ../bend (the Makefile's BEND, run with bun; make BEND=bend builds with the installed one). 2.0.32's check prints SOME PROOFS FAIL for any file that relies on @unsafe, Base's Array.join included, so the frame's tree and the game's view through it moved to src/frame.bend (Frame.cast was Render.frame; Frame.view, view_with, view_more, view_stat and depth were Player's and Render's), which the laws do not import: PROOF.bend prints ALL PROOFS CHECK, and make check takes a program's verdict as a pass when its only fault is the unsafe reach, in one loop over main.bend and test/*.bend. IO.args() starts with the program's name, so size_of skips it (a built binary counts 4 for three arguments), and feed passes the new Look and Scroll events by. 189 tests, the physics hash and the digest c777295b0b490b273e867be0082713c2 unchanged. Against stock 2.0.25, twelve alternated rounds with the order swapped, game closed, fastest frames: 5 -> 3 ms at 512x512, 13 -> 6 at 735x398, 23 -> 9 at 960x540, 48 -> 19 at 1470x796, 91 -> 33 at 1920x1080; the profile's all on at 1470x796, four rounds, 51.6 -> 21.8, the lake 49.2 -> 21.4. The README's numbers and what-costs-what sections, some 430 lines of before/after tables on 2.0.25 that git log also holds, are now the current tables and the lessons that still hold; the ROADMAP's budget (10 ms left at scale 2 for 60 frames a second), its routine and its upstream table are today's, and AGENTS.md says where the compiler comes from and that the frame is the one @unsafe module. test/trace.py does not find 2.0.32's runtime text yet.
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Hey Adriel, thanks for the contribution.

The change seems correct, but on the gate's M4 minis it costs tree-matmul about 10% on PAR-GPU. A one-line change to the condition in gpu_run removes that loss and keeps the raytrace gain.

Where tree-matmul loses: almost all of it is one round: 3072 tasks grown into 24576. On an M4 Pro, with one command buffer per kernel, that round's work pass goes from 273 ms to 311 ms. We changed how pass 3 picks its ring and timed the GPU:

Image

The stored put costs nothing. Balance across groups doesn't explain it either: the chunked flip balances the groups like ring_flip does, yet it gets both the gain and the loss. What matters is which tasks share a SIMD group.

Why. When several lanes of a grow group fork together, the group's cursor puts kid i of lane L at 24i + L. So neighbouring rings hold the same kid of different parents.

  • If the round grew from one root, as raytrace and tree-matmul's first round do, those parents are cousins, and a SIMD group of neighbouring rings runs related work. tree-matmul's first round goes from 15.0 to 3.5 ms.
  • If the round starts from deals made through the cursor (f >= CUBE_T), deal c sits in grow group c % CUBE_G. Neighbouring rings then hold deals CUBE_G apart. ring_flip reads one ring position across 32 neighbouring grow groups, which are consecutive deals: the previous round's siblings. In tree-matmul those share their input blocks (mul(a0, b0) and mul(a0, b1)).

Suggested change. Use pass 3 only when the grow on one group ran:

static void gpu_run(u32 f) {
  if (f < CUBE_T) {
    gpu_kernel(0, 1);
  }
  if (f < LANES) {
    gpu_kernel(0, CUBE_G);
  }
  gpu_kernel(f < CUBE_T ? 3 : 1, CUBE_G);
  gpu_kernel(2, 1);
}

Pass 3 still runs only right after a grow on CUBE_G groups, so the argument for why it cannot hang is unchanged. A round with CUBE_T <= f < LANES runs as on main. The program in #925 is a single round from f = 1, so it behaves the same as with this PR.

Image

Smaller points:

  • Name the stored put. A macro next to the ring macros, e.g. #define ring_held(H, r) (ring_put(H, r) + 1), would document the upper half of the put word instead of the bare + 1 and + (pass == 3).
  • Store it only after a grow. Only the value the grow leaves is read, so the store at the end of bend_dev can be if (pass == 0).
  • Fix the pass header. It still says pass 1 "drains each lane's ring", but pass 1 drains ring_flip(me). The new sentence should also say pass 3 runs only after a grow from fewer than CUBE_T roots, and the BendRT sentence should match.
  • CUBE_LOG on Metal. At d2066a5 it is the default 7 (128 groups, 16384 lanes), not 5 as the description says, so the ring arithmetic there describes CUBE_G 32.

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 1, 2026
AdrielSantana and others added 2 commits October 6, 2026 11:27
…he grow left (bendlang#925)

After a grow on CUBE_G groups, ring g * CUBE_T + k is grow group g's k-th, and ring_flip gave a SIMD group one task from each of 32 unrelated subtrees. The work pass after a grow (pass 3) now drains lane me's own ring, so a SIMD group runs neighbours, and only up to the put the ring held when the grow ended (the put word's unused upper half), so a group that starts in a later wave leaves the tasks an earlier wave's joins dealt to the next round. With the live put it ran each of them whole on one lane: tree-bitonic took 406 rounds and 44 s instead of 240 and 1.9. A round without a grow keeps ring_flip, which gives the c-th deal to lane c. On an M5, by GPU time in 15 AB/BA pairs: raytrace 1.39x, kmeans 1.02x and tree-bitonic 1.01x faster, tree-matmul 0.93x, the other twelve runtime benches within their 95% intervals, every output the same; a voxel raycaster's frame 1.7x to 2.8x faster, the same checksums.
… neighbours are one root's cousins (bendlang#925)

From more roots a grow group's neighbouring rings hold unrelated deals, and
pass 1's ring_flip keeps a fork's kids in one SIMD group: with pass 3 on
every grow, tree-matmul's PAR-GPU ran 1.11x on the M4 minis. The put a grow
left is named ring_held and stored by grow passes only, the pass header says
which ring pass 1 drains, and the BendRT sentence says when pass 3 runs.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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