Skip to content

compute.ndarray.broadcastTo

Summary

compute.ndarray.broadcastTo presents an array at a compatible shape using zero strides for repeated values. Use it when a consumer accepts strided inputs and multiple logical indices can read the same element.

Syntax

broadcastTo<T extends CPUndarray | GPUndarray>(a: T, shape: ReadonlyArray<number>): T
const b = nd.broadcastTo(a, [4, 3]);

Parameters

Name Type Required Description
a CPUndarray | GPUndarray Yes Live input array.
shape ReadonlyArray<number> Yes Non-negative integer dimensions, with at least the input rank and an element count that fits in u32. Align the shapes at the right. Each source dimension must equal the target or be one. Missing leading dimensions count as one.

Returns

T - View that shares values with its root and retains dtype and residency. Incompatible shapes throw.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const row = nd.fromArray("f32", [3], [1, 2, 3]);
const repeated = nd.broadcastTo(row, [4, 3]);
console.log(repeated.get(3, 1)); // 2
repeated.destroy();
row.destroy();

Notes

Writing to a repeated address affects all its logical indices. Materialize separate elements with compute.ndarray.contiguous() before assigning different values to them.

See Also