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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2799 - (hide annotations)
Thu Dec 3 01:35:08 2009 UTC (9 years, 9 months ago) by jfenwick
File MIME type: text/plain
File size: 9447 byte(s)
resolveGroup function added to python.
Unit tests for resolveGroup, resolve and delay.
1 jfenwick 1865
2     /*******************************************************
3     *
4 jfenwick 2548 * Copyright (c) 2003-2009 by University of Queensland
5 jfenwick 1865 * 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 jfenwick 1868 #include <functional>
23 jfenwick 1865
24 jfenwick 1879 #include "LocalOps.h" // for tensor_binary_op
25 jfenwick 2500 #include "DataVector.h" // for ElementType
26 jfenwick 1879
27 jfenwick 2500
28 jfenwick 2770 //#define LAZY_NODE_STORAGE
29 jfenwick 2500
30 jfenwick 1865 namespace escript {
31    
32 jfenwick 1926 // For the purposes of unit testing and maintaining sanity, it is important that this enum be contiguous
33 jfenwick 1865 enum ES_optype
34     {
35     UNKNOWNOP=0,
36 jfenwick 1868 IDENTITY=1,
37     ADD=2,
38     SUB=3,
39     MUL=4,
40 jfenwick 1886 DIV=5,
41 jfenwick 1910 POW=6,
42     SIN=POW+1,
43     COS=SIN+1,
44     TAN=SIN+2,
45     ASIN=SIN+3,
46     ACOS=SIN+4,
47     ATAN=SIN+5,
48     SINH=SIN+6,
49     COSH=SIN+7,
50     TANH=SIN+8,
51     ERF=SIN+9,
52     ASINH=SIN+10,
53     ACOSH=SIN+11,
54     ATANH=SIN+12,
55     LOG10=ATANH+1,
56     LOG=LOG10+1,
57     SIGN=LOG10+2,
58     ABS=LOG10+3,
59     NEG=LOG10+4,
60     POS=LOG10+5,
61     EXP=LOG10+6,
62     SQRT=LOG10+7,
63     RECIP=LOG10+8,
64     GZ=RECIP+1,
65     LZ=GZ+1,
66     GEZ=GZ+2,
67 jfenwick 2037 LEZ=GZ+3,
68 jfenwick 2147 NEZ=GZ+4,
69     EZ=GZ+5,
70     SYM=EZ+1,
71 jfenwick 2066 NSYM=SYM+1,
72 jfenwick 2084 PROD=NSYM+1,
73     TRANS=PROD+1,
74 jfenwick 2496 TRACE=TRANS+1,
75 jfenwick 2721 SWAP=TRACE+1,
76     MINVAL=SWAP+1,
77     MAXVAL=MINVAL+1
78 jfenwick 1865 };
79    
80 phornby 1992 ESCRIPT_DLL_API
81 jfenwick 1865 const std::string&
82     opToString(ES_optype op);
83    
84     /**
85     \class escript::DataLazy
86     \brief Wraps an expression tree of other DataObjects.
87 jfenwick 1903 The data will be evaluated when required.
88 jfenwick 1879
89 jfenwick 1903
90 jfenwick 1879 NOTE: This class assumes that the Data being pointed at are immutable.
91 jfenwick 1865 */
92    
93 jfenwick 1879 class DataLazy;
94    
95     typedef POINTER_WRAPPER_CLASS(DataLazy) DataLazy_ptr;
96     typedef POINTER_WRAPPER_CLASS(const DataLazy) const_DataLazy_ptr;
97    
98 jfenwick 1865 class DataLazy : public DataAbstract
99     {
100    
101     typedef DataAbstract parent;
102     typedef DataTypes::ValueType ValueType;
103     typedef DataTypes::ShapeType ShapeType;
104    
105     public:
106 jfenwick 1899 /**
107     \brief Create an IDENTITY DataLazy for the given DataAbstract.
108     \param p DataAbstract to be wrapped.
109     \throws DataException if p is lazy data or it is not constant, tagged or expanded.
110     */
111 jfenwick 1865 ESCRIPT_DLL_API
112     DataLazy(DataAbstract_ptr p);
113    
114 jfenwick 1899
115     /**
116     \brief Produce a DataLazy for a unary operation.
117     \param left DataAbstract to be operated on.
118     \param op unary operation to perform.
119     \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
120     Note that IDENTITY is not considered a unary operation.
121     */
122 jfenwick 1865 ESCRIPT_DLL_API
123 jfenwick 1886 DataLazy(DataAbstract_ptr left, ES_optype op);
124    
125 jfenwick 1899 /**
126 jfenwick 2147 \brief Produce a DataLazy for a unary operation.
127     \param left DataAbstract to be operated on.
128     \param op unary operation to perform.
129     \param tol tolerance for operation
130     \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
131     Note that IDENTITY is not considered a unary operation.
132     */
133     ESCRIPT_DLL_API
134     DataLazy(DataAbstract_ptr left, ES_optype op, double tol);
135    
136     /**
137 jfenwick 2084 \brief Produce a DataLazy for a unary operation which requires a parameter.
138     \param left DataAbstract to be operated on.
139     \param op unary operation to perform.
140     \param axis_offset the parameter for the operation
141     \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
142     Note that IDENTITY is not considered a unary operation.
143     */
144     ESCRIPT_DLL_API
145     DataLazy(DataAbstract_ptr left, ES_optype op, int axis_offset);
146    
147    
148     /**
149 jfenwick 1899 \brief Produce a DataLazy for a binary operation.
150     \param left left operand
151     \param right right operand
152     \param op unary operation to perform.
153     \throws DataException if op is not a binary operation or if left or right cannot be converted to a DataLazy.
154     */
155 jfenwick 1886 ESCRIPT_DLL_API
156 jfenwick 1865 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);
157    
158 jfenwick 2066 /**
159     \brief Produce a DataLazy for a binary operation with additional paramters.
160     \param left left operand
161     \param right right operand
162     \param op unary operation to perform.
163     \param axis_offset
164     \param transpose
165     \throws DataException if op is not a binary operation requiring parameters or if left or right cannot be converted to a DataLazy.
166     */
167 jfenwick 1865 ESCRIPT_DLL_API
168 jfenwick 2066 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op, int axis_offset, int transpose);
169    
170 jfenwick 2496 /**
171     \brief Produce a DataLazy for a unary operation which requires two integer parameters.
172     \param left DataAbstract to be operated on.
173     \param op unary operation to perform.
174     \param axis0 the first parameter for the operation
175     \param axis1 the second parameter for the operation
176     \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
177     Note that IDENTITY is not considered a unary operation.
178     */
179 jfenwick 2066 ESCRIPT_DLL_API
180 jfenwick 2496 DataLazy(DataAbstract_ptr left, ES_optype op, const int axis0, const int axis1);
181    
182     ESCRIPT_DLL_API
183 jfenwick 1865 ~DataLazy();
184    
185     /**
186 jfenwick 1899 \brief Evaluate the lazy expression.
187     \return A DataReady with the value of the lazy expresion.
188 jfenwick 1865 */
189     ESCRIPT_DLL_API
190 jfenwick 1879 DataReady_ptr
191     resolve();
192 jfenwick 1865
193     ESCRIPT_DLL_API
194     std::string
195     toString() const;
196    
197     ESCRIPT_DLL_API
198     DataAbstract*
199     deepCopy();
200    
201    
202     /**
203     \brief
204 jfenwick 2066 This method throws an exception. It does not really make sense to ask this question of lazy data.
205 jfenwick 1865 */
206     ESCRIPT_DLL_API
207     ValueType::size_type
208     getLength() const;
209    
210    
211     ESCRIPT_DLL_API
212     DataAbstract*
213     getSlice(const DataTypes::RegionType& region) const;
214    
215    
216     DataTypes::ValueType::size_type
217     getPointOffset(int sampleNo,
218     int dataPointNo) const;
219    
220 jfenwick 1935 DataTypes::ValueType::size_type
221     getPointOffset(int sampleNo,
222     int dataPointNo);
223 jfenwick 1879
224 jfenwick 1899 /**
225 jfenwick 2066 \return the largest samplesize required to evaluate the expression.
226     */
227     ESCRIPT_DLL_API
228     size_t
229     getMaxSampleSize() const;
230    
231 jfenwick 2271 /**
232     \brief Compute the value of the expression for the given sample.
233     \return Vector which stores the value of the subexpression for the given sample.
234     \param sampleNo Sample number to evaluate.
235     \param roffset (output parameter) the offset in the return vector where the result begins.
236    
237     The return value will be an existing vector so do not deallocate it.
238     */
239     ESCRIPT_DLL_API
240     const ValueType*
241 jfenwick 2770 resolveSample(int sampleNo, size_t& roffset);
242 jfenwick 2271
243     /**
244     \brief if resolve() was called would it produce expanded data.
245     */
246     ESCRIPT_DLL_API
247     bool
248     actsExpanded() const;
249    
250     /**
251 jfenwick 1901 \brief Produces an IDENTITY DataLazy containing zero.
252     The result will have the same shape and functionspace as before.
253     */
254     ESCRIPT_DLL_API
255     virtual void
256     setToZero();
257 jfenwick 1889
258 jfenwick 2799
259     ESCRIPT_DLL_API
260     void
261     resolveGroupWorker(std::vector<DataLazy*>& dats);
262    
263    
264 jfenwick 1865 private:
265 jfenwick 1899 DataReady_ptr m_id; // For IDENTITY nodes, stores a wrapped value.
266     DataLazy_ptr m_left, m_right; // operands for operation.
267     ES_optype m_op; // operation to perform.
268 jfenwick 1879
269     size_t m_samplesize; // number of values required to store a sample
270    
271 jfenwick 1899 char m_readytype; // E for expanded, T for tagged, C for constant
272 jfenwick 1879
273 jfenwick 2066 int m_axis_offset; // required extra info for general tensor product
274 jfenwick 2496 int m_transpose; // offset and transpose are used for swapaxes as well
275 jfenwick 2066 int m_SL, m_SM, m_SR; // computed properties used in general tensor product
276 jfenwick 1889
277 jfenwick 2496
278 jfenwick 2147 double m_tol; // required extra info for <>0 and ==0
279    
280 jfenwick 2177 size_t m_children;
281     size_t m_height;
282 jfenwick 2066
283 jfenwick 2500 int* m_sampleids; // may be NULL
284     DataVector m_samples;
285    
286 jfenwick 1899 /**
287 jfenwick 2500 Allocates sample storage at each node
288     */
289     void LazyNodeSetup();
290    
291    
292     const DataTypes::ValueType*
293     resolveNodeUnary(int tid, int sampleNo, size_t& roffset);
294    
295 jfenwick 2721
296 jfenwick 2500 const DataTypes::ValueType*
297 jfenwick 2721 resolveNodeReduction(int tid, int sampleNo, size_t& roffset);
298    
299     const DataTypes::ValueType*
300 jfenwick 2500 resolveNodeSample(int tid, int sampleNo, size_t& roffset);
301    
302     const DataTypes::ValueType*
303     resolveNodeBinary(int tid, int sampleNo, size_t& roffset);
304    
305     const DataTypes::ValueType*
306     resolveNodeNP1OUT(int tid, int sampleNo, size_t& roffset);
307    
308     const DataTypes::ValueType*
309     resolveNodeNP1OUT_P(int tid, int sampleNo, size_t& roffset);
310    
311     const DataTypes::ValueType*
312     resolveNodeTProd(int tid, int sampleNo, size_t& roffset);
313    
314     const DataTypes::ValueType*
315     resolveNodeNP1OUT_2P(int tid, int sampleNo, size_t& roffset);
316    
317     /**
318 jfenwick 1899 Does the work for toString.
319     */
320 jfenwick 1886 void
321     intoString(std::ostringstream& oss) const;
322    
323 jfenwick 1899 /**
324 jfenwick 2737 \warning internal use only!!
325     */
326     void
327     intoTreeString(std::ostringstream& oss,std::string indent) const;
328    
329     /**
330 jfenwick 1899 \brief Converts the DataLazy into an IDENTITY storing the value of the expression.
331     This method uses the original methods on the Data class to evaluate the expressions.
332     For this reason, it should not be used on DataExpanded instances. (To do so would defeat
333     the purpose of using DataLazy in the first place).
334     */
335 jfenwick 1889 void
336     collapse(); // converts the node into an IDENTITY node
337    
338 jfenwick 1899
339     /**
340     \brief Evaluates the expression using methods on Data.
341     This does the work for the collapse method.
342     For reasons of efficiency do not call this method on DataExpanded nodes.
343     */
344 jfenwick 1889 DataReady_ptr
345     collapseToReady();
346    
347 jfenwick 1899 /**
348 jfenwick 2177 \brief resolve the expression can store it in the current node
349     The current node will be converted to an identity node.
350     */
351     void
352     resolveToIdentity();
353    
354     /**
355     \brief helper method for resolveToIdentity and the identity constructor
356     */
357     void
358     makeIdentity(const DataReady_ptr& p);
359    
360 jfenwick 2497
361     /**
362 jfenwick 2500 \brief resolve to a ReadyData object using storage at nodes
363     */
364     DataReady_ptr
365     resolveNodeWorker();
366    
367 jfenwick 1865 };
368    
369     }
370     #endif

  ViewVC Help
Powered by ViewVC 1.1.26