Skip to content

A ! runs on the GPU under WSL2: a CUDA device without concurrent managed access gets a twin of the corpus - #1078

Closed
MattCozendey wants to merge 11 commits into
bendlang:mainfrom
MattCozendey:feature/cuda-twin-refactor
Closed

MattCozendey wants to merge 11 commits into
bendlang:mainfrom
MattCozendey:feature/cuda-twin-refactor

Conversation

@MattCozendey

@MattCozendey MattCozendey commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

WONTFIX tells Windows users to use WSL, and a user there expects ! to use
the GPU. Today it does not build (cannot find -lcuda), or, with
LIBRARY_PATH set, it runs on the cores without a message. This PR makes
the WONTFIX advice true.

Terms

  • Concurrent managed access: a CUDA device attribute
    (CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS). With it, the host and
    the device can use one managed allocation at the same time; the CUDA lane
    on main needs it for its corpus. A GPU under WSL2 reports 0, so main's
    probe refuses it.
  • Twin: this PR's answer for such a device. The corpus stays in host
    memory, and a second copy of it (the twin) lives in VRAM. A ! turn
    copies what it can touch to the twin before it runs, and back after.
  • Watchdog: Windows' display driver model (WDDM), which every WSL2 GPU
    runs under, stops any single GPU launch after about 2 s (the TDR).
  • Parking: a lane that has used its time budget saves its state and
    stops; the next launch resumes it. So no launch reaches the watchdog,
    however long the whole ! runs.

Why it is safe, and why it is needed

1. Linux: nothing changes for existing users

On native Linux, the CPU build is byte-identical to main's, and a CUDA
device with managed access takes main's path and main's device program.

  • The CPU build's assembly (clang -O3 -S) is byte-identical to main's for
    the 16 runtime benches and the 19 ! tests that compile.
  • The CUDA host build contains the twin code behind if (gpu_twin). On a
    device with managed access those branches never run.
  • Without a twin, NVRTC (NVIDIA's runtime compiler, which builds the device
    program at --gpu-build) gets main's options, and the program's text
    differs from main's only where it compiles out or expands to main's
    macros (point 2). Its cubin is byte-identical to main's for the same 35
    programs.

2. Metal: no Metal code changed

No Metal code changed; every shared change compiles out or expands to
main's text on Metal. Please confirm on the minis (test and perf gates).

  • The emitter's jumps now pass the number of registers they fill, which a
    parking lane saves: WL_GO(F, K) expands to WL_JMP(F) and
    WL_AGAIN(F, K) to continue, as before.
  • The parking code is under #if BEND_PARK, which only the twin's NVRTC
    build sets.
  • gpu_twin is #defined as false outside CUDA, so POOL_ALT (the
    signal stack's size, larger for a twin) is SIGSTKSZ, and gpu_sync
    (a twin's copies around a turn) does nothing.
  • The new header words H_PARKED and H_BUDGET (LINE + 1, LINE + 2)
    are read and written only by the twin and parking code.

3. WSL: ! now builds and runs on the GPU

  • The build links libcuda against the toolkit's stub, which comes after
    the other -L paths; its soname is libcuda.so.1, so at run time the
    loader loads the driver's library.
  • A device without concurrent managed access gets a twin. The heap is
    synced lazily, in 2 MB chunks: after a turn, the host downloads a chunk
    only when it touches it (a page fault), and the next turn uploads only
    the chunks the host wrote. So data that stays on the device is not
    copied back and forth.
  • The 20 ! tests that compile pass on WSL2 (RTX 4050, CUDA 13.3), by
    default, with --gpu off and with --gpu 512MB.

4. Why parking is needed, despite the comment on #1012

  • A different problem. A ! call's long sequential loop parks between launches, so no dispatch holds the GPU (#942) #1012 was about a ! with no parallelism on
    Metal. Here the program is parallel, and Windows' display driver (WDDM,
    which every WSL2 GPU uses) stops any launch after about 2 s. The
    maintainer's fixes do apply, but they give up the GPU:
    • "Use the CPU" would move every long-running ! on WSL to the cores, so
      WSL users would still not have the GPU for real work (point 5).
    • "Fail cleanly" is in this PR (the watchdog message), but by itself it
      would stop the program instead of finishing it.
    • Parking is the only one of the three that lets a long, parallel !
      finish on the GPU under a 2 s limit.
  • No cost for others. Parking is compiled only for a twin
    (-DBEND_PARK). Metal and Linux with managed access run main's device
    program.
  • Simpler than A ! call's long sequential loop parks between launches, so no dispatch holds the GPU (#942) #1012: no new task tag, no call-graph pass, no fuel
    constant and no build knob. The budget is time, read from %globaltimer.
  • Tested on a GPU: on WSL2, queens at size 18 ran about 12 s on the
    GPU with no driver reset. With the budget forced down to 0.2 ms, so that
    lanes parked every few thousand steps, 2,501 rounds of parking and
    resuming gave the same results as the cores (RTX 4050 Laptop, under
    WDDM).

5. WONTFIX sends Windows users to WSL

WONTFIX says "Use WSL", and a user there expects ! to use the GPU. Today
it does not build, or it runs on the cores without a message. This PR
makes the WONTFIX advice true.

The commits

  1. A ! program links libcuda against the toolkit's stub where the
    driver's is off the linker's path.
  2. A ! runs on a CUDA device without concurrent managed access: the corpus
    gets a twin.
  3. A twin's GPU lanes park at a budget, so no launch meets the display
    driver's watchdog.
  4. A twin's ring sync copies the slots written since the last sync, so a
    take racing a push no longer reads a dead task (the race found by
    @costamatheus97; twin-only).
  5. A twin's copies run pinned: gpu_alias pins each 8 MiB step it carries
    hot.
  6. A twin's ring sync mends the slots the other side took, so no busy
    turn's planes cross whole (it replaces commit 4's copy, which cost a
    busy program about 15%, with @costamatheus97's mend; twin-only).
  7. A twin's heap splits between the host and the lanes, so a turn leaves the
    host's pages alone.

