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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2497 - (show annotations)
Mon Jun 29 00:04:45 2009 UTC (11 years ago) by jfenwick
File MIME type: text/plain
File size: 14235 byte(s)
Calling resolve now replaces resolved expression with an identity node.

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 #include "BufferGroup.h"
26
27 namespace escript {
28
29 // For the purposes of unit testing and maintaining sanity, it is important that this enum be contiguous
30 enum ES_optype
31 {
32 UNKNOWNOP=0,
33 IDENTITY=1,
34 ADD=2,
35 SUB=3,
36 MUL=4,
37 DIV=5,
38 POW=6,
39 SIN=POW+1,
40 COS=SIN+1,
41 TAN=SIN+2,
42 ASIN=SIN+3,
43 ACOS=SIN+4,
44 ATAN=SIN+5,
45 SINH=SIN+6,
46 COSH=SIN+7,
47 TANH=SIN+8,
48 ERF=SIN+9,
49 ASINH=SIN+10,
50 ACOSH=SIN+11,
51 ATANH=SIN+12,
52 LOG10=ATANH+1,
53 LOG=LOG10+1,
54 SIGN=LOG10+2,
55 ABS=LOG10+3,
56 NEG=LOG10+4,
57 POS=LOG10+5,
58 EXP=LOG10+6,
59 SQRT=LOG10+7,
60 RECIP=LOG10+8,
61 GZ=RECIP+1,
62 LZ=GZ+1,
63 GEZ=GZ+2,
64 LEZ=GZ+3,
65 NEZ=GZ+4,
66 EZ=GZ+5,
67 SYM=EZ+1,
68 NSYM=SYM+1,
69 PROD=NSYM+1,
70 TRANS=PROD+1,
71 TRACE=TRANS+1,
72 SWAP=TRACE+1
73 };
74
75 ESCRIPT_DLL_API
76 const std::string&
77 opToString(ES_optype op);
78
79 /**
80 \class escript::DataLazy
81 \brief Wraps an expression tree of other DataObjects.
82 The data will be evaluated when required.
83
84
85 NOTE: This class assumes that the Data being pointed at are immutable.
86 */
87
88 class DataLazy;
89
90 typedef POINTER_WRAPPER_CLASS(DataLazy) DataLazy_ptr;
91 typedef POINTER_WRAPPER_CLASS(const DataLazy) const_DataLazy_ptr;
92
93 class DataLazy : public DataAbstract
94 {
95
96 typedef DataAbstract parent;
97 typedef DataTypes::ValueType ValueType;
98 typedef DataTypes::ShapeType ShapeType;
99
100 public:
101 /**
102 \brief Create an IDENTITY DataLazy for the given DataAbstract.
103 \param p DataAbstract to be wrapped.
104 \throws DataException if p is lazy data or it is not constant, tagged or expanded.
105 */
106 ESCRIPT_DLL_API
107 DataLazy(DataAbstract_ptr p);
108
109
110 /**
111 \brief Produce a DataLazy for a unary operation.
112 \param left DataAbstract to be operated on.
113 \param op unary operation to perform.
114 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
115 Note that IDENTITY is not considered a unary operation.
116 */
117 ESCRIPT_DLL_API
118 DataLazy(DataAbstract_ptr left, ES_optype op);
119
120 /**
121 \brief Produce a DataLazy for a unary operation.
122 \param left DataAbstract to be operated on.
123 \param op unary operation to perform.
124 \param tol tolerance for operation
125 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
126 Note that IDENTITY is not considered a unary operation.
127 */
128 ESCRIPT_DLL_API
129 DataLazy(DataAbstract_ptr left, ES_optype op, double tol);
130
131 /**
132 \brief Produce a DataLazy for a unary operation which requires a parameter.
133 \param left DataAbstract to be operated on.
134 \param op unary operation to perform.
135 \param axis_offset the parameter for the operation
136 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
137 Note that IDENTITY is not considered a unary operation.
138 */
139 ESCRIPT_DLL_API
140 DataLazy(DataAbstract_ptr left, ES_optype op, int axis_offset);
141
142
143 /**
144 \brief Produce a DataLazy for a binary operation.
145 \param left left operand
146 \param right right operand
147 \param op unary operation to perform.
148 \throws DataException if op is not a binary operation or if left or right cannot be converted to a DataLazy.
149 */
150 ESCRIPT_DLL_API
151 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);
152
153 /**
154 \brief Produce a DataLazy for a binary operation with additional paramters.
155 \param left left operand
156 \param right right operand
157 \param op unary operation to perform.
158 \param axis_offset
159 \param transpose
160 \throws DataException if op is not a binary operation requiring parameters or if left or right cannot be converted to a DataLazy.
161 */
162 ESCRIPT_DLL_API
163 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op, int axis_offset, int transpose);
164
165 /**
166 \brief Produce a DataLazy for a unary operation which requires two integer parameters.
167 \param left DataAbstract to be operated on.
168 \param op unary operation to perform.
169 \param axis0 the first parameter for the operation
170 \param axis1 the second parameter for the operation
171 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
172 Note that IDENTITY is not considered a unary operation.
173 */
174 ESCRIPT_DLL_API
175 DataLazy(DataAbstract_ptr left, ES_optype op, const int axis0, const int axis1);
176
177 ESCRIPT_DLL_API
178 ~DataLazy();
179
180 /**
181 \brief Evaluate the lazy expression.
182 \return A DataReady with the value of the lazy expresion.
183 */
184 ESCRIPT_DLL_API
185 DataReady_ptr
186 resolve();
187
188 ESCRIPT_DLL_API
189 std::string
190 toString() const;
191
192 ESCRIPT_DLL_API
193 DataAbstract*
194 deepCopy();
195
196
197 /**
198 \brief
199 This method throws an exception. It does not really make sense to ask this question of lazy data.
200 */
201 ESCRIPT_DLL_API
202 ValueType::size_type
203 getLength() const;
204
205
206 ESCRIPT_DLL_API
207 DataAbstract*
208 getSlice(const DataTypes::RegionType& region) const;
209
210
211 DataTypes::ValueType::size_type
212 getPointOffset(int sampleNo,
213 int dataPointNo) const;
214
215 DataTypes::ValueType::size_type
216 getPointOffset(int sampleNo,
217 int dataPointNo);
218
219
220 /**
221 \return the number of samples which need to be stored to evaluate the expression.
222 */
223 ESCRIPT_DLL_API
224 int
225 getBuffsRequired() const;
226
227 /**
228 \return the largest samplesize required to evaluate the expression.
229 */
230 ESCRIPT_DLL_API
231 size_t
232 getMaxSampleSize() const;
233
234 /**
235 \return the size of the buffer required to evaulate a sample for this object
236 */
237 ESCRIPT_DLL_API
238 size_t
239 getSampleBufferSize() const;
240
241 /**
242 \brief Compute the value of the expression for the given sample.
243 \return Vector which stores the value of the subexpression for the given sample.
244 \param v A vector to store intermediate results.
245 \param offset Index in v to begin storing results.
246 \param sampleNo Sample number to evaluate.
247 \param roffset (output parameter) the offset in the return vector where the result begins.
248
249 The return value will be an existing vector so do not deallocate it.
250 */
251 ESCRIPT_DLL_API
252 const ValueType*
253 resolveSample(ValueType& v, size_t offset, int sampleNo, size_t& roffset);
254
255 /**
256 \brief Compute the value of the expression for the given sample.
257 \return Vector which stores the value of the subexpression for the given sample.
258 \param v A BufferGroup to store intermediate results.
259 \param sampleNo Sample number to evaluate.
260 \param roffset (output parameter) the offset in the return vector where the result begins.
261
262 The return value will be an existing vector so do not deallocate it.
263 */
264 ESCRIPT_DLL_API
265 const ValueType*
266 resolveSample(BufferGroup& bg, int sampleNo, size_t& roffset);
267
268 /**
269 \brief if resolve() was called would it produce expanded data.
270 */
271 ESCRIPT_DLL_API
272 bool
273 actsExpanded() const;
274
275 /**
276 \brief Produces an IDENTITY DataLazy containing zero.
277 The result will have the same shape and functionspace as before.
278 */
279 ESCRIPT_DLL_API
280 virtual void
281 setToZero();
282
283 private:
284 DataReady_ptr m_id; // For IDENTITY nodes, stores a wrapped value.
285 DataLazy_ptr m_left, m_right; // operands for operation.
286 ES_optype m_op; // operation to perform.
287
288 int m_buffsRequired; // how many samples are required to evaluate this expression
289 size_t m_samplesize; // number of values required to store a sample
290
291 char m_readytype; // E for expanded, T for tagged, C for constant
292
293 int m_axis_offset; // required extra info for general tensor product
294 int m_transpose; // offset and transpose are used for swapaxes as well
295 int m_SL, m_SM, m_SR; // computed properties used in general tensor product
296
297
298 double m_tol; // required extra info for <>0 and ==0
299
300 size_t m_maxsamplesize; // largest samplesize required by any node in the expression
301 size_t m_children;
302 size_t m_height;
303
304
305 /**
306 Does the work for toString.
307 */
308 void
309 intoString(std::ostringstream& oss) const;
310
311 /**
312 \brief Converts the DataLazy into an IDENTITY storing the value of the expression.
313 This method uses the original methods on the Data class to evaluate the expressions.
314 For this reason, it should not be used on DataExpanded instances. (To do so would defeat
315 the purpose of using DataLazy in the first place).
316 */
317 void
318 collapse(); // converts the node into an IDENTITY node
319
320
321 /**
322 \brief Evaluates the expression using methods on Data.
323 This does the work for the collapse method.
324 For reasons of efficiency do not call this method on DataExpanded nodes.
325 */
326 DataReady_ptr
327 collapseToReady();
328
329 /**
330 \brief resolve the expression can store it in the current node
331 The current node will be converted to an identity node.
332 */
333 void
334 resolveToIdentity();
335
336 /**
337 \brief helper method for resolveToIdentity and the identity constructor
338 */
339 void
340 makeIdentity(const DataReady_ptr& p);
341
342 /**
343 \brief resolve to a ReadyData object using a vector buffer.
344 */
345 DataReady_ptr
346 resolveVectorWorker();
347
348 /**
349 \brief Compute the value of the expression (unary operation) for the given sample.
350 \return Vector which stores the value of the subexpression for the given sample.
351 \param v A vector to store intermediate results.
352 \param offset Index in v to begin storing results.
353 \param sampleNo Sample number to evaluate.
354 \param roffset (output parameter) the offset in the return vector where the result begins.
355
356 The return value will be an existing vector so do not deallocate it.
357 If the result is stored in v it should be stored at the offset given.
358 Everything from offset to the end of v should be considered available for this method to use.
359 */
360 ValueType*
361 resolveUnary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const;
362
363 /**
364 \brief Compute the value of the expression (unary non-pointwise operation) for the given sample.
365 \return Vector which stores the value of the subexpression for the given sample.
366 \param v A vector to store intermediate results.
367 \param offset Index in v to begin storing results.
368 \param sampleNo Sample number to evaluate.
369 \param roffset (output parameter) the offset in the return vector where the result begins.
370
371 The return value will be an existing vector so do not deallocate it.
372 If the result is stored in v it should be stored at the offset given.
373 Everything from offset to the end of v should be considered available for this method to use.
374
375 This method differs from the one above in that deals with operations that are not
376 point-wise. That is, the answer cannot just be written on top of the input.
377 Extra buffers are required for these operations.
378 */
379
380 ValueType*
381 resolveNP1OUT(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
382
383 /**
384 \brief Compute the value of the expression (unary operation) for the given sample.
385 \return Vector which stores the value of the subexpression for the given sample.
386 \param v A vector to store intermediate results.
387 \param offset Index in v to begin storing results.
388 \param sampleNo Sample number to evaluate.
389 \param roffset (output parameter) the offset in the return vector where the result begins.
390
391 The return value will be an existing vector so do not deallocate it.
392 If the result is stored in v it should be stored at the offset given.
393 Everything from offset to the end of v should be considered available for this method to use.
394 */
395 DataTypes::ValueType*
396 resolveNP1OUT_P(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
397
398 /**
399 \brief Compute the value of the expression (unary operation with int params) for the given sample.
400 \return Vector which stores the value of the subexpression for the given sample.
401 \param v A vector to store intermediate results.
402 \param offset Index in v to begin storing results.
403 \param sampleNo Sample number to evaluate.
404 \param roffset (output parameter) the offset in the return vector where the result begins.
405
406 The return value will be an existing vector so do not deallocate it.
407 If the result is stored in v it should be stored at the offset given.
408 Everything from offset to the end of v should be considered available for this method to use.
409 */
410 DataTypes::ValueType*
411 resolveNP1OUT_2P(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
412
413
414 /**
415 \brief Compute the value of the expression (binary operation) for the given sample.
416 \return Vector which stores the value of the subexpression for the given sample.
417 \param v A vector to store intermediate results.
418 \param offset Index in v to begin storing results.
419 \param sampleNo Sample number to evaluate.
420 \param roffset (output parameter) the offset in the return vector where the result begins.
421
422 The return value will be an existing vector so do not deallocate it.
423 If the result is stored in v it should be stored at the offset given.
424 Everything from offset to the end of v should be considered available for this method to use.
425 */
426 ValueType*
427 resolveBinary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const;
428
429 /**
430 \brief Compute the value of the expression (tensor product) for the given sample.
431 \return Vector which stores the value of the subexpression for the given sample.
432 \param v A vector to store intermediate results.
433 \param offset Index in v to begin storing results.
434 \param sampleNo Sample number to evaluate.
435 \param roffset (output parameter) the offset in the return vector where the result begins.
436
437 The return value will be an existing vector so do not deallocate it.
438 If the result is stored in v it should be stored at the offset given.
439 Everything from offset to the end of v should be considered available for this method to use.
440 */
441 DataTypes::ValueType*
442 resolveTProd(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
443
444 };
445
446 }
447 #endif

  ViewVC Help
Powered by ViewVC 1.1.26