liveBook · Manning

Chapter 9: The Stencil pattern

What you will learn in chapter 9 of Grokking GPU Programming

Introduces stencil computation through a parallel image blur and contrasts its neighbor reads with histogram write conflicts.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces stencil computation through a parallel image blur and contrasts its neighbor reads with histogram write conflicts.

Key terms: stencil, neighbors, output pixel, write conflicts, atomic operations, reads, edges

9.1 What a stencil is: a window around every element

This section introduces stencil computations, their neighborhood shapes and radii, and how their neighbor reads differ from Map and extend to distributed grids.

Key terms: stencil, neighborhood, weighted combination, radius, 1D stencil, 2D stencil, window, 3-point average, 5-point stencil, stencil pattern

9.2 A 1D stencil in code

This section implements a one-dimensional smoothing stencil that averages each element with its available neighbors.

Key terms: 1D stencil, neighbors, boundary checks, average, float division, integer division, cast, sum, count, input

9.3 Stepping up to 2D

This section extends a stencil to 2D images using a two-dimensional thread layout and a checked 3×3 neighborhood average.

Key terms: 2D indexing, row-major layout, width, 2D grid, 2D blocks, dim3, blockIdx, threadIdx, ceiling division, 3×3 neighborhood

9.4 From averages to weights

This section extends averaging stencils with position-specific weights and explains their applications and boundary effects.

Key terms: weighted stencil, weights, Gaussian-style 3×3, blur2D, valid-neighbor check, count, border, weighted averages, weighted differences, Sobel

9.5 How fast is the stencil?

This section compares sequential and parallel 2D stencil work and explains the limits of ideal parallel speedup.

Key terms: stencil side length, radius, depth of the computation, SMs, blocks, write conflicts, output location

9.6 Handling the boundaries

This section explains why stencil boundaries need explicit handling and introduces four boundary conditions.

Key terms: boundary strategy, valid neighbors, weighted stencil, boundary conditions, clamp, zero padding, skip, wrap-around, Dirichlet boundary condition, Neumann condition

9.7 Clamp

This section explains clamp boundary handling for stencils and distinguishes GPU helper functions from kernels.

Key terms: clamp, boundary strategy, edge value, __device__, __global__, kernel, helper function

9.8 Zero padding

This section explains zero padding as a stencil boundary strategy and relates it to same padding in deep learning.

Key terms: zero padding, boundary, same padding, stride 1, CNNs

9.9 Skip

This section explains the skip strategy for stencil boundaries and its effect on output size and storage.

Key terms: skip, boundary, radius, valid padding, output buffer, border unwritten

9.10 Wrap-around

This section explains wrap-around stencil boundaries and how to implement them safely with C's remainder operator.

Key terms: wrap-around, torus, neighbors, double modulo, % operator

9.11 Where you’ll use the Histogram and Stencil patterns

This section connects histogram and stencil patterns to image processing, simulations, machine learning, and PyTorch convolution.

Key terms: 3×3 averaging stencil, Sobel, edge detection, sharpening, median filter, noise reduction, 3×3 neighborhood, histogram, frequency distributions, auto-exposure

9.12 Common mistakes

This section contrasts two stencil neighbor-read errors: reading beyond the data and reading a value overwritten by another thread.

Key terms: neighbor read, edge of the data, overwritten

9.13 Mistake 1: Missing boundary checks

This section explains how missing neighbor boundary checks can break a stencil and how to prevent the error.

Key terms: boundary check, neighbor read, image bounds

9.14 Mistake 2: In-place stencil (the sneaky one)

This section explains why in-place stencils race and summarizes the stencil’s neighborhood-based input and output pattern.

Key terms: in-place race, separate input and output arrays, map, stencil, __syncthreads(), block, in-place stencil, neighborhood, overlapping reads, data reuse

9.15 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 8: The Histogram pattern Chapter 10: Memory access patterns →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.