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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 757 - (show annotations)
Mon Jun 26 13:12:56 2006 UTC (13 years, 2 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 // $Id$
2 /*
3 ************************************************************
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 */
13
14 #if !defined escript_DataAlgorithm_20040714_H
15 #define escript_DataAlgorithm_20040714_H
16 #include "system_dep.h"
17
18 #include "DataArrayView.h"
19 #include "DataExpanded.h"
20 #include "DataTagged.h"
21 #include "DataConstant.h"
22
23 #include <iostream>
24 #include <algorithm>
25
26 namespace escript {
27
28 /**
29 \brief
30 Adapt binary algorithms so they may be used in DataArrayView reduction operations.
31
32 Description:
33 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 */
38 template <class BinaryFunction>
39 class DataAlgorithmAdapter {
40 public:
41 DataAlgorithmAdapter(double initialValue):
42 m_initialValue(initialValue),
43 m_currentValue(initialValue)
44 {
45 }
46 DataAlgorithmAdapter(const DataAlgorithmAdapter& other):
47 m_initialValue(other.m_initialValue),
48 m_currentValue(other.m_initialValue),
49 operation(other.operation)
50 {
51 }
52 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 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 Return the minimum value of the two given values.
92 */
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 Return the absolute maximum value of the two given values.
104 */
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
113 /**
114 \brief
115 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 Return the length between the two given values.
128 */
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 Return the trace of the two given values.
140 */
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 Perform the given operation upon all values in all data-points in the
152 given Data object and return the final result.
153
154 Calls DataArrayView::reductionOp
155 */
156 template <class BinaryFunction>
157 inline
158 double
159 algorithm(DataExpanded& data,
160 BinaryFunction operation,
161 double initial_value)
162 {
163 int i,j;
164 int numDPPSample=data.getNumDPPSample();
165 int numSamples=data.getNumSamples();
166 double global_current_value=initial_value;
167 double local_current_value;
168 DataArrayView dataView=data.getPointDataView();
169 // calculate the reduction operation value for each data point
170 // 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 }
183 return global_current_value;
184 }
185
186 template <class BinaryFunction>
187 inline
188 double
189 algorithm(DataTagged& data,
190 BinaryFunction operation,
191 double initial_value)
192 {
193 double current_value=initial_value;
194 // perform the operation on each tagged value
195 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 current_value=operation(current_value,dataView.reductionOp(i->second,operation,initial_value));
199 }
200 // perform the operation on the default value
201 current_value=operation(current_value,data.getDefaultValue().reductionOp(operation,initial_value));
202 return current_value;
203 }
204
205 template <class BinaryFunction>
206 inline
207 double
208 algorithm(DataConstant& data,
209 BinaryFunction operation,
210 double initial_value)
211 {
212 return data.getPointDataView().reductionOp(operation,initial_value);
213 }
214
215 /**
216 \brief
217 Perform the given data-point reduction operation on all data-points
218 in data, storing results in corresponding data-points of result.
219
220 Objects data and result must be of the same type, and have the same number
221 of data points, but where data has data points of rank n, result must have
222 data points of rank 0.
223
224 Calls DataArrayView::reductionOp
225 */
226 template <class BinaryFunction>
227 inline
228 void
229 dp_algorithm(DataExpanded& data,
230 DataExpanded& result,
231 BinaryFunction operation,
232 double initial_value)
233 {
234 int i,j;
235 int numSamples=data.getNumSamples();
236 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 #pragma omp parallel for private(i,j) schedule(static)
242 for (i=0;i<numSamples;i++) {
243 for (j=0;j<numDPPSample;j++) {
244 resultView.getData(result.getPointOffset(i,j)) =
245 dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
246 }
247 }
248 }
249
250 template <class BinaryFunction>
251 inline
252 void
253 dp_algorithm(DataTagged& data,
254 DataTagged& result,
255 BinaryFunction operation,
256 double initial_value)
257 {
258 // perform the operation on each tagged value in data
259 // and assign this to the corresponding element in result
260 const DataTagged::DataMapType& lookup=data.getTagLookup();
261 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 }
265 // perform the operation on the default data value
266 // and assign this to the default element in result
267 result.getDefaultValue().getData(0) =
268 data.getDefaultValue().reductionOp(operation,initial_value);
269 }
270
271 template <class BinaryFunction>
272 inline
273 void
274 dp_algorithm(DataConstant& data,
275 DataConstant& result,
276 BinaryFunction operation,
277 double initial_value)
278 {
279 // perform the operation on the data value
280 // and assign this to the element in result
281 result.getPointDataView().getData(0) =
282 data.getPointDataView().reductionOp(operation,initial_value);
283 }
284
285 } // end of namespace
286
287 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26