/[escript]/branches/arrexp_2137_win_merge/escript/src/DataAlgorithm.h
ViewVC logotype

Contents of /branches/arrexp_2137_win_merge/escript/src/DataAlgorithm.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2220 - (show annotations)
Wed Jan 14 05:59:25 2009 UTC (10 years, 6 months ago) by jfenwick
File MIME type: text/plain
File size: 9714 byte(s)
Added getVectorRO to DataReady to deal with some shared data execeptions exposed when using AUTOLAZY.

This version appears to pass most unit tests under AUTOLAZY but build/posix/finley/test/python/run_models.passed  seems very slow.
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 template <class BinaryFunction>
181 inline
182 double
183 algorithm(const DataExpanded& data,
184 BinaryFunction operation,
185 double initial_value)
186 {
187 int i,j;
188 int numDPPSample=data.getNumDPPSample();
189 int numSamples=data.getNumSamples();
190 double global_current_value=initial_value;
191 double local_current_value;
192 // DataArrayView dataView=data.getPointDataView();
193 const DataTypes::ValueType& vec=data.getVectorRO();
194 const DataTypes::ShapeType& shape=data.getShape();
195 // calculate the reduction operation value for each data point
196 // reducing the result for each data-point into the current_value variables
197 #pragma omp parallel private(local_current_value)
198 {
199 local_current_value=initial_value;
200 #pragma omp for private(i,j) schedule(static)
201 for (i=0;i<numSamples;i++) {
202 for (j=0;j<numDPPSample;j++) {
203 /* local_current_value=operation(local_current_value,dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value));*/
204 local_current_value=operation(local_current_value,DataMaths::reductionOp(vec,shape,data.getPointOffset(i,j),operation,initial_value));
205
206 }
207 }
208 #pragma omp critical
209 global_current_value=operation(global_current_value,local_current_value);
210 }
211 return global_current_value;
212 }
213
214 // It is important that the algorithm only be applied to tags which are actually in use.
215 template <class BinaryFunction>
216 inline
217 double
218 algorithm(DataTagged& data,
219 BinaryFunction operation,
220 double initial_value)
221 {
222 double current_value=initial_value;
223
224 const DataTypes::ValueType& vec=data.getVectorRO();
225 const DataTypes::ShapeType& shape=data.getShape();
226 const DataTagged::DataMapType& lookup=data.getTagLookup();
227 const std::list<int> used=data.getFunctionSpace().getListOfTagsSTL();
228 for (std::list<int>::const_iterator i=used.begin();i!=used.end();++i)
229 {
230 int tag=*i;
231 if (tag==0) // check for the default tag
232 {
233 current_value=operation(current_value,DataMaths::reductionOp(vec,shape,data.getDefaultOffset(),operation,initial_value));
234 }
235 else
236 {
237 DataTagged::DataMapType::const_iterator it=lookup.find(tag);
238 if (it!=lookup.end())
239 {
240 current_value=operation(current_value,DataMaths::reductionOp(vec,shape,it->second,operation,initial_value));
241 }
242 }
243 }
244 return current_value;
245 }
246
247 template <class BinaryFunction>
248 inline
249 double
250 algorithm(DataConstant& data,
251 BinaryFunction operation,
252 double initial_value)
253 {
254 return DataMaths::reductionOp(data.getVectorRO(),data.getShape(),0,operation,initial_value);
255 }
256
257 /**
258 \brief
259 Perform the given data-point reduction operation on all data-points
260 in data, storing results in corresponding data-points of result.
261
262 Objects data and result must be of the same type, and have the same number
263 of data points, but where data has data points of rank n, result must have
264 data points of rank 0.
265
266 Calls DataArrayView::reductionOp
267 */
268 template <class BinaryFunction>
269 inline
270 void
271 dp_algorithm(const DataExpanded& data,
272 DataExpanded& result,
273 BinaryFunction operation,
274 double initial_value)
275 {
276 int i,j;
277 int numSamples=data.getNumSamples();
278 int numDPPSample=data.getNumDPPSample();
279 // DataArrayView dataView=data.getPointDataView();
280 // DataArrayView resultView=result.getPointDataView();
281 const DataTypes::ValueType& dataVec=data.getVector();
282 const DataTypes::ShapeType& shape=data.getShape();
283 DataTypes::ValueType& resultVec=result.getVector();
284 // perform the operation on each data-point and assign
285 // this to the corresponding element in result
286 #pragma omp parallel for private(i,j) schedule(static)
287 for (i=0;i<numSamples;i++) {
288 for (j=0;j<numDPPSample;j++) {
289 /* resultView.getData(result.getPointOffset(i,j)) =
290 dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);*/
291 resultVec[result.getPointOffset(i,j)] =
292 DataMaths::reductionOp(dataVec, shape, data.getPointOffset(i,j),operation,initial_value);
293
294 }
295 }
296 }
297
298 template <class BinaryFunction>
299 inline
300 void
301 dp_algorithm(const DataTagged& data,
302 DataTagged& result,
303 BinaryFunction operation,
304 double initial_value)
305 {
306 // perform the operation on each tagged value in data
307 // and assign this to the corresponding element in result
308 const DataTypes::ShapeType& shape=data.getShape();
309 const DataTypes::ValueType& vec=data.getVector();
310 const DataTagged::DataMapType& lookup=data.getTagLookup();
311 for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
312 // result.getDataPointByTag(i->first).getData(0) =
313 // data.getDataPointByTag(i->first).reductionOp(operation,initial_value);
314 result.getDataByTag(i->first,0) =
315 DataMaths::reductionOp(vec,shape,data.getOffsetForTag(i->first),operation,initial_value);
316 }
317 // perform the operation on the default data value
318 // and assign this to the default element in result
319 // result.getDefaultValue().getData(0) =
320 // data.getDefaultValue().reductionOp(operation,initial_value);
321 result.getVector()[result.getDefaultOffset()] = DataMaths::reductionOp(data.getVector(),data.getShape(),data.getDefaultOffset(),operation,initial_value);
322 }
323
324 template <class BinaryFunction>
325 inline
326 void
327 dp_algorithm(DataConstant& data,
328 DataConstant& result,
329 BinaryFunction operation,
330 double initial_value)
331 {
332 // perform the operation on the data value
333 // and assign this to the element in result
334 // result.getPointDataView().getData(0) =
335 // data.getPointDataView().reductionOp(operation,initial_value);
336 result.getVector()[0] =
337 DataMaths::reductionOp(data.getVector(),data.getShape(),0,operation,initial_value);
338 }
339
340 } // end of namespace
341
342 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26