-->

Saturday, March 3, 2018

Stencil codes are a class of iterative kernels which update array elements according to some fixed pattern, called a stencil. They are most commonly found in the codes of 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 codes 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 codes.

Definition




Screencast 1 - Unified Stencil Code Running on CUDA and OpenMP - Get it for free: https://github.com/muellermichel/Hybrid-Fortran Follow me on Twitter for the newest updates: https://twitter.com/mueller_michel Hybrid Fortran was developed at Tokyo Institute...

Stencil codes 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, the stencil code updates all array elements. Using neighboring array elements in a fixed pattern (called 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 codes as a 5-tuple ( I , S , S 0 , s , T ) {\displaystyle (I,S,S_{0},s,T)} with the following meaning:

  • I = ∏ i = 1 k [ 0 , … , n i ] {\displaystyle I=\prod _{i=1}^{k}[0,\ldots ,n_{i}]} is the index set. It defines the topology of the array.
  • S {\displaystyle S} is the (not necessarily finite) set of states, one of which each cell may take on on any given timestep.
  • S 0 : Z k â†' S {\displaystyle S_{0}\colon \mathbb {Z} ^{k}\to S} defines the initial state of the system at time 0.
  • s ∈ ∏ i = 1 l Z k {\displaystyle s\in \prod _{i=1}^{l}\mathbb {Z} ^{k}} is the stencil itself and describes the actual shape of the neighborhood. There are l {\displaystyle l} elements in the stencil.
  • T : S l â†' S {\displaystyle T\colon S^{l}\to 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 {\displaystyle c\in I} . The stencil is an ordered set of l {\displaystyle l} relative coordinates. We can now obtain for each cell c {\displaystyle c} the tuple of its neighbors indices I c {\displaystyle I_{c}}

I c = { j ∣ ∃ x ∈ s : j = c + x } {\displaystyle I_{c}=\{j\mid \exists x\in s:j=c+x\}\,}

Their states are given by mapping the tuple I c {\displaystyle I_{c}} to the corresponding tuple of states N i ( c ) {\displaystyle N_{i}(c)} , where N i : I â†' S l {\displaystyle N_{i}\colon I\to S^{l}} is defined as follows:

N i ( c ) = ( s 1 , … , s l )  with  s j = S i ( I c ( j ) ) {\displaystyle N_{i}(c)=(s_{1},\ldots ,s_{l}){\text{ with }}s_{j}=S_{i}(I_{c}(j))\,}

This is all we need to define the system's state for the following time steps S i + 1 : Z k â†' S {\displaystyle S_{i+1}\colon \mathbb {Z} ^{k}\to S} with i ∈ N {\displaystyle i\in \mathbb {N} } :

S i + 1 ( c ) = { T ( N i ( c ) ) , c ∈ I S i ( c ) , c ∈ Z k ∖ I {\displaystyle S_{i+1}(c)={\begin{cases}T(N_{i}(c)),&c\in I\\S_{i}(c),&c\in \mathbb {Z} ^{k}\setminus I\end{cases}}}

Note that S i {\displaystyle S_{i}} is defined on Z k {\displaystyle \mathbb {Z} ^{k}} and not just on I {\displaystyle I} since the boundary conditions need to be set, too. Sometimes the elements of I c {\displaystyle I_{c}} may be defined by a vector addition modulo the simulation space's dimension to realize toroidal topologies:

I c = { j ∣ ∃ x ∈ s : j = ( ( c + x ) mod ( n 1 , … , n k ) ) } {\displaystyle I_{c}=\{j\mid \exists x\in s:j=((c+x)\mod (n_{1},\ldots ,n_{k}))\}}

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 ] 2 S = R S 0 : Z 2 â†' R S 0 ( ( x , y ) ) = { 1 , x < 0 0 , 0 ≤ x < 100 1 , x ≥ 100 s = ( ( 0 , âˆ' 1 ) , ( âˆ' 1 , 0 ) , ( 1 , 0 ) , ( 0 , 1 ) ) T : R 4 â†' R T ( ( x 1 , x 2 , x 3 , x 4 ) ) = 0.25 â‹… ( x 1 + x 2 + x 3 + x 4 ) {\displaystyle {\begin{aligned}I&=[0,\ldots ,99]^{2}\\S&=\mathbb {R} \\S_{0}&:\mathbb {Z} ^{2}\to \mathbb {R} \\S_{0}((x,y))&={\begin{cases}1,&x<0\\0,&0\leq x<100\\1,&x\geq 100\end{cases}}\\s&=((0,-1),(-1,0),(1,0),(0,1))\\T&\colon \mathbb {R} ^{4}\to \mathbb {R} \\T((x_{1},x_{2},x_{3},x_{4}))&=0.25\cdot (x_{1}+x_{2}+x_{3}+x_{4})\end{aligned}}}

Stencils


A generic VHDL template for 2D stencil code applications on FPGAs ...
A generic VHDL template for 2D stencil code applications on FPGAs .... Source : www.researchgate.net

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


rude / love / issues / #1199 - Stencils do not work on a Windows 7 ...
rude / love / issues / #1199 - Stencils do not work on a Windows 7 .... Source : bitbucket.org

Many simulation codes may be formulated naturally as stencil codes. Since computing time and memory consumption grow linearly with the number of array elements, parallel implementations of stencil codes 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 codes 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 codes efficiently; at the moment GPGPUs have proven to be most efficient.

Libraries


ExaStencils: Advanced Stencil-Code Engineering (PDF Download ...
ExaStencils: Advanced Stencil-Code Engineering (PDF Download .... Source : www.researchgate.net

Due to both, the importance of stencil codes to computer simulations and their high computational requirements, there are a number of efforts which aim at creating reusable libraries to support scientists in implementing new stencil codes. 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 API.

Patch-based libraries

This is a traditional design. The library manages a set of n-dimensional scalar arrays, which the user code 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 code may loop over the arrays, which makes it easy to integrate legacy codes . The disadvantage is that the library can not handle cache blocking (as this has to be done within the loops) or wrapping of the code for accelerators (e.g. via CUDA or OpenCL). Notable implementations include Cactus, a physics problem solving environment, and waLBerla.

Cell-based libraries

These libraries move the interface to updating single simulation cells: only the current cell and its neighbors are exposed to the user code, e.g. via getter/setter methods. The advantage of this approach is that the library can control tightly which cells are updated in which order, which is useful not only to implement cache blocking, but also to run the same code on multi-cores and GPUs. This approach requires the user to recompile the source code together with the library. Otherwise a function call for every cell update would be required, which would seriously impair performance. This is only feasible with techniques such as class templates or metaprogramming, which is also the reason why this design is only found in newer libraries. Examples are Physis and LibGeoDecomp.

See also


PJ Masks Cookie Stencil Cupcake stencils Cake stencil set
PJ Masks Cookie Stencil Cupcake stencils Cake stencil set. Source : www.etsy.com

  • Advanced Simulation Library
  • Finite difference method
  • Computer simulation
  • Five-point stencil
  • Stencil jumping

References


Unique 'Custom Depth Stencil Value' | Game Engine Bread
Unique 'Custom Depth Stencil Value' | Game Engine Bread. Source : gameenginebread.blogspot.com

External links


X-Press It Australia | X-Press It Stencil Sheet A4 4pk
X-Press It Australia | X-Press It Stencil Sheet A4 4pk. Source : www.x-pressit.com.au

  • Physis
  • LibGeoDecomp
  • waLBerla

Code modernization strategies to 3-D Stencil-based applications on ...
Code modernization strategies to 3-D Stencil-based applications on .... Source : www.sciencedirect.com

 
Sponsored Links