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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 757 - (hide annotations)
Mon Jun 26 13:12:56 2006 UTC (13 years, 4 months ago) by woo409
File MIME type: text/plain
File size: 8080 byte(s)
+ Merge of intelc_win32 branch (revision 741:755) with trunk. Tested on iVEC altix (run_tests and py_tests all pass)

1 jgs 102 // $Id$
2 jgs 82 /*
3 elspeth 615 ************************************************************
4     * Copyright 2006 by ACcESS MNRF *
5     * *
6     * http://www.access.edu.au *
7     * Primary Business: Queensland, Australia *
8     * Licensed under the Open Software License version 3.0 *
9     * http://www.opensource.org/licenses/osl-3.0.php *
10     * *
11     ************************************************************
12 jgs 82 */
13    
14 jgs 102 #if !defined escript_DataAlgorithm_20040714_H
15 jgs 82 #define escript_DataAlgorithm_20040714_H
16 woo409 757 #include "system_dep.h"
17 jgs 82
18 jgs 478 #include "DataArrayView.h"
19 jgs 474 #include "DataExpanded.h"
20     #include "DataTagged.h"
21     #include "DataConstant.h"
22 jgs 82
23     #include <iostream>
24     #include <algorithm>
25    
26     namespace escript {
27 jgs 102
28 jgs 82 /**
29     \brief
30 jgs 113 Adapt binary algorithms so they may be used in DataArrayView reduction operations.
31 jgs 82
32     Description:
33 jgs 113 This functor adapts the given BinaryFunction operation by starting with the
34     given inital value applying this operation to successive values, storing the
35     rolling result in m_currentValue - which can be accessed or reset by getResult
36     and resetResult respectively.
37 jgs 82 */
38 jgs 113 template <class BinaryFunction>
39     class DataAlgorithmAdapter {
40     public:
41     DataAlgorithmAdapter(double initialValue):
42     m_initialValue(initialValue),
43     m_currentValue(initialValue)
44     {
45     }
46 jgs 147 DataAlgorithmAdapter(const DataAlgorithmAdapter& other):
47     m_initialValue(other.m_initialValue),
48     m_currentValue(other.m_initialValue),
49     operation(other.operation)
50     {
51     }
52 jgs 113 inline void operator()(double value)
53     {
54     m_currentValue=operation(m_currentValue,value);
55     return;
56     }
57     inline void resetResult()
58     {
59     m_currentValue=m_initialValue;
60     }
61     inline double getResult() const
62     {
63     return m_currentValue;
64     }
65     private:
66     //
67     // the initial operation value
68     double m_initialValue;
69     //
70     // the current operation value
71     double m_currentValue;
72     //
73     // The operation to perform
74     BinaryFunction operation;
75     };
76    
77     /**
78     \brief
79     Return the maximum value of the two given values.
80     */
81 jgs 82 struct FMax : public std::binary_function<double,double,double>
82     {
83     inline double operator()(double x, double y) const
84     {
85     return std::max(x,y);
86     }
87     };
88    
89     /**
90     \brief
91 jgs 113 Return the minimum value of the two given values.
92 jgs 82 */
93     struct FMin : public std::binary_function<double,double,double>
94     {
95     inline double operator()(double x, double y) const
96     {
97     return std::min(x,y);
98     }
99     };
100    
101     /**
102     \brief
103 jgs 113 Return the absolute maximum value of the two given values.
104 jgs 82 */
105     struct AbsMax : public std::binary_function<double,double,double>
106     {
107     inline double operator()(double x, double y) const
108     {
109     return std::max(fabs(x),fabs(y));
110     }
111     };
112 jgs 102
113 jgs 82 /**
114     \brief
115 jgs 117 Return the absolute minimum value of the two given values.
116     */
117     struct AbsMin : public std::binary_function<double,double,double>
118     {
119     inline double operator()(double x, double y) const
120     {
121     return std::min(fabs(x),fabs(y));
122     }
123     };
124    
125     /**
126     \brief
127 jgs 113 Return the length between the two given values.
128 jgs 106 */
129     struct Length : public std::binary_function<double,double,double>
130     {
131     inline double operator()(double x, double y) const
132     {
133     return std::sqrt(std::pow(x,2)+std::pow(y,2));
134     }
135     };
136    
137     /**
138     \brief
139 jgs 113 Return the trace of the two given values.
140 jgs 106 */
141     struct Trace : public std::binary_function<double,double,double>
142     {
143     inline double operator()(double x, double y) const
144     {
145     return x+y;
146     }
147     };
148    
149     /**
150     \brief
151 jgs 113 Perform the given operation upon all values in all data-points in the
152     given Data object and return the final result.
153 jgs 82
154 jgs 113 Calls DataArrayView::reductionOp
155 jgs 82 */
156 jgs 147 template <class BinaryFunction>
157 jgs 102 inline
158     double
159     algorithm(DataExpanded& data,
160 jgs 147 BinaryFunction operation,
161     double initial_value)
162 jgs 82 {
163     int i,j;
164 jgs 113 int numDPPSample=data.getNumDPPSample();
165     int numSamples=data.getNumSamples();
166 jgs 148 double global_current_value=initial_value;
167     double local_current_value;
168 jgs 113 DataArrayView dataView=data.getPointDataView();
169     // calculate the reduction operation value for each data point
170 jgs 148 // reducing the result for each data-point into the current_value variables
171     #pragma omp parallel private(local_current_value)
172     {
173     local_current_value=initial_value;
174     #pragma omp for private(i,j) schedule(static)
175     for (i=0;i<numSamples;i++) {
176     for (j=0;j<numDPPSample;j++) {
177     local_current_value=operation(local_current_value,dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value));
178     }
179     }
180     #pragma omp critical
181     global_current_value=operation(global_current_value,local_current_value);
182 jgs 82 }
183 jgs 148 return global_current_value;
184 jgs 82 }
185    
186 jgs 147 template <class BinaryFunction>
187 jgs 102 inline
188     double
189     algorithm(DataTagged& data,
190 jgs 147 BinaryFunction operation,
191     double initial_value)
192 jgs 82 {
193 jgs 148 double current_value=initial_value;
194 jgs 113 // perform the operation on each tagged value
195 jgs 559 DataArrayView& dataView=data.getPointDataView();
196     const DataTagged::DataMapType& lookup=data.getTagLookup();
197     for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
198 jgs 148 current_value=operation(current_value,dataView.reductionOp(i->second,operation,initial_value));
199 jgs 82 }
200 jgs 113 // perform the operation on the default value
201 jgs 148 current_value=operation(current_value,data.getDefaultValue().reductionOp(operation,initial_value));
202 jgs 147 return current_value;
203 jgs 82 }
204    
205 jgs 147 template <class BinaryFunction>
206 jgs 102 inline
207     double
208     algorithm(DataConstant& data,
209 jgs 147 BinaryFunction operation,
210     double initial_value)
211 jgs 82 {
212 jgs 147 return data.getPointDataView().reductionOp(operation,initial_value);
213 jgs 82 }
214    
215 jgs 102 /**
216     \brief
217 jgs 113 Perform the given data-point reduction operation on all data-points
218     in data, storing results in corresponding data-points of result.
219 jgs 82
220 jgs 102 Objects data and result must be of the same type, and have the same number
221 jgs 106 of data points, but where data has data points of rank n, result must have
222     data points of rank 0.
223 jgs 102
224 jgs 113 Calls DataArrayView::reductionOp
225 jgs 102 */
226 jgs 147 template <class BinaryFunction>
227 jgs 102 inline
228     void
229 jgs 106 dp_algorithm(DataExpanded& data,
230     DataExpanded& result,
231 jgs 147 BinaryFunction operation,
232     double initial_value)
233 jgs 102 {
234     int i,j;
235 jgs 121 int numSamples=data.getNumSamples();
236 jgs 113 int numDPPSample=data.getNumDPPSample();
237     DataArrayView dataView=data.getPointDataView();
238     DataArrayView resultView=result.getPointDataView();
239     // perform the operation on each data-point and assign
240     // this to the corresponding element in result
241 jgs 147 #pragma omp parallel for private(i,j) schedule(static)
242 jgs 122 for (i=0;i<numSamples;i++) {
243     for (j=0;j<numDPPSample;j++) {
244     resultView.getData(result.getPointOffset(i,j)) =
245 jgs 147 dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
246 jgs 102 }
247     }
248     }
249    
250 jgs 147 template <class BinaryFunction>
251 jgs 102 inline
252     void
253 jgs 106 dp_algorithm(DataTagged& data,
254     DataTagged& result,
255 jgs 147 BinaryFunction operation,
256     double initial_value)
257 jgs 102 {
258 jgs 559 // perform the operation on each tagged value in data
259     // and assign this to the corresponding element in result
260 jgs 102 const DataTagged::DataMapType& lookup=data.getTagLookup();
261 jgs 559 for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
262     result.getDataPointByTag(i->first).getData(0) =
263     data.getDataPointByTag(i->first).reductionOp(operation,initial_value);
264 jgs 102 }
265 jgs 113 // perform the operation on the default data value
266 jgs 106 // and assign this to the default element in result
267 jgs 559 result.getDefaultValue().getData(0) =
268 jgs 147 data.getDefaultValue().reductionOp(operation,initial_value);
269 jgs 102 }
270    
271 jgs 147 template <class BinaryFunction>
272 jgs 102 inline
273     void
274 jgs 106 dp_algorithm(DataConstant& data,
275     DataConstant& result,
276 jgs 147 BinaryFunction operation,
277     double initial_value)
278 jgs 102 {
279 jgs 113 // perform the operation on the data value
280     // and assign this to the element in result
281 jgs 106 result.getPointDataView().getData(0) =
282 jgs 147 data.getPointDataView().reductionOp(operation,initial_value);
283 jgs 102 }
284    
285 jgs 82 } // end of namespace
286 jgs 122
287 jgs 82 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26