# 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@/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. import Base def Heap.home(mib: U32) -> IO(U32): import "./effs/heap.c" import "./effs/heap.js"