Debian Codegraph @

blitz/generate/genstencils.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 #!/usr/bin/python3
2
3 # Generates stencil code. This replaces the macros in stencil-et.h,
4 # which make it impossible to debug the generated code.
5
6 from __future__ import print_function
7
8 import sys
9
10 os=open(sys.argv[1],'w')
11 print("Generating file %s"%sys.argv[1])
12
13 def BZ_ET_STENCIL_REDIRECT(name):
14 stub="""
15
16
17 /* Explicit operators for arrays for stencil name. */
18 template<typename T, int N>
19 inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> >
20 #name#(const Array<T,N>& d1)
21 { return #name#(d1.wrap()); }
22
23 template<typename T, int N>
24 inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> >
25 #name#(Array<T,N>& d1)
26 { return #name#(d1.wrap()); }
27 """
28 os.write(stub.replace("#name#",name))
29
30 def BZ_ET_STENCIL(name, result, etresult, MINB, MAXB):
31 stub="""
32
33
34 /** Defines a stencil ET "#name#" that operates on an Array<P_numtype, N_rank>
35 and specifies the return type as Array<#result#, N_rank>. The result
36 type is used when running on an array and the etresult type when
37 running on an expression. The extent of the stencil is MINB-MAXB.
38 If you want to refer to the native type
39 of the expression, set result="P_numtype" and etresult="typename
40 T1::T_numtype". Sorry for that ugliness, but they define types
41 differently. The stencil ET calls the stencil operator
42 name_stencilop, defined in stencilops.h. **/
43
44 template<typename P_expr, _bz_typename P_numtype>
45 class #name#_et : public _bz_StencilExpr<P_expr, P_numtype>
46 {
47 public:
48 typedef _bz_StencilExpr<P_expr, P_numtype> T_base;
49 typedef _bz_typename T_base::T_numtype T_numtype;
50 typedef _bz_typename T_base::T_expr T_expr;
51
52 // if P_numtype is an ET-type, we need to return an expr
53 typedef typename selectET<P_numtype,
54 T_numtype,
55 ETBase<_bz_ArrayExpr<_bz_ArrayExprConstant<P_numtype> > > >::T_selected T_typeprop;
56 typedef typename unwrapET<T_typeprop>::T_unwrapped T_result;
57 typedef T_numtype T_optype;
58
59 template<int N> struct tvresult {
60 typedef #name#_et<
61 typename T_expr::template tvresult<N>::Type,
62 T_numtype> Type;
63 };
64
65 typedef #name#_et<_bz_typename P_expr::T_range_result, T_numtype> T_range_result;
66
67 using T_base::iter_;
68 using T_base::rank_;
69 public:
70 #name#_et(const #name#_et& a) :
71 _bz_StencilExpr<P_expr, T_numtype>(a)
72 { }
73
74 #name#_et(BZ_ETPARM(T_expr) a) :
75 _bz_StencilExpr<P_expr, T_numtype>(a)
76 { }
77
78 #name#_et(_bz_typename T_expr::T_ctorArg1 a) :
79 _bz_StencilExpr<P_expr, T_numtype>(a)
80 { }
81
82 T_result operator*() const
83 { return #name#_stencilop(iter_); }
84
85 /* this is not really const, because we don't undo the moveTo, but
86 that should not be visible to outside. It would be if we used
87 some kind of mixed index and stack traversal, but that will
88 screw things up, const or not. */
89 template<int N_rank2>
90 T_result operator()(const TinyVector<int, N_rank2>& i) const
91 { iter_.moveTo(i); return #name#_stencilop(iter_); }
92 T_range_result operator()(const RectDomain<rank_>& d) const
93 { return T_range_result(iter_(d)); }
94
95 T_result operator[](int i) const
96 { return #name#_stencilop(iter_[i]); }
97 T_result fastRead(diffType i) const
98 {/* this probably isn't very fast... */
99 iter_._bz_offsetData(i);
100 T_result r = #name#_stencilop(iter_);
101 iter_._bz_offsetData(-i);
102 return r;
103 }
104
105 /** This way of vectorizing won't work on stencils. */
106 template<int N>
107 typename tvresult<N>::Type fastRead_tv(diffType i) const {
108 BZPRECHECK(0, "Can't vectorize stencils");
109 return iter_.template fastRead_tv<N>(i); }
110
111 T_result shift(int offset, int dim) const
112 {
113 iter_._bz_offsetData(offset, dim);
114 T_result r = #name#_stencilop(iter_);
115 iter_._bz_offsetData(-offset, dim);
116 return r;
117 }
118
119 T_result shift(int offset1, int dim1, int offset2, int dim2) const
120 {
121 iter_._bz_offsetData(offset1, dim1, offset2, dim2);
122 T_result r = #name#_stencilop (iter_);
123 iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);
124 return r;
125 }
126
127 void prettyPrint(std::string &str,
128 prettyPrintFormat& format) const
129 {
130 str += "#name# (stencil)";
131 str += "(";
132 iter_.prettyPrint(str, format);
133 str += ")";
134 }
135
136 template<typename T1, typename T2 = nilArraySection,
137 class T3 = nilArraySection, typename T4 = nilArraySection,
138 class T5 = nilArraySection, typename T6 = nilArraySection,
139 class T7 = nilArraySection, typename T8 = nilArraySection,
140 class T9 = nilArraySection, typename T10 = nilArraySection,
141 class T11 = nilArraySection>
142 class SliceInfo {
143 public:
144 typedef #name#_et<T_expr, T_numtype> T_slice;
145 };
146
147 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
148 typename T7, typename T8, typename T9, typename T10, typename T11>
149 #name#_et
150 operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const
151 {
152 /* because stencils work inherently in several dimensions it's
153 complicated to slice the domain. slices will be changed to unit
154 ranges instead. slicing stencil result thus *never* changes
155 the rank of the expression, unlike the normal case. */
156 return #name#_et
157 (iter_(_bz_makeRange(r1),
158 _bz_makeRange(r2),
159 _bz_makeRange(r3),
160 _bz_makeRange(r4),
161 _bz_makeRange(r5),
162 _bz_makeRange(r6),
163 _bz_makeRange(r7),
164 _bz_makeRange(r8),
165 _bz_makeRange(r9),
166 _bz_makeRange(r10),
167 _bz_makeRange(r11)));
168 }
169
170 };
171 /* generate an ET object from an expression */
172 template<typename T1>
173 inline _bz_ArrayExpr<#name#_et<typename blitz::asExpr<T1>::T_expr::T_range_result, #etresult#> >
174 #name#(const blitz::ETBase<T1>& d1)
175 {
176 return _bz_ArrayExpr<#name#_et<typename blitz::asExpr<T1>::T_expr::T_range_result, #etresult#> >
177 (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),MINB, MAXB)));
178 }
179 /* redirect calls with bare arrays to the main function */
180 template<typename T, int N>
181 inline _bz_ArrayExpr<#name#_et<typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result, #result#> >
182 #name#(const Array<T,N>& d1)
183 { return #name#(d1.wrap()); }
184
185 template<typename T, int N>
186 inline _bz_ArrayExpr<#name#_et<typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result, #result#> >
187 #name#(Array<T,N>& d1)
188 { return #name#(d1.wrap()); }"""
189 os.write(stub.replace("#name#",name).replace("#result#",result).replace("#etresult#",etresult).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))
190
191
192 def BZ_ET_STENCIL2(name, result, etresult, MINB, MAXB):
193 stub="""
194
195
196 /** Defines a stencil "#name#" ET that operates on two arrays of arbitrary type
197 and specifies the return type as array<#result#, N_rank>. The result
198 type is used when running on an array and the etresult type when
199 running on an expression. If you want to refer to the native type
200 of the expression, set result="P_numtype" and etresult="typename
201 T1::T_numtype". Sorry for that ugliness, but they define types
202 differently. */
203 template<typename P_expr1, typename P_expr2, _bz_typename P_numtype>
204 class #name#_et2 : public _bz_StencilExpr2<P_expr1, P_expr2, P_numtype>
205 {
206 public:
207 typedef _bz_StencilExpr2<P_expr1, P_expr2, P_numtype> T_base;
208 typedef _bz_typename T_base::T_numtype T_numtype;
209 typedef _bz_typename T_base::T_expr1 T_expr1;
210 typedef _bz_typename T_base::T_expr2 T_expr2;
211
212 // if P_numtype is an ET-type, we need to return an expr
213 typedef typename selectET<P_numtype,
214 T_numtype,
215 ETBase<_bz_ArrayExpr<_bz_ArrayExprConstant<P_numtype> > > >::T_selected T_typeprop;
216 typedef typename unwrapET<T_typeprop>::T_unwrapped T_result;
217 typedef T_numtype T_optype;
218
219 /// dummy
220 template<int N> struct tvresult {
221 typedef #name#_et2<
222 typename T_expr1::template tvresult<N>::Type,
223 typename T_expr2::template tvresult<N>::Type,
224 T_numtype> Type;
225 };
226
227 typedef #name#_et2<_bz_typename P_expr1::T_range_result, _bz_typename P_expr2::T_range_result, T_numtype> T_range_result;
228
229 using T_base::iter1_;
230 using T_base::iter2_;
231 using T_base::rank_;
232 public:
233 #name#_et2(const #name#_et2& a) :
234 _bz_StencilExpr2<P_expr1, P_expr2, T_numtype>(a)
235 { }
236
237 #name#_et2(BZ_ETPARM(T_expr1) a, BZ_ETPARM(T_expr2) b) :
238 _bz_StencilExpr2<P_expr1, P_expr2, T_numtype>(a, b)
239 { }
240
241 T_result operator*() const
242 { return #name#_stencilop(iter1_, iter2_); }
243
244 T_result operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const
245 { iter1_.moveTo(i); iter2_.moveTo(i);
246 return #name#_stencilop(iter1_, iter2_); }
247
248 T_range_result operator()(const RectDomain<rank_>& d) const
249 { return T_range_result(iter1_(d), iter2_(d)); }
250
251 T_result operator[](int i) const
252 { return #name#_stencilop(iter1_[i], iter2_[i]); }
253
254 T_result fastRead(diffType i) const
255 {/* this probably isn't very fast... */
256 iter1_._bz_offsetData(i); iter2_._bz_offsetData(i);
257 T_result r = #name#_stencilop (iter1_, iter2_);
258 iter1_._bz_offsetData(-i); iter2_._bz_offsetData(-i);
259 return r;
260 }
261
262 /** This way of vectorizing won't work on stencils. */
263 template<int N>
264 typename tvresult<N>::Type fastRead_tv(diffType i) const {
265 BZPRECHECK(0, "Can't vectorize stencils");
266 return typename tvresult<N>::Type(iter1_.fastRead_tv<N>(i),
267 iter2_.fastRead_tv<N>(i)); }
268
269 T_result shift(int offset, int dim) const
270 {
271 iter1_._bz_offsetData(offset, dim);
272 iter2_._bz_offsetData(offset, dim);
273 T_result r = #name#_stencilop (iter1_, iter2_);
274 iter1_._bz_offsetData(-offset, dim);
275 iter2_._bz_offsetData(-offset, dim);
276 return r;
277 }
278
279 T_result shift(int offset1, int dim1, int offset2, int dim2) const
280 {
281 iter1_._bz_offsetData(offset1, dim1, offset2, dim2);
282 iter2_._bz_offsetData(offset1, dim1, offset2, dim2);
283 T_result r = #name#_stencilop (iter1_, iter2_);
284 iter1_._bz_offsetData(-offset1, dim1, -offset2, dim2);
285 iter2_._bz_offsetData(-offset1, dim1, -offset2, dim2);
286 return r;
287 }
288
289 void prettyPrint(std::string &str,
290 prettyPrintFormat& format) const
291 {
292 str += "name (stencil)";
293 str += "(";
294 iter1_.prettyPrint(str, format);
295 str += ", ";
296 iter2_.prettyPrint(str, format);
297 str += ")";
298 }
299
300 template<typename T1, typename T2 = nilArraySection,
301 class T3 = nilArraySection, typename T4 = nilArraySection,
302 class T5 = nilArraySection, typename T6 = nilArraySection,
303 class T7 = nilArraySection, typename T8 = nilArraySection,
304 class T9 = nilArraySection, typename T10 = nilArraySection,
305 class T11 = nilArraySection>
306 class SliceInfo {
307 public:
308 typedef #name#_et2<T_expr1, T_expr2, T_numtype> T_slice;
309 };
310
311 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
312 typename T7, typename T8, typename T9, typename T10, typename T11>
313 #name#_et2
314 operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const
315 {
316 return #name#_et2
317 (iter_(_bz_makeRange(r1),
318 _bz_makeRange(r2),
319 _bz_makeRange(r3),
320 _bz_makeRange(r4),
321 _bz_makeRange(r5),
322 _bz_makeRange(r6),
323 _bz_makeRange(r7),
324 _bz_makeRange(r8),
325 _bz_makeRange(r9),
326 _bz_makeRange(r10),
327 _bz_makeRange(r11)));
328 }
329 };
330
331 /* create ET object from application to expression */
332 template<typename T1, typename T2>
333 inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<T1>::T_expr::T_range_result, typename blitz::asExpr<T2>::T_expr::T_range_result, #etresult#> >
334 #name#(const blitz::ETBase<T1>& d1,
335 const blitz::ETBase<T2>& d2)
336 {
337 return _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<T1>::T_expr::T_range_result, typename blitz::asExpr<T2>::T_expr::T_range_result, #etresult#> >
338 (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),MINB, MAXB)),
339 blitz::asExpr<T2>::getExpr(d2.unwrap())(_bz_shrinkDomain(d2.unwrap().domain(),MINB, MAXB)));
340 }
341 /* matches to calls involving bare arrays (this is very annoying
342 because we have to exactly match every possible call combination
343 to ensure that this matches instead of the operator in
344 stencilops.h) */
345 template<typename T1, typename T2, int N2>
346 inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<T1>::T_expr::T_range_result, typename blitz::asExpr<Array<T2,N2> >::T_expr::T_range_result, #result#> >
347 #name#(const blitz::ETBase<T1>& d1, Array<T2,N2>& d2)
348 { return #name#(d1.wrap(), d2.wrap()); }
349
350 template<typename T1, typename T2, int N2>
351 inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<T1>::T_expr::T_range_result, typename blitz::asExpr<Array<T2,N2> >::T_expr::T_range_result, #result#> >
352 #name#(const blitz::ETBase<T1>& d1, const Array<T2,N2>& d2)
353 { return #name#(d1.wrap(), d2.wrap()); }
354
355 template<typename T1, int N1, typename T2>
356 inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<Array<T1,N1> >::T_expr::T_range_result, typename blitz::asExpr<T2>::T_expr::T_range_result, #result#> >
357 #name#(Array<T1,N1>& d1, const blitz::ETBase<T2>& d2)
358 { return #name#(d1.wrap(), d2.wrap()); }
359
360 template<typename T1, int N1, typename T2>
361 inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<Array<T1,N1> >::T_expr::T_range_result, typename blitz::asExpr<T2>::T_expr::T_range_result, #result#> >
362 #name#(const Array<T1,N1>& d1, const blitz::ETBase<T2>& d2)
363 { return #name#(d1.wrap(), d2.wrap()); }
364
365 template<typename T1, int N1, typename T2, int N2>
366 inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<Array<T1,N1> >::T_expr::T_range_result, typename blitz::asExpr<Array<T2,N2> >::T_expr::T_range_result, #result#> >
367 #name#(const Array<T1,N1>& d1, Array<T2,N2>& d2)
368 { return #name#(d1.wrap(), d2.wrap()); }
369
370 template<typename T1, int N1, typename T2, int N2>
371 inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<Array<T1,N1> >::T_expr::T_range_result, typename blitz::asExpr<Array<T2,N2> >::T_expr::T_range_result, #result#> >
372 #name#(Array<T1,N1>& d1, const Array<T2,N2>& d2)
373 { return #name#(d1.wrap(), d2.wrap()); }
374
375 template<typename T, int N>
376 inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result, typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result, #result#> >
377 #name#(Array<T,N>& d1, Array<T,N>& d2)
378 { return #name#(d1.wrap(), d2.wrap()); }
379
380 template<typename T, int N>
381 inline _bz_ArrayExpr<#name#_et2<typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result, typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result, #result#> >
382 #name#(const Array<T,N>& d1, const Array<T,N>& d2)
383 { return #name#(d1.wrap(), d2.wrap()); }"""
384 os.write(stub.replace("#name#",name).replace("#result#",result).replace("#etresult#",etresult).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))
385
386 def BZ_ET_STENCILM(name, result_rank, MINB, MAXB):
387 stub="""
388
389
390 /* Defines a stencil ET "#name#" that operates on an array
391 array<P_numtype, N_rank> and returns a multicomponent
392 array<TinyMatrix<P_numtype::T_element, rank, rank> >,
393 N_rank>. P_numtype can be a TinyVector or a scalar, I think. */
394
395 template<typename P_expr>
396 class #name#_et : public _bz_StencilExpr<P_expr, TinyMatrix<_bz_typename multicomponent_traits<typename P_expr::T_numtype>::T_element, result_rank, result_rank> >
397 {
398 public:
399 typedef _bz_StencilExpr<P_expr, TinyMatrix<_bz_typename multicomponent_traits<typename P_expr::T_numtype>::T_element, result_rank, result_rank> > T_base;
400 typedef _bz_typename T_base::T_numtype T_numtype;
401 typedef _bz_typename T_base::T_expr T_expr;
402
403 // there is no return type selection, as we are returning a
404 // TinyMatrix. This must be returned as a FastTMCopyIterator since the
405 // output of the stencil operator is a temporary.
406 typedef ETBase<_bz_ArrayExpr<FastTM2CopyIterator<typename multicomponent_traits<typename P_expr::T_numtype>::T_element, result_rank, result_rank> > > T_typeprop;
407 typedef typename unwrapET<T_typeprop>::T_unwrapped T_result;
408 typedef T_numtype T_optype;
409
410 template<int N> struct tvresult {
411 typedef #name#_et<
412 typename T_expr::template tvresult<N>::Type> Type;
413 };
414
415 typedef #name#_et<_bz_typename P_expr::T_range_result> T_range_result;
416
417 using T_base::iter_;
418 using T_base::rank_;
419 public:
420 #name#_et(const #name#_et& a) :
421 _bz_StencilExpr<P_expr, T_numtype>(a)
422 { }
423
424 #name#_et(BZ_ETPARM(T_expr) a) :
425 _bz_StencilExpr<P_expr, T_numtype>(a)
426 { }
427
428 #name#_et(_bz_typename T_expr::T_ctorArg1 a) :
429 _bz_StencilExpr<P_expr, T_numtype>(a)
430 { }
431
432 T_result operator*() const
433 { return #name#_stencilop(iter_); }
434
435 T_result operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const
436 { iter_.moveTo(i); return #name#_stencilop(iter_); }
437
438 T_range_result operator()(const RectDomain<rank_>& d) const
439 { return T_range_result(iter_(d)); }
440
441 T_result operator[](int i) const
442 { return #name#_stencilop(iter_[i]); }
443
444 T_result fastRead(diffType i) const
445 {/* this probably isn't very fast... */
446 iter_._bz_offsetData(i);
447 T_result r = #name#_stencilop (iter_);
448 iter_._bz_offsetData(-i);
449 return r;
450 }
451
452 /** This way of vectorizing won't work on stencils. */
453 template<int N>
454 typename tvresult<N>::Type fastRead_tv(diffType i) const {
455 BZPRECHECK(0, "Can't vectorize stencils");
456 return iter_.template fastRead_tv<N>(i); }
457
458 T_result shift(int offset, int dim) const
459 {
460 iter_._bz_offsetData(offset, dim);
461 T_result r = #name#_stencilop (iter_);
462 iter_._bz_offsetData(-offset, dim);
463 return r;
464 }
465
466 T_result shift(int offset1, int dim1, int offset2, int dim2) const
467 {
468 iter_._bz_offsetData(offset1, dim1, offset2, dim2);
469 T_result r = #name#_stencilop (iter_);
470 iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);
471 return r;
472 }
473
474 void prettyPrint(std::string &str,
475 prettyPrintFormat& format) const
476 {
477 str += "name (stencil)";
478 str += "(";
479 iter_.prettyPrint(str, format);
480 str += ")";
481 }
482
483 template<typename T1, typename T2 = nilArraySection,
484 class T3 = nilArraySection, typename T4 = nilArraySection,
485 class T5 = nilArraySection, typename T6 = nilArraySection,
486 class T7 = nilArraySection, typename T8 = nilArraySection,
487 class T9 = nilArraySection, typename T10 = nilArraySection,
488 class T11 = nilArraySection>
489 class SliceInfo {
490 public:
491 typedef #name#_et<T_expr> T_slice;
492 };
493
494 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
495 typename T7, typename T8, typename T9, typename T10, typename T11>
496 #name#_et
497 operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const
498 {
499 return #name#_et
500 (iter_(_bz_makeRange(r1),
501 _bz_makeRange(r2),
502 _bz_makeRange(r3),
503 _bz_makeRange(r4),
504 _bz_makeRange(r5),
505 _bz_makeRange(r6),
506 _bz_makeRange(r7),
507 _bz_makeRange(r8),
508 _bz_makeRange(r9),
509 _bz_makeRange(r10),
510 _bz_makeRange(r11)));
511 }
512 };
513 /* create ET from application to expression */
514 template<typename T1>
515 inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<T1>::T_expr::T_range_result> >
516 #name#(const blitz::ETBase<T1>& d1)
517 {
518 return _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<T1>::T_expr::T_range_result> >
519 (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),MINB, MAXB)));
520 }
521 """
522 os.write(stub.replace("#name#",name).replace("result_rank",str(result_rank)).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))
523 BZ_ET_STENCIL_REDIRECT(name)
524
525 def BZ_ET_STENCILV(name, result_rank, MINB, MAXB):
526 stub="""
527
528
529 /* Defines a stencil ET "#name#" that operates on a (scalar) array<P_numtype,
530 N_rank> and returns a multicomponent
531 array<TinyVector<P_numtype::T_element, result_rank> >, N_rank>. */
532
533 template<typename P_expr>
534 class #name#_et : public _bz_StencilExpr<P_expr, TinyVector<typename P_expr::T_numtype,result_rank> >
535 {
536 public:
537 typedef _bz_StencilExpr<P_expr, TinyVector<typename P_expr::T_numtype,result_rank> > T_base;
538 typedef _bz_typename T_base::T_numtype T_numtype;
539 typedef _bz_typename T_base::T_expr T_expr;
540
541 // there is no return type selection, we assume P_numtype is scalar
542 // and that we are returning a TinyVector. This needs to be returned
543 // as a FastTVCopyIterator that keeps a copy of the TV it is
544 // iterating over, since the result of the stencil operator is a temporary.
545 typedef ETBase<_bz_ArrayExpr<FastTV2CopyIterator<typename P_expr::T_numtype, result_rank> > > T_typeprop;
546 typedef typename unwrapET<T_typeprop>::T_unwrapped T_result;
547 typedef typename T_expr::T_numtype T_optype;
548
549 template<int N> struct tvresult {
550 typedef #name#_et<
551 typename T_expr::template tvresult<N>::Type> Type;
552 };
553
554 typedef #name#_et<_bz_typename P_expr::T_range_result> T_range_result;
555
556 using T_base::iter_;
557 using T_base::rank_;
558 public:
559 #name#_et(const #name#_et& a) :
560 _bz_StencilExpr<P_expr, T_numtype>(a)
561 { }
562
563 #name#_et(BZ_ETPARM(T_expr) a) :
564 _bz_StencilExpr<P_expr, T_numtype>(a)
565 { }
566
567 #name#_et(_bz_typename T_expr::T_ctorArg1 a) :
568 _bz_StencilExpr<P_expr, T_numtype>(a)
569 { }
570
571 T_result operator*() const
572 { return #name#_stencilop(iter_); }
573
574 T_result operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const
575 { iter_.moveTo(i); return #name#_stencilop(iter_); }
576
577 T_range_result operator()(const RectDomain<rank_>& d) const
578 { return T_range_result(iter_(d)); }
579
580 T_result operator[](int i) const
581 { return #name#_stencilop(iter_[i]); }
582
583 T_result fastRead(diffType i) const
584 {/* this probably isn't very fast... */
585 iter_._bz_offsetData(i);
586 T_numtype r = #name#_stencilop (iter_);
587 iter_._bz_offsetData(-i);
588 return r;
589 }
590
591 /** This way of vectorizing won't work on stencils. */
592 template<int N>
593 typename tvresult<N>::Type fastRead_tv(diffType i) const {
594 BZPRECHECK(0, "Can't vectorize stencils");
595 return iter_.template fastRead_tv<N>(i); }
596
597 T_numtype shift(int offset, int dim) const
598 {
599 iter_._bz_offsetData(offset, dim);
600 T_numtype r = #name#_stencilop (iter_);
601 iter_._bz_offsetData(-offset, dim);
602 return r;
603 }
604
605 T_numtype shift(int offset1, int dim1, int offset2, int dim2) const
606 {
607 iter_._bz_offsetData(offset1, dim1, offset2, dim2);
608 T_numtype r = #name#_stencilop (iter_);
609 iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);
610 return r;
611 }
612
613 void prettyPrint(std::string &str,
614 prettyPrintFormat& format) const
615 {
616 str += "name (stencil)";
617 str += "(";
618 iter_.prettyPrint(str, format);
619 str += ")";
620 }
621
622 template<typename T1, typename T2 = nilArraySection,
623 class T3 = nilArraySection, typename T4 = nilArraySection,
624 class T5 = nilArraySection, typename T6 = nilArraySection,
625 class T7 = nilArraySection, typename T8 = nilArraySection,
626 class T9 = nilArraySection, typename T10 = nilArraySection,
627 class T11 = nilArraySection>
628 class SliceInfo {
629 public:
630 typedef #name#_et<T_expr> T_slice;
631 };
632
633 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
634 typename T7, typename T8, typename T9, typename T10, typename T11>
635 #name#_et
636 operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const
637 {
638 return #name#_et
639 (iter_(_bz_makeRange(r1),
640 _bz_makeRange(r2),
641 _bz_makeRange(r3),
642 _bz_makeRange(r4),
643 _bz_makeRange(r5),
644 _bz_makeRange(r6),
645 _bz_makeRange(r7),
646 _bz_makeRange(r8),
647 _bz_makeRange(r9),
648 _bz_makeRange(r10),
649 _bz_makeRange(r11)));
650 }
651 };
652 /* create ET from application to expression */
653 template<typename T1>
654 inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<T1>::T_expr::T_range_result> >
655 #name#(const blitz::ETBase<T1>& d1)
656 {
657 return _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<T1>::T_expr::T_range_result> >
658 (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),MINB, MAXB)));
659 }
660 """
661 os.write(stub.replace("#name#",name).replace("result_rank",str(result_rank)).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))
662 BZ_ET_STENCIL_REDIRECT(name)
663
664
665 def BZ_ET_STENCIL_SCA(name, MINB, MAXB):
666 stub="""
667
668
669 /** Defines a stencil ET "#name#" that operates on a multicomponent
670 array<P_numtype, N_rank> and returns a scalar
671 array<P_numtype::T_element, N_rank>. */
672
673 template<typename P_expr>
674 class #name#_et : public _bz_StencilExpr<P_expr, _bz_typename multicomponent_traits<typename P_expr::T_numtype>::T_element>
675 {
676 public:
677 typedef _bz_typename multicomponent_traits<typename P_expr::T_numtype>::T_element T_result;
678 typedef _bz_StencilExpr<P_expr, T_result> T_base;
679 typedef _bz_typename T_base::T_numtype T_numtype;
680 typedef _bz_typename T_base::T_expr T_expr;
681
682 // there is no selecting return type here. because we *know* it is
683 // scalar T_result, there's no question of whether we could be doing
684 // multicomponent evaluations.
685 typedef T_result T_typeprop;
686 typedef T_numtype T_optype;
687
688 template<int N> struct tvresult {
689 typedef #name#_et<
690 typename T_expr::template tvresult<N>::Type> Type;
691 };
692
693 typedef #name#_et<_bz_typename P_expr::T_range_result> T_range_result;
694
695 using T_base::iter_;
696 using T_base::rank_;
697 public:
698 #name#_et(const #name#_et& a) :
699 _bz_StencilExpr<P_expr, T_numtype>(a)
700 { }
701
702 #name#_et(BZ_ETPARM(T_expr) a) :
703 _bz_StencilExpr<P_expr, T_numtype>(a)
704 { }
705
706 #name#_et(_bz_typename T_expr::T_ctorArg1 a) :
707 _bz_StencilExpr<P_expr, T_numtype>(a)
708 { }
709
710 T_numtype operator*() const
711 { return #name#_stencilop(iter_); }
712 T_numtype operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const
713 { iter_.moveTo(i); return #name#_stencilop(iter_); }
714
715 T_range_result operator()(const RectDomain<rank_>& d) const
716 { return T_range_result(iter_(d)); }
717
718 T_numtype operator[](int i) const
719 { return #name#_stencilop(iter_[i]); }
720
721 T_numtype fastRead(diffType i) const
722 {/* this probably isn't very fast... */
723 iter_._bz_offsetData(i);
724 T_numtype r = #name#_stencilop (iter_);
725 iter_._bz_offsetData(-i);
726 return r;
727 }
728
729 /** This way of vectorizing won't work on stencils. */
730 template<int N>
731 typename tvresult<N>::Type fastRead_tv(diffType i) const {
732 BZPRECHECK(0, "Can't vectorize stencils");
733 return iter_.template fastRead_tv<N>(i); }
734
735 T_numtype shift(int offset, int dim) const
736 {
737 iter_._bz_offsetData(offset, dim);
738 T_numtype r = #name#_stencilop (iter_);
739 iter_._bz_offsetData(-offset, dim);
740 return r;
741 }
742
743 T_numtype shift(int offset1, int dim1, int offset2, int dim2) const
744 {
745 iter_._bz_offsetData(offset1, dim1, offset2, dim2);
746 T_numtype r = #name#_stencilop (iter_);
747 iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);
748 return r;
749 }
750
751 void prettyPrint(std::string &str,
752 prettyPrintFormat& format) const
753 {
754 str += "name (stencil)";
755 str += "(";
756 iter_.prettyPrint(str, format);
757 str += ")";
758 }
759
760 template<typename T1, typename T2 = nilArraySection,
761 class T3 = nilArraySection, typename T4 = nilArraySection,
762 class T5 = nilArraySection, typename T6 = nilArraySection,
763 class T7 = nilArraySection, typename T8 = nilArraySection,
764 class T9 = nilArraySection, typename T10 = nilArraySection,
765 class T11 = nilArraySection>
766 class SliceInfo {
767 public:
768 typedef #name#_et<T_expr> T_slice;
769 };
770
771 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
772 typename T7, typename T8, typename T9, typename T10, typename T11>
773 #name#_et
774 operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const
775 {
776 return #name#_et
777 (iter_(_bz_makeRange(r1),
778 _bz_makeRange(r2),
779 _bz_makeRange(r3),
780 _bz_makeRange(r4),
781 _bz_makeRange(r5),
782 _bz_makeRange(r6),
783 _bz_makeRange(r7),
784 _bz_makeRange(r8),
785 _bz_makeRange(r9),
786 _bz_makeRange(r10),
787 _bz_makeRange(r11)));
788 }
789 };
790 /* create ET from application to expression */
791 template<typename T1>
792 inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<T1>::T_expr::T_range_result> >
793 #name#(const blitz::ETBase<T1>& d1)
794 {
795 return _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<T1>::T_expr::T_range_result> >
796 (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),MINB, MAXB)));
797 }
798 """
799 os.write(stub.replace("#name#",name).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))
800 BZ_ET_STENCIL_REDIRECT(name)
801
802
803 def BZ_ET_STENCIL_DIFF(name, MINB, MAXB):
804 stub="""
805
806
807 /* Defines a stencil ET difference operator "#name#" that operates on
808 an array<P_numtype, N_rank> and returns an array of identical
809 type. (The only significance of the "difference" aspect is that the
810 operator is assumed to take a second argument which is the
811 dimension to do the difference in). MINB and MAXB are integer
812 expressions describing the extent of the operator in the operating
813 dimension. */
814
815 template<typename P_expr>
816 class #name#_et : public _bz_StencilExpr<P_expr, _bz_typename P_expr::T_numtype>
817 {
818 public:
819 typedef _bz_StencilExpr<P_expr, _bz_typename P_expr::T_numtype> T_base;
820 typedef _bz_typename T_base::T_numtype T_numtype;
821 typedef _bz_typename T_base::T_expr T_expr;
822
823 // select return type
824 typedef typename unwrapET<typename T_expr::T_result>::T_unwrapped test;
825 typedef typename selectET<typename T_expr::T_typeprop,
826 T_numtype,
827 #name#_et<test> >::T_selected T_typeprop;
828 typedef typename unwrapET<T_typeprop>::T_unwrapped T_result;
829 typedef T_numtype T_optype;
830
831 template<int N> struct tvresult {
832 typedef #name#_et<
833 typename T_expr::template tvresult<N>::Type> Type;
834 };
835
836 typedef #name#_et<_bz_typename P_expr::T_range_result> T_range_result;
837
838 using T_base::iter_;
839 using T_base::rank_;
840 public:
841 #name#_et(const #name#_et& a) :
842 _bz_StencilExpr<P_expr, T_numtype>(a), dim_(a.dim_)
843 { }
844
845 #name#_et(BZ_ETPARM(T_expr) a, int dim) :
846 _bz_StencilExpr<P_expr, T_numtype>(a), dim_(dim)
847 { }
848
849 #name#_et(_bz_typename T_expr::T_ctorArg1 a, int dim) :
850 _bz_StencilExpr<P_expr, T_numtype>(a), dim_(dim)
851 { }
852
853 T_result operator*() const
854 { return #name#_stencilop(iter_, dim_); }
855 T_result operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const
856 { iter_.moveTo(i); return #name#_stencilop(iter_, dim_); }
857
858 T_range_result operator()(const RectDomain<rank_>& d) const
859 { return T_range_result(iter_(d), dim_); }
860
861 T_result operator[](int i) const
862 { return #name#_stencilop(iter_[i], dim_); }
863
864 T_result fastRead(diffType i) const
865 {/* this probably isn't very fast... */
866 iter_._bz_offsetData(i);
867 T_result r = #name#_stencilop (iter_, dim_);
868 iter_._bz_offsetData(-i);
869 return r;
870 }
871
872 /** This way of vectorizing won't work on stencils. */
873 template<int N>
874 typename tvresult<N>::Type fastRead_tv(diffType i) const {
875 BZPRECHECK(0, "Can't vectorize stencils");
876 return typename tvresult<N>::Type(iter_.template fastRead_tv<N>(i),dim_); }
877
878 T_result shift(int offset, int dim) const
879 {
880 iter_._bz_offsetData(offset, dim);
881 T_result r = #name#_stencilop (iter_);
882 iter_._bz_offsetData(-offset, dim);
883 return r;
884 }
885
886 T_result shift(int offset1, int dim1, int offset2, int dim2) const
887 {
888 iter_._bz_offsetData(offset1, dim1, offset2, dim2);
889 T_result r = #name#_stencilop (iter_);
890 iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);
891 return r;
892 }
893
894 void prettyPrint(std::string &str,
895 prettyPrintFormat& format) const
896 {
897 str += "name (stencil)";
898 str += "(";
899 iter_.prettyPrint(str, format);
900 str += ")";
901 }
902
903 template<typename T1, typename T2 = nilArraySection,
904 class T3 = nilArraySection, typename T4 = nilArraySection,
905 class T5 = nilArraySection, typename T6 = nilArraySection,
906 class T7 = nilArraySection, typename T8 = nilArraySection,
907 class T9 = nilArraySection, typename T10 = nilArraySection,
908 class T11 = nilArraySection>
909 class SliceInfo {
910 public:
911 typedef #name#_et<T_expr> T_slice;
912 };
913
914 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
915 typename T7, typename T8, typename T9, typename T10, typename T11>
916 #name#_et
917 operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const
918 {
919 return #name#_et
920 (iter_(_bz_makeRange(r1),
921 _bz_makeRange(r2),
922 _bz_makeRange(r3),
923 _bz_makeRange(r4),
924 _bz_makeRange(r5),
925 _bz_makeRange(r6),
926 _bz_makeRange(r7),
927 _bz_makeRange(r8),
928 _bz_makeRange(r9),
929 _bz_makeRange(r10),
930 _bz_makeRange(r11)),dim_);
931 }
932
933 private:
934 int dim_;
935 };
936 /* create ET from application to expression */
937 template<typename T1>
938 inline _bz_ArrayExpr<#name#_et<typename blitz::asExpr<T1>::T_expr::T_range_result> >
939 #name#(const blitz::ETBase<T1>& d1, int dim)
940 {
941 TinyVector<int, blitz::asExpr<T1>::T_expr::rank_> minb(0), maxb(0);
942 minb[dim]=MINB; maxb[dim]=MAXB;
943 return _bz_ArrayExpr<#name#_et<typename blitz::asExpr<T1>::T_expr::T_range_result> >
944 (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),minb, maxb)), dim);
945 }
946 /* forward operations on arrays to main function */
947 template<typename T, int N>
948 inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> >
949 #name#(const Array<T,N>& d1, int dim)
950 { return #name#(d1.wrap(), dim); }
951 template<typename T, int N>
952 inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> >
953 #name#(Array<T,N>& d1, int dim)
954 { return #name#(d1.wrap(), dim); }
955 """
956 os.write(stub.replace("#name#",name).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))
957
958
959 def BZ_ET_STENCIL_MULTIDIFF(name, MINB, MAXB):
960 stub="""
961
962
963 /** Defines a stencil ET difference operator "#name#" that operates on a
964 multicomponent array<P_numtype, N_rank> and returns an
965 array<P_numtype::T_element, N_rank>. */
966 template<typename P_expr>
967 class #name#_et_multi : public _bz_StencilExpr<P_expr, _bz_typename multicomponent_traits<typename P_expr::T_numtype>::T_element>
968 {
969 public:
970 typedef _bz_typename multicomponent_traits<typename P_expr::T_numtype>::T_element T_result;
971 typedef _bz_StencilExpr<P_expr, T_result> T_base;
972 typedef _bz_typename T_base::T_numtype T_numtype;
973 typedef _bz_typename T_base::T_expr T_expr;
974
975 // there is no selecting return type here. because we *know* it is
976 // T_result, there's no question of whether we could be doing
977 // multicomponent evaluations.
978 typedef T_result T_typeprop;
979 typedef T_numtype T_optype;
980
981 template<int N> struct tvresult {
982 typedef #name#_et_multi<
983 typename T_expr::template tvresult<N>::Type> Type;
984 };
985
986 typedef #name#_et_multi<_bz_typename P_expr::T_range_result> T_range_result;
987
988 using T_base::iter_;
989 using T_base::rank_;
990 public:
991 #name#_et_multi(const #name#_et_multi& a) :
992 _bz_StencilExpr<P_expr, T_numtype>(a), comp_(a.comp_), dim_(a.dim_)
993 { }
994
995 #name#_et_multi(BZ_ETPARM(T_expr) a, int comp, int dim) :
996 _bz_StencilExpr<P_expr, T_numtype>(a),
997 comp_(comp), dim_(dim)
998 { }
999
1000 #name#_et_multi(_bz_typename T_expr::T_ctorArg1 a, int comp, int dim) :
1001 _bz_StencilExpr<P_expr, T_numtype>(a),
1002 comp_(comp), dim_(dim)
1003 { }
1004
1005 T_numtype operator*() const
1006 { return #name#_stencilop(iter_, comp_, dim_); }
1007 T_numtype operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const
1008 { iter_.moveTo(i); return #name#_stencilop(iter_, comp_, dim_); }
1009
1010 T_range_result operator()(const RectDomain<rank_>& d) const
1011 { return T_range_result(iter_(d), comp_, dim_); }
1012
1013 T_numtype operator[](int i) const
1014 { return #name#_stencilop(iter_[i], comp_, dim_); }
1015
1016 T_numtype fastRead(diffType i) const
1017 {/* this probably isn't very fast... */
1018 iter_._bz_offsetData(i);
1019 T_numtype r = #name#_stencilop (iter_, comp_, dim_);
1020 iter_._bz_offsetData(-i);
1021 return r;
1022 }
1023
1024 /** This way of vectorizing won't work on stencils. */
1025 template<int N>
1026 typename tvresult<N>::Type fastRead_tv(diffType i) const {
1027 BZPRECHECK(0, "Can't vectorize stencils");
1028 return typename tvresult<N>::Type(iter_.template fastRead_tv<N>(i),comp_,dim_); }
1029
1030 T_numtype shift(int offset, int dim) const
1031 {
1032 iter_._bz_offsetData(offset, dim);
1033 T_numtype r = #name#_stencilop (iter_);
1034 iter_._bz_offsetData(-offset, dim);
1035 return r;
1036 }
1037
1038 T_numtype shift(int offset1, int dim1, int offset2, int dim2) const
1039 {
1040 iter_._bz_offsetData(offset1, dim1, offset2, dim2);
1041 T_numtype r = #name#_stencilop (iter_);
1042 iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);
1043 return r;
1044 }
1045
1046 void prettyPrint(std::string &str,
1047 prettyPrintFormat& format) const
1048 {
1049 str += "name (stencil)";
1050 str += "(";
1051 iter_.prettyPrint(str, format);
1052 str += ")";
1053 }
1054
1055 template<typename T1, typename T2 = nilArraySection,
1056 class T3 = nilArraySection, typename T4 = nilArraySection,
1057 class T5 = nilArraySection, typename T6 = nilArraySection,
1058 class T7 = nilArraySection, typename T8 = nilArraySection,
1059 class T9 = nilArraySection, typename T10 = nilArraySection,
1060 class T11 = nilArraySection>
1061 class SliceInfo {
1062 public:
1063 typedef #name#_et_multi<T_expr> T_slice;
1064 };
1065
1066 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
1067 typename T7, typename T8, typename T9, typename T10, typename T11>
1068 #name#_et_multi
1069 operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const
1070 {
1071 return #name#_et_multi
1072 (iter_(_bz_makeRange(r1),
1073 _bz_makeRange(r2),
1074 _bz_makeRange(r3),
1075 _bz_makeRange(r4),
1076 _bz_makeRange(r5),
1077 _bz_makeRange(r6),
1078 _bz_makeRange(r7),
1079 _bz_makeRange(r8),
1080 _bz_makeRange(r9),
1081 _bz_makeRange(r10),
1082 _bz_makeRange(r11)),comp_, dim_);
1083 }
1084
1085 private:
1086 int comp_;
1087 int dim_;
1088 };
1089 /* create ET from application to expression */
1090 template<typename T1>
1091 inline _bz_ArrayExpr<#name#_et_multi<typename blitz::asExpr<T1>::T_expr::T_range_result> >
1092 #name#(const blitz::ETBase<T1>& d1, int comp, int dim)
1093 {
1094 TinyVector<int, blitz::asExpr<T1>::T_expr::rank_> minb(0), maxb(0);
1095 minb[dim]=MINB; maxb[dim]=MAXB;
1096 return _bz_ArrayExpr<#name#_et_multi<typename blitz::asExpr<T1>::T_expr::T_range_result> >
1097 (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),minb, maxb)), comp, dim);
1098 }
1099 /* forward operations on arrays to main function */
1100 template<typename T, int N>
1101 inline _bz_ArrayExpr<#name#_et_multi<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> >
1102 #name#(const Array<T,N>& d1, int comp, int dim)
1103 { return #name#(d1.wrap(), comp, dim); }
1104
1105 template<typename T, int N>
1106 inline _bz_ArrayExpr<#name#_et_multi<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> >
1107 #name#(Array<T,N>& d1, int comp, int dim)
1108 { return #name#(d1.wrap(), comp, dim); }
1109 """
1110 os.write(stub.replace("#name#",name).replace("MINB",str(MINB)).replace("MAXB",str(MAXB)))
1111 BZ_ET_STENCIL_REDIRECT(name)
1112
1113
1114 def BZ_ET_STENCIL_DIFF2(name, MINB1, MAXB1, MINB2, MAXB2):
1115 stub="""
1116
1117
1118 /** Defines a stencil ET double-difference operator "#name#" that
1119 operates on an array<P_numtype, N_rank> and returns an array of
1120 identical type. (The only significance of the "double-difference" aspect
1121 is that the operator is assumed to take two extra arguments which
1122 are the dimensions to do the differences in). */
1123
1124 template<typename P_expr>
1125 class #name#_et : public _bz_StencilExpr<P_expr, _bz_typename P_expr::T_numtype>
1126 {
1127 public:
1128 typedef _bz_StencilExpr<P_expr, _bz_typename P_expr::T_numtype> T_base;
1129 typedef _bz_typename T_base::T_numtype T_numtype;
1130 typedef _bz_typename T_base::T_expr T_expr;
1131
1132 // select return type
1133 typedef typename unwrapET<typename T_expr::T_result>::T_unwrapped test;
1134 typedef typename selectET<typename T_expr::T_typeprop,
1135 T_numtype,
1136 #name#_et<test> >::T_selected T_typeprop;
1137 typedef typename unwrapET<T_typeprop>::T_unwrapped T_result;
1138 typedef T_numtype T_optype;
1139
1140 template<int N> struct tvresult {
1141 typedef #name#_et<
1142 typename T_expr::template tvresult<N>::Type> Type;
1143 };
1144
1145 typedef #name#_et<_bz_typename P_expr::T_range_result> T_range_result;
1146
1147 using T_base::iter_;
1148 using T_base::rank_;
1149 public:
1150 #name#_et(const #name#_et& a) :
1151 _bz_StencilExpr<P_expr, T_numtype>(a),
1152 dim1_(a.dim1_), dim2_(a.dim2_)
1153 { }
1154
1155 #name#_et(BZ_ETPARM(T_expr) a, int dim1, int dim2) :
1156 _bz_StencilExpr<P_expr, T_numtype>(a),
1157 dim1_(dim1), dim2_(dim2)
1158 { }
1159
1160 #name#_et(_bz_typename T_expr::T_ctorArg1 a,
1161 int dim1, int dim2) :
1162 _bz_StencilExpr<P_expr, T_numtype>(a),
1163 dim1_(dim1), dim2_(dim2)
1164 { }
1165
1166 T_numtype operator*() const
1167 { return #name#_stencilop(iter_, dim1_, dim2_); }
1168 T_numtype operator()(_bz_typename _bz_IndexParameter<TinyVector<int, rank_> >::type i) const
1169 { iter_.moveTo(i); return #name#_stencilop(iter_, dim1_, dim2_); }
1170
1171 T_range_result operator()(const RectDomain<rank_>& d) const
1172 { return T_range_result(iter_(d), dim1_, dim2_); }
1173
1174 T_numtype operator[](int i) const
1175 { return #name#_stencilop(iter_[i], dim1_, dim2_); }
1176
1177 T_numtype fastRead(diffType i) const
1178 {/* this probably isn't very fast... */
1179 iter_._bz_offsetData(i);
1180 T_numtype r = #name#_stencilop (iter_, dim1_, dim2_);
1181 iter_._bz_offsetData(-i);
1182 return r;
1183 }
1184
1185 /** This way of vectorizing won't work on stencils. */
1186 template<int N>
1187 typename tvresult<N>::Type fastRead_tv(diffType i) const {
1188 BZPRECHECK(0, "Can't vectorize stencils");
1189 return typename tvresult<N>::Type(iter_.template fastRead_tv<N>(i),dim1_,dim2_); }
1190
1191 T_numtype shift(int offset, int dim) const
1192 {
1193 iter_._bz_offsetData(offset, dim);
1194 T_numtype r = #name#_stencilop (iter_);
1195 iter_._bz_offsetData(-offset, dim);
1196 return r;
1197 }
1198
1199 T_numtype shift(int offset1, int dim1, int offset2, int dim2) const
1200 {
1201 iter_._bz_offsetData(offset1, dim1, offset2, dim2);
1202 T_numtype r = #name#_stencilop (iter_);
1203 iter_._bz_offsetData(-offset1, dim1, -offset2, dim2);
1204 return r;
1205 }
1206
1207 void prettyPrint(std::string &str,
1208 prettyPrintFormat& format) const
1209 {
1210 str += "name (stencil)";
1211 str += "(";
1212 iter_.prettyPrint(str, format);
1213 str += ")";
1214 }
1215
1216 template<typename T1, typename T2 = nilArraySection,
1217 class T3 = nilArraySection, typename T4 = nilArraySection,
1218 class T5 = nilArraySection, typename T6 = nilArraySection,
1219 class T7 = nilArraySection, typename T8 = nilArraySection,
1220 class T9 = nilArraySection, typename T10 = nilArraySection,
1221 class T11 = nilArraySection>
1222 class SliceInfo {
1223 public:
1224 typedef #name#_et<T_expr> T_slice;
1225 };
1226
1227 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
1228 typename T7, typename T8, typename T9, typename T10, typename T11>
1229 #name#_et
1230 operator()(T1 r1, T2 r2, T3 r3, T4 r4, T5 r5, T6 r6, T7 r7, T8 r8, T9 r9, T10 r10, T11 r11) const
1231 {
1232 return #name#_et
1233 (iter_(_bz_makeRange(r1),
1234 _bz_makeRange(r2),
1235 _bz_makeRange(r3),
1236 _bz_makeRange(r4),
1237 _bz_makeRange(r5),
1238 _bz_makeRange(r6),
1239 _bz_makeRange(r7),
1240 _bz_makeRange(r8),
1241 _bz_makeRange(r9),
1242 _bz_makeRange(r10),
1243 _bz_makeRange(r11)), dim1_, dim2_);
1244 }
1245
1246 private:
1247 int dim1_, dim2_;
1248 };
1249
1250 /* create ET from application to expression */
1251 template<typename T1>
1252 inline _bz_ArrayExpr<#name#_et<typename blitz::asExpr<T1>::T_expr::T_range_result> >
1253 #name#(const blitz::ETBase<T1>& d1, int dim1, int dim2)
1254 {
1255 TinyVector<int, blitz::asExpr<T1>::T_expr::rank_> minb(0), maxb(0);
1256 minb[dim1]=MINB1; maxb[dim1]=MAXB1;
1257 minb[dim2]=MINB2; maxb[dim2]=MAXB2;
1258 return _bz_ArrayExpr<#name#_et<typename blitz::asExpr<T1>::T_expr::T_range_result> >
1259 (blitz::asExpr<T1>::getExpr(d1.unwrap())(_bz_shrinkDomain(d1.unwrap().domain(),minb, maxb)), dim1, dim2);
1260 }
1261 /* forward operations on arrays to main function */
1262 template<typename T, int N>
1263 inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> >
1264 #name#(const Array<T,N>& d1, int dim1, int dim2)
1265 { return #name#(d1.wrap(), dim1, dim2); }
1266
1267 template<typename T, int N>
1268 inline _bz_ArrayExpr<#name#_et<_bz_typename blitz::asExpr<Array<T,N> >::T_expr::T_range_result> >
1269 #name#(Array<T,N>& d1, int dim1, int dim2)
1270 { return #name#(d1.wrap(), dim1, dim2); }
1271 """
1272 os.write(stub.replace("#name#",name).replace("MINB1",str(MINB1)).replace("MAXB1",str(MAXB1)).replace("MINB2",str(MINB2)).replace("MAXB2",str(MAXB2)))
1273
1274
1275 # now generate the file
1276
1277 os.write("""
1278 /* Definitions of ET stencil operators. This file was generated by
1279 generate/genstencils.py. Do not edit.
1280
1281 Note: You can't pass templates with >1 parameter as macro
1282 parameters because cpp doesn't recognize that the comma is balanced
1283 between the angle brackets and interprets them as multiple
1284 arguments, i.e., the following alternative declaration of grad2D
1285 will not work:
1286
1287 BZ_ET_STENCIL(grad2D, TinyVector<P_numtype, 2>,
1288 TinyVector<typename T1::T_numtype, 2>, shape(-1,1), shape(1,1))
1289
1290 instead, you have to use the above bzCC ("ConCatenate") macro to
1291 protect the things containing commas. The following would work:
1292
1293 BZ_ET_STENCIL(grad2D, bzCC(TinyVector<P_numtype, 2>),
1294 bzCC(TinyVector<typename T1::T_numtype, 2>), shape(-1,-1), shape(1,1))
1295
1296 */
1297
1298 namespace blitz {
1299
1300 """)
1301
1302 BZ_ET_STENCIL_DIFF("central12", -1, 1)
1303 BZ_ET_STENCIL_DIFF("central22", -1, 1)
1304 BZ_ET_STENCIL_DIFF("central32", -2, 2)
1305 BZ_ET_STENCIL_DIFF("central42", -2, 2)
1306 BZ_ET_STENCIL_DIFF("central14", -2, 2)
1307 BZ_ET_STENCIL_DIFF("central24", -2, 2)
1308 BZ_ET_STENCIL_DIFF("central34", -2, 2)
1309 BZ_ET_STENCIL_DIFF("central44", -2, 2)
1310 BZ_ET_STENCIL_DIFF("central12n", -1, 1)
1311 BZ_ET_STENCIL_DIFF("central22n", -1, 1)
1312 BZ_ET_STENCIL_DIFF("central32n", -2, 2)
1313 BZ_ET_STENCIL_DIFF("central42n", -2, 2)
1314 BZ_ET_STENCIL_DIFF("central14n", -2, 2)
1315 BZ_ET_STENCIL_DIFF("central24n", -2, 2)
1316 BZ_ET_STENCIL_DIFF("central34n", -2, 2)
1317 BZ_ET_STENCIL_DIFF("central44n", -2, 2)
1318
1319 BZ_ET_STENCIL_DIFF("backward11", -1, 0)
1320 BZ_ET_STENCIL_DIFF("backward21", -2, 0)
1321 BZ_ET_STENCIL_DIFF("backward31", -3, 0)
1322 BZ_ET_STENCIL_DIFF("backward41", -4, 0)
1323 BZ_ET_STENCIL_DIFF("backward12", -2, 0)
1324 BZ_ET_STENCIL_DIFF("backward22", -3, 0)
1325 BZ_ET_STENCIL_DIFF("backward32", -4, 0)
1326 BZ_ET_STENCIL_DIFF("backward42", -5, 0)
1327 BZ_ET_STENCIL_DIFF("backward11n", -1, 0)
1328 BZ_ET_STENCIL_DIFF("backward21n", -2, 0)
1329 BZ_ET_STENCIL_DIFF("backward31n", -3, 0)
1330 BZ_ET_STENCIL_DIFF("backward41n", -4, 0)
1331 BZ_ET_STENCIL_DIFF("backward12n", -2, 0)
1332 BZ_ET_STENCIL_DIFF("backward22n", -3, 0)
1333 BZ_ET_STENCIL_DIFF("backward32n", -4, 0)
1334 BZ_ET_STENCIL_DIFF("backward42n", -5, 0)
1335
1336 BZ_ET_STENCIL_DIFF("forward11", 0, 1)
1337 BZ_ET_STENCIL_DIFF("forward21", 0, 2)
1338 BZ_ET_STENCIL_DIFF("forward31", 0, 3)
1339 BZ_ET_STENCIL_DIFF("forward41", 0, 4)
1340 BZ_ET_STENCIL_DIFF("forward12", 0, 2)
1341 BZ_ET_STENCIL_DIFF("forward22", 0, 3)
1342 BZ_ET_STENCIL_DIFF("forward32", 0, 4)
1343 BZ_ET_STENCIL_DIFF("forward42", 0, 5)
1344 BZ_ET_STENCIL_DIFF("forward11n", 0, 1)
1345 BZ_ET_STENCIL_DIFF("forward21n", 0, 2)
1346 BZ_ET_STENCIL_DIFF("forward31n", 0, 3)
1347 BZ_ET_STENCIL_DIFF("forward41n", 0, 4)
1348 BZ_ET_STENCIL_DIFF("forward12n", 0, 2)
1349 BZ_ET_STENCIL_DIFF("forward22n", 0, 3)
1350 BZ_ET_STENCIL_DIFF("forward32n", 0, 4)
1351 BZ_ET_STENCIL_DIFF("forward42n", 0, 5)
1352
1353 BZ_ET_STENCIL_MULTIDIFF("central12", -1, 1)
1354 BZ_ET_STENCIL_MULTIDIFF("central22", -1, 1)
1355 BZ_ET_STENCIL_MULTIDIFF("central32", -2, 2)
1356 BZ_ET_STENCIL_MULTIDIFF("central42", -2, 2)
1357 BZ_ET_STENCIL_MULTIDIFF("central14", -2, 2)
1358 BZ_ET_STENCIL_MULTIDIFF("central24", -2, 2)
1359 BZ_ET_STENCIL_MULTIDIFF("central34", -2, 2)
1360 BZ_ET_STENCIL_MULTIDIFF("central44", -2, 2)
1361 BZ_ET_STENCIL_MULTIDIFF("central12n", -1, 1)
1362 BZ_ET_STENCIL_MULTIDIFF("central22n", -1, 1)
1363 BZ_ET_STENCIL_MULTIDIFF("central32n", -2, 2)
1364 BZ_ET_STENCIL_MULTIDIFF("central42n", -2, 2)
1365 BZ_ET_STENCIL_MULTIDIFF("central14n", -2, 2)
1366 BZ_ET_STENCIL_MULTIDIFF("central24n", -2, 2)
1367 BZ_ET_STENCIL_MULTIDIFF("central34n", -2, 2)
1368 BZ_ET_STENCIL_MULTIDIFF("central44n", -2, 2)
1369
1370 BZ_ET_STENCIL_MULTIDIFF("backward11", -1, 0)
1371 BZ_ET_STENCIL_MULTIDIFF("backward21", -2, 0)
1372 BZ_ET_STENCIL_MULTIDIFF("backward31", -3, 0)
1373 BZ_ET_STENCIL_MULTIDIFF("backward41", -4, 0)
1374 BZ_ET_STENCIL_MULTIDIFF("backward12", -2, 0)
1375 BZ_ET_STENCIL_MULTIDIFF("backward22", -3, 0)
1376 BZ_ET_STENCIL_MULTIDIFF("backward32", -4, 0)
1377 BZ_ET_STENCIL_MULTIDIFF("backward42", -5, 0)
1378 BZ_ET_STENCIL_MULTIDIFF("backward11n", -1, 0)
1379 BZ_ET_STENCIL_MULTIDIFF("backward21n", -2, 0)
1380 BZ_ET_STENCIL_MULTIDIFF("backward31n", -3, 0)
1381 BZ_ET_STENCIL_MULTIDIFF("backward41n", -4, 0)
1382 BZ_ET_STENCIL_MULTIDIFF("backward12n", -2, 0)
1383 BZ_ET_STENCIL_MULTIDIFF("backward22n", -3, 0)
1384 BZ_ET_STENCIL_MULTIDIFF("backward32n", -4, 0)
1385 BZ_ET_STENCIL_MULTIDIFF("backward42n", -5, 0)
1386
1387 BZ_ET_STENCIL_MULTIDIFF("forward11", 0, 1)
1388 BZ_ET_STENCIL_MULTIDIFF("forward21", 0, 2)
1389 BZ_ET_STENCIL_MULTIDIFF("forward31", 0, 3)
1390 BZ_ET_STENCIL_MULTIDIFF("forward41", 0, 4)
1391 BZ_ET_STENCIL_MULTIDIFF("forward12", 0, 2)
1392 BZ_ET_STENCIL_MULTIDIFF("forward22", 0, 3)
1393 BZ_ET_STENCIL_MULTIDIFF("forward32", 0, 4)
1394 BZ_ET_STENCIL_MULTIDIFF("forward42", 0, 5)
1395 BZ_ET_STENCIL_MULTIDIFF("forward11n", 0, 1)
1396 BZ_ET_STENCIL_MULTIDIFF("forward21n", 0, 2)
1397 BZ_ET_STENCIL_MULTIDIFF("forward31n", 0, 3)
1398 BZ_ET_STENCIL_MULTIDIFF("forward41n", 0, 4)
1399 BZ_ET_STENCIL_MULTIDIFF("forward12n", 0, 2)
1400 BZ_ET_STENCIL_MULTIDIFF("forward22n", 0, 3)
1401 BZ_ET_STENCIL_MULTIDIFF("forward32n", 0, 4)
1402 BZ_ET_STENCIL_MULTIDIFF("forward42n", 0, 5)
1403
1404 BZ_ET_STENCIL("Laplacian2D", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-1,-1)", "shape(1,1)")
1405 BZ_ET_STENCIL("Laplacian3D", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-1,-1,-1)", "shape(1,1,1)")
1406 BZ_ET_STENCIL("Laplacian2D4", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2)", "shape(2,2)")
1407 BZ_ET_STENCIL("Laplacian2D4n", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2)", "shape(2,2)")
1408 BZ_ET_STENCIL("Laplacian3D4", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2,-2)", "shape(2,2,2)")
1409 BZ_ET_STENCIL("Laplacian3D4n", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2,-2)", "shape(2,2,2)")
1410
1411 BZ_ET_STENCILV("grad2D", 2, "shape(-1,-1)", "shape(1,1)")
1412 BZ_ET_STENCILV("grad2D4", 2, "shape(-2,-2)", "shape(2,2)")
1413 BZ_ET_STENCILV("grad3D", 3, "shape(-1,-1,-1)", "shape(1,1,1)")
1414 BZ_ET_STENCILV("grad3D4", 3, "shape(-2,-2,-2)", "shape(2,2,2)")
1415 BZ_ET_STENCILV("grad2Dn", 2, "shape(-1,-1)", "shape(1,1)")
1416 BZ_ET_STENCILV("grad2D4n", 2, "shape(-2,-2)", "shape(2,2)")
1417 BZ_ET_STENCILV("grad3Dn", 3, "shape(-1,-1,-1)", "shape(1,1,1)")
1418 BZ_ET_STENCILV("grad3D4n", 3, "shape(-2,-2,-2)", "shape(2,2,2)")
1419 BZ_ET_STENCILV("gradSqr2D", 2, "shape(-1,-1)", "shape(1,1)")
1420 BZ_ET_STENCILV("gradSqr2D4", 2, "shape(-2,-2)", "shape(2,2)")
1421 BZ_ET_STENCILV("gradSqr3D", 3, "shape(-1,-1,-1)", "shape(1,1,1)")
1422 BZ_ET_STENCILV("gradSqr3D4", 3, "shape(-2,-2,-2)", "shape(2,2,2)")
1423 BZ_ET_STENCILV("gradSqr2Dn", 2, "shape(-1,-1)", "shape(1,1)")
1424 BZ_ET_STENCILV("gradSqr2D4n", 2, "shape(-2,-2)", "shape(2,2)")
1425 BZ_ET_STENCILV("gradSqr3Dn", 3, "shape(-1,-1,-1)", "shape(1,1,1)")
1426 BZ_ET_STENCILV("gradSqr3D4n", 3, "shape(-2,-2,-2)", "shape(2,2,2)")
1427
1428 BZ_ET_STENCILM("Jacobian3D", 3, "shape(-1,-1,-1)", "shape(1,1,1)")
1429 BZ_ET_STENCILM("Jacobian3Dn", 3, "shape(-1,-1,-1)", "shape(1,1,1)")
1430 BZ_ET_STENCILM("Jacobian3D4", 3, "shape(-2,-2,-2)", "shape(2,2,2)")
1431 BZ_ET_STENCILM("Jacobian3D4n", 3, "shape(-2,-2,-2)", "shape(2,2,2)")
1432
1433 BZ_ET_STENCIL("curl3D", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-1,-1,-1)", "shape(1,1,1)")
1434 BZ_ET_STENCIL("curl3Dn", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-1,-1,-1)", "shape(1,1,1)")
1435 BZ_ET_STENCIL("curl3D4", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2,-2)", "shape(2,2,2)")
1436 BZ_ET_STENCIL("curl3D4n", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2,-2)", "shape(2,2,2)")
1437 BZ_ET_STENCIL("curl2D", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-1,-1)", "shape(1,1)")
1438 BZ_ET_STENCIL("curl2Dn", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-1,-1)", "shape(1,1)")
1439 BZ_ET_STENCIL("curl2D4", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2)", "shape(2,2)")
1440 BZ_ET_STENCIL("curl2D4n", "T", "typename blitz::asExpr<T1>::T_expr::T_numtype", "shape(-2,-2)", "shape(2,2)")
1441
1442 BZ_ET_STENCIL_SCA("div2D", "shape(-1,-1)", "shape(1,1)")
1443 BZ_ET_STENCIL_SCA("div2Dn", "shape(-1,-1)", "shape(1,1)")
1444 BZ_ET_STENCIL_SCA("div2D4", "shape(-2,-2)", "shape(2,2)")
1445 BZ_ET_STENCIL_SCA("div2D4n", "shape(-2,-2)", "shape(2,2)")
1446 BZ_ET_STENCIL_SCA("div3D", "shape(-1,-1,-1)", "shape(1,1,1)")
1447 BZ_ET_STENCIL_SCA("div3Dn", "shape(-1,-1,-1)", "shape(1,1,1)")
1448 BZ_ET_STENCIL_SCA("div3D4", "shape(-2,-2,-2)", "shape(2,2,2)")
1449
1450
1451 BZ_ET_STENCIL2("div", "double" , "BZ_PROMOTE(typename blitz::asExpr<T1>::T_expr::T_numtype, typename blitz::asExpr<T2>::T_expr::T_numtype)", "shape(-1,-1)", "shape(1,1)")
1452 BZ_ET_STENCIL2("divn", "double" , "BZ_PROMOTE(typename blitz::asExpr<T1>::T_expr::T_numtype, typename blitz::asExpr<T2>::T_expr::T_numtype)", "shape(-1,-1)", "shape(1,1)")
1453 BZ_ET_STENCIL2("div4", "double" , "BZ_PROMOTE(typename blitz::asExpr<T1>::T_expr::T_numtype, typename blitz::asExpr<T2>::T_expr::T_numtype)", "shape(-2,-2)", "shape(2,2)")
1454 BZ_ET_STENCIL2("div4n", "double" , "BZ_PROMOTE(typename blitz::asExpr<T1>::T_expr::T_numtype, typename blitz::asExpr<T2>::T_expr::T_numtype)", "shape(-2,-2)", "shape(2,2)")
1455
1456 BZ_ET_STENCIL_DIFF2("mixed22", -1, 1, -1, 1)
1457 BZ_ET_STENCIL_DIFF2("mixed22n", -1, 1, -1, 1)
1458 BZ_ET_STENCIL_DIFF2("mixed24", -2, 2, -2, 2)
1459 BZ_ET_STENCIL_DIFF2("mixed24n", -2, 2, -2, 2)
1460
1461 os.write("""
1462 }
1463 """)
1464 os.close()