Chombo + EB  3.2
Metaprograms.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 // These are about ten times faster than the STL analogues, since here the
12 // number of elements is known at compile time. These functions are as
13 // fast as hand-unrolled loops (but you need -O3 or better on gcc).
14 //
15 // For examples, see lib/src/BoxTools/BaseFabImplem.H in the "template" branch
16 // of the Chombo_prev module under ~tdsternberg/my-cvsroot.
17 //
18 // Author: Ted
19 //
20 
21 #ifndef _METAPROGRAMS_H_
22 #define _METAPROGRAMS_H_
23 
24 #include <functional>
25 
26 namespace Metaprograms
27 {
28 
29 template<typename T> struct Identity
30 {
31  T const& operator()( T const& t )
32  {
33  return t;
34  }
35 };
36 
37 //-------------------------------------------------------------------
38 template<int N, typename T, typename RT, typename PlusT, typename TimesT>
40 {
41  //
42  // Binary mode with null-constructed functors.
43  //
44  RT operator()( T const* v1, T const* v2 )
45  {
46  return PlusT()( InnerProduct<N-1,T,RT,PlusT,TimesT>()(v1,v2),
47  TimesT()(v1[N-1],v2[N-1]) );
48  }
49  RT operator()( T const* v, T const& x )
50  {
51  return PlusT()( InnerProduct<N-1,T,RT,PlusT,TimesT>()(v,x),
52  TimesT()(v[N-1],x) );
53 
54  }
55 };
56 
57 template<typename PlusT, typename T, typename RT, typename TimesT>
58 struct InnerProduct<1,T,RT,PlusT,TimesT>
59 {
60  RT operator()( T const* v1, T const* v2 )
61  {
62  return TimesT()( v1[0], v2[0] );
63  }
64  RT operator()( T const* v, T const& x )
65  {
66  return TimesT()( v[0], x );
67  }
68 };
69 //-------------------------------------------------------------------
70 
71 //-------------------------------------------------------------------
72 template<int N, typename T, typename CompareT>
73 inline bool pointwiseCompare( T const* v1, T const* v2 )
74 {
75  return InnerProduct< N,T,bool,
76  std::logical_and<bool>,
77  CompareT >()( v1, v2 );
78 }
79 
80 template<int N, typename T, typename CompareT>
81 inline bool pointwiseCompare( T const* v, T const& x )
82 {
83  return InnerProduct< N,T,bool,
84  std::logical_and<bool>,
85  CompareT >()( v, x );
86 }
87 //-------------------------------------------------------------------
88 
89 //-------------------------------------------------------------------
90 template<int N, typename T, typename ReduceT, typename TransformT=Identity<T> >
91 struct Accum
92 {
93  T operator()( T const * v ) const
94  {
95  return ReduceT()( TransformT()(v[N-1]), Accum<N-1,T,ReduceT,TransformT>()(v) );
96  }
97 
98  // Stateful TransformT
99  T operator()( T const * v, TransformT const& xform ) const
100  {
101  return ReduceT()( xform(v,N-1), Accum<N-1,T,ReduceT,TransformT>()(v,xform) );
102  }
103 };
104 
105 template<typename T, typename ReduceT, typename TransformT >
106 struct Accum<1,T,ReduceT,TransformT>
107 {
108  T operator()( T const * v ) const
109  {
110  return TransformT()(v[0]);
111  }
112 
113  // Stateful TransformT
114  T operator()( T const * v, TransformT const& xform ) const
115  {
116  return xform(v,0);
117  }
118 };
119 //-------------------------------------------------------------------
120 
121 //-------------------------------------------------------------------
122 template<int N, typename T> struct LexLT
123 {
124  bool operator()( const T* v1, const T* v2 )
125  {
126  return doit( v1+N, v2+N );
127  }
128 
129  static bool doit( const T* v1, const T* v2 )
130  {
131  if ( v1[-N] < v2[-N] ) return true;
132  else if ( v1[-N] > v2[-N] ) return false;
133  else return LexLT<N-1,T>::doit(v1,v2);
134  }
135 };
136 
137 template<typename T> struct LexLT<1,T>
138 {
139  bool operator()( const T* v1, const T* v2 )
140  {
141  return doit( v1+1, v2+1 );
142  }
143  static bool doit( const T* v1, const T* v2 )
144  {
145  if ( v1[-1] < v2[-1] ) return true;
146  else return false;
147  }
148 };
149 //-------------------------------------------------------------------
150 
151 //-------------------------------------------------------------------
152 template<int N, typename T, typename FunctorT> struct Transform
153 {
154  void operator()( T* v )
155  {
156  v[N-1] = FunctorT()(v[N-1]);
157  Transform<N-1,T,FunctorT>()(v);
158  }
159  void operator()( T* v, const T& x )
160  {
161  v[N-1] = FunctorT()(v[N-1],x);
162  Transform<N-1,T,FunctorT>()(v,x);
163  }
164  void operator()( T* v1, const T* v2 )
165  {
166  v1[N-1] = FunctorT()(v1[N-1],v2[N-1]);
167  Transform<N-1,T,FunctorT>()(v1,v2);
168  }
169 };
170 
171 template<typename T, typename FunctorT> struct Transform<1,T,FunctorT>
172 {
173  void operator()( T* v )
174  {
175  v[0] = FunctorT()(v[0]);
176  }
177  void operator()( T* v, const T& x )
178  {
179  v[0] = FunctorT()(v[0],x);
180  }
181  void operator()( T* v1, const T* v2 )
182  {
183  v1[0] = FunctorT()(v1[0],v2[0]);
184  }
185 };
186 //-------------------------------------------------------------------
187 
188 //-------------------------------------------------------------------
189 template<int N, int P> struct Pow
190 {
191  static const int value = N * Pow<N,P-1>::value;
192 };
193 
194 template<int N> struct Pow<N,1>
195 {
196  static const int value = N;
197 };
198 //-------------------------------------------------------------------
199 
200 //-------------------------------------------------------------------
201 template<int N, class OP> struct NestedLoop
202 {
203  void operator()( int * index, int lo, int hi, OP& op ) const
204  {
205  for ( index[N-1]=lo; index[N-1]<hi; ++index[N-1] )
206  {
207  NestedLoop<N-1,OP>()( index, lo, hi, op );
208  }
209  }
210  void operator()( int * index, const int * lo, const int * hi, OP& op ) const
211  {
212  for ( index[N-1]=lo[N-1]; index[N-1]<hi[N-1]; ++index[N-1] )
213  {
214  NestedLoop<N-1,OP>()( index, lo, hi, op );
215  }
216  }
217 };
218 
219 template<class OP> struct NestedLoop<0,OP>
220 {
221  void operator()( int * index, int lo, int hi, OP& op ) const
222  {
223  op( index );
224  }
225  void operator()( int * index, const int * lo, const int * hi, OP& op ) const
226  {
227  op( index );
228  }
229 };
230 //-------------------------------------------------------------------
231 
232 //-------------------------------------------------------------------
233 template<int N, class OP> struct NestedPrestagedLoop
234 {
235  void operator()( int * index, int lo, int hi, OP& op ) const
236  {
237  for ( index[N-1]=lo; index[N-1]<hi; ++index[N-1] )
238  {
239  op.prestage(N-1);
240  NestedPrestagedLoop<N-1,OP>()( index, lo, hi, op );
241  }
242  }
243  void operator()( int * index, const int * lo, const int * hi, OP& op ) const
244  {
245  for ( index[N-1]=lo[N-1]; index[N-1]<hi[N-1]; ++index[N-1] )
246  {
247  op.prestage(N-1);
248  NestedPrestagedLoop<N-1,OP>()( index, lo, hi, op );
249  }
250  }
251 };
252 
253 template<class OP> struct NestedPrestagedLoop<0,OP>
254 {
255  void operator()( int * index, int lo, int hi, OP& op ) const
256  {
257  op( index );
258  }
259  void operator()( int * index, const int * lo, const int * hi, OP& op ) const
260  {
261  op( index );
262  }
263 };
264 //-------------------------------------------------------------------
265 
266 //-------------------------------------------------------------------
267 /** Named for defunct D_TERM macro.
268  * Applies an operation once for n = 0,...N-1.
269 */
270 template<int N, class OP> struct dterm
271 {
272  void operator()( OP& op ) const
273  {
274  dterm<N-1,OP>()( op );
275  op( N-1 );
276  }
277 };
278 
279 template<class OP> struct dterm<1,OP>
280 {
281  void operator()( OP& op ) const
282  {
283  op( 0 );
284  }
285 };
286 //-------------------------------------------------------------------
287 
288 } // namespace Metaprograms
289 
290 #endif // include guard
void operator()(int *index, const int *lo, const int *hi, OP &op) const
Definition: Metaprograms.H:259
T const & operator()(T const &t)
Definition: Metaprograms.H:31
T operator()(T const *v) const
Definition: Metaprograms.H:108
bool operator()(const T *v1, const T *v2)
Definition: Metaprograms.H:139
static bool doit(const T *v1, const T *v2)
Definition: Metaprograms.H:129
T operator()(T const *v) const
Definition: Metaprograms.H:93
Definition: Metaprograms.H:233
void operator()(int *index, int lo, int hi, OP &op) const
Definition: Metaprograms.H:255
Definition: Metaprograms.H:152
RT operator()(T const *v1, T const *v2)
Definition: Metaprograms.H:60
RT operator()(T const *v, T const &x)
Definition: Metaprograms.H:64
Definition: Metaprograms.H:270
T operator()(T const *v, TransformT const &xform) const
Definition: Metaprograms.H:114
void operator()(T *v1, const T *v2)
Definition: Metaprograms.H:164
Definition: Metaprograms.H:201
void operator()(int *index, int lo, int hi, OP &op) const
Definition: Metaprograms.H:235
void operator()(OP &op) const
Definition: Metaprograms.H:272
T operator()(T const *v, TransformT const &xform) const
Definition: Metaprograms.H:99
void operator()(T *v)
Definition: Metaprograms.H:154
void operator()(int *index, int lo, int hi, OP &op) const
Definition: Metaprograms.H:203
void operator()(T *v1, const T *v2)
Definition: Metaprograms.H:181
Definition: Metaprograms.H:29
void operator()(OP &op) const
Definition: Metaprograms.H:281
void operator()(int *index, const int *lo, const int *hi, OP &op) const
Definition: Metaprograms.H:210
void operator()(int *index, const int *lo, const int *hi, OP &op) const
Definition: Metaprograms.H:243
static bool doit(const T *v1, const T *v2)
Definition: Metaprograms.H:143
void operator()(T *v, const T &x)
Definition: Metaprograms.H:159
Definition: Metaprograms.H:122
Definition: Metaprograms.H:39
static const int value
Definition: Metaprograms.H:191
RT operator()(T const *v, T const &x)
Definition: Metaprograms.H:49
Definition: Metaprograms.H:91
Definition: Metaprograms.H:189
RT operator()(T const *v1, T const *v2)
Definition: Metaprograms.H:44
void const char const int const int * N
Definition: Lapack.H:83
void operator()(int *index, int lo, int hi, OP &op) const
Definition: Metaprograms.H:221
void operator()(int *index, const int *lo, const int *hi, OP &op) const
Definition: Metaprograms.H:225
void operator()(T *v, const T &x)
Definition: Metaprograms.H:177
bool operator()(const T *v1, const T *v2)
Definition: Metaprograms.H:124
void operator()(T *v)
Definition: Metaprograms.H:173
bool pointwiseCompare(T const *v1, T const *v2)
Definition: Metaprograms.H:73