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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1802 - (hide annotations)
Tue Sep 23 01:03:29 2008 UTC (11 years, 2 months ago) by jfenwick
File MIME type: text/plain
File size: 9405 byte(s)
Added canTag methods to FunctionSpace and AbstractDomain (and its 
offspring).
This checks to see if the domain supports tags for the given type of 
function space.

Constructors for DataTagged now throw exceptions if you attempt to make 
a DataTagged with a FunctionSpace which does not support tags.
To allow the default constructor to work, NullDomain has a single 
functioncode which "supports" tagging.

Fixed a bug in DataTagged::toString and DataTypes::pointToString.

Added FunctionSpace::getListOfTagsSTL.

algorithm(DataTagged, BinaryFunction) in DataAlgorithm now only 
processes tags known to be in use.
This fixes mantis issue #0000186.

Added comment to Data.h intro warning about holding references if the 
underlying DataAbstract changes.

_python_ unit tests have been updated to test TaggedData with invalid 
FunctionSpaces and to give the correct answers to Lsup etc.


1 ksteube 1312
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 jgs 102 #if !defined escript_DataAlgorithm_20040714_H
17 jgs 82 #define escript_DataAlgorithm_20040714_H
18 woo409 757 #include "system_dep.h"
19 jgs 82
20 jgs 474 #include "DataExpanded.h"
21     #include "DataTagged.h"
22     #include "DataConstant.h"
23 jgs 82
24 jfenwick 1796 #include "DataMaths.h"
25    
26 jgs 82 #include <iostream>
27     #include <algorithm>
28 jfenwick 1802 #include <list>
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 jgs 148 double global_current_value=initial_value;
171     double local_current_value;
172 jfenwick 1796 // DataArrayView dataView=data.getPointDataView();
173     DataTypes::ValueType& vec=data.getVector();
174     const DataTypes::ShapeType& shape=data.getShape();
175 jgs 113 // calculate the reduction operation value for each data point
176 jgs 148 // 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 jfenwick 1796 /* 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 jgs 148 }
187     }
188     #pragma omp critical
189     global_current_value=operation(global_current_value,local_current_value);
190 jgs 82 }
191 jgs 148 return global_current_value;
192 jgs 82 }
193    
194 jfenwick 1802 // It is important that the algorithm only be applied to tags which are actually in use.
195 jgs 147 template <class BinaryFunction>
196 jgs 102 inline
197     double
198     algorithm(DataTagged& data,
199 jgs 147 BinaryFunction operation,
200     double initial_value)
201 jgs 82 {
202 jgs 148 double current_value=initial_value;
203 jfenwick 1796
204     DataTypes::ValueType& vec=data.getVector();
205     const DataTypes::ShapeType& shape=data.getShape();
206 jgs 559 const DataTagged::DataMapType& lookup=data.getTagLookup();
207 jfenwick 1802 const std::list<int> used=data.getFunctionSpace().getListOfTagsSTL();
208     for (std::list<int>::const_iterator i=used.begin();i!=used.end();++i)
209     {
210     int tag=*i;
211     if (tag==0) // check for the default tag
212     {
213     current_value=operation(current_value,DataMaths::reductionOp(vec,shape,data.getDefaultOffset(),operation,initial_value));
214     }
215     else
216     {
217     DataTagged::DataMapType::const_iterator it=lookup.find(tag);
218     if (it!=lookup.end())
219     {
220     current_value=operation(current_value,DataMaths::reductionOp(vec,shape,it->second,operation,initial_value));
221     }
222     }
223 jgs 82 }
224 jgs 147 return current_value;
225 jgs 82 }
226    
227 jgs 147 template <class BinaryFunction>
228 jgs 102 inline
229     double
230     algorithm(DataConstant& data,
231 jgs 147 BinaryFunction operation,
232     double initial_value)
233 jgs 82 {
234 jfenwick 1796 return DataMaths::reductionOp(data.getVector(),data.getShape(),0,operation,initial_value);
235     // return data.getPointDataView().reductionOp(operation,initial_value);
236 jgs 82 }
237    
238 jgs 102 /**
239     \brief
240 jgs 113 Perform the given data-point reduction operation on all data-points
241     in data, storing results in corresponding data-points of result.
242 jgs 82
243 jgs 102 Objects data and result must be of the same type, and have the same number
244 jgs 106 of data points, but where data has data points of rank n, result must have
245     data points of rank 0.
246 jgs 102
247 jgs 113 Calls DataArrayView::reductionOp
248 jgs 102 */
249 jgs 147 template <class BinaryFunction>
250 jgs 102 inline
251     void
252 jfenwick 1796 dp_algorithm(const DataExpanded& data,
253 jgs 106 DataExpanded& result,
254 jgs 147 BinaryFunction operation,
255     double initial_value)
256 jgs 102 {
257     int i,j;
258 jgs 121 int numSamples=data.getNumSamples();
259 jgs 113 int numDPPSample=data.getNumDPPSample();
260 jfenwick 1796 // DataArrayView dataView=data.getPointDataView();
261     // DataArrayView resultView=result.getPointDataView();
262     const DataTypes::ValueType& dataVec=data.getVector();
263     const DataTypes::ShapeType& shape=data.getShape();
264     DataTypes::ValueType& resultVec=result.getVector();
265 jgs 113 // perform the operation on each data-point and assign
266     // this to the corresponding element in result
267 jgs 147 #pragma omp parallel for private(i,j) schedule(static)
268 jgs 122 for (i=0;i<numSamples;i++) {
269     for (j=0;j<numDPPSample;j++) {
270 jfenwick 1796 /* resultView.getData(result.getPointOffset(i,j)) =
271     dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);*/
272     resultVec[result.getPointOffset(i,j)] =
273     DataMaths::reductionOp(dataVec, shape, data.getPointOffset(i,j),operation,initial_value);
274    
275 jgs 102 }
276     }
277     }
278    
279 jgs 147 template <class BinaryFunction>
280 jgs 102 inline
281     void
282 jfenwick 1796 dp_algorithm(const DataTagged& data,
283 jgs 106 DataTagged& result,
284 jgs 147 BinaryFunction operation,
285     double initial_value)
286 jgs 102 {
287 jgs 559 // perform the operation on each tagged value in data
288     // and assign this to the corresponding element in result
289 jfenwick 1796 const DataTypes::ShapeType& shape=data.getShape();
290     const DataTypes::ValueType& vec=data.getVector();
291 jgs 102 const DataTagged::DataMapType& lookup=data.getTagLookup();
292 jgs 559 for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
293 jfenwick 1796 // result.getDataPointByTag(i->first).getData(0) =
294     // data.getDataPointByTag(i->first).reductionOp(operation,initial_value);
295     result.getDataByTag(i->first,0) =
296     DataMaths::reductionOp(vec,shape,data.getOffsetForTag(i->first),operation,initial_value);
297 jgs 102 }
298 jgs 113 // perform the operation on the default data value
299 jgs 106 // and assign this to the default element in result
300 jfenwick 1796 // result.getDefaultValue().getData(0) =
301     // data.getDefaultValue().reductionOp(operation,initial_value);
302     result.getVector()[result.getDefaultOffset()] = DataMaths::reductionOp(data.getVector(),data.getShape(),data.getDefaultOffset(),operation,initial_value);
303 jgs 102 }
304    
305 jgs 147 template <class BinaryFunction>
306 jgs 102 inline
307     void
308 jgs 106 dp_algorithm(DataConstant& data,
309     DataConstant& result,
310 jgs 147 BinaryFunction operation,
311     double initial_value)
312 jgs 102 {
313 jgs 113 // perform the operation on the data value
314     // and assign this to the element in result
315 jfenwick 1796 // result.getPointDataView().getData(0) =
316     // data.getPointDataView().reductionOp(operation,initial_value);
317     result.getVector()[0] =
318     DataMaths::reductionOp(data.getVector(),data.getShape(),0,operation,initial_value);
319 jgs 102 }
320    
321 jgs 82 } // end of namespace
322 jgs 122
323 jgs 82 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26