Review notes

  • The only shared code this PR modifies is the allocator's (commit 7),
    and every change is behind an #if.
    heap_alloc_miss, heap_free and
    bank_at change only under BEND_SPLIT, which only a twin's NVRTC program
    sets, or TWIN_SPLIT, the CUDA host build's. The CPU build's assembly and
    the managed CUDA program's cubin are byte-identical to main's; Metal
    sets neither (TWIN_SPLIT is 0 off CUDA; not built here); in the CUDA
    host build a managed device never takes the split's branches.
  • The SIGSEGV handler calls CUDA. It is safe here because only a host
    touch of a stale chunk causes that fault, on the thread that touched it.
  • CUDA now has two memory models. The twin runs only when the device
    reports no concurrent managed access; everything else is main's.
  • The twin duplicates the HIP lane's design (! on AMD GPUs: an experimental HIP/ROCm lane works #891). It follows the same
    design on the CUDA driver API; a shared twin layer is possible if HIP
    merges.
  • Parking depends on gpu_twin, not on the watchdog attribute. A Linux
    desktop that shows X on the GPU does not get it yet.
  • One kind of loop cannot park. The compiler emits some pure sequential
    loops as a plain C loop (spin_N, from emit_native), which has no point
    where a lane can stop and save its state. One such loop in a ! must
    still end within 2 s, and the README states this. For that case, a
    warning or a CPU fallback (A ! call with no available parallelism holds the GPU indefinitely and crashes macOS (WindowServer watchdog) #942) is the right fix.

Request

Please run the gates (test and perf) on the minis for Metal, and on a
native Linux CUDA machine if one is available. These were not tested here.

Evidence

On the branch rebased onto Bend 2.0.29 (574b6d39); times are medians of 5
runs, with the range.

Check Result
Linux CPU assembly against main (clang 20 -O3 -S), 16 runtime benches and 19 ! tests 35 of 35 byte-identical
CUDA device program without a twin against main (NVRTC 13.3, sm_89), the same 35 35 of 35 cubins byte-identical
! tests on WSL2 (RTX 4050): default, --gpu off, --gpu 512MB 20 of 20 tests, 60 of 60 runs
queens size 18 on WSL2 Same answer as the cores, no driver reset; 12.3 s (10.5–14.7), the cores 9.3 s
gpu_turns with a tree of 2^24 leaves, four ! turns, WSL2 Same answer as the cores; 1.7 s (1.2–2.5), the cores 0.33 s
Parking with the budget forced to 0.2 ms (WDDM): queens size 18 (2,501 resume rounds), the 2^24 tree (4), 5 ! tests Same results as the cores
Ring audit of both sides' lap words after each sync: gpu_turns, a 2^20 tree folded 24 times on each side, and Slash Boss's probe Stale slots before commit 4: 468,960, 22.1M and 209M on the host (and 24.8M on the device in Slash); none after commits 4 and 6
Slash Boss's probe frame (SLASH_PROBE=300), WSL2, before and after commit 5 42.9 to 35.3 ms (draw 29.1 to 24.0), output unchanged; the other checks hold
Slash Boss's probe frame, WSL2: no ring fix, commit 5 and commit 6, interleaved 29.2, 33.6 and 29.4 ms
Commit 7 on WSL2: Slash Boss's probe frame; particles (@costamatheus97's demo) at 16K, 64K, 262K with host binning; the tree of 2^24 leaves and the folded 2^20 tree 29.9 -> 14.2 ms; 22.9 -> 15.3, 52.9 -> 40.7, 171.8 -> 123.4 ms; 1.07 -> 1.33 s and 0.66 -> 0.76 s (they walk everything the device built)

Not tested: Metal, native Linux CUDA, the perf gate.

🤖 Generated with Claude Code

MattCozendey added a commit to MattCozendey/bend that referenced this pull request Sep 25, 2026
…the register cap

