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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2147 - (hide annotations)
Wed Dec 10 04:41:26 2008 UTC (10 years, 11 months ago) by jfenwick
File MIME type: text/plain
File size: 9800 byte(s)
Made some changes to c++ unit tests to accomodate AUTOLAZY.
whereZero and whereNonZero can now work with lazy data.
There are some double frees if AUTOLAZY is turned on so don't use it yet.
1 ksteube 1312
2     /*******************************************************
3 ksteube 1811 *
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 ksteube 1312
14 ksteube 1811
15 jgs 102 #if !defined escript_DataAlgorithm_20040714_H
16 jgs 82 #define escript_DataAlgorithm_20040714_H
17 woo409 757 #include "system_dep.h"
18 jgs 82
19 jgs 474 #include "DataExpanded.h"
20     #include "DataTagged.h"
21     #include "DataConstant.h"
22 jgs 82
23 jfenwick 1796 #include "DataMaths.h"
24    
25 jgs 82 #include <iostream>
26     #include <algorithm>
27 jfenwick 1802 #include <list>
28 jgs 82
29     namespace escript {
30 jgs 102
31 jgs 82 /**
32     \brief
33 jgs 113 Adapt binary algorithms so they may be used in DataArrayView reduction operations.
34 jgs 82
35     Description:
36 jgs 113 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 jgs 82 */
41 jgs 113 template <class BinaryFunction>
42     class DataAlgorithmAdapter {
43     public:
44     DataAlgorithmAdapter(double initialValue):
45     m_initialValue(initialValue),
46     m_currentValue(initialValue)
47     {
48     }
49 jgs 147 DataAlgorithmAdapter(const DataAlgorithmAdapter& other):
50     m_initialValue(other.m_initialValue),
51     m_currentValue(other.m_initialValue),
52     operation(other.operation)
53     {
54     }
55 jgs 113 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 jgs 82 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 jgs 113 Return the minimum value of the two given values.
95 jgs 82 */
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 jgs 113 Return the absolute maximum value of the two given values.
107 jgs 82 */
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 jgs 102
116 jgs 82 /**
117     \brief
118 jgs 117 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 jgs 113 Return the length between the two given values.
131 jgs 106 */
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 jgs 113 Return the trace of the two given values.
143 jgs 106 */
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 jfenwick 2147 \brief Return 1 if abs(x)>y, otherwise return 0.
154     */
155     struct AbsGT : public std::binary_function<double,double,double>
156     {
157     inline double operator()(double x, double y) const
158     {
159     return fabs(x)>y;
160     }
161     };
162    
163     /**
164     \brief Return 1 if abs(x)<=y, otherwise return 0.
165     */
166     struct AbsLTE : public std::binary_function<double,double,double>
167     {
168     inline double operator()(double x, double y) const
169     {
170     return fabs(x)<=y;
171     }
172     };
173    
174    
175     /**
176 jgs 106 \brief
177 jgs 113 Perform the given operation upon all values in all data-points in the
178     given Data object and return the final result.
179 jgs 82
180 jgs 113 Calls DataArrayView::reductionOp
181 jgs 82 */
182 jgs 147 template <class BinaryFunction>
183 jgs 102 inline
184     double
185     algorithm(DataExpanded& data,
186 jgs 147 BinaryFunction operation,
187     double initial_value)
188 jgs 82 {
189     int i,j;
190 jgs 113 int numDPPSample=data.getNumDPPSample();
191     int numSamples=data.getNumSamples();
192 jgs 148 double global_current_value=initial_value;
193     double local_current_value;
194 jfenwick 1796 // DataArrayView dataView=data.getPointDataView();
195     DataTypes::ValueType& vec=data.getVector();
196     const DataTypes::ShapeType& shape=data.getShape();
197 jgs 113 // calculate the reduction operation value for each data point
198 jgs 148 // reducing the result for each data-point into the current_value variables
199     #pragma omp parallel private(local_current_value)
200     {
201     local_current_value=initial_value;
202     #pragma omp for private(i,j) schedule(static)
203     for (i=0;i<numSamples;i++) {
204     for (j=0;j<numDPPSample;j++) {
205 jfenwick 1796 /* local_current_value=operation(local_current_value,dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value));*/
206     local_current_value=operation(local_current_value,DataMaths::reductionOp(vec,shape,data.getPointOffset(i,j),operation,initial_value));
207    
208 jgs 148 }
209     }
210     #pragma omp critical
211     global_current_value=operation(global_current_value,local_current_value);
212 jgs 82 }
213 jgs 148 return global_current_value;
214 jgs 82 }
215    
216 jfenwick 1802 // It is important that the algorithm only be applied to tags which are actually in use.
217 jgs 147 template <class BinaryFunction>
218 jgs 102 inline
219     double
220     algorithm(DataTagged& data,
221 jgs 147 BinaryFunction operation,
222     double initial_value)
223 jgs 82 {
224 jgs 148 double current_value=initial_value;
225 jfenwick 1796
226     DataTypes::ValueType& vec=data.getVector();
227     const DataTypes::ShapeType& shape=data.getShape();
228 jgs 559 const DataTagged::DataMapType& lookup=data.getTagLookup();
229 jfenwick 1802 const std::list<int> used=data.getFunctionSpace().getListOfTagsSTL();
230     for (std::list<int>::const_iterator i=used.begin();i!=used.end();++i)
231     {
232     int tag=*i;
233     if (tag==0) // check for the default tag
234     {
235     current_value=operation(current_value,DataMaths::reductionOp(vec,shape,data.getDefaultOffset(),operation,initial_value));
236     }
237     else
238     {
239     DataTagged::DataMapType::const_iterator it=lookup.find(tag);
240     if (it!=lookup.end())
241     {
242     current_value=operation(current_value,DataMaths::reductionOp(vec,shape,it->second,operation,initial_value));
243     }
244     }
245 jgs 82 }
246 jgs 147 return current_value;
247 jgs 82 }
248    
249 jgs 147 template <class BinaryFunction>
250 jgs 102 inline
251     double
252     algorithm(DataConstant& data,
253 jgs 147 BinaryFunction operation,
254     double initial_value)
255 jgs 82 {
256 jfenwick 1796 return DataMaths::reductionOp(data.getVector(),data.getShape(),0,operation,initial_value);
257     // return data.getPointDataView().reductionOp(operation,initial_value);
258 jgs 82 }
259    
260 jgs 102 /**
261     \brief
262 jgs 113 Perform the given data-point reduction operation on all data-points
263     in data, storing results in corresponding data-points of result.
264 jgs 82
265 jgs 102 Objects data and result must be of the same type, and have the same number
266 jgs 106 of data points, but where data has data points of rank n, result must have
267     data points of rank 0.
268 jgs 102
269 jgs 113 Calls DataArrayView::reductionOp
270 jgs 102 */
271 jgs 147 template <class BinaryFunction>
272 jgs 102 inline
273     void
274 jfenwick 1796 dp_algorithm(const DataExpanded& data,
275 jgs 106 DataExpanded& result,
276 jgs 147 BinaryFunction operation,
277     double initial_value)
278 jgs 102 {
279     int i,j;
280 jgs 121 int numSamples=data.getNumSamples();
281 jgs 113 int numDPPSample=data.getNumDPPSample();
282 jfenwick 1796 // DataArrayView dataView=data.getPointDataView();
283     // DataArrayView resultView=result.getPointDataView();
284     const DataTypes::ValueType& dataVec=data.getVector();
285     const DataTypes::ShapeType& shape=data.getShape();
286     DataTypes::ValueType& resultVec=result.getVector();
287 jgs 113 // perform the operation on each data-point and assign
288     // this to the corresponding element in result
289 jgs 147 #pragma omp parallel for private(i,j) schedule(static)
290 jgs 122 for (i=0;i<numSamples;i++) {
291     for (j=0;j<numDPPSample;j++) {
292 jfenwick 1796 /* resultView.getData(result.getPointOffset(i,j)) =
293     dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);*/
294     resultVec[result.getPointOffset(i,j)] =
295     DataMaths::reductionOp(dataVec, shape, data.getPointOffset(i,j),operation,initial_value);
296    
297 jgs 102 }
298     }
299     }
300    
301 jgs 147 template <class BinaryFunction>
302 jgs 102 inline
303     void
304 jfenwick 1796 dp_algorithm(const DataTagged& data,
305 jgs 106 DataTagged& result,
306 jgs 147 BinaryFunction operation,
307     double initial_value)
308 jgs 102 {
309 jgs 559 // perform the operation on each tagged value in data
310     // and assign this to the corresponding element in result
311 jfenwick 1796 const DataTypes::ShapeType& shape=data.getShape();
312     const DataTypes::ValueType& vec=data.getVector();
313 jgs 102 const DataTagged::DataMapType& lookup=data.getTagLookup();
314 jgs 559 for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
315 jfenwick 1796 // result.getDataPointByTag(i->first).getData(0) =
316     // data.getDataPointByTag(i->first).reductionOp(operation,initial_value);
317     result.getDataByTag(i->first,0) =
318     DataMaths::reductionOp(vec,shape,data.getOffsetForTag(i->first),operation,initial_value);
319 jgs 102 }
320 jgs 113 // perform the operation on the default data value
321 jgs 106 // and assign this to the default element in result
322 jfenwick 1796 // result.getDefaultValue().getData(0) =
323     // data.getDefaultValue().reductionOp(operation,initial_value);
324     result.getVector()[result.getDefaultOffset()] = DataMaths::reductionOp(data.getVector(),data.getShape(),data.getDefaultOffset(),operation,initial_value);
325 jgs 102 }
326    
327 jgs 147 template <class BinaryFunction>
328 jgs 102 inline
329     void
330 jgs 106 dp_algorithm(DataConstant& data,
331     DataConstant& result,
332 jgs 147 BinaryFunction operation,
333     double initial_value)
334 jgs 102 {
335 jgs 113 // perform the operation on the data value
336     // and assign this to the element in result
337 jfenwick 1796 // result.getPointDataView().getData(0) =
338     // data.getPointDataView().reductionOp(operation,initial_value);
339     result.getVector()[0] =
340     DataMaths::reductionOp(data.getVector(),data.getShape(),0,operation,initial_value);
341 jgs 102 }
342    
343 jgs 82 } // end of namespace
344 jgs 122
345 jgs 82 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26