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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26