The previous merge of feature/cuda-twin was reverted; this one takes the
refactored series (bendlang#1078) and the main it sits on. Where the two met:
v3's two-word Env, then the park registers' defines; the _WIN32 pool
group, then the POSIX trap for a twin; main's process_drain after
process_run's _WIN32 group; the README's watchdog line for Windows and
WSL; main.ts builds the lane on Windows (no !win on bangs). The connecting
commit's pool_fault hook and setenv still stand. comp.ts's cap goes to
74000, process_run.c's to 5000.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@SiliconState

SiliconState commented Sep 26, 2026 •

Copy link
Copy Markdown

Tested c357d59 on WSL2 with an RTX 5090 (sm_120), driver 610.62, CUDA 13.1 / NVRTC 13.1.115, clang 21.1.8, Bun 1.3.14.

  • Without LIBRARY_PATH, ! programs build and ldd resolves libcuda.so.1 to /usr/lib/wsl/lib/libcuda.so.1; main 11c65a2 still fails with cannot find -lcuda.
  • Every program below prints the same answer with --gpu 512MB --threads 2 as with --gpu off, exits 0 with empty stderr. Runs were serial, and nvidia-smi showed 0 MiB used after each.

Turns and copies were counted from outside with an LD_PRELOAD shim that forwards cuMemAlloc, cuMemcpyHtoD, cuMemcpyDtoH and cuLaunchKernel unchanged; a turn is a header upload to the twin that no launch follows (gpu_sync(true)). It counts 1 turn for a one-! program and 10 for the ten-turn one, with 4 launches per turn.

Program ! calls Output GPU turns Up MiB Down MiB
tests/run/gpu_turns.bend 4 691345207 2 20.5 40.0
same, h built sequentially 4 691345207 4 60.8 64.0
#1066's ten-turn repro 10 854543159 10 181.5 184.0

The new test doesn't exercise what its comment describes. h = gen(10n, 3) forks on the host, and after that fork corpus_eval leaves its seam loop for the CPU cube (task_deal, pool_open, cube_run(H, false), break), so flip!(Nd{t3, h}) and sum! run on the cores (#1065), at --threads 1 too. The turn where the host allocates a fresh tree and the device mirrors and folds both never reaches the device, and the answer is still right, so a stale chunk there would go unnoticed. Building h sequentially keeps the answer and reaches all four turns:

def gens(+n: Nat, +x: U32) -> Tr:
  match n:
    case 0n:
      Lf{x}
    case 1n+p:
      l = gens(p, U32.shl(x))
      r = gens(p, U32.inc(U32.shl(x)))
      Nd{l, r}

with h = gens(10n, 3).

Not tested here: the default --gpu span, parking (nothing ran near the watchdog), native Linux, Metal, and the gates.

@MattCozendey

Copy link
Copy Markdown
Contributor Author

Thank you for the review and the shim. You are correct.

gen forks on the host. After the fork, corpus_eval sends all remaining ! calls to the CPU (#1065). Thus the test did not run the mixed host/device turn on the device.

I got the same numbers on WSL2 (RTX 4050 Laptop, driver 610.62):

  • As written: 2 turns, 20.5 / 40.0 MiB.
  • With gens: 4 turns, 60.8 / 64.0 MiB.

2088344 uses your fix: h = gens(10n, 3). The expected output does not change. A comment refers to #1065.

I also tested two items from your list:

  • Default span: gen!(n) then sum! for n = 20, 22, 24. The output is the same as --gpu off.
  • Parking: a sequential recursion (cnt!(n, 1)) parks 17 times at n = 22 and 70 times at n = 24. The output is the same as --gpu off. A pure spin_N loop cannot park (see comp.ts). This GPU does not have the display watchdog, so I did not test a real TDR stop.

Not tested: native Linux, Metal, the gates.

@costamatheus97

Copy link
Copy Markdown
Contributor

Nice to see the twin on CUDA. It's the design from my HIP lane (#891), and I think gpu_rings has a race that I hit and fixed there.

gpu_rings copies only each ring's live slots, [get, put). A slot that one side pushed and took between two syncs never crosses, so the other side keeps that slot's lap word from an older lap. When that side's put comes round to the slot, a take racing the push reads the stale word as the new lap's and takes a dead task.

I ran your CUDA host code over HIP on an RX 7800 XT under WSL2, through a small cuda.h/nvrtc.h shim. I have no NVIDIA card, so this is not an NVIDIA run.

  • An audit of both sides after each sync found 36.4M stale host slots and 1.6M stale device slots on particles at 64K. gpu_turns (before 2088344) had 65,528 stale host slots.
  • Actual failures depend on the L2. At CUBE_LOG 7, which an L2 of 8 MB or more gives (your 4050's 24 MB included), particles at 64K with host binning on 16 threads failed 14 of 16 runs: 10 out of memory, 3 "frontier drained without a result", 1 timeout. At CUBE_LOG 6 it passed 28/28, so it can hide.

I opened MattCozendey#1 against your branch.

  • Commit 1 is the fix. After each sync it gives the slots taken since the last one their own lap and no task, which is what a take leaves. The host does this for its copy, and a small ring_mend_dev kernel does it for the device's. With it, the audit finds no stale slot, the run passes 16/16, and the 21 bang tests pass.
  • Commits 2–6 are optional speedups from the same lane: no copies for empty banks, 256 KB chunks, one fault per write, per-chunk locks and prefetch by bang. They make host binning 1.5–1.9x faster on particles. On gpu_turns at 2088344, they halve copy time (75 → 37 ms) with the same output.

Each commit stands alone, so take just commit 1 if the ttok budget is tight (it's +600). It would be great if you could check it on the 4050, since that's the configuration where the race should actually land.

MattCozendey and others added 7 commits September 26, 2026 11:24
…'s is off the linker's path

WSL keeps the driver's libcuda in /usr/lib/wsl/lib, which the loader finds
through ld.so.conf but ld does not search for -lcuda: a ! program failed
to build there ("cannot find -lcuda") instead of running on the cores.
$CUDA_HOME/lib64/stubs goes after the other -L paths, so a driver's
libcuda on them still wins; the stub's soname is libcuda.so.1, and the
loader maps the driver's at run time. The same holds for a machine that
builds with the toolkit and no driver.

On WSL (Ubuntu 24.04, CUDA 13.3, clang 22, RTX 4050 Laptop), without
LIBRARY_PATH: tests/run/gpu_turns builds, ldd names
/usr/lib/wsl/lib/libcuda.so.1, and --gpu off prints its line. Not run:
native Linux.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…us gets a twin

Under WDDM (WSL2, and native Windows) a device reports no concurrent
managed access: a managed block is made resident whole at each launch,
and the CUDA lane's one managed corpus the size of VRAM took the video
memory manager down (bugcheck 0x10E on an RTX 4050 Laptop). The probe
refused such a device, so a ! there ran on the cores.

Such a device (a discrete Pascal or later) now gets a twin: device memory
as large as the host's corpus. Every Loc is an index and the host never
runs during a turn, so a turn copies up what it can touch and down after
it. The heap is lazy, in 2 MB chunks: after a turn each is stale (no
access); a host touch downloads it clean (read-only), a host write makes
it dirty, and the next turn uploads only the dirty ones. The host's corpus
is two mappings of one section, so a fault fills its chunk through the
second while the first still traps; the trap runs on a 1 MB signal stack,
as it calls CUDA. The twin's span stops an eighth of the free memory (at
least 512 MB) short of it. A pass the display driver's watchdog stops
fails with its own message. The .gpu is read whole, without mmap, on every
host. A twin fills a window's frame on the cores.

A device with concurrent managed access keeps the managed corpus, its span
and its signal stack; Metal is untouched, and the Linux CPU build's
assembly is main's. tests/run/gpu_turns pins three !
turns with the host rebuilding a device-built tree between them. comp.ts's
cap goes from 64000 to 66500.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…river's watchdog

WDDM (WSL2, native Windows) stops a launch past about 2 s (the TDR; the
RTX 4050 Laptop reports kernel exec timeout 1), and a drain runs each
lane's whole subtree in one launch. A twin's program is now built with
-DBEND_PARK: a lane past its budget parks at its next jump (WL_GO, the
emitter's WL_JMP with the K registers it fills), return or self-jump: the
K registers go on its own stack, its fid, seq, rn, K and depth into two
words under the stack, and it replies TERM_HOLE. A later pass 1 resumes it
before its ring (monk_step takes no ring task then, and its join deals
across the bag); a grow pass skips it. gpu_pass relaunches drains while
H_PARKED counts a parked lane. The budget is GPU_WALL (250 ms) over the
waves of groups the device fits at once, timed by %globaltimer from each
group's start. A pure spin_N's loop cannot park: one pure loop in a ! must
end within the watchdog, which the README states.

Linux and Metal: the host's WL_GO and WL_AGAIN(F, K) expand as WL_JMP and
WL_AGAIN(F) did, and the plain device program is main's. The twin's
program is not held to the plain one's register count: on WSL2 (RTX 4050)
queens runs in 13.9 s at size 18 against 15.7 to 17.8 s with the cap,
whose spills cost more than the groups it kept. comp.ts's cap goes
from 66500 to 68500.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A fork on the host sends every later ! to the cores (bendlang#1065): with
h = gen(10n, 3) the last flip! and sum! ran on the CPU, and the turn
that mixes a host-built tree with a device-built one was never checked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@MattCozendey
MattCozendey force-pushed the feature/cuda-twin-refactor branch from 2088344 to 6a9914d Compare September 26, 2026 14:45
… take racing a push no longer reads a dead task

gpu_rings copied each ring's live slots, [get, put), and nothing else. A
slot one side pushed and took between two syncs never crossed, so the
other side kept that slot's word from an older lap; when that side's put
came round to it, a take racing the push read the old word as the new
lap's and took a dead task (found by Matheus Costa on bendlang#1078, with the
fix on his HIP lane, bendlang#891). gpu_rings now keeps each ring's put at the
last sync (from) and copies [from, put), or [get, put) when get is older:
the slots taken since cross with their own lap. The fix stays in the
twin's host code: no mend, no kernel, and Linux, Metal and the managed
CUDA program are as before.

An audit comparing both sides' lap words after each sync (WSL2, RTX 4050
Laptop, CUBE_LOG 7) found stale host slots on every sync, 468,960 on
gpu_turns and 22.1M on a tree of 2^20 leaves folded 24 times on the
device and 24 on the host; none with the fix, and every answer as
before. Times within run noise on both (medians of 5).

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@MattCozendey

Copy link
Copy Markdown
Contributor Author

@costamatheus97 Thank you. You are right, and the race still holds after this branch's rebase onto 2.0.29.

On WSL2 with an RTX 4050 Laptop (CUBE_LOG 7), I ran an audit of both sides' lap words after each sync. It found stale host slots on every sync:

  • gpu_turns: 468,960.
  • A tree of 2^20 leaves folded 24 times on the device and 24 on the host: 22.1M.

Every answer was still right, so the race did not land in these runs, but the stale slots are there.

I have committed the fix to this PR's branch in a twin-only form, with you as co-author. It lands with my next push, together with the rebase onto 2.0.29. gpu_rings now copies each ring's slots from its put at the last sync, instead of from get. So the slots one side pushed and took since then cross with their own lap. It needs no mend and no kernel, so the managed CUDA program and Metal stay byte-identical to main, which this PR's claim for Linux and Metal rests on. With it, the same audit finds no stale slot, and the times are within noise.

I'll review commits 2–6 shortly.

… hot

Every twin copy goes through gpu_alias, and a step of it that has carried
twice its size is pinned with cuMemHostRegister (2 GiB at most; a refusal
ends pinning). A copy is cut at the steps, since CUDA refuses one across
two registrations. A pinned copy runs about twice as fast as a pageable
one and a pin costs about two copies, so a loop that crosses the seam each
frame pins its heap in a few frames, and a single run pins nothing. The
steps are 8 MiB: natively on Windows, a 64 MiB pin stalled a frame 10 to
22 ms, an 8 MiB one about 2.

WSL2, RTX 4050 Laptop, medians of 5: Slash Boss's probe frame
(SLASH_PROBE=300) goes from 42.9 to 35.3 ms (draw 29.1 to 24.0), its
output unchanged; fold24, a tree of 2^24 leaves, gpu_turns and queens at
size 18 hold. Only a twin runs this code: Metal, and CUDA with managed
memory, are untouched.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@MattCozendey

Copy link
Copy Markdown
Contributor Author

@costamatheus97 As promised, I reviewed commits 2–6. Thank you for them, and for taking the time to port them from your HIP lane.

I measured each prefix of MattCozendey#1 on the RTX 4050 Laptop (WSL2, NVIDIA, no shim), on 2088344c as you built them. Times are medians of 5 interleaved runs; every run matched --gpu off.

gpu_turns tree of 2^24 leaves a 2^20 tree, 24 device + 24 host rounds
2088344c 0.323 s 1.209 s 0.717 s
+ 1 ring mend 0.372 s 1.072 s 0.744 s
+ 2 empty banks 0.322 s 1.099 s 0.678 s
+ 3 256 KB chunks 0.313 s 1.195 s 0.748 s
+ 4 write faults 0.311 s 1.189 s 0.732 s
+ 5 chunk locks 0.312 s 1.198 s 0.739 s
+ 6 prefetch 0.300 s 1.168 s 0.653 s
  • Commit 1 is superseded by the twin-only fix above, which clears the same audit without a kernel.
  • Commit 3 measured slower here: about 10% on both tree workloads against commit 2. I haven't counted faults, but a host walk of a device-built tree takes 8x as many at 256 KB, which is the likely cause. On this card the 2 MB chunk stays.
  • Commits 2, 4 and 5 are within noise on these workloads. Your largest gains are on particles with host binning, which isn't public, so I couldn't reproduce that case. If you can share it, I'd like to run it on NVIDIA.
  • Commit 6 is the one that shows here: about 9% on the folded tree.

This PR now also pins the twin's copies (commit 5 in the description): Slash Boss's probe frame goes from 42.9 to 35.3 ms on WSL2. That speeds up the copies themselves, so it doesn't overlap with 2–6.

For now, 2–6 stay out of #1078, to keep its scope, and its claim that Linux and Metal are untouched, easy to review. Commit 6 in particular looks like a good fit for later work on the twin's sync cost.

@SiliconState

Copy link
Copy Markdown

Re-tested the current head 617c897 on the RTX 5090: WSL2, driver 610.62, CUDA 13.1, sm_120. Its L2 is 96 MiB, so gpu_shape gives CUBE_LOG 7 here too, as on your 4050. Built from the tarball without LIBRARY_PATH, run with --gpu 512MB --threads 2, one run at a time. Every output equals --gpu off, stderr is empty, and nvidia-smi showed 0 MiB used before and after the runs. Counts come from the same pass-through shim, which now also wraps cuMemHostRegister.

Program GPU turns Output Up MiB Down MiB Pinned Copy ms at 9d4eaef (2 runs) Copy ms at 617c897 (7 runs)
tests/run/gpu_turns.bend 4 of 4 691345207 60.8 69.0 2 steps, 16 MiB, about 1 ms 31 to 32 30 to 40
#1066's ten-turn repro 10 of 10 854543159 181.5 199.5 5 steps, 40 MiB, 6.5 to 7.4 ms 83 60 to 71
  • Copy ms is time inside cuMemcpyHtoD/cuMemcpyDtoH as seen by the shim; pin time is separate. In the three ten-turn runs where I also timed the pins, copies plus pins came to 67 to 76 ms, against 83 ms before. Treat it as indicative, since the builds were measured in separate sessions.
  • cuMemHostRegister never failed and no copy was refused. Cutting copies at the steps matters on this driver too: earlier I found that a copy starting in a pinned range must stay inside one registration, and adjacent registrations don't merge.
  • Against c357d59, downloads grew by 0.6 MiB (1 turn) up to 15.5 MiB (10 turns) per run, with uploads unchanged. That's consistent with the ring fix copying the slots written since the last sync.
  • A parallel host phase makes later ! calls silently run on CPU instead of GPU #1065 is unchanged, as expected: the parallel host variant still reaches 1 of 3 turns.

This isn't a stress test of the ring race. I didn't run the lap audit or a host-binning workload, only these programs.

…rn's planes cross whole

The ring fix (9d4eaef) copied every slot plane some ring wrote since the
last sync. At CUBE_LOG 7 the rings are 128 MiB of planes, and a busy turn
touches most of them: Slash Boss's probe frame went from 29.2 to 33.6 ms
on WSL2 (RTX 4050), 13.2 to 17.6 natively on Windows, where syncs come
each frame. The sync copies the live planes again, and the side a copy
lands on mends the slots the other took since the last sync: each gets its
lap and no task, as a take leaves it (ring_mend, after the mend on
Matheus Costa's HIP lane). Down the host mends its own copy; up
ring_mend_dev, a lane a ring, mends the device's, launched only when the
host took some. The kernel is under BEND_PARK and the helper in the CUDA
host and the twin's program only, so Linux, Metal and the managed CUDA
program are as before.

WSL2, RTX 4050 Laptop, medians of 5: Slash's probe frame 29.4 ms (18.5 ms
draw), against 29.2 without a fix and 33.6 copying the planes; fold24, a
tree of 2^24 leaves and gpu_turns hold. The lap audit finds no stale slot
on either side after any sync in gpu_turns, fold24 and Slash (without a
fix, Slash leaves 209M on the host and 24.8M on the device over 240
syncs), and Slash's output is the same in every run.

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01G3htSNrQ8QKbP6S7zmTV9B
@MattCozendey

Copy link
Copy Markdown
Contributor Author

A correction on the ring fix, and a follow-up (@costamatheus97, @SiliconState).

I said the twin-only fix's times were within noise. That was wrong for busy programs. It copied every slot plane some ring wrote since the last sync. At CUBE_LOG 7 that's up to 128 MiB per sync, and a program that syncs every frame pays it each time: Slash Boss's probe frame on WSL2 went from 29.2 ms without a fix to 33.6 ms with it. @SiliconState's shim saw the same cost from outside, as the extra 0.6 to 15.5 MiB of downloads per run. I had only timed programs with a few dozen syncs.

Commit 6 (e9c0b4c) replaces the copy with a mend, as in @costamatheus97's commit 1. The sync copies only the live planes again, and the side a copy lands on gives the slots the other side took since the last sync their own lap, as a take leaves them:

  • Down: the host mends its own copy.
  • Up: ring_mend_dev mends the device's copy, a lane a ring, launched only when the host took slots.

Unlike commit 1 as written, the kernel and its helper sit under BEND_PARK (the twin's program) and the CUDA host build, so Linux, Metal and the managed CUDA program stay byte-identical to main. So the mend was the right approach; I should have taken it in this form from the start.

WSL2, RTX 4050 Laptop, medians of 5 interleaved runs:

Build Slash probe frame Stale slots after the syncs (lap audit)
no ring fix 29.2 ms Slash: 209M host, 24.8M device over 240 syncs
commit 5 (copying the planes) 33.6 ms none
commit 6 (the mend) 29.4 ms none, in gpu_turns, the folded tree and Slash

So the device side does go stale in a real program. My earlier runs, all on small programs, only ever showed the host side. gpu_turns, the folded tree and a tree of 2^24 leaves hold, and Slash's output is the same in every run.

@SiliconState, thank you for re-testing on the 5090. Your pinning numbers match what I see here. At e9c0b4c the extra downloads you measured should be gone (the live planes again, plus a 64 KiB upload when the host took slots), though I haven't counted bytes myself.

@costamatheus97

Copy link
Copy Markdown
Contributor

@MattCozendey Thank you for checking this on the 4050, and for measuring 2–6 on real NVIDIA hardware. The mend under BEND_PARK is a better shape than my commit 1: it keeps Linux, Metal and the managed program byte-identical to main, which I didn't think to do. Thanks also for the co-author credit. @SiliconState, thank you for the 5090 runs.

Here is the particles demo: https://github.com/costamatheus97/bend/tree/723084baaa0267ee086f5c223c60b8a0084961d7/demos/app_particles_2d

Only main.bend and clock.c are needed. They're the exact files I measured #1078 with. Copy them into demos/app_particles_2d/ on your branch. The demo imports ../app_slash_boss_3d/bend3d.bend, which is the same on main and on #1078. Then:

bun bend2/main.ts demos/app_particles_2d/main.bend -o /tmp/particles
PARTICLES_SCALE=65536 PARTICLES_PROBE=24 PARTICLES_BIN=host /tmp/particles --threads 16 --gpu 3GB
  • PARTICLES_SCALE is 1024, 16384, 65536 or 262144 for the scales in my table.
  • PARTICLES_BIN is host or device.
  • PARTICLES_PROBE=N runs N frames without a window, after 8 warmup frames, and prints the checksum.

The race failures I saw were at 65536 with host binning on 16 threads.

Your commit 3 result makes sense. A 256 KB chunk downloads less per fault but takes more faults. My guess, which I haven't measured, is that a fault and a copy cost less relative to bytes on rocdxg than on your driver, so the smaller downloads won here and the extra faults cost more there. If the particles numbers show the same split, the chunk size may need to depend on the platform rather than be fixed.

I agree with keeping 2–6 out of #1078. I'll close MattCozendey#1, and once #1078 lands, I can open the prefetch (6) against main as its own PR.

… the host's pages alone

A twin's lanes allocated in the host's pages, so every turn made nearly
every chunk the host held stale, and the host downloaded all of it again:
Slash Boss's probe took 17.8 GiB of downloads over 300 frames on WSL2.
The heap now splits (BEND_SPLIT): the host's pages from the bottom, the
lanes' from the top, a node of the lanes the host frees going back to them
and one of the host's a lane frees coming back, up to 4096 a turn (past
it the side that frees keeps the node: a walk that rebuilds what it reads
reuses it while it's in cache). A turn stales only the chunks it changed
(twin_sum's two sums before and after), the sync walks only the chunks the
host holds, a chunk the host writes each turn stays dirty, and a slice of
the clean ones cools each up. Split, the chunks are 256 KiB (64 KiB made a
walk's faults cost more, 2 MiB lost Slash a millisecond). A window's frame
fills on the device on a twin too.

The only shared code it touches is the allocator's (heap_alloc_miss,
heap_free, bank_at), each change under #if BEND_SPLIT, which only a twin's
NVRTC program sets, or TWIN_SPLIT, the CUDA host build's: the CPU build,
Metal and the managed CUDA program compile as before, and in the CUDA host
build a managed device never takes the split's branches.

WSL2, RTX 4050 Laptop, medians of 5 interleaved runs against the head
before: Slash Boss's probe frame 29.9 -> 14.2 ms; particles (Matheus
Costa's demo) 22.9 -> 15.3 ms at 16K with host binning, 52.9 -> 40.7 at
64K, 171.8 -> 123.4 at 262K, and 39.5 -> 35.7, 63.6 -> 60.6, 184.6 -> 173.7
with device binning; gpu_turns holds (0.31 -> 0.32 s); a tree of 2^24
leaves 1.07 -> 1.33 s and a 2^20 tree folded 24 times on each side
0.66 -> 0.76 s, which walk everything the device built. Every output is the
same as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01G3htSNrQ8QKbP6S7zmTV9B
@VictorTaelin

Copy link
Copy Markdown
Contributor

Thank you for the careful twin and the numbers. We closed #1066 for capacity reasons: a device-resident copy of the corpus is a second memory model that we cannot maintain. --gpu on will name the missing capability instead. WONTFIX.txt: "! runs on the CPU on WSL2 GPUs (#1066)".

Note: an AI wrote this reply after I made the decision. If anything here is wrong, reply and I will review it myself.

@MattCozendey

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I could mantain this myself, as it's neatly separated from the surrounding code and poses no risk to it; but if you insist I will just keep it on my own fork.

MattCozendey added a commit to MattCozendey/bend that referenced this pull request Sep 27, 2026
… take racing a push no longer reads a dead task

gpu_rings copied each ring's live slots, [get, put), and nothing else. A
slot one side pushed and took between two syncs never crossed, so the
other side kept that slot's word from an older lap; when that side's put
came round to it, a take racing the push read the old word as the new
lap's and took a dead task (found by Matheus Costa on bendlang#1078, with the
fix on his HIP lane, bendlang#891). gpu_rings now keeps each ring's put at the
last sync (from) and copies [from, put), or [get, put) when get is older:
the slots taken since cross with their own lap. The fix stays in the
twin's host code: no mend, no kernel, and Linux, Metal and the managed
CUDA program are as before.

An audit comparing both sides' lap words after each sync (WSL2, RTX 4050
Laptop, CUBE_LOG 7) found stale host slots on every sync, 468,960 on
gpu_turns and 22.1M on a tree of 2^20 leaves folded 24 times on the
device and 24 on the host; none with the fix, and every answer as
before. Times within run noise on both (medians of 5).

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Sep 28, 2026
… take racing a push no longer reads a dead task

gpu_rings copied each ring's live slots, [get, put), and nothing else. A
slot one side pushed and took between two syncs never crossed, so the
other side kept that slot's word from an older lap; when that side's put
came round to it, a take racing the push read the old word as the new
lap's and took a dead task (found by Matheus Costa on bendlang#1078, with the
fix on his HIP lane, bendlang#891). gpu_rings now keeps each ring's put at the
last sync (from) and copies [from, put), or [get, put) when get is older:
the slots taken since cross with their own lap. The fix stays in the
twin's host code: no mend, no kernel, and Linux, Metal and the managed
CUDA program are as before.

An audit comparing both sides' lap words after each sync (WSL2, RTX 4050
Laptop, CUBE_LOG 7) found stale host slots on every sync, 468,960 on
gpu_turns and 22.1M on a tree of 2^20 leaves folded 24 times on the
device and 24 on the host; none with the fix, and every answer as
before. Times within run noise on both (medians of 5).

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Sep 28, 2026
… take racing a push no longer reads a dead task

gpu_rings copied each ring's live slots, [get, put), and nothing else. A
slot one side pushed and took between two syncs never crossed, so the
other side kept that slot's word from an older lap; when that side's put
came round to it, a take racing the push read the old word as the new
lap's and took a dead task (found by Matheus Costa on bendlang#1078, with the
fix on his HIP lane, bendlang#891). gpu_rings now keeps each ring's put at the
last sync (from) and copies [from, put), or [get, put) when get is older:
the slots taken since cross with their own lap. The fix stays in the
twin's host code: no mend, no kernel, and Linux, Metal and the managed
CUDA program are as before.

An audit comparing both sides' lap words after each sync (WSL2, RTX 4050
Laptop, CUBE_LOG 7) found stale host slots on every sync, 468,960 on
gpu_turns and 22.1M on a tree of 2^20 leaves folded 24 times on the
device and 24 on the host; none with the fix, and every answer as
before. Times within run noise on both (medians of 5).

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Sep 30, 2026
… take racing a push no longer reads a dead task

gpu_rings copied each ring's live slots, [get, put), and nothing else. A
slot one side pushed and took between two syncs never crossed, so the
other side kept that slot's word from an older lap; when that side's put
came round to it, a take racing the push read the old word as the new
lap's and took a dead task (found by Matheus Costa on bendlang#1078, with the
fix on his HIP lane, bendlang#891). gpu_rings now keeps each ring's put at the
last sync (from) and copies [from, put), or [get, put) when get is older:
the slots taken since cross with their own lap. The fix stays in the
twin's host code: no mend, no kernel, and Linux, Metal and the managed
CUDA program are as before.

An audit comparing both sides' lap words after each sync (WSL2, RTX 4050
Laptop, CUBE_LOG 7) found stale host slots on every sync, 468,960 on
gpu_turns and 22.1M on a tree of 2^20 leaves folded 24 times on the
device and 24 on the host; none with the fix, and every answer as
before. Times within run noise on both (medians of 5).

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Oct 1, 2026
… take racing a push no longer reads a dead task

gpu_rings copied each ring's live slots, [get, put), and nothing else. A
slot one side pushed and took between two syncs never crossed, so the
other side kept that slot's word from an older lap; when that side's put
came round to it, a take racing the push read the old word as the new
lap's and took a dead task (found by Matheus Costa on bendlang#1078, with the
fix on his HIP lane, bendlang#891). gpu_rings now keeps each ring's put at the
last sync (from) and copies [from, put), or [get, put) when get is older:
the slots taken since cross with their own lap. The fix stays in the
twin's host code: no mend, no kernel, and Linux, Metal and the managed
CUDA program are as before.

An audit comparing both sides' lap words after each sync (WSL2, RTX 4050
Laptop, CUBE_LOG 7) found stale host slots on every sync, 468,960 on
gpu_turns and 22.1M on a tree of 2^20 leaves folded 24 times on the
device and 24 on the host; none with the fix, and every answer as
before. Times within run noise on both (medians of 5).

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Oct 2, 2026
… take racing a push no longer reads a dead task

gpu_rings copied each ring's live slots, [get, put), and nothing else. A
slot one side pushed and took between two syncs never crossed, so the
other side kept that slot's word from an older lap; when that side's put
came round to it, a take racing the push read the old word as the new
lap's and took a dead task (found by Matheus Costa on bendlang#1078, with the
fix on his HIP lane, bendlang#891). gpu_rings now keeps each ring's put at the
last sync (from) and copies [from, put), or [get, put) when get is older:
the slots taken since cross with their own lap. The fix stays in the
twin's host code: no mend, no kernel, and Linux, Metal and the managed
CUDA program are as before.

An audit comparing both sides' lap words after each sync (WSL2, RTX 4050
Laptop, CUBE_LOG 7) found stale host slots on every sync, 468,960 on
gpu_turns and 22.1M on a tree of 2^20 leaves folded 24 times on the
device and 24 on the host; none with the fix, and every answer as
before. Times within run noise on both (medians of 5).

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Oct 2, 2026
… take racing a push no longer reads a dead task

