Skip to content

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

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.

See Also