Skip to content

A device peek reads a redirect with one plain load, as before 2.0.32 (#1139) - #1140

Closed
AdrielSantana wants to merge 1 commit into
bendlang:mainfrom
AdrielSantana:redirect-words
Closed

AdrielSantana wants to merge 1 commit into
bendlang:mainfrom
AdrielSantana:redirect-words

Conversation

@AdrielSantana

Copy link
Copy Markdown

Fixes #1139 and keeps #975 fixed. On main (b2111cf4). One commit: one line of bend2/comp.ts and the comment above rfc_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_loc now reads through term_peek, so every read of a shared Array costs two atomic loads, plus a fence when the count is one.

What changes

On the device, term_peek reads the cell with one plain load, H[loc] >> 24. That is how the device reads every other heap word, and how blk_loc read before 2.0.32.

  • The read is exact. The loc cannot change while the peek holds its share.
  • The host is untouched. It still peeks through rfc_view, so its emitted C is main's (checked by diffing it), and ThreadSanitizer sees what it sees on main.
  • ctr_take and rfc_view are unchanged on every target. A take still reads the count atomically, and acquires at count one before it frees the redirect.
  • A device peek no longer fences at count one.
    • That fence only mattered to a write through a handle that was shared, such as Array.set after an @unsafe fork. The same write races whenever the other handle is still alive, and before 2.0.32 the device did not fence there either.
    • I can put it back in arr_op's write path if you want it.
  • Below Metal 3.2, DEV is plain device. The peek is then as coherent as every other plain heap read, as blk_loc was 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.

    • A redirect grows from class 0 to class 1.
    • hashmap's redirects per table then pass KEEP_WORDS and share their class with its nodes, so blocks move between threads and the counts' atomics fight over cache lines.
    hashmap, --gpu off main two words two words, class 3
    1 thread 2096 ms 1811 ms 1982 ms
    2 threads 1068 ms 1683 ms 1053 ms
    8 threads 456 ms 1118 ms 657 ms
  • A volatile load under rfc_view on Metal. It was as fast as this, but ctr_take would 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 on a32_load.

Checked

Measured

The method is #1132's:

  • Apple M5 (8 GPU cores), macOS 26.6.2, on a machine in use.
  • Each program is emitted by b2111cf4 and by this branch.
  • The time is the GPU's, summed over each round's command buffer (GPUEndTime - GPUStartTime).
  • Runs go in pairs, before then after and after then before in turn. Each row gives the median ratio of before over after, and a bootstrap 95% interval of it.
  • The CPU was not re-timed, since its C is main's.

Bendcraft (test/bench.bend, the fastest frame of each size, every checksum equal to main's), 10 pairs:

frame main this main / this 95% interval
512 x 512 25.3 ms 7.0 ms 3.75 [3.00, 4.40]
512 x 512, 300 blocks built 24.9 ms 6.8 ms 3.69 [2.94, 4.27]
735 x 398 53.1 ms 14.6 ms 3.68 [3.04, 4.02]
960 x 540 103.6 ms 26.1 ms 3.97 [3.33, 4.26]
1470 x 796 215.1 ms 58.5 ms 3.77 [3.68, 3.90]
1920 x 1080 449.8 ms 122.9 ms 3.58 [3.22, 4.43]

Against main with only blk_loc's line put back to the plain load of 84a16cf3 (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.bend with 2^22 leaves and its work in a !.

program main this main / this 95% interval
#1139's repro 17.3 ms 2.5 ms 7.475 [5.672, 10.100]
a tree of 2^22 leaves that two parallel walks share 3520.5 ms 3524.4 ms 1.001 [0.990, 1.002]

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.

  • kmeans first read 0.982 [0.949, 0.999]; 20 more pairs read 0.999 [0.989, 1.007], which is the row below.
  • With 16 intervals, one or two just past 1 is what chance gives.
bench main this main / this 95% interval
bfs 727.4 ms 745.4 ms 0.983 [0.944, 1.041]
editdist 468.0 ms 476.5 ms 1.001 [0.925, 1.035]
gameoflife 81.5 ms 84.2 ms 1.002 [0.956, 1.039]
hashmap 1086.4 ms 1070.1 ms 1.018 [1.006, 1.133]
kmeans 542.2 ms 542.2 ms 0.999 [0.989, 1.007]
lexer 4457.4 ms 4492.2 ms 0.991 [0.983, 1.013]
mandelbrot 53.0 ms 53.1 ms 0.993 [0.977, 1.017]
merkle 42.1 ms 41.9 ms 0.998 [0.978, 1.036]
nbody 43.8 ms 39.8 ms 1.012 [0.985, 1.048]
queens 3376.7 ms 3409.7 ms 0.988 [0.970, 1.023]
raytrace 581.1 ms 569.1 ms 1.020 [0.952, 1.072]
symreg 711.1 ms 695.3 ms 1.019 [0.999, 1.047]
terrain 293.3 ms 293.5 ms 0.984 [0.958, 1.022]
tree-bitonic 2290.7 ms 2323.5 ms 0.992 [0.977, 1.006]
tree-matmul 766.0 ms 792.0 ms 0.983 [0.952, 1.021]
tree-radix 1099.8 ms 1109.0 ms 0.984 [0.964, 1.010]

Written by Claude (Anthropic) with Adriel Santana driving; the numbers are from Adriel's M5.

…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%.
@nicolas-abril

Copy link
Copy Markdown
Collaborator

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. blk_loc no longer reads through term_peek. It loads the redirect word directly with w64_load, which is a plain load on Metal and CUDA and a relaxed 64-bit load on the CPU. #1155 also lets a spin use the storage its caller already resolved, so it doesn't go through the redirect at all. After that, the only device callers of term_peek left are the borrowed constructor match and window_pix, and your shared-tree row (1.001×) shows nothing to gain there. Changing term_peek now would only drop the count-one fence. The branch also conflicts with #1155 on the rfc_view comment.

I checked #1139's repro on Metal (Apple M4, wall time with hyperfine, 30 runs), built from 6de56b97 (right before #1155) and from main:

before #1155 main
repro as written 65.6 ms min 58.7 ms min
scaled to 8192 reads per leaf 238.0 ± 1.1 ms 69.1 ± 2.1 ms

The outputs match on both. The unscaled ! is only a few ms, so process startup dominates that row.

Your offer to add #1139's repro as a bench is a good one, since nothing in bench/runtime shares an Array. A PR adding a BLK_SHR bench would be welcome. Thanks again, and please keep them coming!


This review was written with AI assistance and checked by a maintainer.

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.0.32 makes each read of a shared Array two atomic loads: frames 3 to 4x slower on Metal

2 participants