docs / GPU Programming / The Forbidden Pattern

The forbidden pattern — element cross-read

Reading a single element of a device-resident binding from host code is a compile error. It looks harmless, but each read would force its own readback — in a loop, that is N round-trips instead of one bulk copy:

gpu var arr = [0, 0, 0, 0, 0, 0, 0, 0]
forall i in 0..8
    arr[i] = i * i

let v = arr[0]     // COMPILE ERROR: a per-element read would require a readback

The fix the compiler points to is to bulk-copy first (let h = arr), then index the host copy — exactly the readback pattern above. This keeps the readback cost visible at one line.