SpatialOps
NeboReductions.h
1 /* This file was generated by fulmar version 0.9.2. */
2 
3 /*
4  * Copyright (c) 2014 The University of Utah
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #ifndef NEBO_REDUCTIONS_H
26  #define NEBO_REDUCTIONS_H
27 
28  #define field_max nebo_max
29 
30  #define field_max_interior nebo_max_interior
31 
32  #define field_min nebo_min
33 
34  #define field_min_interior nebo_min_interior
35 
36  #define field_sum nebo_sum
37 
38  #define field_sum_interior nebo_sum_interior
39 
40  #define field_norm nebo_norm
41 
42  #define field_norm_interior nebo_norm_interior
43 
44  namespace SpatialOps {
45  template<typename ExprType, typename FieldType>
46  inline typename FieldType::value_type nebo_fold(bool useGhost,
47  typename FieldType::
48  value_type (*proc)(typename
49  FieldType::
50  value_type,
51  typename
52  FieldType::
53  value_type),
54  typename FieldType::
55  value_type initialValue,
56  NeboExpression<ExprType,
57  FieldType>
58  const & fexpr) {
59  typename FieldType::value_type typedef value_type;
60 
61  ExprType const initial = fexpr.expr();
62 
63  GhostData const ghosts = (useGhost ? initial.ghosts_without_bc() :
64  GhostData(0));
65 
66  IntVec const extents = initial.extents();
67 
68  IntVec const hasBC = initial.has_bc();
69 
70  const int xLow = - ghosts.get_minus(0);
71 
72  const int xHigh = extents[0] + ghosts.get_plus(0);
73 
74  const int yLow = - ghosts.get_minus(1);
75 
76  const int yHigh = extents[1] + ghosts.get_plus(1);
77 
78  const int zLow = - ghosts.get_minus(2);
79 
80  const int zHigh = extents[2] + ghosts.get_plus(2);
81 
82  value_type result = initialValue;
83 
84  typename ExprType::SeqWalkType expr = initial.init(extents,
85  ghosts,
86  hasBC);
87 
88  for(int z = zLow; z < zHigh; z++) {
89  for(int y = yLow; y < yHigh; y++) {
90  for(int x = xLow; x < xHigh; x++) {
91  result = proc(result, expr.eval(x, y, z));
92  };
93  };
94  };
95 
96  return result;
97  };
98 
99  template<typename AtomicType>
100  inline AtomicType nebo_scalar_max(AtomicType a, AtomicType b) {
101  return ((a < b) ? b : a);
102  };
103 
104  template<typename AtomicType>
105  inline AtomicType nebo_scalar_min(AtomicType a, AtomicType b) {
106  return ((a > b) ? b : a);
107  };
108 
109  template<typename AtomicType>
110  inline AtomicType nebo_scalar_sum(AtomicType a, AtomicType b) {
111  return a + b;
112  };
113 
114  template<typename ExprType, typename FieldType>
115  inline typename FieldType::value_type nebo_max(NeboExpression<ExprType,
116  FieldType>
117  const & fexpr) {
118  return nebo_fold(true,
119  nebo_scalar_max,
120  -(std::numeric_limits<double>().infinity()),
121  fexpr);
122  };
123 
124  template<typename FieldType>
125  inline typename FieldType::value_type nebo_max(FieldType const & field) {
126  NeboConstField<Initial, FieldType> typedef ExprType;
127 
128  return nebo_max(NeboExpression<ExprType, FieldType>(ExprType(field)));
129  };
130 
131  template<typename ExprType, typename FieldType>
132  inline typename FieldType::value_type nebo_max_interior(NeboExpression<ExprType,
133  FieldType>
134  const & fexpr) {
135  return nebo_fold(false,
136  nebo_scalar_max,
137  -(std::numeric_limits<double>().infinity()),
138  fexpr);
139  };
140 
141  template<typename FieldType>
142  inline typename FieldType::value_type nebo_max_interior(FieldType const &
143  field) {
144  NeboConstField<Initial, FieldType> typedef ExprType;
145 
146  return nebo_max_interior(NeboExpression<ExprType, FieldType>(ExprType(field)));
147  };
148 
149  template<typename ExprType, typename FieldType>
150  inline typename FieldType::value_type nebo_min(NeboExpression<ExprType,
151  FieldType>
152  const & fexpr) {
153  return nebo_fold(true,
154  nebo_scalar_min,
155  std::numeric_limits<double>().infinity(),
156  fexpr);
157  };
158 
159  template<typename FieldType>
160  inline typename FieldType::value_type nebo_min(FieldType const & field) {
161  NeboConstField<Initial, FieldType> typedef ExprType;
162 
163  return nebo_min(NeboExpression<ExprType, FieldType>(ExprType(field)));
164  };
165 
166  template<typename ExprType, typename FieldType>
167  inline typename FieldType::value_type nebo_min_interior(NeboExpression<ExprType,
168  FieldType>
169  const & fexpr) {
170  return nebo_fold(false,
171  nebo_scalar_min,
172  std::numeric_limits<double>().infinity(),
173  fexpr);
174  };
175 
176  template<typename FieldType>
177  inline typename FieldType::value_type nebo_min_interior(FieldType const &
178  field) {
179  NeboConstField<Initial, FieldType> typedef ExprType;
180 
181  return nebo_min_interior(NeboExpression<ExprType, FieldType>(ExprType(field)));
182  };
183 
184  template<typename ExprType, typename FieldType>
185  inline typename FieldType::value_type nebo_sum(NeboExpression<ExprType,
186  FieldType>
187  const & fexpr) {
188  return nebo_fold(true, nebo_scalar_sum, 0.0, fexpr);
189  };
190 
191  template<typename FieldType>
192  inline typename FieldType::value_type nebo_sum(FieldType const & field) {
193  NeboConstField<Initial, FieldType> typedef ExprType;
194 
195  return nebo_sum(NeboExpression<ExprType, FieldType>(ExprType(field)));
196  };
197 
198  template<typename ExprType, typename FieldType>
199  inline typename FieldType::value_type nebo_sum_interior(NeboExpression<ExprType,
200  FieldType>
201  const & fexpr) {
202  return nebo_fold(false, nebo_scalar_sum, 0.0, fexpr);
203  };
204 
205  template<typename FieldType>
206  inline typename FieldType::value_type nebo_sum_interior(FieldType const &
207  field) {
208  NeboConstField<Initial, FieldType> typedef ExprType;
209 
210  return nebo_sum_interior(NeboExpression<ExprType, FieldType>(ExprType(field)));
211  };
212 
213  template<typename ExprType, typename FieldType>
214  inline typename FieldType::value_type nebo_norm(NeboExpression<ExprType,
215  FieldType>
216  const & fexpr) {
217  return std::sqrt(nebo_sum(fexpr*fexpr));
218  };
219 
220  template<typename FieldType>
221  inline typename FieldType::value_type nebo_norm(FieldType const & field) {
222  NeboConstField<Initial, FieldType> typedef ExprType;
223 
224  return nebo_norm(NeboExpression<ExprType, FieldType>(ExprType(field)));
225  };
226 
227  template<typename ExprType, typename FieldType>
228  inline typename FieldType::value_type nebo_norm_interior(NeboExpression<ExprType,
229  FieldType>
230  const & fexpr) {
231  return std::sqrt(nebo_sum_interior(fexpr*fexpr));
232  };
233 
234  template<typename FieldType>
235  inline typename FieldType::value_type nebo_norm_interior(FieldType const
236  & field) {
237  NeboConstField<Initial, FieldType> typedef ExprType;
238 
239  return nebo_norm_interior(NeboExpression<ExprType, FieldType>(ExprType(field)));
240  };
241  } /* SpatialOps */
242 
243 #endif
244 /* NEBO_REDUCTIONS_H */