liveBook · Manning

Double buffering

As explained in Grokking GPU Programming

Double buffering uses two buffers alternately to separate data being read from data being written.

Double buffering keeps values being read separate from values being written. In each Hillis–Steele scan round, threads read from one buffer and write to the other; the buffers swap roles before the next round. section 6.2

A scan across three rounds

For [3, 1, 7, 0, 4, 1, 6, 3], the scan uses strides 1, 2, and 4. Each round reads the previous round’s values and writes to the alternate buffer, so no thread overwrites a value another still needs. section 6.2

With one array, a thread may read a neighbor’s value while another thread overwrites it, making results depend on execution order. In CUDA, a barrier at the end of each round ensures its writes land before the next round reads; swapping buffer indices needs no separate barrier. section 6.16 section 6.8

buf[pout][tid] = buf[pin][tid]
               + buf[pin][tid - stride];    #A

This assignment shows the essential pattern: read both operands from pin and write the result to pout. section 6.16

How Grokking GPU Programming covers it

How deep it goes
Explains alternating buffers and their round-by-round swaps, while deferring arrays larger than one block to a multi-block strategy.
Examples
Uses a fresh-paper analogy, two shared-memory buffers, and an eight-element prefix-sum trace.
What you can do after
Apply it via CUDA shared-memory buffers to prevent Hillis–Steele threads from reading values overwritten in the same round.
How it connects
Connects parallel scan with shared memory, thread synchronization, Hillis–Steele, and Blelloch scan.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.