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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26