﻿# Double-buffered GPU tiles: compute one tile while preparing another

Double buffering succeeds when buffer reuse follows both copy completion and consumer completion; extra shared memory is a measurable tradeoff.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/learn-double-buffered-gpu-tiles/
Language: en

Double buffering gives a tiled GPU kernel separate storage for current and incoming data. The useful idea is temporal ownership, not merely allocating a larger shared-memory array. One buffer feeds computation while another is filled; their roles change only after the relevant copy and consumption operations have completed. Boundary handling must preserve the same ownership rules.

## The sequential schedule exposes two waits

A simple tiled loop loads a tile, waits, computes, and then starts the next load. This is easy to reason about but leaves opportunities for overlap unused. Double buffering rearranges the loop so a future transfer can progress during current arithmetic. It does not eliminate the dependency between a tile's load and its own computation. Confusing these dependencies is the source of many superficially plausible incorrect diagrams.

Primary reference: [CUDA Programming Guide: Asynchronous Data Copies](https://docs.nvidia.com/cuda/cuda-programming-guide/04-special-topics/async-copies.html).

## The prologue establishes the first valid tile

Before the steady-state loop begins, the initial buffer must receive valid data. This prologue has no earlier computation behind which to hide its transfer. A developer who starts with the steady-state pattern can accidentally compute from uninitialized shared memory on the first iteration. Write the prologue explicitly in the conceptual schedule, even if a production implementation later combines it with a more compact control structure.

Primary reference: [CUDA C++ Best Practices Guide](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/).

## Swapping names does not finish an operation

Swapping buffer pointers changes the program's naming convention. It does not wait for an outstanding copy or prove that all consumers finished reading the old tile. The swap must occur at a point justified by both completion relationships. Consider a slow consumer warp: the next producer must not overwrite its source tile merely because another warp has already advanced to the next iteration of the loop.

Primary reference: [Nsight Compute Profiling Guide](https://docs.nvidia.com/nsight-compute/ProfilingGuide/index.html).

## The epilogue must not fetch nonexistent input

Near the end, the current tile may be valid while no next tile exists. The final iteration still consumes its valid data but must avoid an out-of-range future transfer. Partial boundary tiles also require a defined treatment of invalid elements. Predication, neutral values, or a specialized boundary path can work, provided their synchronization behavior matches the required participants. Test a single tile before testing a long stream.

## Buffer depth competes with residency

Additional storage can reduce the number of blocks that fit on a multiprocessor. If the original kernel already hides memory latency through other resident blocks, more buffering may provide little benefit. Compare the total kernel duration with identical tile shapes, then explain changes using resource limits and stalls. A useful result can be that the simpler single-buffer design wins for a particular shape; that is evidence about the workload.

Related reading: [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/); [Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/).
