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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3454 - (show annotations)
Tue Jan 25 04:00:29 2011 UTC (8 years, 7 months ago) by caltinay
File MIME type: text/plain
File size: 9930 byte(s)
Fixed some minor doxygen issues.

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2010 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 CONDEVAL=MAXVAL+1
79 };
80
81 ESCRIPT_DLL_API
82 const std::string&
83 opToString(ES_optype op);
84
85 /**
86 \class escript::DataLazy
87 \brief Wraps an expression tree of other DataObjects.
88 The data will be evaluated when required.
89
90
91 NOTE: This class assumes that the Data being pointed at are immutable.
92 */
93
94 class DataLazy;
95
96 typedef POINTER_WRAPPER_CLASS(DataLazy) DataLazy_ptr;
97 typedef POINTER_WRAPPER_CLASS(const DataLazy) const_DataLazy_ptr;
98
99 class DataLazy : public DataAbstract
100 {
101
102 typedef DataAbstract parent;
103 typedef DataTypes::ValueType ValueType;
104 typedef DataTypes::ShapeType ShapeType;
105
106 public:
107 /**
108 \brief Create an IDENTITY DataLazy for the given DataAbstract.
109 \param p DataAbstract to be wrapped.
110 \throws DataException if p is lazy data or it is not constant, tagged or expanded.
111 */
112 ESCRIPT_DLL_API
113 DataLazy(DataAbstract_ptr p);
114
115
116 /**
117 \brief Produce a DataLazy for a unary operation.
118 \param left DataAbstract to be operated on.
119 \param op unary operation to perform.
120 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
121 Note that IDENTITY is not considered a unary operation.
122 */
123 ESCRIPT_DLL_API
124 DataLazy(DataAbstract_ptr left, ES_optype op);
125
126 /**
127 \brief Produce a DataLazy for a unary operation.
128 \param left DataAbstract to be operated on.
129 \param op unary operation to perform.
130 \param tol tolerance for operation
131 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
132 Note that IDENTITY is not considered a unary operation.
133 */
134 ESCRIPT_DLL_API
135 DataLazy(DataAbstract_ptr left, ES_optype op, double tol);
136
137 /**
138 \brief Produce a DataLazy for a unary operation which requires a parameter.
139 \param left DataAbstract to be operated on.
140 \param op unary operation to perform.
141 \param axis_offset the parameter for the operation
142 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
143 Note that IDENTITY is not considered a unary operation.
144 */
145 ESCRIPT_DLL_API
146 DataLazy(DataAbstract_ptr left, ES_optype op, int axis_offset);
147
148
149 /**
150 \brief Produce a DataLazy for a binary operation.
151 \param left left operand
152 \param right right operand
153 \param op unary operation to perform.
154 \throws DataException if op is not a binary operation or if left or right cannot be converted to a DataLazy.
155 */
156 ESCRIPT_DLL_API
157 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);
158
159 /**
160 \brief Produce a DataLazy for a binary operation with additional paramters.
161 \param left left operand
162 \param right right operand
163 \param op unary operation to perform.
164 \param axis_offset
165 \param transpose
166 \throws DataException if op is not a binary operation requiring parameters or if left or right cannot be converted to a DataLazy.
167 */
168 ESCRIPT_DLL_API
169 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op, int axis_offset, int transpose);
170
171 /**
172 \brief Produce a DataLazy for a unary operation which requires two integer parameters.
173 \param left DataAbstract to be operated on.
174 \param op unary operation to perform.
175 \param axis0 the first parameter for the operation
176 \param axis1 the second parameter for the operation
177 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
178 Note that IDENTITY is not considered a unary operation.
179 */
180 ESCRIPT_DLL_API
181 DataLazy(DataAbstract_ptr left, ES_optype op, const int axis0, const int axis1);
182
183 /**
184 \brief Produce a DataLazy for a unary operation which requires two integer parameters.
185 \param mask scalar mask to select values.
186 \param left DataAbstract to use for true mask.
187 \param right DataAbstract to use for false mask.
188 */
189 ESCRIPT_DLL_API
190 DataLazy(DataAbstract_ptr mask, DataAbstract_ptr left, DataAbstract_ptr right/*, double tol*/);
191
192 ESCRIPT_DLL_API
193 ~DataLazy();
194
195 /**
196 \brief Evaluate the lazy expression.
197 \return A DataReady with the value of the lazy expresion.
198 */
199 ESCRIPT_DLL_API
200 DataReady_ptr
201 resolve();
202
203 ESCRIPT_DLL_API
204 std::string
205 toString() const;
206
207 ESCRIPT_DLL_API
208 DataAbstract*
209 deepCopy();
210
211
212 /**
213 \brief
214 This method throws an exception. It does not really make sense to ask this question of lazy data.
215 */
216 ESCRIPT_DLL_API
217 ValueType::size_type
218 getLength() const;
219
220
221 ESCRIPT_DLL_API
222 DataAbstract*
223 getSlice(const DataTypes::RegionType& region) const;
224
225
226 DataTypes::ValueType::size_type
227 getPointOffset(int sampleNo,
228 int dataPointNo) const;
229
230 DataTypes::ValueType::size_type
231 getPointOffset(int sampleNo,
232 int dataPointNo);
233
234 /**
235 \return the largest samplesize required to evaluate the expression.
236 */
237 ESCRIPT_DLL_API
238 size_t
239 getMaxSampleSize() const;
240
241 /**
242 \brief Compute the value of the expression for the given sample.
243 \return Vector which stores the value of the subexpression for the given sample.
244 \param sampleNo Sample number to evaluate.
245 \param roffset (output parameter) the offset in the return vector where the result begins.
246
247 The return value will be an existing vector so do not deallocate it.
248 */
249 ESCRIPT_DLL_API
250 const ValueType*
251 resolveSample(int sampleNo, size_t& roffset);
252
253 /**
254 \brief if resolve() was called would it produce expanded data.
255 */
256 ESCRIPT_DLL_API
257 bool
258 actsExpanded() const;
259
260 /**
261 \brief Produces an IDENTITY DataLazy containing zero.
262 The result will have the same shape and functionspace as before.
263 */
264 ESCRIPT_DLL_API
265 virtual void
266 setToZero();
267
268
269 ESCRIPT_DLL_API
270 void
271 resolveGroupWorker(std::vector<DataLazy*>& dats);
272
273
274 private:
275 DataReady_ptr m_id; // For IDENTITY nodes, stores a wrapped value.
276 DataLazy_ptr m_left, m_right, m_mask; // operands for operation.
277 ES_optype m_op; // operation to perform.
278
279 size_t m_samplesize; // number of values required to store a sample
280
281 char m_readytype; // E for expanded, T for tagged, C for constant
282
283 int m_axis_offset; // required extra info for general tensor product
284 int m_transpose; // offset and transpose are used for swapaxes as well
285 int m_SL, m_SM, m_SR; // computed properties used in general tensor product
286
287
288 double m_tol; // required extra info for <>0 and ==0
289
290 size_t m_children;
291 size_t m_height;
292
293 int* m_sampleids; // may be NULL
294 DataVector m_samples;
295
296 /**
297 Allocates sample storage at each node
298 */
299 void LazyNodeSetup();
300
301
302 const DataTypes::ValueType*
303 resolveNodeUnary(int tid, int sampleNo, size_t& roffset);
304
305
306 const DataTypes::ValueType*
307 resolveNodeReduction(int tid, int sampleNo, size_t& roffset);
308
309 const DataTypes::ValueType*
310 resolveNodeSample(int tid, int sampleNo, size_t& roffset);
311
312 const DataTypes::ValueType*
313 resolveNodeBinary(int tid, int sampleNo, size_t& roffset);
314
315 const DataTypes::ValueType*
316 resolveNodeNP1OUT(int tid, int sampleNo, size_t& roffset);
317
318 const DataTypes::ValueType*
319 resolveNodeNP1OUT_P(int tid, int sampleNo, size_t& roffset);
320
321 const DataTypes::ValueType*
322 resolveNodeTProd(int tid, int sampleNo, size_t& roffset);
323
324 const DataTypes::ValueType*
325 resolveNodeNP1OUT_2P(int tid, int sampleNo, size_t& roffset);
326
327 const DataTypes::ValueType*
328 resolveNodeCondEval(int tid, int sampleNo, size_t& roffset);
329
330 /**
331 Does the work for toString.
332 */
333 void
334 intoString(std::ostringstream& oss) const;
335
336 /**
337 \warning internal use only!!
338 */
339 void
340 intoTreeString(std::ostringstream& oss,std::string indent) const;
341
342 /**
343 \brief Converts the DataLazy into an IDENTITY storing the value of the expression.
344 This method uses the original methods on the Data class to evaluate the expressions.
345 For this reason, it should not be used on DataExpanded instances. (To do so would defeat
346 the purpose of using DataLazy in the first place).
347 */
348 void
349 collapse(); // converts the node into an IDENTITY node
350
351
352 /**
353 \brief Evaluates the expression using methods on Data.
354 This does the work for the collapse method.
355 For reasons of efficiency do not call this method on DataExpanded nodes.
356 */
357 DataReady_ptr
358 collapseToReady();
359
360 /**
361 \brief resolve the expression can store it in the current node
362 The current node will be converted to an identity node.
363 */
364 void
365 resolveToIdentity();
366
367 /**
368 \brief helper method for resolveToIdentity and the identity constructor
369 */
370 void
371 makeIdentity(const DataReady_ptr& p);
372
373
374 /**
375 \brief resolve to a ReadyData object using storage at nodes
376 */
377 DataReady_ptr
378 resolveNodeWorker();
379
380 };
381
382 }
383 #endif

  ViewVC Help
Powered by ViewVC 1.1.26