~/bend-docscommunity

heap.bend relies on unsafe/foreign

raw source on the hub · import bend-gpu-heap@0.1.0.0/heap.bend as Heap

bend-gpu-heap: move Bend's GPU heap back to the CPU in one bulk prefetch, so reusing memory a GPU kernel touched is cheap. import bend-gpu-heap@<version>/heap.bend as HP

Heap.home(mib): moves the first mib MiB of Bend's heap (all of it for 0) to the CPU in one cuMemPrefetchAsync, and answers how many MiB it covered. Why: in a native CUDA build Bend's heap is CUDA managed memory, and the runtime creates arrays (Array.new, and anything the CPU writes) on the CPU. Heap pages a GPU kernel used, handed to a new array, then come back to the CPU one page fault at a time, about 2 s a GB; never-touched pages cost about 0.5 s a GB on first touch. One bulk prefetch moves either kind at about 10 GB/s, after which creating arrays is cheap. Call it between phases of a program: before a phase that makes large arrays, after one that ran GPU kernels. Set HEAP_TRACE=1 to print each call's size and time on stderr.

Needs a native CUDA build of a program that uses !, CUDA 13 (the cuMemPrefetchAsync with a CUmemLocation), and Bend 2.0.40 (the C reads runtime internals: e.mem, gpu_dev). Foreign code: nothing here is proved.

1 import
import Base

Effects (foreign code)

effect Heap.home source · line 16 · raw

@mib:U32 -> IO(U32)

foreign: effs/heap.c, effs/heap.js