gpu_rings copied each ring's live slots, [get, put), and nothing else. A
slot one side pushed and took between two syncs never crossed, so the
other side kept that slot's word from an older lap; when that side's put
came round to it, a take racing the push read the old word as the new
lap's and took a dead task (found by Matheus Costa on bendlang#1078, with the
fix on his HIP lane, bendlang#891). gpu_rings now keeps each ring's put at the
last sync (from) and copies [from, put), or [get, put) when get is older:
the slots taken since cross with their own lap. The fix stays in the
twin's host code: no mend, no kernel, and Linux, Metal and the managed
CUDA program are as before.

An audit comparing both sides' lap words after each sync (WSL2, RTX 4050
Laptop, CUBE_LOG 7) found stale host slots on every sync, 468,960 on
gpu_turns and 22.1M on a tree of 2^20 leaves folded 24 times on the
device and 24 on the host; none with the fix, and every answer as
before. Times within run noise on both (medians of 5).

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Oct 3, 2026
… take racing a push no longer reads a dead task

gpu_rings copied each ring's live slots, [get, put), and nothing else. A
slot one side pushed and took between two syncs never crossed, so the
other side kept that slot's word from an older lap; when that side's put
came round to it, a take racing the push read the old word as the new
lap's and took a dead task (found by Matheus Costa on bendlang#1078, with the
fix on his HIP lane, bendlang#891). gpu_rings now keeps each ring's put at the
last sync (from) and copies [from, put), or [get, put) when get is older:
the slots taken since cross with their own lap. The fix stays in the
twin's host code: no mend, no kernel, and Linux, Metal and the managed
CUDA program are as before.

