/[escript]/branches/schroedinger/escript/src/DataLazy.h
ViewVC logotype

Annotation of /branches/schroedinger/escript/src/DataLazy.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1935 - (hide annotations)
Mon Oct 27 06:06:39 2008 UTC (10 years, 6 months ago) by jfenwick
File MIME type: text/plain
File size: 7136 byte(s)
Branch commit
More cleanup of DataTestCase - still don't have all the LazyTests in 
there yet.
Added tests to make sure the resolve() operation does what it is 
supposed to.
Added non-constant versions of getPointOffset to DataAbstract 
classes.
Fixed a bug in deepCopy on DataLazy.
Changed setToZero to not check the type of the data.

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     LEZ=GZ+3
64 jfenwick 1865 };
65    
66     const std::string&
67     opToString(ES_optype op);
68    
69     /**
70     \class escript::DataLazy
71     \brief Wraps an expression tree of other DataObjects.
72 jfenwick 1903 The data will be evaluated when required.
73 jfenwick 1879
74 jfenwick 1903
75 jfenwick 1879 NOTE: This class assumes that the Data being pointed at are immutable.
76 jfenwick 1865 */
77    
78 jfenwick 1879 class DataLazy;
79    
80     typedef POINTER_WRAPPER_CLASS(DataLazy) DataLazy_ptr;
81     typedef POINTER_WRAPPER_CLASS(const DataLazy) const_DataLazy_ptr;
82    
83 jfenwick 1865 class DataLazy : public DataAbstract
84     {
85    
86     typedef DataAbstract parent;
87     typedef DataTypes::ValueType ValueType;
88     typedef DataTypes::ShapeType ShapeType;
89    
90     public:
91 jfenwick 1899 /**
92     \brief Create an IDENTITY DataLazy for the given DataAbstract.
93     \param p DataAbstract to be wrapped.
94     \throws DataException if p is lazy data or it is not constant, tagged or expanded.
95     */
96 jfenwick 1865 ESCRIPT_DLL_API
97     DataLazy(DataAbstract_ptr p);
98    
99 jfenwick 1899
100     /**
101     \brief Produce a DataLazy for a unary operation.
102     \param left DataAbstract to be operated on.
103     \param op unary operation to perform.
104     \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
105     Note that IDENTITY is not considered a unary operation.
106     */
107 jfenwick 1865 ESCRIPT_DLL_API
108 jfenwick 1886 DataLazy(DataAbstract_ptr left, ES_optype op);
109    
110 jfenwick 1899 /**
111     \brief Produce a DataLazy for a binary operation.
112     \param left left operand
113     \param right right operand
114     \param op unary operation to perform.
115     \throws DataException if op is not a binary operation or if left or right cannot be converted to a DataLazy.
116     */
117 jfenwick 1886 ESCRIPT_DLL_API
118 jfenwick 1865 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);
119    
120     ESCRIPT_DLL_API
121     ~DataLazy();
122    
123     /**
124 jfenwick 1899 \brief Evaluate the lazy expression.
125     \return A DataReady with the value of the lazy expresion.
126 jfenwick 1865 */
127     ESCRIPT_DLL_API
128 jfenwick 1879 DataReady_ptr
129     resolve();
130 jfenwick 1865
131     ESCRIPT_DLL_API
132     std::string
133     toString() const;
134    
135     ESCRIPT_DLL_API
136     DataAbstract*
137     deepCopy();
138    
139    
140     /**
141     \brief
142     Return the number of doubles that would be stored for this Data object if it were resolved.
143     */
144     ESCRIPT_DLL_API
145     ValueType::size_type
146     getLength() const;
147    
148    
149     ESCRIPT_DLL_API
150     DataAbstract*
151     getSlice(const DataTypes::RegionType& region) const;
152    
153    
154     DataTypes::ValueType::size_type
155     getPointOffset(int sampleNo,
156     int dataPointNo) const;
157    
158 jfenwick 1935 DataTypes::ValueType::size_type
159     getPointOffset(int sampleNo,
160     int dataPointNo);
161 jfenwick 1879
162 jfenwick 1935
163 jfenwick 1899 /**
164     \return the number of samples which need to be stored to evaluate the expression.
165     */
166 jfenwick 1879 ESCRIPT_DLL_API
167     int
168     getBuffsRequired() const;
169    
170 jfenwick 1901 /**
171     \brief Produces an IDENTITY DataLazy containing zero.
172     The result will have the same shape and functionspace as before.
173     */
174     ESCRIPT_DLL_API
175     virtual void
176     setToZero();
177 jfenwick 1889
178 jfenwick 1865 private:
179 jfenwick 1899 DataReady_ptr m_id; // For IDENTITY nodes, stores a wrapped value.
180     DataLazy_ptr m_left, m_right; // operands for operation.
181     ES_optype m_op; // operation to perform.
182 jfenwick 1879 size_t m_length; // number of values represented by the operation
183    
184 jfenwick 1899 int m_buffsRequired; // how many samples are required to evaluate this expression
185 jfenwick 1879 size_t m_samplesize; // number of values required to store a sample
186    
187 jfenwick 1899 char m_readytype; // E for expanded, T for tagged, C for constant
188 jfenwick 1879
189 jfenwick 1889
190 jfenwick 1899 /**
191     Does the work for toString.
192     */
193 jfenwick 1886 void
194     intoString(std::ostringstream& oss) const;
195    
196 jfenwick 1899 /**
197     \brief Converts the DataLazy into an IDENTITY storing the value of the expression.
198     This method uses the original methods on the Data class to evaluate the expressions.
199     For this reason, it should not be used on DataExpanded instances. (To do so would defeat
200     the purpose of using DataLazy in the first place).
201     */
202 jfenwick 1889 void
203     collapse(); // converts the node into an IDENTITY node
204    
205 jfenwick 1899
206     /**
207     \brief Evaluates the expression using methods on Data.
208     This does the work for the collapse method.
209     For reasons of efficiency do not call this method on DataExpanded nodes.
210     */
211 jfenwick 1889 DataReady_ptr
212     collapseToReady();
213    
214 jfenwick 1899 /**
215     \brief Compute the value of the expression for the given sample.
216     \return Vector which stores the value of the subexpression for the given sample.
217     \param v A vector to store intermediate results.
218     \param offset Index in v to begin storing results.
219     \param sampleNo Sample number to evaluate.
220     \param roffset (output parameter) the offset in the return vector where the result begins.
221 jfenwick 1889
222 jfenwick 1899 The return value will be an existing vector so do not deallocate it.
223     */
224     const ValueType*
225     resolveSample(ValueType& v, size_t offset ,int sampleNo, size_t& roffset);
226 jfenwick 1889
227 jfenwick 1899 /**
228     \brief Compute the value of the expression (binary operation) for the given sample.
229     \return Vector which stores the value of the subexpression for the given sample.
230     \param v A vector to store intermediate results.
231     \param offset Index in v to begin storing results.
232     \param sampleNo Sample number to evaluate.
233     \param roffset (output parameter) the offset in the return vector where the result begins.
234    
235     The return value will be an existing vector so do not deallocate it.
236     If the result is stored in v it should be stored at the offset given.
237     Everything from offset to the end of v should be considered available for this method to use.
238     */
239 jfenwick 1898 ValueType*
240     resolveUnary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const;
241 jfenwick 1889
242 jfenwick 1899 /**
243     \brief Compute the value of the expression (binary operation) for the given sample.
244     \return Vector which stores the value of the subexpression for the given sample.
245     \param v A vector to store intermediate results.
246     \param offset Index in v to begin storing results.
247     \param sampleNo Sample number to evaluate.
248     \param roffset (output parameter) the offset in the return vector where the result begins.
249    
250     The return value will be an existing vector so do not deallocate it.
251     If the result is stored in v it should be stored at the offset given.
252     Everything from offset to the end of v should be considered available for this method to use.
253     */
254 jfenwick 1898 ValueType*
255     resolveBinary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const;
256    
257 jfenwick 1865 };
258    
259     }
260     #endif

  ViewVC Help
Powered by ViewVC 1.1.26