/[escript]/trunk/escript/src/DataLazy.h
ViewVC logotype

Contents of /trunk/escript/src/DataLazy.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2066 - (show annotations)
Thu Nov 20 05:31:33 2008 UTC (10 years, 11 months ago) by jfenwick
File MIME type: text/plain
File size: 9771 byte(s)
Fixed Data::toString to look at the amount of data actually stored rather than the number of points in the domain.

Added support for GeneralTensorProduct to LazyData
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 // For the purposes of unit testing and maintaining sanity, it is important that this enum be contiguous
29 enum ES_optype
30 {
31 UNKNOWNOP=0,
32 IDENTITY=1,
33 ADD=2,
34 SUB=3,
35 MUL=4,
36 DIV=5,
37 POW=6,
38 SIN=POW+1,
39 COS=SIN+1,
40 TAN=SIN+2,
41 ASIN=SIN+3,
42 ACOS=SIN+4,
43 ATAN=SIN+5,
44 SINH=SIN+6,
45 COSH=SIN+7,
46 TANH=SIN+8,
47 ERF=SIN+9,
48 ASINH=SIN+10,
49 ACOSH=SIN+11,
50 ATANH=SIN+12,
51 LOG10=ATANH+1,
52 LOG=LOG10+1,
53 SIGN=LOG10+2,
54 ABS=LOG10+3,
55 NEG=LOG10+4,
56 POS=LOG10+5,
57 EXP=LOG10+6,
58 SQRT=LOG10+7,
59 RECIP=LOG10+8,
60 GZ=RECIP+1,
61 LZ=GZ+1,
62 GEZ=GZ+2,
63 LEZ=GZ+3,
64 SYM=LEZ+1,
65 NSYM=SYM+1,
66 PROD=NSYM+1
67 };
68
69 ESCRIPT_DLL_API
70 const std::string&
71 opToString(ES_optype op);
72
73 /**
74 \class escript::DataLazy
75 \brief Wraps an expression tree of other DataObjects.
76 The data will be evaluated when required.
77
78
79 NOTE: This class assumes that the Data being pointed at are immutable.
80 */
81
82 class DataLazy;
83
84 typedef POINTER_WRAPPER_CLASS(DataLazy) DataLazy_ptr;
85 typedef POINTER_WRAPPER_CLASS(const DataLazy) const_DataLazy_ptr;
86
87 class DataLazy : public DataAbstract
88 {
89
90 typedef DataAbstract parent;
91 typedef DataTypes::ValueType ValueType;
92 typedef DataTypes::ShapeType ShapeType;
93
94 public:
95 /**
96 \brief Create an IDENTITY DataLazy for the given DataAbstract.
97 \param p DataAbstract to be wrapped.
98 \throws DataException if p is lazy data or it is not constant, tagged or expanded.
99 */
100 ESCRIPT_DLL_API
101 DataLazy(DataAbstract_ptr p);
102
103
104 /**
105 \brief Produce a DataLazy for a unary operation.
106 \param left DataAbstract to be operated on.
107 \param op unary operation to perform.
108 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
109 Note that IDENTITY is not considered a unary operation.
110 */
111 ESCRIPT_DLL_API
112 DataLazy(DataAbstract_ptr left, ES_optype op);
113
114 /**
115 \brief Produce a DataLazy for a binary operation.
116 \param left left operand
117 \param right right operand
118 \param op unary operation to perform.
119 \throws DataException if op is not a binary operation or if left or right cannot be converted to a DataLazy.
120 */
121 ESCRIPT_DLL_API
122 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);
123
124 /**
125 \brief Produce a DataLazy for a binary operation with additional paramters.
126 \param left left operand
127 \param right right operand
128 \param op unary operation to perform.
129 \param axis_offset
130 \param transpose
131 \throws DataException if op is not a binary operation requiring parameters or if left or right cannot be converted to a DataLazy.
132 */
133 ESCRIPT_DLL_API
134 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op, int axis_offset, int transpose);
135
136 ESCRIPT_DLL_API
137 ~DataLazy();
138
139 /**
140 \brief Evaluate the lazy expression.
141 \return A DataReady with the value of the lazy expresion.
142 */
143 ESCRIPT_DLL_API
144 DataReady_ptr
145 resolve();
146
147 ESCRIPT_DLL_API
148 std::string
149 toString() const;
150
151 ESCRIPT_DLL_API
152 DataAbstract*
153 deepCopy();
154
155
156 /**
157 \brief
158 This method throws an exception. It does not really make sense to ask this question of lazy data.
159 */
160 ESCRIPT_DLL_API
161 ValueType::size_type
162 getLength() const;
163
164
165 ESCRIPT_DLL_API
166 DataAbstract*
167 getSlice(const DataTypes::RegionType& region) const;
168
169
170 DataTypes::ValueType::size_type
171 getPointOffset(int sampleNo,
172 int dataPointNo) const;
173
174 DataTypes::ValueType::size_type
175 getPointOffset(int sampleNo,
176 int dataPointNo);
177
178
179 /**
180 \return the number of samples which need to be stored to evaluate the expression.
181 */
182 ESCRIPT_DLL_API
183 int
184 getBuffsRequired() const;
185
186 /**
187 \return the largest samplesize required to evaluate the expression.
188 */
189 ESCRIPT_DLL_API
190 size_t
191 getMaxSampleSize() const;
192
193
194 /**
195 \brief Produces an IDENTITY DataLazy containing zero.
196 The result will have the same shape and functionspace as before.
197 */
198 ESCRIPT_DLL_API
199 virtual void
200 setToZero();
201
202 private:
203 DataReady_ptr m_id; // For IDENTITY nodes, stores a wrapped value.
204 DataLazy_ptr m_left, m_right; // operands for operation.
205 ES_optype m_op; // operation to perform.
206
207 int m_buffsRequired; // how many samples are required to evaluate this expression
208 size_t m_samplesize; // number of values required to store a sample
209
210 char m_readytype; // E for expanded, T for tagged, C for constant
211
212 int m_axis_offset; // required extra info for general tensor product
213 int m_transpose;
214 int m_SL, m_SM, m_SR; // computed properties used in general tensor product
215
216 unsigned int m_maxsamplesize; // largest samplesize required by any node in the expression
217
218
219 /**
220 Does the work for toString.
221 */
222 void
223 intoString(std::ostringstream& oss) const;
224
225 /**
226 \brief Converts the DataLazy into an IDENTITY storing the value of the expression.
227 This method uses the original methods on the Data class to evaluate the expressions.
228 For this reason, it should not be used on DataExpanded instances. (To do so would defeat
229 the purpose of using DataLazy in the first place).
230 */
231 void
232 collapse(); // converts the node into an IDENTITY node
233
234
235 /**
236 \brief Evaluates the expression using methods on Data.
237 This does the work for the collapse method.
238 For reasons of efficiency do not call this method on DataExpanded nodes.
239 */
240 DataReady_ptr
241 collapseToReady();
242
243 /**
244 \brief Compute the value of the expression for the given sample.
245 \return Vector which stores the value of the subexpression for the given sample.
246 \param v A vector to store intermediate results.
247 \param offset Index in v to begin storing results.
248 \param sampleNo Sample number to evaluate.
249 \param roffset (output parameter) the offset in the return vector where the result begins.
250
251 The return value will be an existing vector so do not deallocate it.
252 */
253 const ValueType*
254 resolveSample(ValueType& v, size_t offset ,int sampleNo, size_t& roffset);
255
256 /**
257 \brief Compute the value of the expression (unary operation) for the given sample.
258 \return Vector which stores the value of the subexpression for the given sample.
259 \param v A vector to store intermediate results.
260 \param offset Index in v to begin storing results.
261 \param sampleNo Sample number to evaluate.
262 \param roffset (output parameter) the offset in the return vector where the result begins.
263
264 The return value will be an existing vector so do not deallocate it.
265 If the result is stored in v it should be stored at the offset given.
266 Everything from offset to the end of v should be considered available for this method to use.
267 */
268 ValueType*
269 resolveUnary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const;
270
271 /**
272 \brief Compute the value of the expression (unary non-pointwise operation) for the given sample.
273 \return Vector which stores the value of the subexpression for the given sample.
274 \param v A vector to store intermediate results.
275 \param offset Index in v to begin storing results.
276 \param sampleNo Sample number to evaluate.
277 \param roffset (output parameter) the offset in the return vector where the result begins.
278
279 The return value will be an existing vector so do not deallocate it.
280 If the result is stored in v it should be stored at the offset given.
281 Everything from offset to the end of v should be considered available for this method to use.
282
283 This method differs from the one above in that deals with operations that are not
284 point-wise. That is, the answer cannot just be written on top of the input.
285 Extra buffers are required for these operations.
286 */
287
288 ValueType*
289 resolveNP1OUT(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
290
291
292 /**
293 \brief Compute the value of the expression (binary operation) for the given sample.
294 \return Vector which stores the value of the subexpression for the given sample.
295 \param v A vector to store intermediate results.
296 \param offset Index in v to begin storing results.
297 \param sampleNo Sample number to evaluate.
298 \param roffset (output parameter) the offset in the return vector where the result begins.
299
300 The return value will be an existing vector so do not deallocate it.
301 If the result is stored in v it should be stored at the offset given.
302 Everything from offset to the end of v should be considered available for this method to use.
303 */
304 ValueType*
305 resolveBinary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const;
306
307 /**
308 \brief Compute the value of the expression (tensor product) for the given sample.
309 \return Vector which stores the value of the subexpression for the given sample.
310 \param v A vector to store intermediate results.
311 \param offset Index in v to begin storing results.
312 \param sampleNo Sample number to evaluate.
313 \param roffset (output parameter) the offset in the return vector where the result begins.
314
315 The return value will be an existing vector so do not deallocate it.
316 If the result is stored in v it should be stored at the offset given.
317 Everything from offset to the end of v should be considered available for this method to use.
318 */
319 DataTypes::ValueType*
320 resolveTProd(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
321
322 };
323
324 }
325 #endif

  ViewVC Help
Powered by ViewVC 1.1.26