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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2635 - (show annotations)
Thu Aug 27 04:54:41 2009 UTC (10 years, 7 months ago) by jfenwick
File MIME type: text/plain
File size: 15498 byte(s)
A bunch of changes related to saveDataCSV.
[Not completed or unit tested yet]

Added saveDataCSV to util.py
AbstractDomain (and MeshAdapter) have a commonFunctionSpace method to 
take a group of FunctionSpaces and return something they can all be interpolated to.

Added pointToStream() in DataTypes to help print points.

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

  ViewVC Help
Powered by ViewVC 1.1.26