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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2084 - (hide annotations)
Fri Nov 21 05:20:42 2008 UTC (11 years, 4 months ago) by jfenwick
File MIME type: text/plain
File size: 11037 byte(s)
Fixed a warning in cpp unit tests under dodebug
Pointed the url for python doco at shake200 rather than iservo.
Added support for trace and transpose to LazyData.
Fixed bug in trace to initialise running totals.
1 jfenwick 1865
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 jfenwick 1868 #include <functional>
23 jfenwick 1865
24 jfenwick 1879 #include "LocalOps.h" // for tensor_binary_op
25    
26 jfenwick 1865 namespace escript {
27    
28 jfenwick 1926 // For the purposes of unit testing and maintaining sanity, it is important that this enum be contiguous
29 jfenwick 1865 enum ES_optype
30     {
31     UNKNOWNOP=0,
32 jfenwick 1868 IDENTITY=1,
33     ADD=2,
34     SUB=3,
35     MUL=4,
36 jfenwick 1886 DIV=5,
37 jfenwick 1910 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 jfenwick 2037 LEZ=GZ+3,
64     SYM=LEZ+1,
65 jfenwick 2066 NSYM=SYM+1,
66 jfenwick 2084 PROD=NSYM+1,
67     TRANS=PROD+1,
68     TRACE=TRANS+1
69 jfenwick 1865 };
70    
71 phornby 1992 ESCRIPT_DLL_API
72 jfenwick 1865 const std::string&
73     opToString(ES_optype op);
74    
75     /**
76     \class escript::DataLazy
77     \brief Wraps an expression tree of other DataObjects.
78 jfenwick 1903 The data will be evaluated when required.
79 jfenwick 1879
80 jfenwick 1903
81 jfenwick 1879 NOTE: This class assumes that the Data being pointed at are immutable.
82 jfenwick 1865 */
83    
84 jfenwick 1879 class DataLazy;
85    
86     typedef POINTER_WRAPPER_CLASS(DataLazy) DataLazy_ptr;
87     typedef POINTER_WRAPPER_CLASS(const DataLazy) const_DataLazy_ptr;
88    
89 jfenwick 1865 class DataLazy : public DataAbstract
90     {
91    
92     typedef DataAbstract parent;
93     typedef DataTypes::ValueType ValueType;
94     typedef DataTypes::ShapeType ShapeType;
95    
96     public:
97 jfenwick 1899 /**
98     \brief Create an IDENTITY DataLazy for the given DataAbstract.
99     \param p DataAbstract to be wrapped.
100     \throws DataException if p is lazy data or it is not constant, tagged or expanded.
101     */
102 jfenwick 1865 ESCRIPT_DLL_API
103     DataLazy(DataAbstract_ptr p);
104    
105 jfenwick 1899
106     /**
107     \brief Produce a DataLazy for a unary operation.
108     \param left DataAbstract to be operated on.
109     \param op unary operation to perform.
110     \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
111     Note that IDENTITY is not considered a unary operation.
112     */
113 jfenwick 1865 ESCRIPT_DLL_API
114 jfenwick 1886 DataLazy(DataAbstract_ptr left, ES_optype op);
115    
116 jfenwick 1899 /**
117 jfenwick 2084 \brief Produce a DataLazy for a unary operation which requires a parameter.
118     \param left DataAbstract to be operated on.
119     \param op unary operation to perform.
120     \param axis_offset the parameter for the operation
121     \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
122     Note that IDENTITY is not considered a unary operation.
123     */
124     ESCRIPT_DLL_API
125     DataLazy(DataAbstract_ptr left, ES_optype op, int axis_offset);
126    
127    
128     /**
129 jfenwick 1899 \brief Produce a DataLazy for a binary operation.
130     \param left left operand
131     \param right right operand
132     \param op unary operation to perform.
133     \throws DataException if op is not a binary operation or if left or right cannot be converted to a DataLazy.
134     */
135 jfenwick 1886 ESCRIPT_DLL_API
136 jfenwick 1865 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);
137    
138 jfenwick 2066 /**
139     \brief Produce a DataLazy for a binary operation with additional paramters.
140     \param left left operand
141     \param right right operand
142     \param op unary operation to perform.
143     \param axis_offset
144     \param transpose
145     \throws DataException if op is not a binary operation requiring parameters or if left or right cannot be converted to a DataLazy.
146     */
147 jfenwick 1865 ESCRIPT_DLL_API
148 jfenwick 2066 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op, int axis_offset, int transpose);
149    
150     ESCRIPT_DLL_API
151 jfenwick 1865 ~DataLazy();
152    
153     /**
154 jfenwick 1899 \brief Evaluate the lazy expression.
155     \return A DataReady with the value of the lazy expresion.
156 jfenwick 1865 */
157     ESCRIPT_DLL_API
158 jfenwick 1879 DataReady_ptr
159     resolve();
160 jfenwick 1865
161     ESCRIPT_DLL_API
162     std::string
163     toString() const;
164    
165     ESCRIPT_DLL_API
166     DataAbstract*
167     deepCopy();
168    
169    
170     /**
171     \brief
172 jfenwick 2066 This method throws an exception. It does not really make sense to ask this question of lazy data.
173 jfenwick 1865 */
174     ESCRIPT_DLL_API
175     ValueType::size_type
176     getLength() const;
177    
178    
179     ESCRIPT_DLL_API
180     DataAbstract*
181     getSlice(const DataTypes::RegionType& region) const;
182    
183    
184     DataTypes::ValueType::size_type
185     getPointOffset(int sampleNo,
186     int dataPointNo) const;
187    
188 jfenwick 1935 DataTypes::ValueType::size_type
189     getPointOffset(int sampleNo,
190     int dataPointNo);
191 jfenwick 1879
192 jfenwick 1935
193 jfenwick 1899 /**
194     \return the number of samples which need to be stored to evaluate the expression.
195     */
196 jfenwick 1879 ESCRIPT_DLL_API
197     int
198     getBuffsRequired() const;
199    
200 jfenwick 1901 /**
201 jfenwick 2066 \return the largest samplesize required to evaluate the expression.
202     */
203     ESCRIPT_DLL_API
204     size_t
205     getMaxSampleSize() const;
206    
207    
208     /**
209 jfenwick 1901 \brief Produces an IDENTITY DataLazy containing zero.
210     The result will have the same shape and functionspace as before.
211     */
212     ESCRIPT_DLL_API
213     virtual void
214     setToZero();
215 jfenwick 1889
216 jfenwick 1865 private:
217 jfenwick 1899 DataReady_ptr m_id; // For IDENTITY nodes, stores a wrapped value.
218     DataLazy_ptr m_left, m_right; // operands for operation.
219     ES_optype m_op; // operation to perform.
220 jfenwick 1879
221 jfenwick 1899 int m_buffsRequired; // how many samples are required to evaluate this expression
222 jfenwick 1879 size_t m_samplesize; // number of values required to store a sample
223    
224 jfenwick 1899 char m_readytype; // E for expanded, T for tagged, C for constant
225 jfenwick 1879
226 jfenwick 2066 int m_axis_offset; // required extra info for general tensor product
227     int m_transpose;
228     int m_SL, m_SM, m_SR; // computed properties used in general tensor product
229 jfenwick 1889
230 jfenwick 2066 unsigned int m_maxsamplesize; // largest samplesize required by any node in the expression
231    
232    
233 jfenwick 1899 /**
234     Does the work for toString.
235     */
236 jfenwick 1886 void
237     intoString(std::ostringstream& oss) const;
238    
239 jfenwick 1899 /**
240     \brief Converts the DataLazy into an IDENTITY storing the value of the expression.
241     This method uses the original methods on the Data class to evaluate the expressions.
242     For this reason, it should not be used on DataExpanded instances. (To do so would defeat
243     the purpose of using DataLazy in the first place).
244     */
245 jfenwick 1889 void
246     collapse(); // converts the node into an IDENTITY node
247    
248 jfenwick 1899
249     /**
250     \brief Evaluates the expression using methods on Data.
251     This does the work for the collapse method.
252     For reasons of efficiency do not call this method on DataExpanded nodes.
253     */
254 jfenwick 1889 DataReady_ptr
255     collapseToReady();
256    
257 jfenwick 1899 /**
258     \brief Compute the value of the expression for the given sample.
259     \return Vector which stores the value of the subexpression for the given sample.
260     \param v A vector to store intermediate results.
261     \param offset Index in v to begin storing results.
262     \param sampleNo Sample number to evaluate.
263     \param roffset (output parameter) the offset in the return vector where the result begins.
264 jfenwick 1889
265 jfenwick 1899 The return value will be an existing vector so do not deallocate it.
266     */
267     const ValueType*
268     resolveSample(ValueType& v, size_t offset ,int sampleNo, size_t& roffset);
269 jfenwick 1889
270 jfenwick 1899 /**
271 jfenwick 2037 \brief Compute the value of the expression (unary operation) for the given sample.
272 jfenwick 1899 \return Vector which stores the value of the subexpression for the given sample.
273     \param v A vector to store intermediate results.
274     \param offset Index in v to begin storing results.
275     \param sampleNo Sample number to evaluate.
276     \param roffset (output parameter) the offset in the return vector where the result begins.
277    
278     The return value will be an existing vector so do not deallocate it.
279     If the result is stored in v it should be stored at the offset given.
280     Everything from offset to the end of v should be considered available for this method to use.
281     */
282 jfenwick 1898 ValueType*
283     resolveUnary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const;
284 jfenwick 1889
285 jfenwick 1899 /**
286 jfenwick 2037 \brief Compute the value of the expression (unary non-pointwise 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     This method differs from the one above in that deals with operations that are not
298     point-wise. That is, the answer cannot just be written on top of the input.
299     Extra buffers are required for these operations.
300     */
301    
302     ValueType*
303     resolveNP1OUT(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
304    
305 jfenwick 2084 /**
306     \brief Compute the value of the expression (unary operation) for the given sample.
307     \return Vector which stores the value of the subexpression for the given sample.
308     \param v A vector to store intermediate results.
309     \param offset Index in v to begin storing results.
310     \param sampleNo Sample number to evaluate.
311     \param roffset (output parameter) the offset in the return vector where the result begins.
312 jfenwick 2037
313 jfenwick 2084 The return value will be an existing vector so do not deallocate it.
314     If the result is stored in v it should be stored at the offset given.
315     Everything from offset to the end of v should be considered available for this method to use.
316     */
317     DataTypes::ValueType*
318     resolveNP1OUT_P(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
319    
320    
321 jfenwick 2037 /**
322 jfenwick 1899 \brief Compute the value of the expression (binary operation) for the given sample.
323     \return Vector which stores the value of the subexpression for the given sample.
324     \param v A vector to store intermediate results.
325     \param offset Index in v to begin storing results.
326     \param sampleNo Sample number to evaluate.
327     \param roffset (output parameter) the offset in the return vector where the result begins.
328    
329     The return value will be an existing vector so do not deallocate it.
330     If the result is stored in v it should be stored at the offset given.
331     Everything from offset to the end of v should be considered available for this method to use.
332     */
333 jfenwick 1898 ValueType*
334     resolveBinary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const;
335    
336 jfenwick 2066 /**
337     \brief Compute the value of the expression (tensor product) 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     DataTypes::ValueType*
349     resolveTProd(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
350    
351 jfenwick 1865 };
352    
353     }
354     #endif

  ViewVC Help
Powered by ViewVC 1.1.26