GPU Graph Cookbook
Use these recipes to build and run the GPU Graph introduced in the overview.
| What you want to do | GPU Graph API |
|---|---|
| Supply data when the graph runs | Declare a named input with graph.addInput(). |
| Connect operations | Use graph.add(operation, props) and refer to earlier outputs by name. |
| Run steps conditionally or repeatedly | Group the steps with graph.if() or graph.loop(). |
| Choose which results to return | Pass outputs to graph.compile(device, {outputs}). |
| Reuse the computation with new data | Call compiledGraph.run({inputs}) for each set of inputs. |
Grouped aggregation and scalar reduction​
gpuGroupAggregate and gpuReduce are built-in semantic operations.
They lower to the existing GPUGroupAggregation and
GPUReduction kernels without custom compiler registration:
import {
GPUGraph, gpuGroupAggregate, gpuReduce
} from '@luma.gl/graph/gpu-graph';
const graph = new GPUGraph();
graph.addInput('keys', {type: 'vector', format: 'uint32'});
graph.addInput('weights', {type: 'vector', format: 'float32'});
graph.addInput('selection', {type: 'vector', format: 'uint32'});
graph
.add(gpuGroupAggregate, {
keys: 'keys', values: 'weights', mask: 'selection',
output: 'groups', groupCount: 100, operation: 'sum'
})
.add(gpuReduce, {input: 'groups', output: 'domain', operation: 'extent'});
const compiledGraph = graph.compile(device, {outputs: ['groups', 'domain']});
const result = await compiledGraph.run({inputs: {keys, weights, selection}});
// Both outputs are GPUVectors. Readback is explicit.
result.groups.destroy();
result.domain.destroy();
compiledGraph.destroy();
Inputs and outputs​
gpuGroupAggregateaccepts denseuint32keys and an optionaluint32mask. Nonzero mask values select rows; keys outside[0, groupCount)are ignored.countreturnsuint32;sum,min,max, andmeanaccept finitefloat32contributions and returnfloat32.groupCountinfers a packed result length. It can be omitted with the concrete compiler API when output dimensions are declared explicitly usinggraph.compiler.vector(...).gpuReduceaccepts scalaruint32,sint32, orfloat32inputs and an optionaluint32mask.sum,min, andmaxreturn one row;extentreturns two rows containing minimum and maximum. Output format matches input format.- Logical references may be names or typed
GPUGraphVectorobjects. Inputs retain their supported chunk/stride layouts and masks may have independent partitions. Destinations must be one packed chunk. UseoutputTargetsfor caller-owned result storage; operation-defined results cannot overwrite inputs oraddState().
Execution and ownership​
The compiler uses existing kernels and graph-owned scratch storage. Operations encode no CPU readback. Inputs and caller-provided targets remain borrowed; returned results belong to the caller. Repeated executions reset grouped statistics, including empty groups. Empty counts and sums are zero; empty min/max/mean groups are NaN. Scalar reductions retain the underlying kernel's masking and nonfinite-value rules.