This example calls Gridwise's scan primitive. You can look at
your developer console and see the input and output of this primitive, and
whether the output matches what is expected (in which case the developer
console will indicate "Validation passed").
It computes an exclusive prefix-sum (scan) over an input array of 224 i32s, producing an output array of the same length. In an exclusive scan, output element i is the sum of all input elements before position i; the first output element is always the identity value (0 for addition). The entire JS source file is in github. While the entire file contains substantial WebGPU and input-output setup boilerplate, the important parts follow.
First, we declare the scan primitive. We configure its datatype (i32), the binary operation (sum), and the scan type (exclusive).
const datatype = "i32";
const binop = new BinOpAdd({ datatype });
const scanType = "exclusive";
const dldfscanPrimitive = new DLDFScan({
device,
binop,
type: scanType,
datatype,
});
const primitive = dldfscanPrimitive;
We have declared buffers (using WebGPU's device.createBuffer)
called memsrcBuffer and memdestBuffer. For scan,
the output buffer is the same size as the input. We then call the
primitive's execute procedure (note that
execute is an async call):
await primitive.execute({
inputBuffer: memsrcBuffer,
outputBuffer: memdestBuffer,
});
We then read back the result from the GPU and validate it.
Your developer console should show a result that allows you to inspect the input and output, and prints "Validation passed" if the output matches the (CPU-computed) reference. It should look something like:
input array Int32Array(16777216) [11, 12, 13, 14, ...]
output Int32Array(16777216) [0, 11, 23, 36, ...]
Validation passed