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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1724 - (show annotations)
Mon Aug 25 05:38:57 2008 UTC (10 years, 11 months ago) by jfenwick
File MIME type: text/plain
File size: 9698 byte(s)
Branch commit

Moved createShapeErrorMessage() into DataTypes.h
Modified functions in DataAlgorithm.h to use non-DataArrayView accesses.

Added getVector() to each of DataTagged, DataConstant, DataExpanded - This method returns 
the underlying DataVector by reference/constant reference. Note that this method does not 
exist in DataAbstract so (at the momement) in order to pull the data from something you 
need to know what you are looking at. (Lower level access is still possible via double* 
though).

DataTagged now has a getOffsetForTag method and a getDefaultOffset method.

DataMaths.h - A new file containing the reductionOps etc from DataArrayView (but without 
requiring DAV).
This file requires significant commenting improvements.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26