Composing Primitives
Some primitives need another primitive as one of their steps. A primitive’s compute() can include an ExecutePrimitive action, which runs another primitive at that point, on the same command encoder, with the other primitive’s buffers bound to this primitive’s buffers by name.
Motivation
The first primitive that needed this was MultisplitPerm, a random permutation built from multisplit passes. Each pass is three steps:
- a histogram kernel counts how many elements go to each bucket;
- an exclusive scan of those counts gives every bucket its output offset;
- a scatter kernel moves each element to its bucket.
Steps 1 and 3 are short kernels that belong to the new primitive. Step 2 is a scan, and Gridwise already has the hardest-won primitive it could want for that: DLDFScan, with its decoupled lookback and fallback, its handling of different subgroup sizes, and its emulation path. The new primitive should use that scan, not a copy of it.
Before ExecutePrimitive, there were three ways to do that, and none was satisfactory:
- Let the caller chain the primitives. As Ordering and Synchronization Guarantees describes, an application can record several primitives into one encoder. That works for an application, but the result is not a primitive. Every caller has to know the composite’s internal steps and allocate its internal buffers (histograms, offsets), and the composite cannot be executed, timed, tested or benchmarked as one unit.
- Copy the scan’s kernels into the new primitive. This duplicates the most delicate code in Gridwise, and the copy would not get later fixes to the scan.
- Override
execute()and call the child’sexecute({ encoder })from it. This records the right passes, but it bypassescompute(), so the rest ofBasePrimitivecannot see the child. GPU timing breaks: the timing helper is sized from the parent’s kernels only, and the child’sexecute()resets the shared helper. The child also cannot use buffers the parent allocates withAllocateBuffer, because those do not exist until execution starts.
ExecutePrimitive makes the child one of the parent’s actions, so the parent stays a single primitive with the usual execute(), timing and buffer handling.
Usage
import { BasePrimitive, Kernel, AllocateBuffer, ExecutePrimitive, DLDFScan, BinOpAdd } from "gridwise";
class HistogramThenScan extends BasePrimitive {
constructor(args) {
super(args);
this.knownBuffers = ["inputBuffer", "offsets"];
/* create the child once, here, not in compute() */
this.scan = new DLDFScan({
device: this.device,
binop: new BinOpAdd({ datatype: "u32" }),
type: "exclusive",
datatype: "u32",
});
}
compute() {
return [
new AllocateBuffer({ label: "counts", size: this.countsBytes }),
new Kernel({ /* histogram: inputBuffer -> counts */ }),
new ExecutePrimitive({
primitive: this.scan,
/* the child's buffer name: this primitive's buffer name */
buffers: { inputBuffer: "counts", outputBuffer: "offsets" },
label: "scan of counts",
}),
];
}
destroy() {
super.destroy();
this.scan.destroy();
}
}
- Create the child once, typically in the constructor, and return the same object from every
compute()call. Kernel counting and execution must see the same child. buffersmaps each of the child’s buffer names to one of this primitive’s buffer names: one it was given by its caller, or one it allocates withAllocateBuffer.enable(optional, defaulttrue) works as it does forKernel: a disabled action is skipped and not counted.destroy()of the parent does not destroy its children. A primitive that owns children should destroy them in its owndestroy(), as above.
How it works
- Order. The child’s actions run at the position of the
ExecutePrimitiveaction in the parent’s list, on the parent’s encoder, each kernel in its own compute pass as usual. WebGPU therefore orders the child’s reads and writes after the parent’s earlier passes and before its later ones, exactly as for kernels the parent records itself. - Buffers. Before the child runs, each mapped buffer is registered in the child under the child’s name. If the parent has not allocated a mapped buffer yet, it allocates it at that moment from its own
AllocateBufferaction, so that the child can size itself from it. This matters when kernels are counted for GPU timing, which happens before any action runs. - GPU timing.
countKernels()counts the enabledKernelactions and adds each enabled child’scountKernels(), recursively.execute()uses it to size the timing helper. The child then records into the parent’s timing helper instead of setting up its own, sogetTimingResult()on the parent returns one time per kernel, the child’s included, in execution order. See Timing Strategy. - Trials. The parent’s
trialsargument is passed to the child, so every kernel, the child’s included, is dispatched that many times. compute()is called once perexecute(), and the same action list is used for counting kernels and for execution.
Nesting is recursive: a child may itself contain ExecutePrimitive actions. HierarchicalScan relies on this: it scans its per-tile reductions with a nested HierarchicalScan, which nests another when those reductions do not fit in one tile. The regression suite covers one level (the ScanPlusOne test and MultisplitPerm) and two levels (HierarchicalScan on inputs of more than 220 elements).