Skip to content

CPUndarray.fromArray

Summary

CPUndarray.fromArray creates a contiguous CPU ndarray from JavaScript array-like data. It allocates by shape and copies source values into wasm memory. Use this for quick conversion of host arrays/typed arrays into WasmGPU ndarray format.

Syntax

CPUndarray.fromArray<T extends ArrayLike<number>>(dtype: DType, shape: ReadonlyArray<number>, src: T): CPUndarray
const a = CPUndarray.fromArray(dtype, shape, src);

Parameters

Name Type Required Description
dtype DType Yes Destination element data type.
shape ReadonlyArray<number> Yes Output ndarray dimensions.
src ArrayLike<number> Yes Source numeric data copied into the new ndarray.

Returns

CPUndarray - Contiguous CPU ndarray populated from source data.

Call initWebAssembly() before using this class without a WasmGPU instance. Pass at least one source value per real element or two interleaved real and imaginary components per complex element. Extra values are ignored.

Example

import { CPUndarray, initWebAssembly } from "@zushah/wasmgpu";
await initWebAssembly();

const src = new Float32Array([1, 2, 3, 4, 5, 6]);
const a = CPUndarray.fromArray("f32", [2, 3], src);
console.log(a.get(1, 2));
a.destroy();

See Also