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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (show annotations)
Fri May 6 04:26:16 2005 UTC (14 years, 4 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataAlgorithm.h
File MIME type: text/plain
File size: 8514 byte(s)
Merge of development branch back to main trunk on 2005-05-06

1 // $Id$
2 /*
3 ******************************************************************************
4 * *
5 * COPYRIGHT ACcESS 2004 - All Rights Reserved *
6 * *
7 * This software is the property of ACcESS. No part of this code *
8 * may be copied in any form or by any means without the expressed written *
9 * consent of ACcESS. Copying, use or modification of this software *
10 * by any unauthorised person is illegal unless that person has a software *
11 * license agreement with ACcESS. *
12 * *
13 ******************************************************************************
14 */
15
16 #if !defined escript_DataAlgorithm_20040714_H
17 #define escript_DataAlgorithm_20040714_H
18
19 #include "escript/Data/DataExpanded.h"
20 #include "escript/Data/DataTagged.h"
21 #include "escript/Data/DataConstant.h"
22 #include "escript/Data/DataArrayView.h"
23
24 #include <iostream>
25 #include <algorithm>
26 #include <math.h>
27 #include <limits>
28 #include <vector>
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 inline void operator()(double value)
51 {
52 m_currentValue=operation(m_currentValue,value);
53 return;
54 }
55 inline void resetResult()
56 {
57 m_currentValue=m_initialValue;
58 }
59 inline double getResult() const
60 {
61 return m_currentValue;
62 }
63 private:
64 //
65 // the initial operation value
66 double m_initialValue;
67 //
68 // the current operation value
69 double m_currentValue;
70 //
71 // The operation to perform
72 BinaryFunction operation;
73 };
74
75 /**
76 \brief
77 Return the maximum value of the two given values.
78 */
79 struct FMax : public std::binary_function<double,double,double>
80 {
81 inline double operator()(double x, double y) const
82 {
83 return std::max(x,y);
84 }
85 };
86
87 /**
88 \brief
89 Return the minimum value of the two given values.
90 */
91 struct FMin : public std::binary_function<double,double,double>
92 {
93 inline double operator()(double x, double y) const
94 {
95 return std::min(x,y);
96 }
97 };
98
99 /**
100 \brief
101 Return the absolute maximum value of the two given values.
102 */
103 struct AbsMax : public std::binary_function<double,double,double>
104 {
105 inline double operator()(double x, double y) const
106 {
107 return std::max(fabs(x),fabs(y));
108 }
109 };
110
111 /**
112 \brief
113 Return the absolute minimum value of the two given values.
114 */
115 struct AbsMin : public std::binary_function<double,double,double>
116 {
117 inline double operator()(double x, double y) const
118 {
119 return std::min(fabs(x),fabs(y));
120 }
121 };
122
123 /**
124 \brief
125 Return the length between the two given values.
126 */
127 struct Length : public std::binary_function<double,double,double>
128 {
129 inline double operator()(double x, double y) const
130 {
131 return std::sqrt(std::pow(x,2)+std::pow(y,2));
132 }
133 };
134
135 /**
136 \brief
137 Return the trace of the two given values.
138 */
139 struct Trace : public std::binary_function<double,double,double>
140 {
141 inline double operator()(double x, double y) const
142 {
143 return x+y;
144 }
145 };
146
147 /**
148 \brief
149 Perform the given operation upon all values in all data-points in the
150 given Data object and return the final result.
151
152 Calls DataArrayView::reductionOp
153 */
154 template <class UnaryFunction>
155 inline
156 double
157 algorithm(DataExpanded& data,
158 UnaryFunction operation)
159 {
160 int i,j;
161 int numDPPSample=data.getNumDPPSample();
162 int numSamples=data.getNumSamples();
163 int resultVectorLength=numDPPSample*numSamples;
164 std::vector<double> resultVector(resultVectorLength);
165 DataArrayView dataView=data.getPointDataView();
166 // calculate the reduction operation value for each data point
167 // storing the result for each data-point in successive entries
168 // in resultVector
169 {
170 #pragma omp for private(i,j) schedule(static)
171 for (i=0;i<numSamples;i++) {
172 for (j=0;j<numDPPSample;j++) {
173 #pragma omp critical (reductionOp)
174 resultVector[j*numSamples+i]=dataView.reductionOp(data.getPointOffset(i,j),operation);
175 }
176 }
177 }
178 // now calculate the reduction operation value across the results
179 // for each data-point
180 operation.resetResult();
181 for (int l=0;l<resultVectorLength;l++) {
182 operation(resultVector[l]);
183 }
184 return operation.getResult();
185 }
186
187 template <class UnaryFunction>
188 inline
189 double
190 algorithm(DataTagged& data,
191 UnaryFunction operation)
192 {
193 const DataTagged::DataMapType& lookup=data.getTagLookup();
194 DataTagged::DataMapType::const_iterator i;
195 DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
196 DataArrayView& dataView=data.getPointDataView();
197 std::vector<double> resultVector;
198 int resultVectorLength;
199 // perform the operation on each tagged value
200 for (i=lookup.begin();i!=lookupEnd;i++) {
201 resultVector.push_back(dataView.reductionOp(i->second,operation));
202 }
203 // perform the operation on the default value
204 resultVector.push_back(data.getDefaultValue().reductionOp(operation));
205 // now calculate the reduction operation value across the results
206 // for each tagged value
207 resultVectorLength=resultVector.size();
208 operation.resetResult();
209 for (int l=0;l<resultVectorLength;l++) {
210 operation(resultVector[l]);
211 }
212 return operation.getResult();
213 }
214
215 template <class UnaryFunction>
216 inline
217 double
218 algorithm(DataConstant& data,
219 UnaryFunction operation)
220 {
221 return data.getPointDataView().reductionOp(operation);
222 }
223
224 /**
225 \brief
226 Perform the given data-point reduction operation on all data-points
227 in data, storing results in corresponding data-points of result.
228
229 Objects data and result must be of the same type, and have the same number
230 of data points, but where data has data points of rank n, result must have
231 data points of rank 0.
232
233 Calls DataArrayView::reductionOp
234 */
235 template <class UnaryFunction>
236 inline
237 void
238 dp_algorithm(DataExpanded& data,
239 DataExpanded& result,
240 UnaryFunction operation)
241 {
242 int i,j;
243 int numSamples=data.getNumSamples();
244 int numDPPSample=data.getNumDPPSample();
245 DataArrayView dataView=data.getPointDataView();
246 DataArrayView resultView=result.getPointDataView();
247 // perform the operation on each data-point and assign
248 // this to the corresponding element in result
249 {
250 #pragma omp for private(i,j) schedule(static)
251 for (i=0;i<numSamples;i++) {
252 for (j=0;j<numDPPSample;j++) {
253 #pragma omp critical (reductionOp)
254 resultView.getData(result.getPointOffset(i,j)) =
255 dataView.reductionOp(data.getPointOffset(i,j),operation);
256 }
257 }
258 }
259 }
260
261 template <class UnaryFunction>
262 inline
263 void
264 dp_algorithm(DataTagged& data,
265 DataTagged& result,
266 UnaryFunction operation)
267 {
268 const DataTagged::DataMapType& lookup=data.getTagLookup();
269 DataTagged::DataMapType::const_iterator i;
270 DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
271 DataArrayView dataView=data.getPointDataView();
272 DataArrayView resultView=result.getPointDataView();
273 // perform the operation on each tagged data value
274 // and assign this to the corresponding element in result
275 for (i=lookup.begin();i!=lookupEnd;i++) {
276 resultView.getData(i->second) =
277 dataView.reductionOp(i->second,operation);
278 }
279 // perform the operation on the default data value
280 // and assign this to the default element in result
281 resultView.getData(0) =
282 data.getDefaultValue().reductionOp(operation);
283 }
284
285 template <class UnaryFunction>
286 inline
287 void
288 dp_algorithm(DataConstant& data,
289 DataConstant& result,
290 UnaryFunction operation)
291 {
292 // perform the operation on the data value
293 // and assign this to the element in result
294 result.getPointDataView().getData(0) =
295 data.getPointDataView().reductionOp(operation);
296 }
297
298 } // end of namespace
299 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26