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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26