📄 blitz_4.html
字号:
<HTML><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><!-- Created on October, 14 2005 by texi2html 1.64 --><!-- Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author) Karl Berry <karl@freefriends.org> Olaf Bachmann <obachman@mathematik.uni-kl.de> and many others.Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de>Send bugs and suggestions to <texi2html@mathematik.uni-kl.de> --><HEAD><TITLE>Blitz++: Stencils</TITLE><META NAME="description" CONTENT="Blitz++: Stencils"><META NAME="keywords" CONTENT="Blitz++: Stencils"><META NAME="resource-type" CONTENT="document"><META NAME="distribution" CONTENT="global"><META NAME="Generator" CONTENT="texi2html 1.64"></HEAD><BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000"><A NAME="SEC104"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_3.html#SEC103"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC105"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H1> 4. Stencils </H1><!--docid::SEC104::--><P><A NAME="IDX309"></A><A NAME="IDX310"></A></P><P>Blitz++ provides an implementation of stencil objects which is currently<STRONG>experimental</STRONG>. This means that the exact details of how they aredeclared and used may change in future releases. Use at your own risk.</P><P><HR SIZE="6"><A NAME="SEC105"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC104"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC106"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC104"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC104"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 4.1 Motivation: a nicer notation for stencils </H2><!--docid::SEC105::--><P>Suppose we wanted to implement the 3-D acoustic wave equation using finitedifferencing. Here is how a single iteration would look using subarraysyntax:</P><P><TABLE><tr><td> </td><td class=example><pre>Range I(1,N-2), J(1,N-2), K(1,N-2);P3(I,J,K) = (2-6*c(I,J,K)) * P2(I,J,K) + c(I,J,K)*(P2(I-1,J,K) + P2(I+1,J,K) + P2(I,J-1,K) + P2(I,J+1,K) + P2(I,J,K-1) + P2(I,J,K+1)) - P1(I,J,K);</pre></td></tr></table></P><P>This syntax is a bit klunky. With stencil objects, the implementationbecomes:</P><P><TABLE><tr><td> </td><td class=example><pre>BZ_DECLARE_STENCIL4(acoustic3D_stencil,P1,P2,P3,c) P3 = 2 * P2 + c * Laplacian3D(P2) - P1;BZ_END_STENCIL . .applyStencil(acoustic3D_stencil(), P1, P2, P3, c);</pre></td></tr></table></P><P><HR SIZE="6"><A NAME="SEC106"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC105"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC107"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC107"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC104"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 4.2 Declaring stencil objects </H2><!--docid::SEC106::--><P>A stencil declaration may not be inside a function. It can appear inside aclass declaration (in which case the stencil object is a nested type).</P><P>Stencil objects are declared using the macros <CODE>BZ_DECLARE_STENCIL1</CODE>,<CODE>BZ_DECLARE_STENCIL2</CODE>, etc. The number suffix is how many arrays areinvolved in the stencil (in the above example, 4 arrays-- P1, P2, P3, c -- areused, so the macro <CODE>BZ_DECLARE_STENCIL4</CODE> is invoked).</P><P>The first argument is a name for the stencil object. Subsequent argumentsare names for the arrays on which the stencil operates.</P><P>After the stencil declaration, the macro <CODE>BZ_END_STENCIL</CODE> must appear(or the macro <CODE>BZ_END_STENCIL_WITH_SHAPE</CODE>, described in the nextsection).</P><P>In between the two macros, you can have multiple assignment statements,if/else/elseif constructs, function calls, loops, etc.</P><P>Here are some simple examples:</P><P><A NAME="IDX311"></A></P><P><TABLE><tr><td> </td><td class=example><pre>BZ_DECLARE_STENCIL2(smooth2D,A,B) A = (B(0,0) + B(0,1) + B(0,-1) + B(1,0) + B(-1,0)) / 5.0;BZ_END_STENCILBZ_DECLARE_STENCIL4(acoustic2D,P1,P2,P3,c) A = 2 * P2 + c * (-4 * P2(0,0) + P2(0,1) + P2(0,-1) + P2(1,0) + P2(-1,0)) - P1;BZ_END_STENCILBZ_DECLARE_STENCIL8(prop2D,E1,E2,E3,M1,M2,M3,cE,cM) E3 = 2 * E2 + cE * Laplacian2D(E2) - E1; M3 = 2 * M2 + cM * Laplacian2D(M2) - M1;BZ_END_STENCILBZ_DECLARE_STENCIL3(smooth2Db,A,B,c) if ((c > 0.0) && (c < 1.0)) A = c * (B(0,0) + B(0,1) + B(0,-1) + B(1,0) + B(-1,0)) / 5.0 + (1-c)*B; else A = 0;BZ_END_STENCIL</pre></td></tr></table></P><P>Currently, a stencil can take up to 11 array parameters.</P><P>You can use the notation <CODE>A(i,j,k)</CODE> to read the element at an offset<CODE>(i,j,k)</CODE> from the current element. If you omit the parentheses(i.e. as in "<CODE>A</CODE>" then the current element is read.</P><P>You can invoke <EM>stencil operators</EM> which calculate finite differencesand laplacians.</P><P><HR SIZE="6"><A NAME="SEC107"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC106"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC108"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC108"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC104"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_5.html#SEC121"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 4.3 Automatic determination of stencil extent </H2><!--docid::SEC107::--><P>In stencil declarations such as</P><P><TABLE><tr><td> </td><td class=example><pre>BZ_DECLARE_STENCIL2(smooth2D,A,B) A = (B(0,0) + B(0,1) + B(0,-1) + B(1,0) + B(-1,0)) / 5.0;BZ_END_STENCIL</pre></td></tr></table></P><P>Blitz++ will try to automatically determine the spatial extent of thestencil. This will usually work for stencils defined on integer or floatarrays. However, the mechanism does not work well for complex-valuedarrays, or arrays of user-defined types. If you get a peculiar error whenyou try to use a stencil, you probably need to tell Blitz++ the specialextent of the stencil manually.</P><P>You do this by ending a stencil declaration with<CODE>BZ_END_STENCIL_WITH_SHAPE</CODE>:</P><P><TABLE><tr><td> </td><td class=example><pre>BZ_DECLARE_STENCIL2(smooth2D,A,B) A = (B(0,0) + B(0,1) + B(0,-1) + B(1,0) + B(-1,0)) / 5.0;BZ_END_STENCIL_WITH_SHAPE(shape(-1,-1),shape(+1,+1))</pre></td></tr></table></P><P>The parameters of this macro are: a <CODE>TinyVector</CODE> (constructed by the<CODE>shape()</CODE> function) containing the lower bounds of the stencil offsets,and a <CODE>TinyVector</CODE> containing the upper bounds. You can determine thisby looking at the the terms in the stencil and finding the minimum andmaximum value of each index:</P><P><TABLE><tr><td> </td><td class=example><pre> A = (B(0, 0) + B(0, +1) + B(0, -1) + B(+1, 0) + B(-1, 0)) / 5.0; --------min indices -1, -1max indices +1, +1</pre></td></tr></table></P><P><HR SIZE="6"><A NAME="SEC108"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC107"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC109"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC119"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC104"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_4.html#SEC119"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 4.4 Stencil operators </H2><!--docid::SEC108::--><P>This section lists all the stencil operators provided by Blitz++. Theyassume that an array represents evenly spaced data points separated by a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -