ndarrayf.copy¶
Summary¶
ndarrayf.copy, ndarrayf.pad, ndarrayf.affine, ndarrayf.resize and ndarrayf.reduce operate on caller-managed buffers in the built-in WebAssembly memory. For ordinary arrays, use compute.ndarray, which builds the layout descriptors and manages the storage.
Syntax¶
copy(dstPtr: WasmPtr, dstLen: number, srcPtr: WasmPtr, srcLen: number, layoutPtr: WasmPtr, ndim: number, codes: number): number
pad(dstPtr: WasmPtr, dstLen: number, srcPtr: WasmPtr, srcLen: number, layoutPtr: WasmPtr, ndim: number, codes: number, valuePtr: WasmPtr): number
affine(dstPtr: WasmPtr, dstLen: number, srcPtr: WasmPtr, srcLen: number, layoutPtr: WasmPtr, ndim: number, codes: number, paramsPtr: WasmPtr): number
resize(dstPtr: WasmPtr, dstLen: number, srcPtr: WasmPtr, srcLen: number, layoutPtr: WasmPtr, ndim: number, codes: number, indexPtr: WasmPtr, weightPtr: WasmPtr): number
reduce(dstPtr: WasmPtr, dstLen: number, dst2Ptr: WasmPtr, dst2Len: number, srcPtr: WasmPtr, srcLen: number, layoutPtr: WasmPtr, ndim: number, codes: number, paramsPtr: WasmPtr): number
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
dstPtr, srcPtr |
WasmPtr |
Yes | Byte addresses of destination and source allocations in the WasmGPU driver memory. |
dstLen, srcLen |
number |
Yes | Lengths of those backing ranges in bytes. |
dst2Ptr, dst2Len |
WasmPtr, number |
For reduce |
Second result address and byte length for a fused reduction. Pass zeros for a single result. |
layoutPtr |
WasmPtr |
Yes | Four-byte-aligned layout word array. |
ndim, codes |
number |
Yes | Descriptor rank and packed dtype and operation fields. |
valuePtr |
WasmPtr |
For pad |
One encoded element of the input and output dtype. |
paramsPtr |
WasmPtr |
For affine, reduce |
Eight-byte-aligned f64 scalars for affine statistics or the variance ddof. |
indexPtr, weightPtr |
WasmPtr |
For resize |
Tap tables in aligned 32-bit index words and 64-bit weights, respectively. |
Returns¶
number - Zero on success. Nonzero values report invalid dtype (1), mode (2), rank (3), pointer (4), overlap (5), source bounds (6), destination bounds (7), shape (8), tap table (9) or parameters (10). Initialize the driver before calling. The caller owns all buffers and descriptor allocations.
Type Details¶
WasmPtr is a number representing a byte address. The wrapper converts arguments to unsigned 32-bit integers. Pass live in-bounds ranges and ensure source and writable destination ranges are disjoint. Descriptor and parameter blocks must also stay outside the destination ranges.
Layout entries are 32-bit words with signed strides stored in two's complement. The table summarizes the raw layouts used by the matching driver and runtime. The ndarray namespace should be used when descriptor portability matters.
| Operation | Layout words in order |
|---|---|
copy |
Destination offset, source offset, shape, source byte strides, destination byte strides. |
pad |
Destination offset, source offset, output shape, input shape, leading pad widths, source byte strides, destination byte strides. |
affine |
Destination offset, source offset, four parameter records (pointer, length in f64 values, offset in elements), shape, source byte strides, destination byte strides, four sets of parameter strides in f64 elements. Parameters point to aligned f64 arrays. The separate scalar block stores epsilon and the two range endpoints. |
resize |
Destination offset, source offset, level count, triples of (axis, input length, tap count), output shape, source byte strides, destination byte strides. The index table holds all level row offsets, all tap indices and four constant words. The weight table holds all tap weights followed by real and imaginary constant scalars. |
reduce |
First output offset, second output offset, source offset, input shape, source byte strides, and one reduction flag per axis. Use 1 on reduced axes and 0 on kept axes. |
Dtype codes are 0 through 9 in the order i8, u8, i16, u16, i32, u32, f32, f64, c64, c128. In codes, use bits 0–3 for the destination dtype. Except for padding, use bits 4–7 for the source dtype. Leave unused bits clear.
- For
copy, bit 8 enables saturation and bit 9 enables nearest-even rounding. - For
pad, bits 8–11 select constant, edge, reflect, symmetric or wrap with codes 0–4. - For
affine, bits 8–9 select scale/offset, mean/std, z-score or min-max with codes 0–3. List the parameter records in formula order. Z-score supplies mean, std, min and max. - For
resize, bit 8 selects weighted sampling when set and nearest when clear. - For
reduce, bits 8–11 select sum, product, mean, variance, standard deviation, min, max, argmin, argmax, min/max, variance/mean or standard deviation/mean with codes 0–11. Bits 12 and 13 enable NaN omission and integer sum saturation. Bits 16–19 encode the second output dtype for a fused operation.
Example¶
import { initWebAssembly, ndarrayf, wasm } from "@zushah/wasmgpu";
await initWebAssembly();
const src = wasm.allocF32(2);
const dst = wasm.allocF32(2);
const layout = wasm.allocU32(5);
try {
wasm.f32view(src, 2).set([1, 2]);
wasm.u32view(layout, 5).set([0, 0, 2, 4, 4]);
const f32 = 6;
const codes = f32 | (f32 << 4); // Destination and source dtype codes
const status = ndarrayf.copy(dst, 8, src, 8, layout, 1, codes);
if (status !== 0) throw new Error(`ndarrayf.copy failed: ${status}`);
console.log(wasm.f32view(dst, 2)); // [1, 2]
} finally {
wasm.freeU32(layout, 5);
wasm.freeF32(dst, 2);
wasm.freeF32(src, 2);
}