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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2799 - (show 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
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2009 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 "DataVector.h" // for ElementType
26
27
28 //#define LAZY_NODE_STORAGE
29
30 namespace escript {
31
32 // For the purposes of unit testing and maintaining sanity, it is important that this enum be contiguous
33 enum ES_optype
34 {
35 UNKNOWNOP=0,
36 IDENTITY=1,
37 ADD=2,
38 SUB=3,
39 MUL=4,
40 DIV=5,
41 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 LEZ=GZ+3,
68 NEZ=GZ+4,
69 EZ=GZ+5,
70 SYM=EZ+1,
71 NSYM=SYM+1,
72 PROD=NSYM+1,
73 TRANS=PROD+1,
74 TRACE=TRANS+1,
75 SWAP=TRACE+1,
76 MINVAL=SWAP+1,
77 MAXVAL=MINVAL+1
78 };
79
80 ESCRIPT_DLL_API
81 const std::string&
82 opToString(ES_optype op);
83
84 /**
85 \class escript::DataLazy
86 \brief Wraps an expression tree of other DataObjects.
87 The data will be evaluated when required.
88
89
90 NOTE: This class assumes that the Data being pointed at are immutable.
91 */
92
93 class DataLazy;
94
95 typedef POINTER_WRAPPER_CLASS(DataLazy) DataLazy_ptr;
96 typedef POINTER_WRAPPER_CLASS(const DataLazy) const_DataLazy_ptr;
97
98 class DataLazy : public DataAbstract
99 {
100
101 typedef DataAbstract parent;
102 typedef DataTypes::ValueType ValueType;
103 typedef DataTypes::ShapeType ShapeType;
104
105 public:
106 /**
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 ESCRIPT_DLL_API
112 DataLazy(DataAbstract_ptr p);
113
114
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 ESCRIPT_DLL_API
123 DataLazy(DataAbstract_ptr left, ES_optype op);
124
125 /**
126 \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 \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 \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 ESCRIPT_DLL_API
156 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);
157
158 /**
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 ESCRIPT_DLL_API
168 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op, int axis_offset, int transpose);
169
170 /**
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 ESCRIPT_DLL_API
180 DataLazy(DataAbstract_ptr left, ES_optype op, const int axis0, const int axis1);
181
182 ESCRIPT_DLL_API
183 ~DataLazy();
184
185 /**
186 \brief Evaluate the lazy expression.
187 \return A DataReady with the value of the lazy expresion.
188 */
189 ESCRIPT_DLL_API
190 DataReady_ptr
191 resolve();
192
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 This method throws an exception. It does not really make sense to ask this question of lazy data.
205 */
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 DataTypes::ValueType::size_type
221 getPointOffset(int sampleNo,
222 int dataPointNo);
223
224 /**
225 \return the largest samplesize required to evaluate the expression.
226 */
227 ESCRIPT_DLL_API
228 size_t
229 getMaxSampleSize() const;
230
231 /**
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 resolveSample(int sampleNo, size_t& roffset);
242
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 \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
258
259 ESCRIPT_DLL_API
260 void
261 resolveGroupWorker(std::vector<DataLazy*>& dats);
262
263
264 private:
265 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
269 size_t m_samplesize; // number of values required to store a sample
270
271 char m_readytype; // E for expanded, T for tagged, C for constant
272
273 int m_axis_offset; // required extra info for general tensor product
274 int m_transpose; // offset and transpose are used for swapaxes as well
275 int m_SL, m_SM, m_SR; // computed properties used in general tensor product
276
277
278 double m_tol; // required extra info for <>0 and ==0
279
280 size_t m_children;
281 size_t m_height;
282
283 int* m_sampleids; // may be NULL
284 DataVector m_samples;
285
286 /**
287 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
296 const DataTypes::ValueType*
297 resolveNodeReduction(int tid, int sampleNo, size_t& roffset);
298
299 const DataTypes::ValueType*
300 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 Does the work for toString.
319 */
320 void
321 intoString(std::ostringstream& oss) const;
322
323 /**
324 \warning internal use only!!
325 */
326 void
327 intoTreeString(std::ostringstream& oss,std::string indent) const;
328
329 /**
330 \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 void
336 collapse(); // converts the node into an IDENTITY node
337
338
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 DataReady_ptr
345 collapseToReady();
346
347 /**
348 \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
361 /**
362 \brief resolve to a ReadyData object using storage at nodes
363 */
364 DataReady_ptr
365 resolveNodeWorker();
366
367 };
368
369 }
370 #endif

  ViewVC Help
Powered by ViewVC 1.1.26