16 |
#if !defined escript_DataAlgorithm_20040714_H |
#if !defined escript_DataAlgorithm_20040714_H |
17 |
#define escript_DataAlgorithm_20040714_H |
#define escript_DataAlgorithm_20040714_H |
18 |
|
|
19 |
#include "escript/Data/DataExpanded.h" |
#include "DataExpanded.h" |
20 |
#include "escript/Data/DataTagged.h" |
#include "DataTagged.h" |
21 |
#include "escript/Data/DataConstant.h" |
#include "DataConstant.h" |
22 |
#include "escript/Data/DataArrayView.h" |
#include "DataArrayView.h" |
23 |
|
|
24 |
#include <iostream> |
#include <iostream> |
25 |
#include <algorithm> |
#include <algorithm> |
47 |
m_currentValue(initialValue) |
m_currentValue(initialValue) |
48 |
{ |
{ |
49 |
} |
} |
50 |
|
DataAlgorithmAdapter(const DataAlgorithmAdapter& other): |
51 |
|
m_initialValue(other.m_initialValue), |
52 |
|
m_currentValue(other.m_initialValue), |
53 |
|
operation(other.operation) |
54 |
|
{ |
55 |
|
} |
56 |
inline void operator()(double value) |
inline void operator()(double value) |
57 |
{ |
{ |
58 |
m_currentValue=operation(m_currentValue,value); |
m_currentValue=operation(m_currentValue,value); |
116 |
|
|
117 |
/** |
/** |
118 |
\brief |
\brief |
119 |
|
Return the absolute minimum value of the two given values. |
120 |
|
*/ |
121 |
|
struct AbsMin : public std::binary_function<double,double,double> |
122 |
|
{ |
123 |
|
inline double operator()(double x, double y) const |
124 |
|
{ |
125 |
|
return std::min(fabs(x),fabs(y)); |
126 |
|
} |
127 |
|
}; |
128 |
|
|
129 |
|
/** |
130 |
|
\brief |
131 |
Return the length between the two given values. |
Return the length between the two given values. |
132 |
*/ |
*/ |
133 |
struct Length : public std::binary_function<double,double,double> |
struct Length : public std::binary_function<double,double,double> |
157 |
|
|
158 |
Calls DataArrayView::reductionOp |
Calls DataArrayView::reductionOp |
159 |
*/ |
*/ |
160 |
template <class UnaryFunction> |
template <class BinaryFunction> |
161 |
inline |
inline |
162 |
double |
double |
163 |
algorithm(DataExpanded& data, |
algorithm(DataExpanded& data, |
164 |
UnaryFunction operation) |
BinaryFunction operation, |
165 |
|
double initial_value) |
166 |
{ |
{ |
167 |
int i,j; |
int i,j; |
168 |
int numDPPSample=data.getNumDPPSample(); |
int numDPPSample=data.getNumDPPSample(); |
169 |
int numSamples=data.getNumSamples(); |
int numSamples=data.getNumSamples(); |
170 |
int resultVectorLength=numDPPSample*numSamples; |
double global_current_value=initial_value; |
171 |
std::vector<double> resultVector(resultVectorLength); |
double local_current_value; |
172 |
DataArrayView dataView=data.getPointDataView(); |
DataArrayView dataView=data.getPointDataView(); |
173 |
// calculate the reduction operation value for each data point |
// calculate the reduction operation value for each data point |
174 |
// storing the result for each data-point in successive entries |
// reducing the result for each data-point into the current_value variables |
175 |
// in resultVector |
#pragma omp parallel private(local_current_value) |
176 |
{ |
{ |
177 |
#pragma omp for private(i,j) schedule(static) |
local_current_value=initial_value; |
178 |
for (i=0;i<numSamples;i++) { |
#pragma omp for private(i,j) schedule(static) |
179 |
for (j=0;j<numDPPSample;j++) { |
for (i=0;i<numSamples;i++) { |
180 |
#pragma omp critical (reductionOp) |
for (j=0;j<numDPPSample;j++) { |
181 |
resultVector[j*numSamples+i]=dataView.reductionOp(data.getPointOffset(i,j),operation); |
local_current_value=operation(local_current_value,dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value)); |
182 |
|
} |
183 |
} |
} |
184 |
} |
#pragma omp critical |
185 |
|
global_current_value=operation(global_current_value,local_current_value); |
186 |
} |
} |
187 |
// now calculate the reduction operation value across the results |
return global_current_value; |
|
// for each data-point |
|
|
operation.resetResult(); |
|
|
for (int l=0;l<resultVectorLength;l++) { |
|
|
operation(resultVector[l]); |
|
|
} |
|
|
return operation.getResult(); |
|
188 |
} |
} |
189 |
|
|
190 |
template <class UnaryFunction> |
template <class BinaryFunction> |
191 |
inline |
inline |
192 |
double |
double |
193 |
algorithm(DataTagged& data, |
algorithm(DataTagged& data, |
194 |
UnaryFunction operation) |
BinaryFunction operation, |
195 |
|
double initial_value) |
196 |
{ |
{ |
197 |
const DataTagged::DataMapType& lookup=data.getTagLookup(); |
const DataTagged::DataMapType& lookup=data.getTagLookup(); |
198 |
DataTagged::DataMapType::const_iterator i; |
DataTagged::DataMapType::const_iterator i; |
199 |
DataTagged::DataMapType::const_iterator lookupEnd=lookup.end(); |
DataTagged::DataMapType::const_iterator lookupEnd=lookup.end(); |
200 |
DataArrayView& dataView=data.getPointDataView(); |
DataArrayView& dataView=data.getPointDataView(); |
201 |
std::vector<double> resultVector; |
double current_value=initial_value; |
|
int resultVectorLength; |
|
202 |
// perform the operation on each tagged value |
// perform the operation on each tagged value |
203 |
for (i=lookup.begin();i!=lookupEnd;i++) { |
for (i=lookup.begin();i!=lookupEnd;i++) { |
204 |
resultVector.push_back(dataView.reductionOp(i->second,operation)); |
current_value=operation(current_value,dataView.reductionOp(i->second,operation,initial_value)); |
205 |
} |
} |
206 |
// perform the operation on the default value |
// perform the operation on the default value |
207 |
resultVector.push_back(data.getDefaultValue().reductionOp(operation)); |
current_value=operation(current_value,data.getDefaultValue().reductionOp(operation,initial_value)); |
208 |
// now calculate the reduction operation value across the results |
return current_value; |
|
// for each tagged value |
|
|
resultVectorLength=resultVector.size(); |
|
|
operation.resetResult(); |
|
|
for (int l=0;l<resultVectorLength;l++) { |
|
|
operation(resultVector[l]); |
|
|
} |
|
|
return operation.getResult(); |
|
209 |
} |
} |
210 |
|
|
211 |
template <class UnaryFunction> |
template <class BinaryFunction> |
212 |
inline |
inline |
213 |
double |
double |
214 |
algorithm(DataConstant& data, |
algorithm(DataConstant& data, |
215 |
UnaryFunction operation) |
BinaryFunction operation, |
216 |
|
double initial_value) |
217 |
{ |
{ |
218 |
return data.getPointDataView().reductionOp(operation); |
return data.getPointDataView().reductionOp(operation,initial_value); |
219 |
} |
} |
220 |
|
|
221 |
/** |
/** |
229 |
|
|
230 |
Calls DataArrayView::reductionOp |
Calls DataArrayView::reductionOp |
231 |
*/ |
*/ |
232 |
template <class UnaryFunction> |
template <class BinaryFunction> |
233 |
inline |
inline |
234 |
void |
void |
235 |
dp_algorithm(DataExpanded& data, |
dp_algorithm(DataExpanded& data, |
236 |
DataExpanded& result, |
DataExpanded& result, |
237 |
UnaryFunction operation) |
BinaryFunction operation, |
238 |
|
double initial_value) |
239 |
{ |
{ |
240 |
int i,j; |
int i,j; |
|
int numDPPSample=data.getNumDPPSample(); |
|
241 |
int numSamples=data.getNumSamples(); |
int numSamples=data.getNumSamples(); |
242 |
|
int numDPPSample=data.getNumDPPSample(); |
243 |
DataArrayView dataView=data.getPointDataView(); |
DataArrayView dataView=data.getPointDataView(); |
244 |
DataArrayView resultView=result.getPointDataView(); |
DataArrayView resultView=result.getPointDataView(); |
245 |
// perform the operation on each data-point and assign |
// perform the operation on each data-point and assign |
246 |
// this to the corresponding element in result |
// this to the corresponding element in result |
247 |
{ |
#pragma omp parallel for private(i,j) schedule(static) |
248 |
#pragma omp for private(i,j) schedule(static) |
for (i=0;i<numSamples;i++) { |
249 |
for (i=0;i<numSamples;i++) { |
for (j=0;j<numDPPSample;j++) { |
250 |
for (j=0;j<numDPPSample;j++) { |
resultView.getData(result.getPointOffset(i,j)) = |
251 |
#pragma omp critical (reductionOp) |
dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value); |
|
resultView.getData(data.getPointOffset(i,j)) = |
|
|
dataView.reductionOp(data.getPointOffset(i,j),operation); |
|
|
} |
|
252 |
} |
} |
253 |
} |
} |
254 |
} |
} |
255 |
|
|
256 |
template <class UnaryFunction> |
template <class BinaryFunction> |
257 |
inline |
inline |
258 |
void |
void |
259 |
dp_algorithm(DataTagged& data, |
dp_algorithm(DataTagged& data, |
260 |
DataTagged& result, |
DataTagged& result, |
261 |
UnaryFunction operation) |
BinaryFunction operation, |
262 |
|
double initial_value) |
263 |
{ |
{ |
264 |
const DataTagged::DataMapType& lookup=data.getTagLookup(); |
const DataTagged::DataMapType& lookup=data.getTagLookup(); |
265 |
DataTagged::DataMapType::const_iterator i; |
DataTagged::DataMapType::const_iterator i; |
270 |
// and assign this to the corresponding element in result |
// and assign this to the corresponding element in result |
271 |
for (i=lookup.begin();i!=lookupEnd;i++) { |
for (i=lookup.begin();i!=lookupEnd;i++) { |
272 |
resultView.getData(i->second) = |
resultView.getData(i->second) = |
273 |
dataView.reductionOp(i->second,operation); |
dataView.reductionOp(i->second,operation,initial_value); |
274 |
} |
} |
275 |
// perform the operation on the default data value |
// perform the operation on the default data value |
276 |
// and assign this to the default element in result |
// and assign this to the default element in result |
277 |
resultView.getData(0) = |
resultView.getData(0) = |
278 |
data.getDefaultValue().reductionOp(operation); |
data.getDefaultValue().reductionOp(operation,initial_value); |
279 |
} |
} |
280 |
|
|
281 |
template <class UnaryFunction> |
template <class BinaryFunction> |
282 |
inline |
inline |
283 |
void |
void |
284 |
dp_algorithm(DataConstant& data, |
dp_algorithm(DataConstant& data, |
285 |
DataConstant& result, |
DataConstant& result, |
286 |
UnaryFunction operation) |
BinaryFunction operation, |
287 |
|
double initial_value) |
288 |
{ |
{ |
289 |
// perform the operation on the data value |
// perform the operation on the data value |
290 |
// and assign this to the element in result |
// and assign this to the element in result |
291 |
result.getPointDataView().getData(0) = |
result.getPointDataView().getData(0) = |
292 |
data.getPointDataView().reductionOp(operation); |
data.getPointDataView().reductionOp(operation,initial_value); |
293 |
} |
} |
294 |
|
|
295 |
} // end of namespace |
} // end of namespace |
296 |
|
|
297 |
#endif |
#endif |