Skip to content

compute.ndarray.copyInto

Summary

compute.ndarray.copyInto writes values into existing storage, including a strided destination view. It broadcasts the source to the destination shape and converts to the destination dtype. Use it to replace part of an array or write an operation's input into a reusable allocation.

Syntax

copyInto(dst: CPUndarray | GPUndarray, src: CPUndarray | GPUndarray, options?: NdarrayCopyIntoOptions): void
nd.copyInto(dst, src);

Parameters

Name Type Required Description
dst CPUndarray | GPUndarray Yes Live destination whose logical elements have distinct addresses.
src CPUndarray | GPUndarray Yes Live source with a shape broadcastable to dst.shape. Both arrays must have the same residency and, on the GPU, belong to the compute device.
options NdarrayCopyIntoOptions No Conversion policy and GPU-only command settings.

Returns

void - Writes into the destination. If the input and output can overlap, copying uses the original source values. An empty destination requires no copy.

Type Details

type NdarrayCopyIntoOptions = {
    overflow?: NdarrayOverflowMode;
    rounding?: NdarrayRoundingMode;
    encoder?: GPUCommandEncoder;
    label?: string;
};

Conversion rules and defaults match compute.ndarray.cast. The GPU path submits unless given an encoder. Keep the arrays live until you submit recorded work.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const src = nd.fromArray("f32", [2], [0, 1]);
const dst = nd.empty("u8", { shape: [3, 2] });
nd.copyInto(dst, src);
console.log(dst.data()); // [0, 1, 0, 1, 0, 1]
dst.destroy();
src.destroy();

Notes

A broadcast or other potentially overlapping destination layout throws. For 8-bit and 16-bit GPU destinations, the destination must also be C-contiguous. Use toCPU or toGPU first when moving data between residencies.

See Also