Skip to content

The host grows its column to a row per thread, so a 10-thread pool drains 80 units, not 16 - #1171

Merged
nicolas-abril merged 1 commit into
mainfrom
pool-row-per-thread
Sep 29, 2026
Merged

nicolas-abril merged 1 commit into
mainfrom
pool-row-per-thread

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

The idle time

  • One drain turn: hashmap at 10 threads, on the CPU, runs as a single drain turn over 256 top-level tasks.
  • 16 work units: before the drain, cube_run grows the host's column only until there is a unit per thread, (P + 7) / 8 rows. That is 2 rows at P = 10, and each row is 8 drain units, so the drain has 16 units of 16 tasks each.
  • The imbalance: six workers take two units (about 460 ms) and four take one (about 270 ms), then wait for the turn to end.
  • Instrumented: 780 ms of 4.4 s worker time is idle.

The allocator and the bank lock take no samples.

The change

The host's column grows to a row per thread, so the drain has 8 units per thread, and a worker that finishes early takes another. The two expressions become one:

-      if (f * (CUBE_T / LINE) < pool_size) {
-        row_grow((Env){ H, ALC[0] }, io_stk, 0, CUBE_G,
-          (pool_size + CUBE_T / LINE - 1) / (CUBE_T / LINE));
+      if (f < pool_size) {
+        row_grow((Env){ H, ALC[0] }, io_stk, 0, CUBE_G, pool_size);
       }
  • Size: comp.ts 63,794 → 63,762 ttok.
  • Emitted C: the same as main's except this hunk and its comment.
  • GPU lane: gpu_pass doesn't use this path.

Measurements

These are from an Apple M4 laptop (4 performance and 6 efficiency cores), CPU only (--gpu off), with the builds interleaved.

bench/runtime at 10 threads, the default here, median of 5. Every output matches main.

bench main this PR
bfs 0.604 s 0.510 s 1.18x
editdist 0.514 s 0.438 s 1.17x
gameoflife 1.433 s 1.295 s 1.11x
hashmap 0.508 s 0.440 s 1.15x
kmeans 0.475 s 0.474 s 1.00x
lexer 0.455 s 0.408 s 1.11x
mandelbrot 0.849 s 0.736 s 1.15x
merkle 0.934 s 0.835 s 1.12x
nbody 1.032 s 0.912 s 1.13x
queens 0.985 s 0.880 s 1.12x
raytrace 0.866 s 0.780 s 1.11x
symreg 0.576 s 0.507 s 1.14x
terrain 0.411 s 0.366 s 1.12x
tree-bitonic 1.403 s 1.465 s 0.96x (median of 9)
tree-matmul 0.505 s 0.454 s 1.11x
tree-radix 0.860 s 0.777 s 1.11x

Other thread counts: these come from the same rule measured earlier by patching the emitted C, median of 3.

threads effect
3 1.08x on most
5 1.19x on most
6 1.03x to 1.06x
8 the same as main (main's rule already splits 8 units evenly there), except tree-bitonic 0.95x and queens 0.98x
1, 4 the same as main

Two others considered:

  • The fewest rows whose units divide evenly among the threads. It ties this at 3 and 5 threads, but is 1–3% slower than main at 6 and 1–4 points behind this at 10. With mixed fast and slow cores, more, smaller units balance better than an exact split.
  • 4 units per thread. It sits between main and this.

Tests

  • Every program: all 1,584 under tests/, bench/runtime and demos, on the native lane at the default thread count, --threads 3 and --threads 5. They print the same as main. The exception is demos/io_rollback_netcode/walkers_test, which answers from whatever relay is running, as it does on main.
  • Repo gate: gates/repo.ts passes, 49 / 49.

Not run

  • the cluster perf gate, which is where this should be confirmed;
  • Linux, x86 and CUDA.

🤖 Generated with Claude Code

…ains 80 units, not 16

cube_run grew the host's column only until a unit per thread had tasks:
(P + 7) / 8 rows, each 8 drain units. At 10 threads that is 2 rows, 16
units: six workers took two units and four took one, then waited out the
turn (hashmap: 780 ms idle of 4.4 s worker time). The column now grows to
a row per thread, 8 units each, and a worker that finishes early takes
another.

Every program under tests/, bench/runtime and demos prints the same on
the native lane at the default thread count, 3 and 5 threads (the
netcode walkers test depends on its relay, as on main). bench/runtime at
10 threads on an M4, CPU only, median of 5: 1.09x to 1.23x on 14 of 16,
kmeans 1.00x, tree-bitonic 0.96x. The GPU lane does not use this path.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril merged commit 3ffc1f9 into main Sep 29, 2026
@nicolas-abril
nicolas-abril deleted the pool-row-per-thread branch September 29, 2026 12:20
nicolas-abril added a commit to costamatheus97/bend that referenced this pull request Oct 1, 2026
…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]>
Lorenzobattistela added a commit that referenced this pull request Oct 3, 2026
36 commits since 2.0.34: the M1/M2 Metal pipeline crash (#1154), names
keyed ns:name so a library's CID(None) means its own None (#1045), less
CPU memory from the host's column and a spin's lent variable (#1171,
#1269, #1243), equality-proof arrays (#1130), timers firing under a busy
loop (#1122), and the CLI and --verdict fixes listed in CHANGELOG.md.

Gates on 5e56772 over the bastion: repo 49/49, ping 46/46, test
1540/1540, perf 121/124.

CHANGELOG.md and VERSION name 2.0.35.

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.

1 participant