/[escript]/trunk/escript/src/DataAlgorithm.h
ViewVC logotype

Contents of /trunk/escript/src/DataAlgorithm.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2147 - (show annotations)
Wed Dec 10 04:41:26 2008 UTC (10 years, 9 months ago) by jfenwick
File MIME type: text/plain
File size: 9800 byte(s)
Made some changes to c++ unit tests to accomodate AUTOLAZY.
whereZero and whereNonZero can now work with lazy data.
There are some double frees if AUTOLAZY is turned on so don't use it yet.
1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14
15 #if !defined escript_DataAlgorithm_20040714_H
16 #define escript_DataAlgorithm_20040714_H
17 #include "system_dep.h"
18
19 #include "DataExpanded.h"
20 #include "DataTagged.h"
21 #include "DataConstant.h"
22
23 #include "DataMaths.h"
24
25 #include <iostream>
26 #include <algorithm>
27 #include <list>
28
29 namespace escript {
30
31 /**
32 \brief
33 Adapt binary algorithms so they may be used in DataArrayView reduction operations.
34
35 Description:
36 This functor adapts the given BinaryFunction operation by starting with the
37 given inital value applying this operation to successive values, storing the
38 rolling result in m_currentValue - which can be accessed or reset by getResult
39 and resetResult respectively.
40 */
41 template <class BinaryFunction>
42 class DataAlgorithmAdapter {
43 public:
44 DataAlgorithmAdapter(double initialValue):
45 m_initialValue(initialValue),
46 m_currentValue(initialValue)
47 {
48 }
49 DataAlgorithmAdapter(const DataAlgorithmAdapter& other):
50 m_initialValue(other.m_initialValue),
51 m_currentValue(other.m_initialValue),
52 operation(other.operation)
53 {
54 }
55 inline void operator()(double value)
56 {
57 m_currentValue=operation(m_currentValue,value);
58 return;
59 }
60 inline void resetResult()
61 {
62 m_currentValue=m_initialValue;
63 }
64 inline double getResult() const
65 {
66 return m_currentValue;
67 }
68 private:
69 //
70 // the initial operation value
71 double m_initialValue;
72 //
73 // the current operation value
74 double m_currentValue;
75 //
76 // The operation to perform
77 BinaryFunction operation;
78 };
79
80 /**
81 \brief
82 Return the maximum value of the two given values.
83 */
84 struct FMax : public std::binary_function<double,double,double>
85 {
86 inline double operator()(double x, double y) const
87 {
88 return std::max(x,y);
89 }
90 };
91
92 /**
93 \brief
94 Return the minimum value of the two given values.
95 */
96 struct FMin : public std::binary_function<double,double,double>
97 {
98 inline double operator()(double x, double y) const
99 {
100 return std::min(x,y);
101 }
102 };
103
104 /**
105 \brief
106 Return the absolute maximum value of the two given values.
107 */
108 struct AbsMax : public std::binary_function<double,double,double>
109 {
110 inline double operator()(double x, double y) const
111 {
112 return std::max(fabs(x),fabs(y));
113 }
114 };
115
116 /**
117 \brief
118 Return the absolute minimum value of the two given values.
119 */
120 struct AbsMin : public std::binary_function<double,double,double>
121 {
122 inline double operator()(double x, double y) const
123 {
124 return std::min(fabs(x),fabs(y));
125 }
126 };
127
128 /**
129 \brief
130 Return the length between the two given values.
131 */
132 struct Length : public std::binary_function<double,double,double>
133 {
134 inline double operator()(double x, double y) const
135 {
136 return std::sqrt(std::pow(x,2)+std::pow(y,2));
137 }
138 };
139
140 /**
141 \brief
142 Return the trace of the two given values.
143 */
144 struct Trace : public std::binary_function<double,double,double>
145 {
146 inline double operator()(double x, double y) const
147 {
148 return x+y;
149 }
150 };
151
152 /**
153 \brief Return 1 if abs(x)>y, otherwise return 0.
154 */
155 struct AbsGT : public std::binary_function<double,double,double>
156 {
157 inline double operator()(double x, double y) const
158 {
159 return fabs(x)>y;
160 }
161 };
162
163 /**
164 \brief Return 1 if abs(x)<=y, otherwise return 0.
165 */
166 struct AbsLTE : public std::binary_function<double,double,double>
167 {
168 inline double operator()(double x, double y) const
169 {
170 return fabs(x)<=y;
171 }
172 };
173
174
175 /**
176 \brief
177 Perform the given operation upon all values in all data-points in the
178 given Data object and return the final result.
179
180 Calls DataArrayView::reductionOp
181 */
182 template <class BinaryFunction>
183 inline
184 double
185 algorithm(DataExpanded& data,
186 BinaryFunction operation,
187 double initial_value)
188 {
189 int i,j;
190 int numDPPSample=data.getNumDPPSample();
191 int numSamples=data.getNumSamples();
192 double global_current_value=initial_value;
193 double local_current_value;
194 // DataArrayView dataView=data.getPointDataView();
195 DataTypes::ValueType& vec=data.getVector();
196 const DataTypes::ShapeType& shape=data.getShape();
197 // calculate the reduction operation value for each data point
198 // reducing the result for each data-point into the current_value variables
199 #pragma omp parallel private(local_current_value)
200 {
201 local_current_value=initial_value;
202 #pragma omp for private(i,j) schedule(static)
203 for (i=0;i<numSamples;i++) {
204 for (j=0;j<numDPPSample;j++) {
205 /* local_current_value=operation(local_current_value,dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value));*/
206 local_current_value=operation(local_current_value,DataMaths::reductionOp(vec,shape,data.getPointOffset(i,j),operation,initial_value));
207
208 }
209 }
210 #pragma omp critical
211 global_current_value=operation(global_current_value,local_current_value);
212 }
213 return global_current_value;
214 }
215
216 // It is important that the algorithm only be applied to tags which are actually in use.
217 template <class BinaryFunction>
218 inline
219 double
220 algorithm(DataTagged& data,
221 BinaryFunction operation,
222 double initial_value)
223 {
224 double current_value=initial_value;
225
226 DataTypes::ValueType& vec=data.getVector();
227 const DataTypes::ShapeType& shape=data.getShape();
228 const DataTagged::DataMapType& lookup=data.getTagLookup();
229 const std::list<int> used=data.getFunctionSpace().getListOfTagsSTL();
230 for (std::list<int>::const_iterator i=used.begin();i!=used.end();++i)
231 {
232 int tag=*i;
233 if (tag==0) // check for the default tag
234 {
235 current_value=operation(current_value,DataMaths::reductionOp(vec,shape,data.getDefaultOffset(),operation,initial_value));
236 }
237 else
238 {
239 DataTagged::DataMapType::const_iterator it=lookup.find(tag);
240 if (it!=lookup.end())
241 {
242 current_value=operation(current_value,DataMaths::reductionOp(vec,shape,it->second,operation,initial_value));
243 }
244 }
245 }
246 return current_value;
247 }
248
249 template <class BinaryFunction>
250 inline
251 double
252 algorithm(DataConstant& data,
253 BinaryFunction operation,
254 double initial_value)
255 {
256 return DataMaths::reductionOp(data.getVector(),data.getShape(),0,operation,initial_value);
257 // return data.getPointDataView().reductionOp(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.getVector();
285 const DataTypes::ShapeType& shape=data.getShape();
286 DataTypes::ValueType& resultVec=result.getVector();
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.getVector();
313 const DataTagged::DataMapType& lookup=data.getTagLookup();
314 for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
315 // result.getDataPointByTag(i->first).getData(0) =
316 // data.getDataPointByTag(i->first).reductionOp(operation,initial_value);
317 result.getDataByTag(i->first,0) =
318 DataMaths::reductionOp(vec,shape,data.getOffsetForTag(i->first),operation,initial_value);
319 }
320 // perform the operation on the default data value
321 // and assign this to the default element in result
322 // result.getDefaultValue().getData(0) =
323 // data.getDefaultValue().reductionOp(operation,initial_value);
324 result.getVector()[result.getDefaultOffset()] = DataMaths::reductionOp(data.getVector(),data.getShape(),data.getDefaultOffset(),operation,initial_value);
325 }
326
327 template <class BinaryFunction>
328 inline
329 void
330 dp_algorithm(DataConstant& data,
331 DataConstant& result,
332 BinaryFunction operation,
333 double initial_value)
334 {
335 // perform the operation on the data value
336 // and assign this to the element in result
337 // result.getPointDataView().getData(0) =
338 // data.getPointDataView().reductionOp(operation,initial_value);
339 result.getVector()[0] =
340 DataMaths::reductionOp(data.getVector(),data.getShape(),0,operation,initial_value);
341 }
342
343 } // end of namespace
344
345 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26