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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2147 - (show annotations)
Wed Dec 10 04:41:26 2008 UTC (11 years, 7 months ago) by jfenwick
File MIME type: text/plain
File size: 11535 byte(s)
Made some changes to c++ unit tests to accomodate AUTOLAZY.
whereZero and whereNonZero can now work with lazy data.
There are some double frees if AUTOLAZY is turned on so don't use it yet.
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 NEZ=GZ+4,
65 EZ=GZ+5,
66 SYM=EZ+1,
67 NSYM=SYM+1,
68 PROD=NSYM+1,
69 TRANS=PROD+1,
70 TRACE=TRANS+1
71 };
72
73 ESCRIPT_DLL_API
74 const std::string&
75 opToString(ES_optype op);
76
77 /**
78 \class escript::DataLazy
79 \brief Wraps an expression tree of other DataObjects.
80 The data will be evaluated when required.
81
82
83 NOTE: This class assumes that the Data being pointed at are immutable.
84 */
85
86 class DataLazy;
87
88 typedef POINTER_WRAPPER_CLASS(DataLazy) DataLazy_ptr;
89 typedef POINTER_WRAPPER_CLASS(const DataLazy) const_DataLazy_ptr;
90
91 class DataLazy : public DataAbstract
92 {
93
94 typedef DataAbstract parent;
95 typedef DataTypes::ValueType ValueType;
96 typedef DataTypes::ShapeType ShapeType;
97
98 public:
99 /**
100 \brief Create an IDENTITY DataLazy for the given DataAbstract.
101 \param p DataAbstract to be wrapped.
102 \throws DataException if p is lazy data or it is not constant, tagged or expanded.
103 */
104 ESCRIPT_DLL_API
105 DataLazy(DataAbstract_ptr p);
106
107
108 /**
109 \brief Produce a DataLazy for a unary operation.
110 \param left DataAbstract to be operated on.
111 \param op unary operation to perform.
112 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
113 Note that IDENTITY is not considered a unary operation.
114 */
115 ESCRIPT_DLL_API
116 DataLazy(DataAbstract_ptr left, ES_optype op);
117
118 /**
119 \brief Produce a DataLazy for a unary operation.
120 \param left DataAbstract to be operated on.
121 \param op unary operation to perform.
122 \param tol tolerance for operation
123 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
124 Note that IDENTITY is not considered a unary operation.
125 */
126 ESCRIPT_DLL_API
127 DataLazy(DataAbstract_ptr left, ES_optype op, double tol);
128
129 /**
130 \brief Produce a DataLazy for a unary operation which requires a parameter.
131 \param left DataAbstract to be operated on.
132 \param op unary operation to perform.
133 \param axis_offset the parameter for the operation
134 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
135 Note that IDENTITY is not considered a unary operation.
136 */
137 ESCRIPT_DLL_API
138 DataLazy(DataAbstract_ptr left, ES_optype op, int axis_offset);
139
140
141 /**
142 \brief Produce a DataLazy for a binary operation.
143 \param left left operand
144 \param right right operand
145 \param op unary operation to perform.
146 \throws DataException if op is not a binary operation or if left or right cannot be converted to a DataLazy.
147 */
148 ESCRIPT_DLL_API
149 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);
150
151 /**
152 \brief Produce a DataLazy for a binary operation with additional paramters.
153 \param left left operand
154 \param right right operand
155 \param op unary operation to perform.
156 \param axis_offset
157 \param transpose
158 \throws DataException if op is not a binary operation requiring parameters or if left or right cannot be converted to a DataLazy.
159 */
160 ESCRIPT_DLL_API
161 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op, int axis_offset, int transpose);
162
163 ESCRIPT_DLL_API
164 ~DataLazy();
165
166 /**
167 \brief Evaluate the lazy expression.
168 \return A DataReady with the value of the lazy expresion.
169 */
170 ESCRIPT_DLL_API
171 DataReady_ptr
172 resolve();
173
174 ESCRIPT_DLL_API
175 std::string
176 toString() const;
177
178 ESCRIPT_DLL_API
179 DataAbstract*
180 deepCopy();
181
182
183 /**
184 \brief
185 This method throws an exception. It does not really make sense to ask this question of lazy data.
186 */
187 ESCRIPT_DLL_API
188 ValueType::size_type
189 getLength() const;
190
191
192 ESCRIPT_DLL_API
193 DataAbstract*
194 getSlice(const DataTypes::RegionType& region) const;
195
196
197 DataTypes::ValueType::size_type
198 getPointOffset(int sampleNo,
199 int dataPointNo) const;
200
201 DataTypes::ValueType::size_type
202 getPointOffset(int sampleNo,
203 int dataPointNo);
204
205
206 /**
207 \return the number of samples which need to be stored to evaluate the expression.
208 */
209 ESCRIPT_DLL_API
210 int
211 getBuffsRequired() const;
212
213 /**
214 \return the largest samplesize required to evaluate the expression.
215 */
216 ESCRIPT_DLL_API
217 size_t
218 getMaxSampleSize() const;
219
220
221 /**
222 \brief Produces an IDENTITY DataLazy containing zero.
223 The result will have the same shape and functionspace as before.
224 */
225 ESCRIPT_DLL_API
226 virtual void
227 setToZero();
228
229 private:
230 DataReady_ptr m_id; // For IDENTITY nodes, stores a wrapped value.
231 DataLazy_ptr m_left, m_right; // operands for operation.
232 ES_optype m_op; // operation to perform.
233
234 int m_buffsRequired; // how many samples are required to evaluate this expression
235 size_t m_samplesize; // number of values required to store a sample
236
237 char m_readytype; // E for expanded, T for tagged, C for constant
238
239 int m_axis_offset; // required extra info for general tensor product
240 int m_transpose;
241 int m_SL, m_SM, m_SR; // computed properties used in general tensor product
242
243 double m_tol; // required extra info for <>0 and ==0
244
245 unsigned int m_maxsamplesize; // largest samplesize required by any node in the expression
246
247
248 /**
249 Does the work for toString.
250 */
251 void
252 intoString(std::ostringstream& oss) const;
253
254 /**
255 \brief Converts the DataLazy into an IDENTITY storing the value of the expression.
256 This method uses the original methods on the Data class to evaluate the expressions.
257 For this reason, it should not be used on DataExpanded instances. (To do so would defeat
258 the purpose of using DataLazy in the first place).
259 */
260 void
261 collapse(); // converts the node into an IDENTITY node
262
263
264 /**
265 \brief Evaluates the expression using methods on Data.
266 This does the work for the collapse method.
267 For reasons of efficiency do not call this method on DataExpanded nodes.
268 */
269 DataReady_ptr
270 collapseToReady();
271
272 /**
273 \brief Compute the value of the expression for the given sample.
274 \return Vector which stores the value of the subexpression for the given sample.
275 \param v A vector to store intermediate results.
276 \param offset Index in v to begin storing results.
277 \param sampleNo Sample number to evaluate.
278 \param roffset (output parameter) the offset in the return vector where the result begins.
279
280 The return value will be an existing vector so do not deallocate it.
281 */
282 const ValueType*
283 resolveSample(ValueType& v, size_t offset ,int sampleNo, size_t& roffset);
284
285 /**
286 \brief Compute the value of the expression (unary operation) for the given sample.
287 \return Vector which stores the value of the subexpression for the given sample.
288 \param v A vector to store intermediate results.
289 \param offset Index in v to begin storing results.
290 \param sampleNo Sample number to evaluate.
291 \param roffset (output parameter) the offset in the return vector where the result begins.
292
293 The return value will be an existing vector so do not deallocate it.
294 If the result is stored in v it should be stored at the offset given.
295 Everything from offset to the end of v should be considered available for this method to use.
296 */
297 ValueType*
298 resolveUnary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const;
299
300 /**
301 \brief Compute the value of the expression (unary non-pointwise operation) for the given sample.
302 \return Vector which stores the value of the subexpression for the given sample.
303 \param v A vector to store intermediate results.
304 \param offset Index in v to begin storing results.
305 \param sampleNo Sample number to evaluate.
306 \param roffset (output parameter) the offset in the return vector where the result begins.
307
308 The return value will be an existing vector so do not deallocate it.
309 If the result is stored in v it should be stored at the offset given.
310 Everything from offset to the end of v should be considered available for this method to use.
311
312 This method differs from the one above in that deals with operations that are not
313 point-wise. That is, the answer cannot just be written on top of the input.
314 Extra buffers are required for these operations.
315 */
316
317 ValueType*
318 resolveNP1OUT(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
319
320 /**
321 \brief Compute the value of the expression (unary operation) for the given sample.
322 \return Vector which stores the value of the subexpression for the given sample.
323 \param v A vector to store intermediate results.
324 \param offset Index in v to begin storing results.
325 \param sampleNo Sample number to evaluate.
326 \param roffset (output parameter) the offset in the return vector where the result begins.
327
328 The return value will be an existing vector so do not deallocate it.
329 If the result is stored in v it should be stored at the offset given.
330 Everything from offset to the end of v should be considered available for this method to use.
331 */
332 DataTypes::ValueType*
333 resolveNP1OUT_P(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
334
335
336 /**
337 \brief Compute the value of the expression (binary operation) for the given sample.
338 \return Vector which stores the value of the subexpression for the given sample.
339 \param v A vector to store intermediate results.
340 \param offset Index in v to begin storing results.
341 \param sampleNo Sample number to evaluate.
342 \param roffset (output parameter) the offset in the return vector where the result begins.
343
344 The return value will be an existing vector so do not deallocate it.
345 If the result is stored in v it should be stored at the offset given.
346 Everything from offset to the end of v should be considered available for this method to use.
347 */
348 ValueType*
349 resolveBinary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const;
350
351 /**
352 \brief Compute the value of the expression (tensor product) for the given sample.
353 \return Vector which stores the value of the subexpression for the given sample.
354 \param v A vector to store intermediate results.
355 \param offset Index in v to begin storing results.
356 \param sampleNo Sample number to evaluate.
357 \param roffset (output parameter) the offset in the return vector where the result begins.
358
359 The return value will be an existing vector so do not deallocate it.
360 If the result is stored in v it should be stored at the offset given.
361 Everything from offset to the end of v should be considered available for this method to use.
362 */
363 DataTypes::ValueType*
364 resolveTProd(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
365
366 };
367
368 }
369 #endif

  ViewVC Help
Powered by ViewVC 1.1.26