Repository navigation
The work pass after a grow runs each lane's own ring, up to the put the grow left (#925) - #1132
Conversation
…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.
|
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:
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.
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.
Smaller points:
|
…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]>
6aa75c8 to
b62c4e1
Compare


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_rungrows the frontier (one group while f <CUBE_T,CUBE_Ggroups while f <LANES), then the work pass drains every ring withrg = ring_flip(me). On MetalCUBE_LOGis 7, soCUBE_Gis 128 and work grouprowtakes ring positionrowof 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_HOLDholds one group per Apple core, so the work groups run in waves and each reads its rings'putwhen it starts. Withmea group holds a whole grow group, its joins complete in the first wave, and their forks, dealt through the cursor toring_flip(c), land under a later group'sputand run whole on one lane.What changes
CUBE_Troots, the work pass (pass 3) drains each lane's own ring, only up to theputthe ring held when the grow ended. A late group leaves deals made during the pass to the next round.putin the upper half of itsputword (ring_held).ring_putis au32view of a 64-bit plane word whose other half was unused, so the layout does not change at anyCUBE_LOG, and CUDA's memset already zeroes it.ring_flipand the liveput, 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_Troots dealt through the cursor, deal c sits in grow groupc % CUBE_Gand neighbouring rings hold unrelated deals, whilering_flipreads 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
getreaches the storedput.putis fresh. Pass 3 runs only when f <CUBE_T, so the grow onCUBE_Ggroups ran right before it in the samegpu_run; its lanes cover allLANESrings and store at their end.getnever passes it. It starts at or below the storedputand advances only by draining a slot below it, which the grow wrote in an earlier dispatch.Limits
putand runs next round, like a task dealt during pass 3.Measured
Gate minis (Apple M4),
gates/perf.tsover BEND_HOC, mainebd9e904and this branch in turn, 5 rounds, median PAR-GPU time: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.ts50 / 50 on both.comp.tsis 63,949 ttok against its cap of 64,000 (main 63,838).gates/test.ts1576 / 1576 andgates/ping.ts47 / 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.pdfrebuilt with Typst 0.15.1 (--root bend2/docs); main's source rebuilds to within one byte of main's PDF.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