python.copyOut¶
Summary¶
python.copyOut writes the values or backing bytes of a WasmGPU ndarray into a writable Python buffer. CPU copies finish synchronously. GPU copies snapshot the source when called and write the destination when readback completes.
Syntax¶
copyOut(dst: PythonArraySource, src: CPUndarray, options?: PythonCopyOutOptions): void
copyOut(dst: PythonArraySource, src: GPUndarray, options?: PythonCopyOutOptions): Promise<void>
copyOut(dst: PythonArraySource, src: CPUndarray | GPUndarray, options?: PythonCopyOutOptions): void | Promise<void>
await wgpu.python.copyOut(destination, gpu);
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
dst |
PythonArraySource |
Yes | Writable Pyodide proxy or borrowed buffer descriptor. |
src |
CPUndarray | GPUndarray |
Yes | Live source. A GPU source must be on the compute device. |
options |
PythonCopyOutOptions |
No | Materialization policy and whether to copy the raw byte extent. |
Returns¶
void | Promise<void> - CPU inputs write synchronously. GPU inputs return a promise that resolves after the destination is written or rejects if validation or readback fails.
Type Details¶
With extent: false (the default), the destination must match the source dtype and shape and be dense and free of overlaps. Dense destinations can use C or Fortran order, permuted axes or reversed strides. With extent: true, use a dense one-dimensional byte buffer with exactly src.byteLength bytes to receive the backing range, including gaps. A GPU extent copy requires readable storage and a valid aligned readback range.
copy defaults to "if_needed". With "never", the layout and readback range must allow a direct transfer. "if_needed" and "always" can materialize the source in the destination order or stage GPU data without COPY_SRC for a logical copy.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [2], [4, 8], { residency: "gpu" });
const data = new Float32Array(2);
const destination = { data, shape: [2], strides: [1], offset: 0 };
await wgpu.python.copyOut(destination, a);
console.log(data); // [4, 8]
a.destroy();
Notes¶
Submit any encoder that writes the source before calling copyOut. The GPU path acquires a proxy buffer to validate it, releases it, and acquires it again at completion to write the bytes. Keep the proxy or borrowed view live until resolution, and keep the destination metadata stable.