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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 147 - (hide annotations)
Fri Aug 12 01:45:47 2005 UTC (14 years, 3 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataAlgorithm.h
File MIME type: text/plain
File size: 8975 byte(s)
erge of development branch dev-02 back to main trunk on 2005-08-12

1 jgs 102 // $Id$
2 jgs 82 /*
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 jgs 102 #if !defined escript_DataAlgorithm_20040714_H
17 jgs 82 #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 jgs 113 #include <vector>
29 jgs 82
30     namespace escript {
31 jgs 102
32 jgs 82 /**
33     \brief
34 jgs 113 Adapt binary algorithms so they may be used in DataArrayView reduction operations.
35 jgs 82
36     Description:
37 jgs 113 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 jgs 82 */
42 jgs 113 template <class BinaryFunction>
43     class DataAlgorithmAdapter {
44     public:
45     DataAlgorithmAdapter(double initialValue):
46     m_initialValue(initialValue),
47     m_currentValue(initialValue)
48     {
49     }
50 jgs 147 DataAlgorithmAdapter(const DataAlgorithmAdapter& other):
51     m_initialValue(other.m_initialValue),
52     m_currentValue(other.m_initialValue),
53     operation(other.operation)
54     {
55     }
56 jgs 113 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 jgs 82 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 jgs 113 Return the minimum value of the two given values.
96 jgs 82 */
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 jgs 113 Return the absolute maximum value of the two given values.
108 jgs 82 */
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 jgs 102
117 jgs 82 /**
118     \brief
119 jgs 117 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 jgs 113 Return the length between the two given values.
132 jgs 106 */
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 jgs 113 Return the trace of the two given values.
144 jgs 106 */
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 jgs 113 Perform the given operation upon all values in all data-points in the
156     given Data object and return the final result.
157 jgs 82
158 jgs 113 Calls DataArrayView::reductionOp
159 jgs 82 */
160 jgs 147 template <class BinaryFunction>
161 jgs 102 inline
162     double
163     algorithm(DataExpanded& data,
164 jgs 147 BinaryFunction operation,
165     double initial_value)
166 jgs 82 {
167     int i,j;
168 jgs 113 int numDPPSample=data.getNumDPPSample();
169     int numSamples=data.getNumSamples();
170     int resultVectorLength=numDPPSample*numSamples;
171     std::vector<double> resultVector(resultVectorLength);
172     DataArrayView dataView=data.getPointDataView();
173     // calculate the reduction operation value for each data point
174     // storing the result for each data-point in successive entries
175     // in resultVector
176 jgs 147 #pragma omp parallel for private(i,j) schedule(static)
177 jgs 122 for (i=0;i<numSamples;i++) {
178     for (j=0;j<numDPPSample;j++) {
179 jgs 147 resultVector[j*numSamples+i]=dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
180 jgs 82 }
181     }
182 jgs 113 // now calculate the reduction operation value across the results
183     // for each data-point
184 jgs 147 double current_value=initial_value;
185 jgs 113 for (int l=0;l<resultVectorLength;l++) {
186 jgs 147 current_value=operation(current_value,resultVector[l]);
187 jgs 113 }
188 jgs 147 return current_value;
189 jgs 82 }
190    
191 jgs 147 template <class BinaryFunction>
192 jgs 102 inline
193     double
194     algorithm(DataTagged& data,
195 jgs 147 BinaryFunction operation,
196     double initial_value)
197 jgs 82 {
198     const DataTagged::DataMapType& lookup=data.getTagLookup();
199     DataTagged::DataMapType::const_iterator i;
200     DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
201     DataArrayView& dataView=data.getPointDataView();
202 jgs 113 std::vector<double> resultVector;
203     int resultVectorLength;
204     // perform the operation on each tagged value
205 jgs 102 for (i=lookup.begin();i!=lookupEnd;i++) {
206 jgs 147 resultVector.push_back(dataView.reductionOp(i->second,operation,initial_value));
207 jgs 82 }
208 jgs 113 // perform the operation on the default value
209 jgs 147 resultVector.push_back(data.getDefaultValue().reductionOp(operation,initial_value));
210 jgs 113 // now calculate the reduction operation value across the results
211     // for each tagged value
212     resultVectorLength=resultVector.size();
213 jgs 147 double current_value=initial_value;
214 jgs 113 for (int l=0;l<resultVectorLength;l++) {
215 jgs 147 current_value=operation(current_value,resultVector[l]);
216 jgs 113 }
217 jgs 147 return current_value;
218 jgs 82 }
219    
220 jgs 147 template <class BinaryFunction>
221 jgs 102 inline
222     double
223     algorithm(DataConstant& data,
224 jgs 147 BinaryFunction operation,
225     double initial_value)
226 jgs 82 {
227 jgs 147 return data.getPointDataView().reductionOp(operation,initial_value);
228 jgs 82 }
229    
230 jgs 102 /**
231     \brief
232 jgs 113 Perform the given data-point reduction operation on all data-points
233     in data, storing results in corresponding data-points of result.
234 jgs 82
235 jgs 102 Objects data and result must be of the same type, and have the same number
236 jgs 106 of data points, but where data has data points of rank n, result must have
237     data points of rank 0.
238 jgs 102
239 jgs 113 Calls DataArrayView::reductionOp
240 jgs 102 */
241 jgs 147 template <class BinaryFunction>
242 jgs 102 inline
243     void
244 jgs 106 dp_algorithm(DataExpanded& data,
245     DataExpanded& result,
246 jgs 147 BinaryFunction operation,
247     double initial_value)
248 jgs 102 {
249     int i,j;
250 jgs 121 int numSamples=data.getNumSamples();
251 jgs 113 int numDPPSample=data.getNumDPPSample();
252     DataArrayView dataView=data.getPointDataView();
253     DataArrayView resultView=result.getPointDataView();
254     // perform the operation on each data-point and assign
255     // this to the corresponding element in result
256 jgs 147 #pragma omp parallel for private(i,j) schedule(static)
257 jgs 122 for (i=0;i<numSamples;i++) {
258     for (j=0;j<numDPPSample;j++) {
259     resultView.getData(result.getPointOffset(i,j)) =
260 jgs 147 dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
261 jgs 102 }
262     }
263     }
264    
265 jgs 147 template <class BinaryFunction>
266 jgs 102 inline
267     void
268 jgs 106 dp_algorithm(DataTagged& data,
269     DataTagged& result,
270 jgs 147 BinaryFunction operation,
271     double initial_value)
272 jgs 102 {
273     const DataTagged::DataMapType& lookup=data.getTagLookup();
274     DataTagged::DataMapType::const_iterator i;
275     DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
276 jgs 113 DataArrayView dataView=data.getPointDataView();
277     DataArrayView resultView=result.getPointDataView();
278     // perform the operation on each tagged data value
279     // and assign this to the corresponding element in result
280 jgs 102 for (i=lookup.begin();i!=lookupEnd;i++) {
281 jgs 113 resultView.getData(i->second) =
282 jgs 147 dataView.reductionOp(i->second,operation,initial_value);
283 jgs 102 }
284 jgs 113 // perform the operation on the default data value
285 jgs 106 // and assign this to the default element in result
286 jgs 113 resultView.getData(0) =
287 jgs 147 data.getDefaultValue().reductionOp(operation,initial_value);
288 jgs 102 }
289    
290 jgs 147 template <class BinaryFunction>
291 jgs 102 inline
292     void
293 jgs 106 dp_algorithm(DataConstant& data,
294     DataConstant& result,
295 jgs 147 BinaryFunction operation,
296     double initial_value)
297 jgs 102 {
298 jgs 113 // perform the operation on the data value
299     // and assign this to the element in result
300 jgs 106 result.getPointDataView().getData(0) =
301 jgs 147 data.getPointDataView().reductionOp(operation,initial_value);
302 jgs 102 }
303    
304 jgs 82 } // end of namespace
305 jgs 122
306 jgs 82 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26