An audit comparing both sides' lap words after each sync (WSL2, RTX 4050
Laptop, CUBE_LOG 7) found stale host slots on every sync, 468,960 on
gpu_turns and 22.1M on a tree of 2^20 leaves folded 24 times on the
device and 24 on the host; none with the fix, and every answer as
before. Times within run noise on both (medians of 5).

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Oct 6, 2026
… take racing a push no longer reads a dead task

gpu_rings copied each ring's live slots, [get, put), and nothing else. A
slot one side pushed and took between two syncs never crossed, so the
other side kept that slot's word from an older lap; when that side's put
came round to it, a take racing the push read the old word as the new
lap's and took a dead task (found by Matheus Costa on bendlang#1078, with the
fix on his HIP lane, bendlang#891). gpu_rings now keeps each ring's put at the
last sync (from) and copies [from, put), or [get, put) when get is older:
the slots taken since cross with their own lap. The fix stays in the
twin's host code: no mend, no kernel, and Linux, Metal and the managed
CUDA program are as before.

An audit comparing both sides' lap words after each sync (WSL2, RTX 4050
Laptop, CUBE_LOG 7) found stale host slots on every sync, 468,960 on
gpu_turns and 22.1M on a tree of 2^20 leaves folded 24 times on the
device and 24 on the host; none with the fix, and every answer as
before. Times within run noise on both (medians of 5).

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Oct 9, 2026
… take racing a push no longer reads a dead task

