--- slug: /api-reference/experimental/gpu-core/gpu-graph --- # GPUGraph A `GPUGraph` describes a computation as a sequence of GPU operations. Results of operations are available to be used as input for later operations. ```ts import {GPUGraph, gpuExpression} from '@luma.gl/graph/gpu-graph'; const graph = new GPUGraph(); graph.addInput('values', {type: 'vector', format: 'float32'}); graph.add(gpuExpression, {output: 'doubled', expression: 'values * 2'}); graph.add(gpuExpression, {output: 'result', expression: 'doubled + 1'}); ``` Here the named output `doubled` connects the two operations. The [getting started guide](/docs/api-reference/graph/getting-started) shows how to compile the graph, supply a vector, and read back the result. ## Inputs Declare a named input with `graph.addInput(name, descriptor)`. In the example above, `graph.addInput('values', {type: 'vector', format: 'float32'})` tells the graph to expect a vector of 32-bit floats. Operations can then refer to it by the name `values`. Supply the actual data when you run the compiled graph. Here `samples` is a `GPUVector` supplied by the application. ```ts const compiledGraph = graph.compile(device, {outputs: ['result']}); const {result} = await compiledGraph.run({inputs: {values: samples}}); ``` To process another vector, run the same compiled graph again with a different value for `inputs.values`. See [graph inputs, results, and state](./gpu-graph-values.md) for the supported input types. ## GPU Graph operations Add an operation with `graph.add(operation, props)`. The operation defines the work and its properties identify the inputs, outputs, and settings for that step. - [`gpuExpression`](./gpu-expression-operation.md) evaluates an expression for each row of a vector. - [`gpuCompact`](./gpu-compaction-operation.md) selects rows and produces a GPU-resident count of the selected rows. - [`gpuDrawFullscreen`](./gpu-fullscreen-draw-operation.md) draws a fullscreen pass with named texture inputs. You can [define a custom operation](./gpu-operation.md) with its own input and output ports. Add it to a graph the same way you add a built-in operation. ## Data flow In the example above, `values` comes from the caller, `doubled` passes from one operation to the next, and `result` is the output the caller wants to keep. Declare external inputs with `addInput()`, add each producer before its consumers, and select the outputs when you compile. The graph manages storage for intermediate results. Use [mutable state](./gpu-graph-state.md) when a step needs to update a value that a later iteration or execution will read. See [graph inputs, results, and state](./gpu-graph-values.md) for binding and ownership, and [data flow](./data-flow.md) for how values move between CPU and GPU operations. ## The graph is designed to keep data on the GPU and eliminate CPU readbacks In the example above, the second operation reads `doubled` directly on the GPU. The same applies when a render pass consumes a texture produced by an earlier operation. Read a result back when JavaScript needs its contents. ## Scalar GPU values The graph supports scalar values on the GPU. A scalar holds one GPU-resident number. GPU operations can take scalars as inputs and produce them as outputs. Scalars can also control graph execution through `graph.if()` and `graph.loop()`. Applications can declare scalar inputs with `graph.addInput()`. ```ts graph.addInput('scale', {type: 'scalar', format: 'float32'}); ``` This declaration lets you supply `scale` as a JavaScript number when you run the graph. For example, a `float32` scalar named `scale` can multiply every row of a vector in the expression `values * scale`. GPU operations can also write GPU-resident scalars. For example, a solver can write a `uint32` flag after each iteration and let the GPU use it to decide whether to continue, without reading the flag back to JavaScript. ## Reusable scopes Use `graph.createScope()` to build a named group of operations. Pass the scope to `graph.add(scope)` to capture its current steps, or use it as the body of `graph.if()` or `graph.loop()`. ## Control flow GPU Graph provides support for basic conditional and repeated execution using GPU-resident predicates. Use `graph.if(predicate, body)` to run a group of operations when a GPU scalar is nonzero. Use `graph.loop(options, body)` to repeat a group up to a specified iteration limit. A loop can use a GPU predicate to stop doing work once a solver has converged. ```ts graph.loop({maximumIterations: 20, predicate: keepGoing}, scope => scope.add(updateEstimate, props) ); ``` The callback describes the body once. During execution the GPU evaluates `keepGoing` for each iteration. See [GPU Graph control flow](./gpu-control-flow-operation.md) for predicates, bounds, and nested scopes. For practical examples, see the [GPU Graph Cookbook](./gpu-graph-cookbook.md). For execution and resource bindings, see [Compiling GPU Graphs](./gpu-graph-compilation.md).