Skip to content

compute.kernels.scanExclusive

Summary

compute.kernels.scanExclusive computes the unsigned prefix sums of a GPU vector, starting at zero. Each result is the sum of the elements before its own index. Use it to derive offsets from counts or to find where selected elements should be written during compaction.

Syntax

scanExclusive(a: GPUndarray, opts?: KernelOutputOptions): GPUndarray
const prefix = kernels.scanExclusive(a);

Parameters

Name Type Required Description
a GPUndarray Yes Rank-one u32 input. Strided vectors are accepted.
opts KernelOutputOptions No Kernel output and dispatch settings.

Returns

GPUndarray - u32 vector of the same length. A non-empty result starts at zero. Sums wrap modulo 2 ** 32. An empty input yields an empty result, and a C-contiguous input can also be the output.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const { ndarray: nd, kernels } = wgpu.compute;
const a = nd.fromArray("u32", [3], [2, 1, 4], { residency: "gpu" });
const prefix = kernels.scanExclusive(a);
const cpu = await nd.toCPU(prefix);
console.log(cpu.data()); // [0, 2, 3]
cpu.destroy();
prefix.destroy();
a.destroy();

See Also