Repository navigation
A device peek reads a redirect with one plain load, as before 2.0.32 (#1139) - #1140
AdrielSantana wants to merge 1 commit into
Conversation
…endlang#1139) 2.0.32 reads a redirect cell as two atomic halves (bendlang#975), and blk_loc now reads through term_peek, with a fence at count one. On Metal an atomic load is dear: a Bendcraft frame, where every lane reads one shared Array, went from 47 to 204 ms. On the device, term_peek now reads the cell as it reads the rest of the heap, H[loc] >> 24: the loc cannot change while the peek holds its share. The host still peeks through rfc_view, so its C is main's and ThreadSanitizer is as silent on bendlang#975's repro; ctr_take and rfc_view are unchanged everywhere. Tried: a two-word redirect, its loc read plainly on every target, made hashmap's redirects pass KEEP_WORDS in class 1 and share cache lines across threads, 456 -> 1118 ms at 8 threads; a volatile load under rfc_view made ctr_take decide ownership from a non-atomic count. GPU time on an M5 in alternated pairs, main over this: 3.6 to 4.0x on Bendcraft, 7.5x on the shared-read repro, bench/runtime within 2%.
|
Thank you, @AdrielSantana, both for this PR and for #1139. The issue's breakdown of which part of the read costs (the atomic load, not the width or the volatile local), the two-word redirect you tried and measured against hashmap, and the paired GPU timings were all excellent, and they made the problem very easy to confirm. We're closing this one because #1155, merged today, fixed the same slowdown in a different spot. I checked #1139's repro on Metal (Apple M4, wall time with hyperfine, 30 runs), built from
The outputs match on both. The unscaled Your offer to add #1139's repro as a bench is a good one, since nothing in This review was written with AI assistance and checked by a maintainer. |
Fixes #1139 and keeps #975 fixed. On main (
b2111cf4). One commit: one line ofbend2/comp.tsand the comment aboverfc_view, 63,573 → 63,595 ttok.Why
2.0.32 reads a redirect cell as two atomic halves, so that no read overlaps the count's atomic adds (#975).
blk_locnow reads throughterm_peek, so every read of a shared Array costs two atomic loads, plus a fence when the count is one.a32_loadis a volatile load.What changes
On the device,
term_peekreads the cell with one plain load,H[loc] >> 24. That is how the device reads every other heap word, and howblk_locread before 2.0.32.rfc_view, so its emitted C is main's (checked by diffing it), and ThreadSanitizer sees what it sees on main.ctr_takeandrfc_vieware unchanged on every target. A take still reads the count atomically, and acquires at count one before it frees the redirect.Array.setafter an@unsafefork. The same write races whenever the other handle is still alive, and before 2.0.32 the device did not fence there either.arr_op's write path if you want it.DEVis plaindevice. The peek is then as coherent as every other plain heap read, asblk_locwas before 2.0.32.Tried
A redirect of two words. This is Shared Array redirect lookup races with atomic reference-count updates #975's other suggestion: the loc in one word, read plainly on every target, and the count in the next, touched only by atomics. It is race-free by construction and made Bendcraft 4.4x faster, but it lost badly on hashmap with threads.
KEEP_WORDSand share their class with its nodes, so blocks move between threads and the counts' atomics fight over cache lines.--gpu offA volatile load under
rfc_viewon Metal. It was as fast as this, butctr_takewould then decide to free or share from a count that is not read atomically. It would also be a load form that no M1 has compiled, beside the M1 note ona32_load.Checked
tests/rfc/*, on 2.0.32 makes each read of a shared Array two atomic loads: frames 3 to 4x slower on Metal #1139's repro, or on a tree that two parallel walks share. The outputs are right.clang -x c -std=gnu11 -O1 -g -fsanitize=thread, run with--threads 2and--threads 8,--gpu off.#|lines, built natively and run on the M5, gives the same results as main. 661 match their#|lines on both; 13 differ on both, only because my runner reads stdout alone (no stderr, no exit codes) and has a display.bun gates/repo.ts: 47/47.bench/runtimeshares an Array, which is why the perf gate could not see this. I can add 2.0.32 makes each read of a shared Array two atomic loads: frames 3 to 4x slower on Metal #1139's repro as a bench if you want it gated.Measured
The method is #1132's:
b2111cf4and by this branch.GPUEndTime - GPUStartTime).Bendcraft (
test/bench.bend, the fastest frame of each size, every checksum equal to main's), 10 pairs:Against main with only
blk_loc's line put back to the plain load of84a16cf3(8 pairs), every size read 0.97 to 1.06, and each interval held 1.#1139's repro and a shared tree on the GPU, 12 pairs. The tree is
tests/rfc/share_forked.bendwith 2^22 leaves and its work in a!.Against the plain load of
84a16cf3, #1139's repro read 0.996 [0.967, 1.057].bench/runtime on the GPU, 10 pairs, with the flags of
gates/perf.ts. None of these shares an Array, and every output equals main's.Written by Claude (Anthropic) with Adriel Santana driving; the numbers are from Adriel's M5.