compute.ndarray¶
Summary¶
compute.ndarray creates and transforms multidimensional arrays in WebAssembly memory or WebGPU storage. Use the same namespace for layout views and transfers between "cpu" and "gpu" arrays.
Syntax¶
readonly ndarray: NdarrayNamespace
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [2], [1, 2]);
Available APIs¶
- Create with empty or zeros, fromArray or wrap.
- Transfer arrays with toGPU and toCPU.
- Inspect shape and strides, element type, residency and view ownership.
- Select data with slice, transpose, swapAxes or moveAxis, and flip.
- Change the view shape with reshape, squeeze or expandDims, and broadcastTo.
-
Inspect complex components with real or imag and reinterpret layout with viewAsReal or viewAsComplex.
-
Materialize with contiguous, convert with cast or write into existing storage with copyInto.
- Transform fields with pad, resize, resample and normalize.
- Reduce along axes with sum or prod, mean, var or std, min or max and argmin or argmax. Compute pairs with minMax or varMean and stdMean.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const samples = nd.fromArray("f32", [2, 3], [1, 2, 3, 4, 5, 6]);
const lastColumn = nd.slice(samples, ":, -1");
console.log(lastColumn.get(0), lastColumn.get(1)); // 3, 6
lastColumn.destroy();
samples.destroy();
Notes¶
Use value instanceof nd or import the abstract Ndarray class and use value instanceof Ndarray to recognize either residency. The named CPUndarray and GPUndarray classes also support residency-specific checks and have standalone factories.
The compute service owns the namespace. nd.destroy() invalidates its operations. Destroy allocated arrays and views separately when finished with them.