/[escript]/branches/subworld2/escriptcore/src/DataAlgorithm.h
ViewVC logotype

Contents of /branches/subworld2/escriptcore/src/DataAlgorithm.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5504 - (show annotations)
Wed Mar 4 22:58:13 2015 UTC (4 years, 1 month ago) by jfenwick
File MIME type: text/plain
File size: 9558 byte(s)
Again with a more up to date copy


1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2015 by University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development 2012-2013 by School of Earth Sciences
13 * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 *
15 *****************************************************************************/
16
17
18 #if !defined escript_DataAlgorithm_20040714_H
19 #define escript_DataAlgorithm_20040714_H
20 #include "system_dep.h"
21
22 #include "DataExpanded.h"
23 #include "DataTagged.h"
24 #include "DataConstant.h"
25
26 #include "DataMaths.h"
27
28 #include <iostream>
29 #include <algorithm>
30 #include <list>
31
32 namespace escript {
33
34 /**
35 \brief
36 Adapt binary algorithms so they may be used in DataArrayView reduction operations.
37
38 Description:
39 This functor adapts the given BinaryFunction operation by starting with the
40 given inital value applying this operation to successive values, storing the
41 rolling result in m_currentValue - which can be accessed or reset by getResult
42 and resetResult respectively.
43 */
44 template <class BinaryFunction>
45 class DataAlgorithmAdapter {
46 public:
47 DataAlgorithmAdapter(double initialValue):
48 m_initialValue(initialValue),
49 m_currentValue(initialValue)
50 {
51 }
52 DataAlgorithmAdapter(const DataAlgorithmAdapter& other):
53 m_initialValue(other.m_initialValue),
54 m_currentValue(other.m_initialValue),
55 operation(other.operation)
56 {
57 }
58 inline void operator()(double value)
59 {
60 m_currentValue=operation(m_currentValue,value);
61 return;
62 }
63 inline void resetResult()
64 {
65 m_currentValue=m_initialValue;
66 }
67 inline double getResult() const
68 {
69 return m_currentValue;
70 }
71 private:
72 //
73 // the initial operation value
74 double m_initialValue;
75 //
76 // the current operation value
77 double m_currentValue;
78 //
79 // The operation to perform
80 BinaryFunction operation;
81 };
82
83 /**
84 \brief
85 Return the maximum value of the two given values.
86 */
87 struct FMax : public std::binary_function<double,double,double>
88 {
89 inline double operator()(double x, double y) const
90 {
91 return std::max(x,y);
92 }
93 };
94
95 /**
96 \brief
97 Return the minimum value of the two given values.
98 */
99 struct FMin : public std::binary_function<double,double,double>
100 {
101 inline double operator()(double x, double y) const
102 {
103 return std::min(x,y);
104 }
105 };
106
107 /**
108 \brief
109 Return the absolute maximum value of the two given values.
110 */
111 struct AbsMax : public std::binary_function<double,double,double>
112 {
113 inline double operator()(double x, double y) const
114 {
115 return std::max(fabs(x),fabs(y));
116 }
117 };
118
119 /**
120 \brief
121 Return the absolute minimum value of the two given values.
122 */
123 struct AbsMin : public std::binary_function<double,double,double>
124 {
125 inline double operator()(double x, double y) const
126 {
127 return std::min(fabs(x),fabs(y));
128 }
129 };
130
131 /**
132 \brief
133 Return the length between the two given values.
134 */
135 struct Length : public std::binary_function<double,double,double>
136 {
137 inline double operator()(double x, double y) const
138 {
139 return std::sqrt(std::pow(x,2)+std::pow(y,2));
140 }
141 };
142
143 /**
144 \brief
145 Return the trace of the two given values.
146 */
147 struct Trace : public std::binary_function<double,double,double>
148 {
149 inline double operator()(double x, double y) const
150 {
151 return x+y;
152 }
153 };
154
155 /**
156 \brief Return 1 if abs(x)>y, otherwise return 0.
157 */
158 struct AbsGT : public std::binary_function<double,double,double>
159 {
160 inline double operator()(double x, double y) const
161 {
162 return fabs(x)>y;
163 }
164 };
165
166 /**
167 \brief Return 1 if abs(x)<=y, otherwise return 0.
168 */
169 struct AbsLTE : public std::binary_function<double,double,double>
170 {
171 inline double operator()(double x, double y) const
172 {
173 return fabs(x)<=y;
174 }
175 };
176
177
178 /**
179 \brief
180 Perform the given operation upon all values in all data-points in the
181 given Data object and return the final result.
182 */
183 template <class BinaryFunction>
184 inline
185 double
186 algorithm(const DataExpanded& data,
187 BinaryFunction operation,
188 double initial_value)
189 {
190 int i,j;
191 int numDPPSample=data.getNumDPPSample();
192 int numSamples=data.getNumSamples();
193 double global_current_value=initial_value;
194 double local_current_value;
195 // DataArrayView dataView=data.getPointDataView();
196 const DataTypes::ValueType& vec=data.getVectorRO();
197 const DataTypes::ShapeType& shape=data.getShape();
198 // calculate the reduction operation value for each data point
199 // reducing the result for each data-point into the current_value variables
200 #pragma omp parallel private(local_current_value)
201 {
202 local_current_value=initial_value;
203 #pragma omp for private(i,j) schedule(static)
204 for (i=0;i<numSamples;i++) {
205 for (j=0;j<numDPPSample;j++) {
206 /* local_current_value=operation(local_current_value,dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value));*/
207 local_current_value=operation(local_current_value,DataMaths::reductionOp(vec,shape,data.getPointOffset(i,j),operation,initial_value));
208
209 }
210 }
211 #pragma omp critical
212 global_current_value=operation(global_current_value,local_current_value);
213 }
214 return global_current_value;
215 }
216
217 // It is important that the algorithm only be applied to tags which are actually in use.
218 template <class BinaryFunction>
219 inline
220 double
221 algorithm(DataTagged& data,
222 BinaryFunction operation,
223 double initial_value)
224 {
225 double current_value=initial_value;
226
227 const DataTypes::ValueType& vec=data.getVectorRO();
228 const DataTypes::ShapeType& shape=data.getShape();
229 const DataTagged::DataMapType& lookup=data.getTagLookup();
230 const std::list<int> used=data.getFunctionSpace().getListOfTagsSTL();
231 for (std::list<int>::const_iterator i=used.begin();i!=used.end();++i)
232 {
233 int tag=*i;
234 if (tag==0) // check for the default tag
235 {
236 current_value=operation(current_value,DataMaths::reductionOp(vec,shape,data.getDefaultOffset(),operation,initial_value));
237 }
238 else
239 {
240 DataTagged::DataMapType::const_iterator it=lookup.find(tag);
241 if (it!=lookup.end())
242 {
243 current_value=operation(current_value,DataMaths::reductionOp(vec,shape,it->second,operation,initial_value));
244 }
245 }
246 }
247 return current_value;
248 }
249
250 template <class BinaryFunction>
251 inline
252 double
253 algorithm(DataConstant& data,
254 BinaryFunction operation,
255 double initial_value)
256 {
257 return DataMaths::reductionOp(data.getVectorRO(),data.getShape(),0,operation,initial_value);
258 }
259
260 /**
261 \brief
262 Perform the given data-point reduction operation on all data-points
263 in data, storing results in corresponding data-points of result.
264
265 Objects data and result must be of the same type, and have the same number
266 of data points, but where data has data points of rank n, result must have
267 data points of rank 0.
268
269 Calls DataArrayView::reductionOp
270 */
271 template <class BinaryFunction>
272 inline
273 void
274 dp_algorithm(const DataExpanded& data,
275 DataExpanded& result,
276 BinaryFunction operation,
277 double initial_value)
278 {
279 int i,j;
280 int numSamples=data.getNumSamples();
281 int numDPPSample=data.getNumDPPSample();
282 // DataArrayView dataView=data.getPointDataView();
283 // DataArrayView resultView=result.getPointDataView();
284 const DataTypes::ValueType& dataVec=data.getVectorRO();
285 const DataTypes::ShapeType& shape=data.getShape();
286 DataTypes::ValueType& resultVec=result.getVectorRW();
287 // perform the operation on each data-point and assign
288 // this to the corresponding element in result
289 #pragma omp parallel for private(i,j) schedule(static)
290 for (i=0;i<numSamples;i++) {
291 for (j=0;j<numDPPSample;j++) {
292 /* resultView.getData(result.getPointOffset(i,j)) =
293 dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);*/
294 resultVec[result.getPointOffset(i,j)] =
295 DataMaths::reductionOp(dataVec, shape, data.getPointOffset(i,j),operation,initial_value);
296
297 }
298 }
299 }
300
301 template <class BinaryFunction>
302 inline
303 void
304 dp_algorithm(const DataTagged& data,
305 DataTagged& result,
306 BinaryFunction operation,
307 double initial_value)
308 {
309 // perform the operation on each tagged value in data
310 // and assign this to the corresponding element in result
311 const DataTypes::ShapeType& shape=data.getShape();
312 const DataTypes::ValueType& vec=data.getVectorRO();
313 const DataTagged::DataMapType& lookup=data.getTagLookup();
314 for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
315 result.getDataByTagRW(i->first,0) =
316 DataMaths::reductionOp(vec,shape,data.getOffsetForTag(i->first),operation,initial_value);
317 }
318 // perform the operation on the default data value
319 // and assign this to the default element in result
320 result.getVectorRW()[result.getDefaultOffset()] = DataMaths::reductionOp(data.getVectorRO(),data.getShape(),data.getDefaultOffset(),operation,initial_value);
321 }
322
323 template <class BinaryFunction>
324 inline
325 void
326 dp_algorithm(DataConstant& data,
327 DataConstant& result,
328 BinaryFunction operation,
329 double initial_value)
330 {
331 // perform the operation on the data value
332 // and assign this to the element in result
333 result.getVectorRW()[0] =
334 DataMaths::reductionOp(data.getVectorRO(),data.getShape(),0,operation,initial_value);
335 }
336
337 } // end of namespace
338
339 #endif

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26