docs / GPU Programming / Getting Data Back

Getting data back

Assigning a device-resident binding to a host binding copies the bytes back to the host. The transfer is always visible at the assignment line:

use system.gpu

gpu var arr = [0, 0, 0, 0, 0, 0, 0, 0]

forall i in 0..8
    arr[i] = i * i

let h = arr

for j in 0..4
    println(f'{h[j]}')

Output:

0
1
4
9

There is no .to_host() method and no hidden transfer. The keyword on each side of the assignment marks the host/device boundary. The reverse direction works the same way — gpu let g = host_x uploads host_x, and re-assigning a host array into a gpu var re-uploads it (that is how a training loop pushes updated parameters back to the device).

Three sanctioned partial reads avoid the full copy:

  • g.length() — free. The length is known on the host.
  • g.slice(a..b) — fences and reads back just [a, b) as a host array.
  • g.reduce(init, op) — reduces on the device. See On-Device Reduction.

Every other buffer-touching method on a gpu-resident binding in host code — element_at, contains, set, … — is a compile error with the same fix-it as the forbidden pattern: bulk-copy first.