Skip to main content

GPU Graph Cookbook

Use these recipes to build and run the GPU Graph introduced in the overview.

What you want to doGPU Graph API
Supply data when the graph runsDeclare a named input with graph.addInput().
Connect operationsUse graph.add(operation, props) and refer to earlier outputs by name.
Run steps conditionally or repeatedlyGroup the steps with graph.if() or graph.loop().
Choose which results to returnPass outputs to graph.compile(device, {outputs}).
Reuse the computation with new dataCall 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​

  • gpuGroupAggregate accepts dense uint32 keys and an optional uint32 mask. Nonzero mask values select rows; keys outside [0, groupCount) are ignored. count returns uint32; sum, min, max, and mean accept finite float32 contributions and return float32. groupCount infers a packed result length. It can be omitted with the concrete compiler API when output dimensions are declared explicitly using graph.compiler.vector(...).
  • gpuReduce accepts scalar uint32, sint32, or float32 inputs and an optional uint32 mask. sum, min, and max return one row; extent returns two rows containing minimum and maximum. Output format matches input format.
  • Logical references may be names or typed GPUGraphVector objects. Inputs retain their supported chunk/stride layouts and masks may have independent partitions. Destinations must be one packed chunk. Use outputTargets for caller-owned result storage; operation-defined results cannot overwrite inputs or addState().

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.