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

Contents of /branches/numpy/escript/src/DataLazy.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2396 - (show annotations)
Thu Apr 23 23:58:29 2009 UTC (9 years, 11 months ago) by jfenwick
File MIME type: text/plain
File size: 12719 byte(s)
Branching to port escript to use numpy rather than numarray

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 #include "BufferGroup.h"
26
27 namespace escript {
28
29 // For the purposes of unit testing and maintaining sanity, it is important that this enum be contiguous
30 enum ES_optype
31 {
32 UNKNOWNOP=0,
33 IDENTITY=1,
34 ADD=2,
35 SUB=3,
36 MUL=4,
37 DIV=5,
38 POW=6,
39 SIN=POW+1,
40 COS=SIN+1,
41 TAN=SIN+2,
42 ASIN=SIN+3,
43 ACOS=SIN+4,
44 ATAN=SIN+5,
45 SINH=SIN+6,
46 COSH=SIN+7,
47 TANH=SIN+8,
48 ERF=SIN+9,
49 ASINH=SIN+10,
50 ACOSH=SIN+11,
51 ATANH=SIN+12,
52 LOG10=ATANH+1,
53 LOG=LOG10+1,
54 SIGN=LOG10+2,
55 ABS=LOG10+3,
56 NEG=LOG10+4,
57 POS=LOG10+5,
58 EXP=LOG10+6,
59 SQRT=LOG10+7,
60 RECIP=LOG10+8,
61 GZ=RECIP+1,
62 LZ=GZ+1,
63 GEZ=GZ+2,
64 LEZ=GZ+3,
65 NEZ=GZ+4,
66 EZ=GZ+5,
67 SYM=EZ+1,
68 NSYM=SYM+1,
69 PROD=NSYM+1,
70 TRANS=PROD+1,
71 TRACE=TRANS+1
72 };
73
74 ESCRIPT_DLL_API
75 const std::string&
76 opToString(ES_optype op);
77
78 /**
79 \class escript::DataLazy
80 \brief Wraps an expression tree of other DataObjects.
81 The data will be evaluated when required.
82
83
84 NOTE: This class assumes that the Data being pointed at are immutable.
85 */
86
87 class DataLazy;
88
89 typedef POINTER_WRAPPER_CLASS(DataLazy) DataLazy_ptr;
90 typedef POINTER_WRAPPER_CLASS(const DataLazy) const_DataLazy_ptr;
91
92 class DataLazy : public DataAbstract
93 {
94
95 typedef DataAbstract parent;
96 typedef DataTypes::ValueType ValueType;
97 typedef DataTypes::ShapeType ShapeType;
98
99 public:
100 /**
101 \brief Create an IDENTITY DataLazy for the given DataAbstract.
102 \param p DataAbstract to be wrapped.
103 \throws DataException if p is lazy data or it is not constant, tagged or expanded.
104 */
105 ESCRIPT_DLL_API
106 DataLazy(DataAbstract_ptr p);
107
108
109 /**
110 \brief Produce a DataLazy for a unary operation.
111 \param left DataAbstract to be operated on.
112 \param op unary operation to perform.
113 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
114 Note that IDENTITY is not considered a unary operation.
115 */
116 ESCRIPT_DLL_API
117 DataLazy(DataAbstract_ptr left, ES_optype op);
118
119 /**
120 \brief Produce a DataLazy for a unary operation.
121 \param left DataAbstract to be operated on.
122 \param op unary operation to perform.
123 \param tol tolerance for operation
124 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
125 Note that IDENTITY is not considered a unary operation.
126 */
127 ESCRIPT_DLL_API
128 DataLazy(DataAbstract_ptr left, ES_optype op, double tol);
129
130 /**
131 \brief Produce a DataLazy for a unary operation which requires a parameter.
132 \param left DataAbstract to be operated on.
133 \param op unary operation to perform.
134 \param axis_offset the parameter for the operation
135 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
136 Note that IDENTITY is not considered a unary operation.
137 */
138 ESCRIPT_DLL_API
139 DataLazy(DataAbstract_ptr left, ES_optype op, int axis_offset);
140
141
142 /**
143 \brief Produce a DataLazy for a binary operation.
144 \param left left operand
145 \param right right operand
146 \param op unary operation to perform.
147 \throws DataException if op is not a binary operation or if left or right cannot be converted to a DataLazy.
148 */
149 ESCRIPT_DLL_API
150 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);
151
152 /**
153 \brief Produce a DataLazy for a binary operation with additional paramters.
154 \param left left operand
155 \param right right operand
156 \param op unary operation to perform.
157 \param axis_offset
158 \param transpose
159 \throws DataException if op is not a binary operation requiring parameters or if left or right cannot be converted to a DataLazy.
160 */
161 ESCRIPT_DLL_API
162 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op, int axis_offset, int transpose);
163
164 ESCRIPT_DLL_API
165 ~DataLazy();
166
167 /**
168 \brief Evaluate the lazy expression.
169 \return A DataReady with the value of the lazy expresion.
170 */
171 ESCRIPT_DLL_API
172 DataReady_ptr
173 resolve();
174
175 ESCRIPT_DLL_API
176 std::string
177 toString() const;
178
179 ESCRIPT_DLL_API
180 DataAbstract*
181 deepCopy();
182
183
184 /**
185 \brief
186 This method throws an exception. It does not really make sense to ask this question of lazy data.
187 */
188 ESCRIPT_DLL_API
189 ValueType::size_type
190 getLength() const;
191
192
193 ESCRIPT_DLL_API
194 DataAbstract*
195 getSlice(const DataTypes::RegionType& region) const;
196
197
198 DataTypes::ValueType::size_type
199 getPointOffset(int sampleNo,
200 int dataPointNo) const;
201
202 DataTypes::ValueType::size_type
203 getPointOffset(int sampleNo,
204 int dataPointNo);
205
206
207 /**
208 \return the number of samples which need to be stored to evaluate the expression.
209 */
210 ESCRIPT_DLL_API
211 int
212 getBuffsRequired() const;
213
214 /**
215 \return the largest samplesize required to evaluate the expression.
216 */
217 ESCRIPT_DLL_API
218 size_t
219 getMaxSampleSize() const;
220
221 /**
222 \return the size of the buffer required to evaulate a sample for this object
223 */
224 ESCRIPT_DLL_API
225 size_t
226 getSampleBufferSize() const;
227
228 /**
229 \brief Compute the value of the expression for the given sample.
230 \return Vector which stores the value of the subexpression for the given sample.
231 \param v A vector to store intermediate results.
232 \param offset Index in v to begin storing results.
233 \param sampleNo Sample number to evaluate.
234 \param roffset (output parameter) the offset in the return vector where the result begins.
235
236 The return value will be an existing vector so do not deallocate it.
237 */
238 ESCRIPT_DLL_API
239 const ValueType*
240 resolveSample(ValueType& v, size_t offset, int sampleNo, size_t& roffset);
241
242 /**
243 \brief Compute the value of the expression for the given sample.
244 \return Vector which stores the value of the subexpression for the given sample.
245 \param v A BufferGroup to store intermediate results.
246 \param sampleNo Sample number to evaluate.
247 \param roffset (output parameter) the offset in the return vector where the result begins.
248
249 The return value will be an existing vector so do not deallocate it.
250 */
251 ESCRIPT_DLL_API
252 const ValueType*
253 resolveSample(BufferGroup& bg, int sampleNo, size_t& roffset);
254
255 /**
256 \brief if resolve() was called would it produce expanded data.
257 */
258 ESCRIPT_DLL_API
259 bool
260 actsExpanded() const;
261
262 /**
263 \brief Produces an IDENTITY DataLazy containing zero.
264 The result will have the same shape and functionspace as before.
265 */
266 ESCRIPT_DLL_API
267 virtual void
268 setToZero();
269
270 private:
271 DataReady_ptr m_id; // For IDENTITY nodes, stores a wrapped value.
272 DataLazy_ptr m_left, m_right; // operands for operation.
273 ES_optype m_op; // operation to perform.
274
275 int m_buffsRequired; // how many samples are required to evaluate this expression
276 size_t m_samplesize; // number of values required to store a sample
277
278 char m_readytype; // E for expanded, T for tagged, C for constant
279
280 int m_axis_offset; // required extra info for general tensor product
281 int m_transpose;
282 int m_SL, m_SM, m_SR; // computed properties used in general tensor product
283
284 double m_tol; // required extra info for <>0 and ==0
285
286 size_t m_maxsamplesize; // largest samplesize required by any node in the expression
287 size_t m_children;
288 size_t m_height;
289
290
291 /**
292 Does the work for toString.
293 */
294 void
295 intoString(std::ostringstream& oss) const;
296
297 /**
298 \brief Converts the DataLazy into an IDENTITY storing the value of the expression.
299 This method uses the original methods on the Data class to evaluate the expressions.
300 For this reason, it should not be used on DataExpanded instances. (To do so would defeat
301 the purpose of using DataLazy in the first place).
302 */
303 void
304 collapse(); // converts the node into an IDENTITY node
305
306
307 /**
308 \brief Evaluates the expression using methods on Data.
309 This does the work for the collapse method.
310 For reasons of efficiency do not call this method on DataExpanded nodes.
311 */
312 DataReady_ptr
313 collapseToReady();
314
315 /**
316 \brief resolve the expression can store it in the current node
317 The current node will be converted to an identity node.
318 */
319 void
320 resolveToIdentity();
321
322 /**
323 \brief helper method for resolveToIdentity and the identity constructor
324 */
325 void
326 makeIdentity(const DataReady_ptr& p);
327
328 /**
329 \brief Compute the value of the expression (unary operation) for the given sample.
330 \return Vector which stores the value of the subexpression for the given sample.
331 \param v A vector to store intermediate results.
332 \param offset Index in v to begin storing results.
333 \param sampleNo Sample number to evaluate.
334 \param roffset (output parameter) the offset in the return vector where the result begins.
335
336 The return value will be an existing vector so do not deallocate it.
337 If the result is stored in v it should be stored at the offset given.
338 Everything from offset to the end of v should be considered available for this method to use.
339 */
340 ValueType*
341 resolveUnary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const;
342
343 /**
344 \brief Compute the value of the expression (unary non-pointwise operation) for the given sample.
345 \return Vector which stores the value of the subexpression for the given sample.
346 \param v A vector to store intermediate results.
347 \param offset Index in v to begin storing results.
348 \param sampleNo Sample number to evaluate.
349 \param roffset (output parameter) the offset in the return vector where the result begins.
350
351 The return value will be an existing vector so do not deallocate it.
352 If the result is stored in v it should be stored at the offset given.
353 Everything from offset to the end of v should be considered available for this method to use.
354
355 This method differs from the one above in that deals with operations that are not
356 point-wise. That is, the answer cannot just be written on top of the input.
357 Extra buffers are required for these operations.
358 */
359
360 ValueType*
361 resolveNP1OUT(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
362
363 /**
364 \brief Compute the value of the expression (unary operation) for the given sample.
365 \return Vector which stores the value of the subexpression for the given sample.
366 \param v A vector to store intermediate results.
367 \param offset Index in v to begin storing results.
368 \param sampleNo Sample number to evaluate.
369 \param roffset (output parameter) the offset in the return vector where the result begins.
370
371 The return value will be an existing vector so do not deallocate it.
372 If the result is stored in v it should be stored at the offset given.
373 Everything from offset to the end of v should be considered available for this method to use.
374 */
375 DataTypes::ValueType*
376 resolveNP1OUT_P(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
377
378
379 /**
380 \brief Compute the value of the expression (binary operation) for the given sample.
381 \return Vector which stores the value of the subexpression for the given sample.
382 \param v A vector to store intermediate results.
383 \param offset Index in v to begin storing results.
384 \param sampleNo Sample number to evaluate.
385 \param roffset (output parameter) the offset in the return vector where the result begins.
386
387 The return value will be an existing vector so do not deallocate it.
388 If the result is stored in v it should be stored at the offset given.
389 Everything from offset to the end of v should be considered available for this method to use.
390 */
391 ValueType*
392 resolveBinary(ValueType& v, size_t offset,int sampleNo, size_t& roffset) const;
393
394 /**
395 \brief Compute the value of the expression (tensor product) for the given sample.
396 \return Vector which stores the value of the subexpression for the given sample.
397 \param v A vector to store intermediate results.
398 \param offset Index in v to begin storing results.
399 \param sampleNo Sample number to evaluate.
400 \param roffset (output parameter) the offset in the return vector where the result begins.
401
402 The return value will be an existing vector so do not deallocate it.
403 If the result is stored in v it should be stored at the offset given.
404 Everything from offset to the end of v should be considered available for this method to use.
405 */
406 DataTypes::ValueType*
407 resolveTProd(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
408
409 };
410
411 }
412 #endif

  ViewVC Help
Powered by ViewVC 1.1.26