gpu_rings copied each ring's live slots, [get, put), and nothing else. A
slot one side pushed and took between two syncs never crossed, so the
other side kept that slot's word from an older lap; when that side's put
came round to it, a take racing the push read the old word as the new
lap's and took a dead task (found by Matheus Costa on bendlang#1078, with the
fix on his HIP lane, bendlang#891). gpu_rings now keeps each ring's put at the
last sync (from) and copies [from, put), or [get, put) when get is older:
the slots taken since cross with their own lap. The fix stays in the
twin's host code: no mend, no kernel, and Linux, Metal and the managed
CUDA program are as before.

An audit comparing both sides' lap words after each sync (WSL2, RTX 4050
Laptop, CUBE_LOG 7) found stale host slots on every sync, 468,960 on
gpu_turns and 22.1M on a tree of 2^20 leaves folded 24 times on the
device and 24 on the host; none with the fix, and every answer as
before. Times within run noise on both (medians of 5).

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
MattCozendey added a commit to MattCozendey/bend that referenced this pull request Oct 11, 2026
… take racing a push no longer reads a dead task

gpu_rings copied each ring's live slots, [get, put), and nothing else. A
slot one side pushed and took between two syncs never crossed, so the
other side kept that slot's word from an older lap; when that side's put
came round to it, a take racing the push read the old word as the new
lap's and took a dead task (found by Matheus Costa on bendlang#1078, with the
fix on his HIP lane, bendlang#891). gpu_rings now keeps each ring's put at the
last sync (from) and copies [from, put), or [get, put) when get is older:
the slots taken since cross with their own lap. The fix stays in the
twin's host code: no mend, no kernel, and Linux, Metal and the managed
CUDA program are as before.

An audit comparing both sides' lap words after each sync (WSL2, RTX 4050
Laptop, CUBE_LOG 7) found stale host slots on every sync, 468,960 on
gpu_turns and 22.1M on a tree of 2^20 leaves folded 24 times on the
device and 24 on the host; none with the fix, and every answer as
before. Times within run noise on both (medians of 5).

Co-Authored-By: Matheus Costa <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[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.

4 participants