Skip to content

GPUndarray.empty

Summary

GPUndarray.empty allocates a GPU-resident ndarray backed by a storage buffer. You provide dtype and layout metadata; the backing storage is allocated for that layout. Use this for output tensors, scratch arrays, or GPU-only workflows. Descriptor options map to storage-buffer creation flags.

Syntax

GPUndarray.empty(ctx: { device: GPUDevice; queue: GPUQueue }, dtype: DType, layout: NdLayoutDescriptor, desc?: Omit<StorageBufferDescriptor, "byteLength" | "data">): GPUndarray
const g = GPUndarray.empty(wgpu.compute, dtype, layout, desc);

Parameters

Name Type Required Description
ctx { device: GPUDevice; queue: GPUQueue } Yes GPU context used to allocate backing storage.
dtype DType Yes Element data type.
layout NdLayoutDescriptor Yes Shape plus optional byte strides and byte offset.
desc Omit<StorageBufferDescriptor, "byteLength" | "data"> No Optional storage-buffer descriptor overrides. copyDst defaults to true and copySrc to false. Use usage to add flags and label for debugging.

Returns

GPUndarray - Owned GPU ndarray. Destroy it when finished.

The layout rules specify shape, byte stride and offset constraints.

Example

import WasmGPU, { GPUndarray } from "@zushah/wasmgpu";

const canvas = document.querySelector("canvas");
const wgpu = await WasmGPU.create(canvas);

const g = GPUndarray.empty(wgpu.compute, "f32", { shape: [128, 128] }, { copySrc: true });
console.log(g.residency, g.layout());
g.destroy();

See Also