Stencils


An ordinary function maps one or more operands to a value. Extending this idea to grids of values, a function on a grid maps one or more grid values to a value. If these operand grid values are located close to each other, it is called a stencil function. Figure 1 illustrates the pieces of a two-dimensional stencil function: a stencil of coefficients and a portion of a two-dimensional grid of values. (The grid may be larger, but its other values are omitted to simplify the illustration.) This stencil function computes the weighted average of a grid value and its eight neighbors. The stencil (on the left) has the coefficients of the weighted average. For example, the central coefficient, indicated by the heavy lines surrounding its cell, is 1 + b - 4a, while the coefficient to its right is a. To compute the stencil function’s value corresponding to any grid position, place the stencil’s central coefficient on top of the specified grid position and then compute the weighted average of the coefficients and the underlying grid values. For example, consider computing the stencil function’s value corresponding to the grid value 1000. Lay the stencil’s cell with coefficient 1+ b - 4a on top of the 1000 value. The weighted average, starting with the upper-left stencil corner and summing along rows, is 0*37 + a*20 + 0*(–7) + a*20 + (1+b–4a)*1000 + a*10 + 0*23 + a*10 + 0*37. This equals 1000(1+b–4a) + 60a (i.e., the stencil function’s value corresponding to the grid value 1000).

A stencil function can be computed at any grid position as long as it has enough neighbors. Furthermore, since the permissible positions are in a grid and each such computation yields one value, the result is a grid of values. Figure 2 illustrates applying the stencil in Figure 1 to two grid positions: (3,4) and (5,2). This figure also illustrates that grid values, (e.g., the value at (4,3)) can participate in more than one computation. The stencil can be applied to any grid position except those on the grid’s borders. For example, when placing the stencil’s central coefficient at grid position (0,1), the stencil’s left column does not overlap any grid values, so a weighted average cannot be determined.

POOMA supports a wider variety of stencils than that used in the neutron diffusion program. A stencil need not be square, can have any dimensionality, and can support any computation expressible in C++. For example, the POOMA code distribution contains a stencil implementation of Conway’s Game of Life, where the stencil uses a cell’s neighboring values to determine if it should stay alive or die. This computation consists of addition and two if statements, not a weighted average.