compute.ndarray.contiguous¶
Summary¶
compute.ndarray.contiguous materializes the logical elements of an ndarray in packed C order. Use it to turn a reversed, transposed or broadcast view into storage that can be addressed linearly.
Syntax¶
contiguous<T extends CPUndarray | GPUndarray>(a: T, options?: NdarrayOperationOptions): T
const packed = nd.contiguous(view);
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
a |
CPUndarray | GPUndarray |
Yes | Live input array. Its strides can be negative or zero. |
options |
NdarrayOperationOptions |
No | GPU output and command recording settings. Use these options only for GPU inputs. |
Returns¶
T - Result in the input dtype, shape and residency. Without an explicit GPU out, this allocates an independent array even when the input is already contiguous. Destroy each owned array separately.
Type Details¶
type NdarrayOperationOptions = NdarrayGPUOptions & {
encoder?: GPUCommandEncoder;
out?: GPUndarray;
};
A GPU out must be live, C-contiguous and match the result dtype, shape and device. The operation accommodates aliased input through staging. copySrc and copyDst both default to true for a newly allocated GPU result, and usage adds flags. These allocation flags cannot be combined with out. label is optional.
Without an encoder, the method submits its work immediately. With an encoder, it records without submitting. Keep inputs and outputs live until submission and submit before readback.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [2, 2], [1, 2, 3, 4]);
const t = nd.transpose(a);
const packed = nd.contiguous(t);
console.log(packed.data()); // [1, 3, 2, 4]
packed.destroy();
t.destroy();
a.destroy();
Notes¶
Device buffer and binding limits apply to GPU operations. Implementation note. After compatible axes are coalesced, the CPU path supports up to 64 dimensions and the GPU path up to eight. A high-rank view with distinct strides can exceed these limits even when its element count is small.