Skip to content

compute.ndarray.toCPU

Summary

compute.ndarray.toCPU reads a GPU ndarray's backing range into an owned array in WebAssembly memory. The copy preserves the dtype, shape, byte strides and layout offset.

Syntax

toCPU(src: GPUndarray): Promise<CPUndarray>
const cpu = await nd.toCPU(gpu);

Parameters

Name Type Required Description
src GPUndarray Yes Live array on the compute device with COPY_SRC on its storage buffer.

Returns

Promise<CPUndarray> - Resolves to an independent copy of the source's backing bytes. Destroy it when finished.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const gpu = nd.fromArray("u32", [3], [4, 8, 12], { residency: "gpu" });
const cpu = await nd.toCPU(gpu);
console.log(cpu.data());
cpu.destroy();
gpu.destroy();

Notes

Submit any source-writing encoder before calling toCPU. For a non-empty source, the copy submits before the first await, using either the shared ring or direct storage readback.

The range starts at src.baseOffsetBytes and spans src.byteLength. It must fit in the logical storage range, with room in the GPU allocation for the copy length rounded up to four bytes.

See Also