Repository navigation
A forking call no longer pays a condvar wake per pool turn: turns count rows, the main thread takes rows, and a turn visits only the rows that can hold a task - #1092
Conversation
|
Thank you for this, the gains hold up on an Apple M4 too. I pushed three commits to your branch: one dropping the comments the rework added, a merge of current main, and a simplification commit, because the PR put Pool. The same three mechanisms, written more simply:
Emitter. Eight redundancies removed elsewhere in
Measured on an Apple M4:
Note: this comment was written by an AI after it validated and simplified the PR for me. If anything here is wrong, reply and I will review it myself. |
…nt rows, the main thread takes rows, and a turn visits only the rows that can hold a task bendlang#1092's five commits, squashed onto main. The emitter cleanups its last commit carried are already on main (bendlang#1235); the column grow keeps bendlang#1171's row per thread, and a column grow still opens every row to the turns. Co-Authored-By: Nicolas Abril <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… row bound says why it holds pool_turn waited for the last rows with a sched_yield loop that never slept; on macOS that is a busy core for as long as the slowest row runs. The main thread now yields 128 times and then sleeps on pool_join, which the last finisher signals; a short turn still ends inside the yields and pays no wake. cube_run's comment states the invariant the turn bound rests on: a drain deals task g to a row at most g, a row grows into itself, and only a column grow reaches any row. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
6e6ae98 to
869f320
Compare
…lits its rows into units of 32 a thread, and rings rewind between turns (stacked on #1092) (#1269) Co-authored-by: Matheus Costa <[email protected]> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
|
Closing as superseded: #1269 (fb6f32a), which was stacked on this PR, squash-merged these pool changes into main along with later work ( |
On 2.0.29 (
574b6d39). Three commits; the third (a yield before sleeping) is optional and last, so it can be dropped on its own.Why
Once a program forks,
corpus_evalhands it to the pool (task_deal,pool_open,cube_run(H, false)) until the root is done. Each fork tree is onecube_runiteration: a grow turn (pool_turn(true)) and a drain turn (pool_turn(false)), each a full barrier. A call that forks pays at least two barriers per tree, however little work the tree holds. bend-parallel's scan forks two trees per call, its histogram two, its sort six.On 2.0.28 I timed the pool with counters in the generated C: a 1K-key scan at fork depth 6, 2,000 calls in one
corpus_eval. 2.0.29's pool is the same design, and costs the same (the main column below).Three things cost that, each in
bend2/comp.ts:pool_turnsetspool_donetopool_sizeand sleeps onpool_wakeuntil it reaches 0. Each worker must wake from the condvar, find no rows left and check in, and the last one'spthread_cond_broadcast(&pool_wake)also wakes the sleeping workers again, since both directions share one condvar. The main thread sleeps meanwhile.pool_work'spthread_cond_wait). The next turn is often a few microseconds away, but a condvar wake costs over 100 us on this machine.row_growover all 128 rows (CUBE_G) and the drain turn walks all 1024 units (LANES / LINE), reading every ring's head, however small the frontier is. For a 64-leaf tree that walk is most of a turn: on one thread, 48 of the grow turn's 60 us.What changes (
bend2/comp.tsonly; ttok 62,584 → 63,019 with commits 1–2 below, 63,085 with the third, against 64,000)The pool keeps 2.0.29's atomics:
a32_*on plainu32, no_Atomicoratomic_*_explicit.a32_addonpool_row, au32that carries the turn, the grow flag and the row.pool_donecounts down the turn's rows. Each worker subtracts its count once (a32_sub_rel), when a claim comes back past the last row. The worker that brings it to 0 signals the main thread on its own condvar,pool_join. The main thread waits witha32_load_acq(&pool_done) != 0, as before.--threads Nnow runs N − 1 pool threads.pool_tickandpool_growgo away; the worker's body moves intopool_rows, unchanged.ring_flip(i), whose row is at most i, and a grow turn pushes only within its own row (ring_pick(base, 1, cur)).min(f, CUBE_G), and the drain turn the rows below the deals made so far (the frontier, plus any continuation that forked while growing). The main thread's column grow can reach any row, so a turn after it visits all of them.ring_flip) and checked where breaking it would show: a task dealt past a turn's rows would sit in its ring until the frontier drained, socube_run, on that failure path only, walks the rings and fails with "a task left past its pool turn's rows" rather than "frontier drained without a result". A run that succeeds never reaches the walk.POOL_WAITcallssched_yield()up to 128 times while its condition holds (about 80 us here), then blocks on the condvar as before. It serves both waits: a worker's for the next turn, and the main thread's for the last row. See "The third commit" below.The claim word
turn << 20 | rows << 12 | grow << 11 | counter(commit 1 alone has no rows field:turn << 12 | grow << 11 | counter).LANES / LINEunits:CUBE_T(128) ×CUBE_G/LINE(16), withCUBE_Gat most 128 (CUBE_LOGis 7 on the host; CUDA'sgpu_shapeclamps it to 4..7), so 1024. Past them, each thread makes at most one claim per opening: it leavespool_rowson its first claim past the end and then waits for a turn value different from that claim's.pool_sizeis at mostCUBE_T, so the counter stays below 1024 + 128 = 1152 < 2048 and never carries intogrow.Why it's safe
a32_addon the word that names the turn. Two threads can't get the same (turn, row).pool_doneonly when it leaves the loop, so while it holds an unfinished row of turn T,pool_doneisn't 0 and the main thread can't open T + 1. That also means every claim a worker makes before leaving is still in T.pool_done(relaxed) and then opens the turn witha32_store_rel(&pool_row, ...). A worker claims with a relaxeda32_add, then callsa32_acq(&pool_row)before it touches a claimed row. That is the shapetask_deliveralready uses (a32_sub_rel, thena32_acq). The acquire load reads the opening store or a later claim in its release sequence. It can't read the next opening's store, because this worker holds an unfinished row. So the worker sees the rings, the grow's pushes and thepool_donereset. The main thread'sa32_load_acq(&pool_done) == 0reads the last of the release subtractions, so it sees every row's writes.pool_lockbeforepthread_cond_wait, and the main thread opens the turn and broadcasts under the same lock. The join is symmetric: the last row's worker signalspool_joinunder the lock.cube_runiteration, every ring that holds a task is in a row below the bound. After a drain, onlytask_dealwith stride 0 pushes, toring_flip(g)for g below the cursor.row_grow(strideCUBE_G), sets the bound toCUBE_G.ring_flip(i)'s row isi % CUBE_T(atCUBE_LOG7) or below (at smallerCUBE_LOG), so at most i.task_deal,monk_step's push of a fork-free continuation (stride ≠ 0 only, so within its row),row_grow, andcorpus_eval's first deal. The GPU seam'sring_push(H, 0, t)runscube_run(H, true), not the pool.ring_flip(g), where g is below the cursor at the end of the drain. The next iteration reads that cursor as its f before resetting it, so f > g >= the task's row, f > 0, and the next bound,min(f, CUBE_G), covers the task's row. Such a task waits one iteration, where the full walk on one thread could have reached it in the same turn. That changes the one-thread schedule of tree-bitonic, not its result.The third commit: optional
What commit 3 gives on this machine, on top of 1+2 (bend-parallel, c16, 5 rounds, rounds don't overlap):
Before timing, I set the bar for keeping it: at least 25% off scan d10 b6 c16 and at least 3% off sort d17 b12 c16. It clears both: −61% (67 us per call) and −5.8%. At c1 it does nothing: one thread finishes every row before it waits, so there's nothing to yield for.
It's last so it can be dropped. It is tuned to this host, where a futex wake costs about 140 us. Upstream's runtime has no yield-spin anywhere else, and its perf gate runs on Mac minis, where a condvar wake is likely cheaper and 128
sched_yields take an unmeasured time. What it costs:Without it, the branch still takes scan d10 b6 c16 from 1.83 ms to 0.11 ms per call.
Measurements
The machine is an AMD Ryzen 7 5800XT (8 cores / 16 threads) under WSL2 (kernel 6.18), with AMD clang 22 (ROCm 7.2.1) at
-O3and no GPU. It's a shared machine. I give the 1-minute load average with each table; the c16 runs themselves push it up. Every value is a median.bend-parallel benches (ms per call; 5 interleaved rounds, order rotated each round; load 0.8 → 3.8)
bend-parallel 7fb04de. "1+2" is the first two commits, "all" this PR. scan d10 is 2,000 calls in one run; the precise drivers run d=17 64 calls and d=20 and d=22 16 calls each. Checksums match across all builds, lanes and rows.
bench/runtime (seconds; 16 programs, c1 and c16,
--gpu off; 5 interleaved rounds, order rotated; load 2.3 → 4.1)All 16 programs print the same output on every build. Rows not shown are within 2.5% of main on both builds.
Rows that move by more than about 3% are checked at matched layout before they're attributed.
The PR adds libc imports (
pthread_cond_signal, andsched_yieldin commit 3) and a function,pool_rows, with its.eh_frameentry. That moves.text, and with it the program's hot code (WL_FID_*,monk_step), by 16–48 bytes mod 64. On 2.0.28 that shift alone moved CPU time by up to 11% on this Zen 3 host, in a direction that depends on the program and lane. It produced that version's editdist, symreg and tree-matmul c16 slowdowns (+4–10%). On 2.0.29 the layout has moved again, and those three are within 3% of main.The check:
pad.pyputs a 64-aligned pad at the start of.text, so at each pad N (0, 16, 32, 48) main and both branches putmonk_stepandmainat the same address mod 64 (checked withnm). c16, 9 rounds, load 1.0 → 14.6 from the runs themselves; the geometric mean over the four pads, against main:monk_stepmod 64 for main / 1+2 / all is hashmap 0/0/16, lexer 32/16/32, bfs 32/32/48 and tree-matmul 48/48/0. tree-radix (16/16/16) and kmeans (0/0/0) didn't move.--threads 1, a drain turn visits the rows below the deals made before it started, so tree-bitonic runs half as manycube_runiterations (258 → 121 there), with the same output.Verification
pool_turnif any ring at or past the bound holds a task. It sleeps 0–50 us before one claim in eight and before each final subtraction, to force late workers. It aborts if a claim's counter reaches all +pool_size, and it can cut the turn field to k bits (DBG_WRAP), so sleepers constantly meet wrapped turns.tests/*), at--threads 1, 2, 3, 7, 16, 32: every output right, no abort. The first two commits alone: the same at 1, 2, 16, 32.bench/runtimeprograms at 1 to 32 threads: every output right. bend-parallel's tests with a 1-bit turn: right.(f + 1) / 2) makes the debug check fire (ring 128 holds a task past grow bound 1). On the committed code, without hooks, the same mutant fails with the new message:bend: a task left past its pool turn's rows.-O1 -g -fsanitize=thread, the plain runtime, no hooks; the 50 forking tests at--threads 2, 4, 16):blk_loc8-byte read against a 4-byte atomic write inrfc_bumporterm_drop. That is Shared Array redirect lookup races with atomic reference-count updates #975, fixed by A spin reads a shared Array's redirect once, atomically: blk_loc no longer races its count #1084. None are in pool code.bun gates/repo.ts: 46/46.gates/test.ts's rules on one machine, no GPU lane,io_tcp_send_slow_peerexcluded): 1473 / 1477, the same four failures as main (io_audio_only_{open,write,close}: no ALSA headers here;io_process_runon interp and JS).run_tests.sh(check, proof, JS, c1, c16), stacked on A spin reads a shared Array's redirect once, atomically: blk_loc no longer races its count #1084: 41/41.With #1084
comp.tsis 63,463 ttok, or 63,397 without the third commit, under the 64,000 cap.Not run
!calls, through this same code.sched_yieldis POSIX and<sched.h>is included on every host. How long 128 yields take there, and what commit 3 gains there, wasn't measured.