1 |
|
2 |
/******************************************************* |
3 |
* |
4 |
* Copyright (c) 2003-2008 by University of Queensland |
5 |
* Earth Systems Science Computational Center (ESSCC) |
6 |
* http://www.uq.edu.au/esscc |
7 |
* |
8 |
* Primary Business: Queensland, Australia |
9 |
* Licensed under the Open Software License version 3.0 |
10 |
* http://www.opensource.org/licenses/osl-3.0.php |
11 |
* |
12 |
*******************************************************/ |
13 |
|
14 |
|
15 |
#if !defined escript_DataLazy_20081008_H |
16 |
#define escript_DataLazy_20081008_H |
17 |
#include "system_dep.h" |
18 |
|
19 |
#include "DataAbstract.h" |
20 |
|
21 |
#include <string> |
22 |
#include <functional> |
23 |
|
24 |
#include "LocalOps.h" // for tensor_binary_op |
25 |
|
26 |
namespace escript { |
27 |
|
28 |
enum ES_optype |
29 |
{ |
30 |
UNKNOWNOP=0, |
31 |
IDENTITY=1, |
32 |
ADD=2, |
33 |
SUB=3, |
34 |
MUL=4, |
35 |
DIV=5, |
36 |
POW=6, |
37 |
SIN=POW+1, |
38 |
COS=SIN+1, |
39 |
TAN=SIN+2, |
40 |
ASIN=SIN+3, |
41 |
ACOS=SIN+4, |
42 |
ATAN=SIN+5, |
43 |
SINH=SIN+6, |
44 |
COSH=SIN+7, |
45 |
TANH=SIN+8, |
46 |
ERF=SIN+9, |
47 |
ASINH=SIN+10, |
48 |
ACOSH=SIN+11, |
49 |
ATANH=SIN+12, |
50 |
LOG10=ATANH+1, |
51 |
LOG=LOG10+1, |
52 |
SIGN=LOG10+2, |
53 |
ABS=LOG10+3, |
54 |
NEG=LOG10+4, |
55 |
POS=LOG10+5, |
56 |
EXP=LOG10+6, |
57 |
SQRT=LOG10+7, |
58 |
RECIP=LOG10+8, |
59 |
GZ=RECIP+1, |
60 |
LZ=GZ+1, |
61 |
GEZ=GZ+2, |
62 |
LEZ=GZ+3 |
63 |
}; |
64 |
|
65 |
const std::string& |
66 |
opToString(ES_optype op); |
67 |
|
68 |
/** |
69 |
\class escript::DataLazy |
70 |
\brief Wraps an expression tree of other DataObjects. |
71 |
The data will be evaluated when required. |
72 |
|
73 |
|
74 |
NOTE: This class assumes that the Data being pointed at are immutable. |
75 |
*/ |
76 |
|
77 |
class DataLazy; |
78 |
|
79 |
typedef POINTER_WRAPPER_CLASS(DataLazy) DataLazy_ptr; |
80 |
typedef POINTER_WRAPPER_CLASS(const DataLazy) const_DataLazy_ptr; |
81 |
|
82 |
class DataLazy : public DataAbstract |
83 |
{ |
84 |
|
85 |
typedef DataAbstract parent; |
86 |
typedef DataTypes::ValueType ValueType; |
87 |
typedef DataTypes::ShapeType ShapeType; |
88 |
|
89 |
public: |
90 |
/** |
91 |
\brief Create an IDENTITY DataLazy for the given DataAbstract. |
92 |
\param p DataAbstract to be wrapped. |
93 |
\throws DataException if p is lazy data or it is not constant, tagged or expanded. |
94 |
*/ |
95 |
ESCRIPT_DLL_API |
96 |
DataLazy(DataAbstract_ptr p); |
97 |
|
98 |
|
99 |
/** |
100 |
\brief Produce a DataLazy for a unary operation. |
101 |
\param left DataAbstract to be operated on. |
102 |
\param op unary operation to perform. |
103 |
\throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy. |
104 |
Note that IDENTITY is not considered a unary operation. |
105 |
*/ |
106 |
ESCRIPT_DLL_API |
107 |
DataLazy(DataAbstract_ptr left, ES_optype op); |
108 |
|
109 |
/** |
110 |
\brief Produce a DataLazy for a binary operation. |
111 |
\param left left operand |
112 |
\param right right operand |
113 |
\param op unary operation to perform. |
114 |
\throws DataException if op is not a binary operation or if left or right cannot be converted to a DataLazy. |
115 |
*/ |
116 |
ESCRIPT_DLL_API |
117 |
DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op); |
118 |
|
119 |
ESCRIPT_DLL_API |
120 |
~DataLazy(); |
121 |
|
122 |
/** |
123 |
\brief Evaluate the lazy expression. |
124 |
\return A DataReady with the value of the lazy expresion. |
125 |
*/ |
126 |
ESCRIPT_DLL_API |
127 |
DataReady_ptr |
128 |
resolve(); |
129 |
|
130 |
ESCRIPT_DLL_API |
131 |
std::string |
132 |
toString() const; |
133 |
|
134 |
ESCRIPT_DLL_API |
135 |
DataAbstract* |
136 |
deepCopy(); |
137 |
|
138 |
|
139 |
/** |
140 |
\brief |
141 |
Return the number of doubles that would be stored for this Data object if it were resolved. |
142 |
*/ |
143 |
ESCRIPT_DLL_API |
144 |
ValueType::size_type |
145 |
getLength() const; |
146 |
|
147 |
|
148 |
ESCRIPT_DLL_API |
149 |
DataAbstract* |
150 |
getSlice(const DataTypes::RegionType& region) const; |
151 |
|
152 |
|
153 |
DataTypes::ValueType::size_type |
154 |
getPointOffset(int sampleNo, |
155 |
int dataPointNo) const; |
156 |
|
157 |
|
158 |
/** |
159 |
\return the number of samples which need to be stored to evaluate the expression. |
160 |
*/ |
161 |
ESCRIPT_DLL_API |
162 |
int |
163 |
getBuffsRequired() const; |
164 |
|
165 |
/** |
166 |
\brief Produces an IDENTITY DataLazy containing zero. |
167 |
The result will have the same shape and functionspace as before. |
168 |
*/ |
169 |
ESCRIPT_DLL_API |
170 |
virtual void |
171 |
setToZero(); |
172 |
|
173 |
private: |
174 |
DataReady_ptr m_id; // For IDENTITY nodes, stores a wrapped value. |
175 |
DataLazy_ptr m_left, m_right; // operands for operation. |
176 |
ES_optype m_op; // operation to perform. |
177 |
size_t m_length; // number of values represented by the operation |
178 |
|
179 |
int m_buffsRequired; // how many samples are required to evaluate this expression |
180 |
size_t m_samplesize; // number of values required to store a sample |
181 |
|
182 |
char m_readytype; // E for expanded, T for tagged, C for constant |
183 |
|
184 |
|
185 |
/** |
186 |
Does the work for toString. |
187 |
*/ |
188 |
void |
189 |
intoString(std::ostringstream& oss) const; |
190 |
|
191 |
/** |
192 |
\brief Converts the DataLazy into an IDENTITY storing the value of the expression. |
193 |
This method uses the original methods on the Data class to evaluate the expressions. |
194 |
For this reason, it should not be used on DataExpanded instances. (To do so would defeat |
195 |
the purpose of using DataLazy in the first place). |
196 |
*/ |
197 |
void |
198 |
collapse(); // converts the node into an IDENTITY node |
199 |
|
200 |
|
201 |
/** |
202 |
\brief Evaluates the expression using methods on Data. |
203 |
This does the work for the collapse method. |
204 |
For reasons of efficiency do not call this method on DataExpanded nodes. |
205 |
*/ |
206 |
DataReady_ptr |
207 |
collapseToReady(); |
208 |
|
209 |
/** |
210 |
\brief Compute the value of the expression for the given sample. |
211 |
\return Vector which stores the value of the subexpression for the given sample. |
212 |
\param v A vector to store intermediate results. |
213 |
\param offset Index in v to begin storing results. |
214 |
\param sampleNo Sample number to evaluate. |
215 |
\param roffset (output parameter) the offset in the return vector where the result begins. |
216 |
|
217 |
The return value will be an existing vector so do not deallocate it. |
218 |
*/ |
219 |
const ValueType* |
220 |
resolveSample(ValueType& v, size_t offset ,int sampleNo, size_t& roffset); |
221 |
|
222 |
/** |
223 |
\brief Compute the value of the expression (binary operation) for the given sample. |
224 |
\return Vector which stores the value of the subexpression for the given sample. |
225 |
\param v A vector to store intermediate results. |
226 |
\param offset Index in v to begin storing results. |
227 |
\param sampleNo Sample number to evaluate. |
228 |
\param roffset (output parameter) the offset in the return vector where the result begins. |
229 |
|
230 |
The return value will be an existing vector so do not deallocate it. |
231 |
If the result is stored in v it should be stored at the offset given. |
232 |
Everything from offset to the end of v should be considered available for this method to use. |
233 |
*/ |
234 |
ValueType* |
235 |
resolveUnary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const; |
236 |
|
237 |
/** |
238 |
\brief Compute the value of the expression (binary operation) for the given sample. |
239 |
\return Vector which stores the value of the subexpression for the given sample. |
240 |
\param v A vector to store intermediate results. |
241 |
\param offset Index in v to begin storing results. |
242 |
\param sampleNo Sample number to evaluate. |
243 |
\param roffset (output parameter) the offset in the return vector where the result begins. |
244 |
|
245 |
The return value will be an existing vector so do not deallocate it. |
246 |
If the result is stored in v it should be stored at the offset given. |
247 |
Everything from offset to the end of v should be considered available for this method to use. |
248 |
*/ |
249 |
ValueType* |
250 |
resolveBinary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const; |
251 |
|
252 |
}; |
253 |
|
254 |
} |
255 |
#endif |