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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1811 - (show annotations)
Thu Sep 25 23:11:13 2008 UTC (10 years, 11 months ago) by ksteube
File MIME type: text/plain
File size: 9370 byte(s)
Copyright updated in all files

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26