Skip to content

CPUndarray.empty

Summary

CPUndarray.empty allocates a CPU-resident ndarray in WebAssembly memory. You provide dtype and layout metadata; storage is allocated but not initialized. Use this when you need explicit control over shape/strides before filling data. For zero-initialized allocation, use CPUndarray.zeros.

Syntax

CPUndarray.empty(dtype: DType, layout: NdLayoutDescriptor): CPUndarray
const a = CPUndarray.empty(dtype, layout);

Parameters

Name Type Required Description
dtype DType Yes Element data type, including c64 and c128.
layout NdLayoutDescriptor Yes Shape plus optional byte strides and byte offset.

Returns

CPUndarray - Newly allocated CPU ndarray in WebAssembly memory. Destroy it when finished.

Type Details

type NdLayoutDescriptor = {
    shape: ReadonlyArray<number>;
    stridesBytes?: ReadonlyArray<number>;
    offsetBytes?: number;
};

See the layout rules for stride and offset defaults and constraints. Call initWebAssembly() before using this class without a WasmGPU instance.

Example

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

const a = CPUndarray.empty("f32", { shape: [4, 4] });
a.set(1.25, 0, 0);
console.log(a.get(0, 0), a.layout());
a.destroy();

See Also