1 |
jgs |
97 |
// $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 |
97 |
#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 |
97 |
|
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 |
97 |
|
76 |
jgs |
82 |
/** |
77 |
|
|
\brief |
78 |
|
|
Adapt algorithms so they may be used by Data. |
79 |
|
|
|
80 |
|
|
Description: |
81 |
|
|
Adapt algorithms so they may be used by Data. The functor |
82 |
|
|
maintains state, ie the currentValue retuned by the operation. |
83 |
|
|
*/ |
84 |
|
|
template <class BinaryFunction> |
85 |
|
|
class DataAlgorithmAdapter { |
86 |
|
|
public: |
87 |
|
|
DataAlgorithmAdapter(double initialValue): |
88 |
|
|
m_currentValue(initialValue) |
89 |
|
|
{} |
90 |
|
|
inline void operator()(double value) |
91 |
|
|
{ |
92 |
|
|
m_currentValue=operation(m_currentValue,value); |
93 |
|
|
return; |
94 |
|
|
} |
95 |
|
|
inline double getResult() const |
96 |
|
|
{ |
97 |
|
|
return m_currentValue; |
98 |
|
|
} |
99 |
|
|
private: |
100 |
|
|
// |
101 |
jgs |
97 |
// the current operation value |
102 |
jgs |
82 |
double m_currentValue; |
103 |
|
|
// |
104 |
|
|
// The operation to perform |
105 |
|
|
BinaryFunction operation; |
106 |
|
|
}; |
107 |
|
|
|
108 |
|
|
/** |
109 |
|
|
\brief |
110 |
jgs |
97 |
Perform the given operation upon all Data elements and return a single |
111 |
jgs |
82 |
result. |
112 |
|
|
|
113 |
|
|
Description: |
114 |
jgs |
97 |
Perform the given operation upon all Data elements and return a single |
115 |
jgs |
82 |
result. |
116 |
|
|
*/ |
117 |
|
|
template <class UnaryFunction> |
118 |
jgs |
97 |
inline |
119 |
|
|
double |
120 |
|
|
algorithm(DataExpanded& data, |
121 |
|
|
UnaryFunction operation) |
122 |
jgs |
82 |
{ |
123 |
|
|
int i,j; |
124 |
|
|
DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample(); |
125 |
|
|
DataArrayView::ValueType::size_type numSamples=data.getNumSamples(); |
126 |
jgs |
97 |
double resultLocal=0; |
127 |
|
|
#pragma omp parallel private(resultLocal) |
128 |
jgs |
82 |
{ |
129 |
jgs |
97 |
#pragma omp for private(i,j) schedule(static) |
130 |
|
|
for (i=0;i<numSamples;i++) { |
131 |
|
|
for (j=0;j<numDPPSample;j++) { |
132 |
|
|
resultLocal=data.getPointDataView().algorithm(data.getPointOffset(i,j), operation); |
133 |
|
|
#pragma omp critical (algorithm) |
134 |
jgs |
82 |
operation(resultLocal); |
135 |
|
|
} |
136 |
|
|
} |
137 |
|
|
} |
138 |
|
|
return operation.getResult(); |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
template <class UnaryFunction> |
142 |
jgs |
97 |
inline |
143 |
|
|
double |
144 |
|
|
algorithm(DataTagged& data, |
145 |
|
|
UnaryFunction operation) |
146 |
jgs |
82 |
{ |
147 |
|
|
// |
148 |
jgs |
97 |
// perform the operation on each tagged value |
149 |
jgs |
82 |
const DataTagged::DataMapType& lookup=data.getTagLookup(); |
150 |
|
|
DataTagged::DataMapType::const_iterator i; |
151 |
|
|
DataTagged::DataMapType::const_iterator lookupEnd=lookup.end(); |
152 |
|
|
DataArrayView& dataView=data.getPointDataView(); |
153 |
jgs |
97 |
for (i=lookup.begin();i!=lookupEnd;i++) { |
154 |
jgs |
82 |
operation(dataView.algorithm(i->second,operation)); |
155 |
|
|
} |
156 |
|
|
// |
157 |
|
|
// finally perform the operation on the default value |
158 |
|
|
operation(data.getDefaultValue().algorithm(operation)); |
159 |
|
|
return operation.getResult(); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
template <class UnaryFunction> |
163 |
jgs |
97 |
inline |
164 |
|
|
double |
165 |
|
|
algorithm(DataConstant& data, |
166 |
|
|
UnaryFunction operation) |
167 |
jgs |
82 |
{ |
168 |
|
|
return data.getPointDataView().algorithm(operation); |
169 |
|
|
} |
170 |
|
|
|
171 |
jgs |
97 |
/** |
172 |
|
|
\brief |
173 |
|
|
Perform the given data point reduction operation upon all data points |
174 |
|
|
in data, storing results in corresponding elements of result. |
175 |
jgs |
82 |
|
176 |
jgs |
97 |
Objects data and result must be of the same type, and have the same number |
177 |
|
|
of samples and number of data points per sample, but where data has data |
178 |
|
|
points of rank n, result must have data points of rank 0. |
179 |
|
|
|
180 |
|
|
Calls DataArrayView::dp_algorithm. |
181 |
|
|
*/ |
182 |
|
|
template <class UnaryFunction> |
183 |
|
|
inline |
184 |
|
|
void |
185 |
|
|
dp_algorithm(DataExpanded& result, |
186 |
|
|
DataExpanded& data, |
187 |
|
|
UnaryFunction operation) |
188 |
|
|
{ |
189 |
|
|
int i,j; |
190 |
|
|
DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample(); |
191 |
|
|
DataArrayView::ValueType::size_type numSamples=data.getNumSamples(); |
192 |
|
|
{ |
193 |
|
|
#pragma omp for private(i,j) schedule(static) |
194 |
|
|
for (i=0;i<numSamples;i++) { |
195 |
|
|
for (j=0;j<numDPPSample;j++) { |
196 |
|
|
// assign this to corresponding element in result |
197 |
|
|
data.getPointDataView().dp_algorithm(data.getPointOffset(i,j),operation); |
198 |
|
|
#pragma omp critical (dp_algorithm) |
199 |
|
|
} |
200 |
|
|
} |
201 |
|
|
} |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
template <class UnaryFunction> |
205 |
|
|
inline |
206 |
|
|
void |
207 |
|
|
dp_algorithm(DataTagged& result, |
208 |
|
|
DataTagged& data, |
209 |
|
|
UnaryFunction operation) |
210 |
|
|
{ |
211 |
|
|
// |
212 |
|
|
// perform the operation on each tagged value |
213 |
|
|
const DataTagged::DataMapType& lookup=data.getTagLookup(); |
214 |
|
|
DataTagged::DataMapType::const_iterator i; |
215 |
|
|
DataTagged::DataMapType::const_iterator lookupEnd=lookup.end(); |
216 |
|
|
for (i=lookup.begin();i!=lookupEnd;i++) { |
217 |
|
|
// assign this to corresponding element in result |
218 |
|
|
data.getPointDataView().dp_algorithm(i->second,operation); |
219 |
|
|
} |
220 |
|
|
// |
221 |
|
|
// finally perform the operation on the default value |
222 |
|
|
// assign this to corresponding element in result |
223 |
|
|
data.getDefaultValue().dp_algorithm(operation); |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
template <class UnaryFunction> |
227 |
|
|
inline |
228 |
|
|
void |
229 |
|
|
dp_algorithm(DataConstant& result, |
230 |
|
|
DataConstant& data, |
231 |
|
|
UnaryFunction operation) |
232 |
|
|
{ |
233 |
|
|
//result.getPointDataView().getData() |
234 |
|
|
// assign this to corresponding element in result |
235 |
|
|
data.getPointDataView().dp_algorithm(operation); |
236 |
|
|
} |
237 |
|
|
|
238 |
jgs |
82 |
} // end of namespace |
239 |
|
|
#endif |