Skip to content

compute.ndarray.toGPU

Summary

compute.ndarray.toGPU uploads a CPU ndarray's backing range into owned storage on the compute device. It preserves the dtype, shape, byte strides and layout offset, including a strided or reversed view's layout.

Syntax

toGPU(src: CPUndarray, options?: NdarrayGPUOptions): GPUndarray
const gpu = nd.toGPU(cpu);

Parameters

Name Type Required Description
src CPUndarray Yes Live WebAssembly-backed array.
options NdarrayGPUOptions No copySrc and copyDst default to true. label is optional and usage adds usage flags.

Returns

GPUndarray - Independently owned copy of the source's byteLength backing bytes. Destroy the CPU and GPU arrays separately.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const cpu = nd.fromArray("f32", [3], [3, 6, 9]);
const reverse = nd.flip(cpu);
const gpu = nd.toGPU(reverse);
console.log(gpu.layout());
gpu.destroy();
reverse.destroy();
cpu.destroy();

See Also