/[escript]/branches/intelc_win32/escript/src/DataAlgorithm.h
ViewVC logotype

Contents of /branches/intelc_win32/escript/src/DataAlgorithm.h

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26