1 |
/* |
2 |
****************************************************************************** |
3 |
* * |
4 |
* COPYRIGHT ACcESS 2004 - All Rights Reserved * |
5 |
* * |
6 |
* This software is the property of ACcESS. No part of this code * |
7 |
* may be copied in any form or by any means without the expressed written * |
8 |
* consent of ACcESS. Copying, use or modification of this software * |
9 |
* by any unauthorised person is illegal unless that person has a software * |
10 |
* license agreement with ACcESS. * |
11 |
* * |
12 |
****************************************************************************** |
13 |
*/ |
14 |
|
15 |
#if !defined escript_DataAlgorithm_20040714_H |
16 |
#define escript_DataAlgorithm_20040714_H |
17 |
|
18 |
#include "escript/Data/DataExpanded.h" |
19 |
#include "escript/Data/DataTagged.h" |
20 |
#include "escript/Data/DataConstant.h" |
21 |
#include "escript/Data/DataArrayView.h" |
22 |
|
23 |
#include <iostream> |
24 |
#include <algorithm> |
25 |
#include <math.h> |
26 |
#include <limits> |
27 |
|
28 |
namespace escript { |
29 |
/** |
30 |
\brief |
31 |
Return the maximum value. |
32 |
|
33 |
Description: |
34 |
Return the maximum value. |
35 |
*/ |
36 |
struct FMax : public std::binary_function<double,double,double> |
37 |
{ |
38 |
inline double operator()(double x, double y) const |
39 |
{ |
40 |
return std::max(x,y); |
41 |
} |
42 |
}; |
43 |
|
44 |
/** |
45 |
\brief |
46 |
Return the minimum value. |
47 |
|
48 |
Description: |
49 |
Return the minimum value. |
50 |
*/ |
51 |
struct FMin : public std::binary_function<double,double,double> |
52 |
{ |
53 |
inline double operator()(double x, double y) const |
54 |
{ |
55 |
return std::min(x,y); |
56 |
} |
57 |
}; |
58 |
|
59 |
/** |
60 |
\brief |
61 |
Return the absolute maximum value. |
62 |
|
63 |
Description: |
64 |
Return the absolute maximum value. |
65 |
*/ |
66 |
struct AbsMax : public std::binary_function<double,double,double> |
67 |
{ |
68 |
inline double operator()(double x, double y) const |
69 |
{ |
70 |
return std::max(fabs(x),fabs(y)); |
71 |
} |
72 |
}; |
73 |
/** |
74 |
\brief |
75 |
Adapt algorithms so they may be used by Data. |
76 |
|
77 |
Description: |
78 |
Adapt algorithms so they may be used by Data. The functor |
79 |
maintains state, ie the currentValue retuned by the operation. |
80 |
*/ |
81 |
template <class BinaryFunction> |
82 |
class DataAlgorithmAdapter { |
83 |
public: |
84 |
DataAlgorithmAdapter(double initialValue): |
85 |
m_currentValue(initialValue) |
86 |
{} |
87 |
inline void operator()(double value) |
88 |
{ |
89 |
m_currentValue=operation(m_currentValue,value); |
90 |
return; |
91 |
} |
92 |
inline double getResult() const |
93 |
{ |
94 |
return m_currentValue; |
95 |
} |
96 |
private: |
97 |
// |
98 |
// the current maximum value |
99 |
double m_currentValue; |
100 |
// |
101 |
// The operation to perform |
102 |
BinaryFunction operation; |
103 |
}; |
104 |
|
105 |
/** |
106 |
\brief |
107 |
Perform the given operation upon all DataElements and return a single |
108 |
result. |
109 |
|
110 |
Description: |
111 |
Perform the given operation upon all DataElements and return a single |
112 |
result. |
113 |
*/ |
114 |
template <class UnaryFunction> |
115 |
inline double algorithm(DataExpanded& data, UnaryFunction operation) |
116 |
{ |
117 |
int i,j; |
118 |
DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample(); |
119 |
DataArrayView::ValueType::size_type numSamples=data.getNumSamples(); |
120 |
double resultLocal; |
121 |
#pragma omp parallel private(resultLocal) |
122 |
{ |
123 |
#pragma omp parallel for private(i,j) schedule(static) |
124 |
for (i=0;i<numSamples;++i) { |
125 |
for (j=0;j<numDPPSample;++j) { |
126 |
resultLocal=data.getPointDataView().algorithm(data.getPointOffset(i,j), |
127 |
operation); |
128 |
#pragma omp critical (algorithm) |
129 |
operation(resultLocal); |
130 |
} |
131 |
} |
132 |
} |
133 |
return operation.getResult(); |
134 |
} |
135 |
|
136 |
template <class UnaryFunction> |
137 |
inline double algorithm(DataTagged& data, UnaryFunction operation) |
138 |
{ |
139 |
// |
140 |
// perform the operation on each tagged value including the default |
141 |
const DataTagged::DataMapType& lookup=data.getTagLookup(); |
142 |
DataTagged::DataMapType::const_iterator i; |
143 |
DataTagged::DataMapType::const_iterator lookupEnd=lookup.end(); |
144 |
DataArrayView& dataView=data.getPointDataView(); |
145 |
for (i=lookup.begin();i!=lookupEnd;++i) { |
146 |
operation(dataView.algorithm(i->second,operation)); |
147 |
} |
148 |
// |
149 |
// finally perform the operation on the default value |
150 |
operation(data.getDefaultValue().algorithm(operation)); |
151 |
return operation.getResult(); |
152 |
} |
153 |
|
154 |
template <class UnaryFunction> |
155 |
inline double algorithm(DataConstant& data, UnaryFunction operation) |
156 |
{ |
157 |
return data.getPointDataView().algorithm(operation); |
158 |
} |
159 |
|
160 |
|
161 |
} // end of namespace |
162 |
#endif |