Skip to content

compute.ndarray.fromArray

Summary

compute.ndarray.fromArray copies values from a JavaScript array-like source into owned row-major storage. It converts each numeric component using the typed array for the requested dtype.

Syntax

fromArray(dtype: DType, shape: ReadonlyArray<number>, src: ArrayLike<number>, options: NdarrayCreateOptions & { residency: "gpu" }): GPUndarray
fromArray(dtype: DType, shape: ReadonlyArray<number>, src: ArrayLike<number>, options?: NdarrayCreateOptions & { residency?: "cpu" }): CPUndarray
fromArray(dtype: DType, shape: ReadonlyArray<number>, src: ArrayLike<number>, options?: NdarrayCreateOptions): CPUndarray | GPUndarray
const a = wgpu.compute.ndarray.fromArray("f32", [2, 2], [1, 2, 3, 4]);

Parameters

Name Type Required Description
dtype DType Yes Destination element type.
shape ReadonlyArray<number> Yes Dimensions in row-major order, with the last axis changing fastest.
src ArrayLike<number> Yes At least numel values for a real array or 2 * numel interleaved real and imaginary components for a complex array. Extra values are ignored.
options NdarrayCreateOptions No Defaults to CPU residency. For GPU residency the two copy flags default to true.

Returns

CPUndarray | GPUndarray - Contiguous array with a zero layout offset. Call destroy() when finished.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const z = wgpu.compute.ndarray.fromArray("c64", [2], [1, -1, 2, -2]);
console.log(z.get(1)); // [2, -2]
z.destroy();

See Also