ArticleslgStudy

computer science

Stencil computation

Stencil computation is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Stencil computation rather than just read about it. In short: Stencil computations, or iterative stencil loops (ISL), are a class of numerical data processing solution which update array elements according to some fixed pattern, called a stencil. They are most commonly found in computer simulations, e.g. for computational fluid dynamics in the context of scientific and engineering applications.

Stencil computation — main illustration
Stencil computation — illustration

Key takeaways

  • Stencil computation belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Stencil computation to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Stencil computation from memory before moving on to harder problems.

Reference excerpt

Stencil computations, or iterative stencil loops (ISL), are a class of numerical data processing solution which update array elements according to some fixed pattern, called a stencil. They are most commonly found in computer simulations, e.g. for computational fluid dynamics in the context of scientific and engineering applications. Other notable examples include solving partial differential equations, the Jacobi kernel, the Gauss–Seidel method, image processing and cellular automata. The regular structure of the arrays sets stencil techniques apart from other modeling methods such as the finite element method. Most finite difference codes which operate on regular grids can be formulated as stencil computations.

Definition Stencil computations perform a sequence of sweeps (called timesteps) through a given array. Generally this is a 2- or 3-dimensional regular grid. The elements of the arrays are often referred to as cells. In each timestep, all array elements are updated. Using neighboring array elements in a fixed pattern (the stencil), each cell's new value is computed. In most cases boundary values are left unchanged, but in some cases (e.g. LBM codes) those need to be adjusted during the computation as well. Since the stencil is the same for each element, the pattern of data accesses is repeated. More formally, we may define stencil computations as a 5-tuple (I,S,S0,s,T) with the following meaning:

I=∏i=1k[0,…,ni] is the index set. It defines the topology of the array. S is the (not necessarily finite) set of states, one of which each cell may take on any given timestep. S0:ℤk→S defines the initial state of the system at time 0. s∈∏i=1lℤk is the stencil itself and describes the actual shape of the neighborhood. There are l elements in the stencil. T:Sl→S is the transition function which is used to determine a cell's new state, depending on its neighbors. Since I is a k-dimensional integer interval, the array will always have the topology of a finite regular grid. The array is also called simulation space and individual cells are identified by their index c∈I. The stencil is an ordered set of l relative coordinates. We can now obtain for each cell c the tuple of its neighbors indices Ic

Ic={j∣∃x∈s:j=c+x} Their states are given by mapping the tuple Ic to the corresponding tuple of states Ni(c), where Ni:I→Sl is defined as follows:

Ni(c)=(s1,…,sl) with sj=Si(Ic(j)) This is all we need to define the system's state for the following time steps Si+1:ℤk→S with i∈ℕ:

Si+1(c)={T(Ni(c)),c∈ISi(c),c∈ℤk∖I Note that Si is defined on ℤk and not just on I since the boundary conditions need to be set, too. Sometimes the elements of Ic may be defined by a vector addition modulo the simulation space's dimension to realize toroidal topologies:

Ic={j∣∃x∈s:j=((c+x)mod(n1,…,nk))} This may be useful for implementing periodic boundary conditions, which simplifies certain physical models.

Example: 2D Jacobi iteration

To illustrate the formal definition, we'll have a look at how a two dimensional Jacobi iteration can be defined. The update function computes the arithmetic mean of a cell's four neighbors. In this case we set off with an initial solution of 0. The left and right boundary are fixed at 1, while the upper and lower boundaries are set to 0. After a sufficient number of iterations, the system converges against a saddle-shape.

I=[0,…,99]2S=ℝS0:ℤ2→ℝS0((x,y))={1,x<00,0≤x<1001,x≥100s=((0,−1),(−1,0),(1,0),(0,1))T:ℝ4→ℝT((x1,x2,x3,x4))=0.25⋅(x1+x2+x3+x4)

Stencils The shape of the neighborhood used during the updates depends on the application itself. The most common stencils are the 2D or 3D versions of the von Neumann neighborhood and Moore neighborhood. The example above uses a 2D von Neumann stencil while LBM codes generally use its 3D variant. Conway's Game of Life uses the 2D Moore neighborhood. That said, other stencils such as a 25-point stencil for seismic wave propagation can be found, too.

Implementation issues Many simulation codes may be formulated naturally as stencil computations. Since computing time and memory consumption grow linearly with the number of array elements, parallel implementations of stencil computations are of paramount importance to research. This is challenging since the computations are tightly coupled (because of the cell updates depending on neighboring cells) and most stencil computations are memory bound (i.e. the ratio of memory accesses to calculations is high). Virtually all current parallel architectures have been explored for executing stencil computations efficiently; at the moment GPGPUs have proven to be most efficient.

Libraries Due to both the importance of stencil computations to computer simulations and their high computational requirements, there are a number of efforts which aim at creating reusable libraries to support scientists in performing stencil-based computations. The libraries are mostly concerned with the parallelization, but may also tackle other challenges, such as IO, steering and checkpointing. They may be classified by their APIs.

Patch-based libraries This is a traditional design. The library manages a set of n-dimensional scalar arrays, which the user program may access to perform updates. The library handles the synchronization of the boundaries (dubbed ghost zone or halo). The advantage of this interface is that the user program may loop over the arrays, which makes it easy to integrate legacy code . The disadvantage is that the library can not handle cache blocking (as this has to be done within the loops) or wrapping of the API-calls for accelerators (e.g. via CUDA or OpenCL). Implementations include Cactus, a physics problem solving environment, and waLBerla.

… excerpt ends here. Continue reading the full article.

Illustrations

Stencil computation: The shape of a 7-point 3D von Neumann style stencil.
The shape of a 7-point 3D von Neumann style stencil.
Stencil computation: Data dependencies of a selected cell in the 2D array.
Data dependencies of a selected cell in the 2D array.
Stencil computation illustration
Stencil computation illustration
Stencil computation illustration

Worked examples

Example 1 — a first encounter with Stencil computation

Start with the simplest possible case. Write down what Stencil computation claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Stencil computation before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Stencil computation ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Stencil computation

In research
Stencil computation appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Stencil computation in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Stencil computation is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computational fluid dynamics, Simulation software, so understanding it makes those chapters shorter.
In everyday life
Look for Stencil computation outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Stencil computation” →

How to study Stencil computation in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Stencil computation means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Stencil computation out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Stencil computation in simple terms?

Stencil computations, or iterative stencil loops (ISL), are a class of numerical data processing solution which update array elements according to some fixed pattern, called a stencil. They are most commonly found in computer simulations, e.g. for computational fluid dynamics in the context of scie…

Why does Stencil computation matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Stencil computation?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Stencil computation.

Tags

  • Computational fluid dynamics
  • Simulation software

Keep exploring