SpatialOps
NeboReductions.h
1 /* This file was generated by fulmar version 0.9.2. */
2 
3 /*
4  * Copyright (c) 2014-2017 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(PLUS_SIDE);
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,
92  expr.template eval</*
93  TODO: Wrap Into Compile Time Optional Args
94  */void>(x, y, z));
95  };
96  };
97  };
98 
99  return result;
100  };
101 
102  template<typename AtomicType>
103  inline AtomicType nebo_scalar_max(AtomicType a, AtomicType b) {
104  return ((a < b) ? b : a);
105  };
106 
107  template<typename AtomicType>
108  inline AtomicType nebo_scalar_min(AtomicType a, AtomicType b) {
109  return ((a > b) ? b : a);
110  };
111 
112  template<typename AtomicType>
113  inline AtomicType nebo_scalar_sum(AtomicType a, AtomicType b) {
114  return a + b;
115  };
116 
117  template<typename ExprType, typename FieldType>
118  inline typename FieldType::value_type nebo_max(NeboExpression<ExprType,
119  FieldType>
120  const & fexpr) {
121  return nebo_fold(true,
122  nebo_scalar_max,
123  -(std::numeric_limits<double>().infinity()),
124  fexpr);
125  };
126 
127  template<typename FieldType>
128  inline typename FieldType::value_type nebo_max(FieldType const & field) {
129  NeboConstField<Initial, FieldType> typedef ExprType;
130 
131  return nebo_max(NeboExpression<ExprType, FieldType>(ExprType(field)));
132  };
133 
134  template<typename ExprType, typename FieldType>
135  inline typename FieldType::value_type nebo_max_interior(NeboExpression<ExprType,
136  FieldType>
137  const & fexpr) {
138  return nebo_fold(false,
139  nebo_scalar_max,
140  -(std::numeric_limits<double>().infinity()),
141  fexpr);
142  };
143 
144  template<typename FieldType>
145  inline typename FieldType::value_type nebo_max_interior(FieldType const &
146  field) {
147  NeboConstField<Initial, FieldType> typedef ExprType;
148 
149  return nebo_max_interior(NeboExpression<ExprType, FieldType>(ExprType(field)));
150  };
151 
152  template<typename ExprType, typename FieldType>
153  inline typename FieldType::value_type nebo_min(NeboExpression<ExprType,
154  FieldType>
155  const & fexpr) {
156  return nebo_fold(true,
157  nebo_scalar_min,
158  std::numeric_limits<double>().infinity(),
159  fexpr);
160  };
161 
162  template<typename FieldType>
163  inline typename FieldType::value_type nebo_min(FieldType const & field) {
164  NeboConstField<Initial, FieldType> typedef ExprType;
165 
166  return nebo_min(NeboExpression<ExprType, FieldType>(ExprType(field)));
167  };
168 
169  template<typename ExprType, typename FieldType>
170  inline typename FieldType::value_type nebo_min_interior(NeboExpression<ExprType,
171  FieldType>
172  const & fexpr) {
173  return nebo_fold(false,
174  nebo_scalar_min,
175  std::numeric_limits<double>().infinity(),
176  fexpr);
177  };
178 
179  template<typename FieldType>
180  inline typename FieldType::value_type nebo_min_interior(FieldType const &
181  field) {
182  NeboConstField<Initial, FieldType> typedef ExprType;
183 
184  return nebo_min_interior(NeboExpression<ExprType, FieldType>(ExprType(field)));
185  };
186 
187  template<typename ExprType, typename FieldType>
188  inline typename FieldType::value_type nebo_sum(NeboExpression<ExprType,
189  FieldType>
190  const & fexpr) {
191  return nebo_fold(true, nebo_scalar_sum, 0.0, fexpr);
192  };
193 
194  template<typename FieldType>
195  inline typename FieldType::value_type nebo_sum(FieldType const & field) {
196  NeboConstField<Initial, FieldType> typedef ExprType;
197 
198  return nebo_sum(NeboExpression<ExprType, FieldType>(ExprType(field)));
199  };
200 
201  template<typename ExprType, typename FieldType>
202  inline typename FieldType::value_type nebo_sum_interior(NeboExpression<ExprType,
203  FieldType>
204  const & fexpr) {
205  return nebo_fold(false, nebo_scalar_sum, 0.0, fexpr);
206  };
207 
208  template<typename FieldType>
209  inline typename FieldType::value_type nebo_sum_interior(FieldType const &
210  field) {
211  NeboConstField<Initial, FieldType> typedef ExprType;
212 
213  return nebo_sum_interior(NeboExpression<ExprType, FieldType>(ExprType(field)));
214  };
215 
216  template<typename ExprType, typename FieldType>
217  inline typename FieldType::value_type nebo_norm(NeboExpression<ExprType,
218  FieldType>
219  const & fexpr) {
220  return std::sqrt(nebo_sum(fexpr*fexpr));
221  };
222 
223  template<typename FieldType>
224  inline typename FieldType::value_type nebo_norm(FieldType const & field) {
225  NeboConstField<Initial, FieldType> typedef ExprType;
226 
227  return nebo_norm(NeboExpression<ExprType, FieldType>(ExprType(field)));
228  };
229 
230  template<typename ExprType, typename FieldType>
231  inline typename FieldType::value_type nebo_norm_interior(NeboExpression<ExprType,
232  FieldType>
233  const & fexpr) {
234  return std::sqrt(nebo_sum_interior(fexpr*fexpr));
235  };
236 
237  template<typename FieldType>
238  inline typename FieldType::value_type nebo_norm_interior(FieldType const
239  & field) {
240  NeboConstField<Initial, FieldType> typedef ExprType;
241 
242  return nebo_norm_interior(NeboExpression<ExprType, FieldType>(ExprType(field)));
243  };
244  } /* SpatialOps */
245 
246 #endif
247 /* NEBO_REDUCTIONS_H */