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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 113 - (hide annotations)
Mon Feb 28 07:06:33 2005 UTC (15 years ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataAlgorithm.h
File MIME type: text/plain
File size: 8262 byte(s)
*** empty log message ***

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     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 jgs 82 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 jgs 113 Return the minimum value of the two given values.
90 jgs 82 */
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 jgs 113 Return the absolute maximum value of the two given values.
102 jgs 82 */
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 jgs 102
111 jgs 82 /**
112     \brief
113 jgs 113 Return the length between the two given values.
114 jgs 106 */
115     struct Length : public std::binary_function<double,double,double>
116     {
117     inline double operator()(double x, double y) const
118     {
119     return std::sqrt(std::pow(x,2)+std::pow(y,2));
120     }
121     };
122    
123     /**
124     \brief
125 jgs 113 Return the trace of the two given values.
126 jgs 106 */
127     struct Trace : public std::binary_function<double,double,double>
128     {
129     inline double operator()(double x, double y) const
130     {
131     return x+y;
132     }
133     };
134    
135     /**
136     \brief
137 jgs 113 Perform the given operation upon all values in all data-points in the
138     given Data object and return the final result.
139 jgs 82
140 jgs 113 Calls DataArrayView::reductionOp
141 jgs 82 */
142     template <class UnaryFunction>
143 jgs 102 inline
144     double
145     algorithm(DataExpanded& data,
146     UnaryFunction operation)
147 jgs 82 {
148     int i,j;
149 jgs 113 int numDPPSample=data.getNumDPPSample();
150     int numSamples=data.getNumSamples();
151     int resultVectorLength=numDPPSample*numSamples;
152     std::vector<double> resultVector(resultVectorLength);
153     DataArrayView dataView=data.getPointDataView();
154     // calculate the reduction operation value for each data point
155     // storing the result for each data-point in successive entries
156     // in resultVector
157 jgs 82 {
158 jgs 102 #pragma omp for private(i,j) schedule(static)
159     for (i=0;i<numSamples;i++) {
160     for (j=0;j<numDPPSample;j++) {
161 jgs 113 #pragma omp critical (reductionOp)
162     resultVector[j*numSamples+i]=dataView.reductionOp(data.getPointOffset(i,j),operation);
163 jgs 82 }
164     }
165     }
166 jgs 113 // now calculate the reduction operation value across the results
167     // for each data-point
168     operation.resetResult();
169     for (int l=0;l<resultVectorLength;l++) {
170     operation(resultVector[l]);
171     }
172 jgs 82 return operation.getResult();
173     }
174    
175     template <class UnaryFunction>
176 jgs 102 inline
177     double
178     algorithm(DataTagged& data,
179     UnaryFunction operation)
180 jgs 82 {
181     const DataTagged::DataMapType& lookup=data.getTagLookup();
182     DataTagged::DataMapType::const_iterator i;
183     DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
184     DataArrayView& dataView=data.getPointDataView();
185 jgs 113 std::vector<double> resultVector;
186     int resultVectorLength;
187     // perform the operation on each tagged value
188 jgs 102 for (i=lookup.begin();i!=lookupEnd;i++) {
189 jgs 113 resultVector.push_back(dataView.reductionOp(i->second,operation));
190 jgs 82 }
191 jgs 113 // perform the operation on the default value
192     resultVector.push_back(data.getDefaultValue().reductionOp(operation));
193     // now calculate the reduction operation value across the results
194     // for each tagged value
195     resultVectorLength=resultVector.size();
196     operation.resetResult();
197     for (int l=0;l<resultVectorLength;l++) {
198     operation(resultVector[l]);
199     }
200 jgs 82 return operation.getResult();
201     }
202    
203     template <class UnaryFunction>
204 jgs 102 inline
205     double
206     algorithm(DataConstant& data,
207     UnaryFunction operation)
208 jgs 82 {
209 jgs 108 return data.getPointDataView().reductionOp(operation);
210 jgs 82 }
211    
212 jgs 102 /**
213     \brief
214 jgs 113 Perform the given data-point reduction operation on all data-points
215     in data, storing results in corresponding data-points of result.
216 jgs 82
217 jgs 102 Objects data and result must be of the same type, and have the same number
218 jgs 106 of data points, but where data has data points of rank n, result must have
219     data points of rank 0.
220 jgs 102
221 jgs 113 Calls DataArrayView::reductionOp
222 jgs 102 */
223     template <class UnaryFunction>
224     inline
225     void
226 jgs 106 dp_algorithm(DataExpanded& data,
227     DataExpanded& result,
228 jgs 102 UnaryFunction operation)
229     {
230     int i,j;
231 jgs 113 int numDPPSample=data.getNumDPPSample();
232     int numSamples=data.getNumSamples();
233     DataArrayView dataView=data.getPointDataView();
234     DataArrayView resultView=result.getPointDataView();
235     // perform the operation on each data-point and assign
236     // this to the corresponding element in result
237 jgs 102 {
238     #pragma omp for private(i,j) schedule(static)
239     for (i=0;i<numSamples;i++) {
240     for (j=0;j<numDPPSample;j++) {
241 jgs 113 #pragma omp critical (reductionOp)
242     resultView.getData(data.getPointOffset(i,j)) =
243     dataView.reductionOp(data.getPointOffset(i,j),operation);
244 jgs 102 }
245     }
246     }
247     }
248    
249     template <class UnaryFunction>
250     inline
251     void
252 jgs 106 dp_algorithm(DataTagged& data,
253     DataTagged& result,
254 jgs 102 UnaryFunction operation)
255     {
256     const DataTagged::DataMapType& lookup=data.getTagLookup();
257     DataTagged::DataMapType::const_iterator i;
258     DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
259 jgs 113 DataArrayView dataView=data.getPointDataView();
260     DataArrayView resultView=result.getPointDataView();
261     // perform the operation on each tagged data value
262     // and assign this to the corresponding element in result
263 jgs 102 for (i=lookup.begin();i!=lookupEnd;i++) {
264 jgs 113 resultView.getData(i->second) =
265     dataView.reductionOp(i->second,operation);
266 jgs 102 }
267 jgs 113 // perform the operation on the default data value
268 jgs 106 // and assign this to the default element in result
269 jgs 113 resultView.getData(0) =
270     data.getDefaultValue().reductionOp(operation);
271 jgs 102 }
272    
273     template <class UnaryFunction>
274     inline
275     void
276 jgs 106 dp_algorithm(DataConstant& data,
277     DataConstant& result,
278 jgs 102 UnaryFunction operation)
279     {
280 jgs 113 // perform the operation on the data value
281     // and assign this to the element in result
282 jgs 106 result.getPointDataView().getData(0) =
283 jgs 113 data.getPointDataView().reductionOp(operation);
284 jgs 102 }
285    
286 jgs 82 } // end of namespace
287     #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26