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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

branches/schroedinger/escript/src/DataLazy.h revision 1865 by jfenwick, Thu Oct 9 03:53:57 2008 UTC trunk/escript/src/DataLazy.h revision 2737 by jfenwick, Tue Nov 3 00:44:00 2009 UTC
# Line 1  Line 1 
1    
2  /*******************************************************  /*******************************************************
3  *  *
4  * Copyright (c) 2003-2008 by University of Queensland  * Copyright (c) 2003-2009 by University of Queensland
5  * Earth Systems Science Computational Center (ESSCC)  * Earth Systems Science Computational Center (ESSCC)
6  * http://www.uq.edu.au/esscc  * http://www.uq.edu.au/esscc
7  *  *
# Line 17  Line 17 
17  #include "system_dep.h"  #include "system_dep.h"
18    
19  #include "DataAbstract.h"  #include "DataAbstract.h"
 //#include "DataTypes.h"  
 //#include "FunctionSpace.h"  
20    
21  #include <string>  #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 {  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  enum ES_optype
35  {  {
36      UNKNOWNOP=0,      UNKNOWNOP=0,
37      IDENTITY=1      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        MINVAL=SWAP+1,
78        MAXVAL=MINVAL+1
79  };  };
80    
81    ESCRIPT_DLL_API
82  const std::string&  const std::string&
83  opToString(ES_optype op);  opToString(ES_optype op);
84    
85  /**  /**
86  \class escript::DataLazy  \class escript::DataLazy
87  \brief Wraps an expression tree of other DataObjects.  \brief Wraps an expression tree of other DataObjects.
88  The values of DataPoints are computed when requested rather than all at once.  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  class DataLazy : public DataAbstract
100  {  {
101    
# Line 47  typedef DataTypes::ValueType ValueType; Line 104  typedef DataTypes::ValueType ValueType;
104  typedef DataTypes::ShapeType ShapeType;  typedef DataTypes::ShapeType ShapeType;
105    
106  public:  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    ESCRIPT_DLL_API
113    DataLazy(DataAbstract_ptr p);    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    ESCRIPT_DLL_API
157    DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);    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    ESCRIPT_DLL_API
169    ~DataLazy();    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      ESCRIPT_DLL_API
184      ~DataLazy();
185    
186    /**    /**
187    \brief Compute all data points in the expression tree    \brief Evaluate the lazy expression.
188      \return A DataReady with the value of the lazy expresion.
189    */    */
190    ESCRIPT_DLL_API    ESCRIPT_DLL_API
191    DataReady_ptr resolve();    DataReady_ptr
192      resolve();
193    
194    ESCRIPT_DLL_API    ESCRIPT_DLL_API
195    std::string    std::string
# Line 75  public: Line 202  public:
202    
203    /**    /**
204       \brief       \brief
205       Return the number of doubles that would be stored for this Data object if it were resolved.       This method throws an exception. It does not really make sense to ask this question of lazy data.
206    */    */
207    ESCRIPT_DLL_API    ESCRIPT_DLL_API
208    ValueType::size_type    ValueType::size_type
# Line 91  public: Line 218  public:
218    getPointOffset(int sampleNo,    getPointOffset(int sampleNo,
219                   int dataPointNo) const;                   int dataPointNo) const;
220    
221      DataTypes::ValueType::size_type
222      getPointOffset(int sampleNo,
223                     int dataPointNo);
224    
225    
226      /**
227        \return the number of samples which need to be stored to evaluate the expression.
228      */
229      ESCRIPT_DLL_API
230      int
231      getBuffsRequired() const;
232    
233      /**
234        \return the largest samplesize required to evaluate the expression.
235      */
236      ESCRIPT_DLL_API
237      size_t
238      getMaxSampleSize() const;
239    
240      /**
241        \return the size of the buffer required to evaulate a sample for this object
242      */
243      ESCRIPT_DLL_API
244      size_t
245      getSampleBufferSize() const;
246    
247       /**
248      \brief Compute the value of the expression for the given sample.
249      \return Vector which stores the value of the subexpression for the given sample.
250      \param bg A BufferGroup to store intermediate results.
251      \param sampleNo Sample number to evaluate.
252      \param roffset (output parameter) the offset in the return vector where the result begins.
253    
254      The return value will be an existing vector so do not deallocate it.
255      */
256      ESCRIPT_DLL_API
257      const ValueType*
258      resolveSample(BufferGroup& bg, int sampleNo, size_t& roffset);
259    
260      /**
261      \brief if resolve() was called would it produce expanded data.
262      */
263      ESCRIPT_DLL_API
264      bool
265      actsExpanded() const;
266    
267      /**
268         \brief Produces an IDENTITY DataLazy containing zero.
269         The result will have the same shape and functionspace as before.
270      */
271      ESCRIPT_DLL_API
272      virtual void
273      setToZero();
274    
275  private:  private:
276    DataAbstract_ptr m_left, m_right;    DataReady_ptr m_id;   //  For IDENTITY nodes, stores a wrapped value.
277    ES_optype m_op;    DataLazy_ptr m_left, m_right; // operands for operation.
278    size_t length;    // number of values represented by the operation    ES_optype m_op;   // operation to perform.
279    
280      int m_buffsRequired;  // how many samples are required to evaluate this expression
281      size_t m_samplesize;  // number of values required to store a sample
282    
283      char m_readytype; // E for expanded, T for tagged, C for constant
284    
285      int m_axis_offset;    // required extra info for general tensor product
286      int m_transpose;  // offset and transpose are used for swapaxes as well
287      int m_SL, m_SM, m_SR; // computed properties used in general tensor product
288    
289    
290      double m_tol;     // required extra info for <>0 and ==0
291    
292      size_t m_maxsamplesize;   // largest samplesize required by any node in the expression
293      size_t m_children;
294      size_t m_height;
295    
296    #ifdef LAZY_NODE_STORAGE
297    
298      int* m_sampleids;     // may be NULL
299      DataVector m_samples;  
300    
301    #endif // LAZY_NODE_STORAGE
302    
303    
304    #ifdef LAZY_NODE_STORAGE
305      /**
306      Allocates sample storage at each node
307      */
308      void LazyNodeSetup();
309    
310    
311      const DataTypes::ValueType*
312      resolveNodeUnary(int tid, int sampleNo, size_t& roffset);
313    
314    
315      const DataTypes::ValueType*
316      resolveNodeReduction(int tid, int sampleNo, size_t& roffset);  
317    
318      const DataTypes::ValueType*
319      resolveNodeSample(int tid, int sampleNo, size_t& roffset);
320    
321      const DataTypes::ValueType*
322      resolveNodeBinary(int tid, int sampleNo, size_t& roffset);
323    
324      const DataTypes::ValueType*
325      resolveNodeNP1OUT(int tid, int sampleNo, size_t& roffset);
326    
327      const DataTypes::ValueType*
328      resolveNodeNP1OUT_P(int tid, int sampleNo, size_t& roffset);
329    
330      const DataTypes::ValueType*
331      resolveNodeTProd(int tid, int sampleNo, size_t& roffset);
332    
333      const DataTypes::ValueType*
334      resolveNodeNP1OUT_2P(int tid, int sampleNo, size_t& roffset);
335      
336    #endif
337    
338      /**
339      Does the work for toString.
340      */
341      void
342      intoString(std::ostringstream& oss) const;
343    
344      /**
345      \warning internal use only!!
346      */
347      void
348      intoTreeString(std::ostringstream& oss,std::string indent) const;
349    
350      /**
351       \brief Converts the DataLazy into an IDENTITY storing the value of the expression.
352       This method uses the original methods on the Data class to evaluate the expressions.
353       For this reason, it should not be used on DataExpanded instances. (To do so would defeat
354       the purpose of using DataLazy in the first place).
355      */
356      void
357      collapse();       // converts the node into an IDENTITY node
358    
359    
360      /**
361      \brief Evaluates the expression using methods on Data.
362      This does the work for the collapse method.
363      For reasons of efficiency do not call this method on DataExpanded nodes.
364      */
365      DataReady_ptr
366      collapseToReady();
367    
368      /**
369      \brief resolve the expression can store it in the current node
370      The current node will be converted to an identity node.
371      */
372      void
373      resolveToIdentity();
374    
375      /**
376      \brief helper method for resolveToIdentity and the identity constructor
377      */
378      void
379      makeIdentity(const DataReady_ptr& p);
380    
381      /**
382      \brief resolve to a ReadyData object using a vector buffer.
383      */
384      DataReady_ptr
385      resolveVectorWorker();
386    
387    #ifdef LAZY_NODE_STORAGE
388      /**
389      \brief resolve to a ReadyData object using storage at nodes
390      */
391      DataReady_ptr
392      resolveNodeWorker();
393    #endif
394    
395      /**
396      \brief Compute the value of the expression for the given sample - using the vector buffer approach.
397      \return Vector which stores the value of the subexpression for the given sample.
398      \param v A vector to store intermediate results.
399      \param offset Index in v to begin storing results.
400      \param sampleNo Sample number to evaluate.
401      \param roffset (output parameter) the offset in the return vector where the result begins.
402    
403      The return value will be an existing vector so do not deallocate it.
404      */
405      ESCRIPT_DLL_API
406      const ValueType*
407      resolveVectorSample(ValueType& v,  size_t offset, int sampleNo, size_t& roffset);
408    
409    
410      /**
411      \brief Compute the value of the expression (unary operation) for the given sample.
412      \return Vector which stores the value of the subexpression for the given sample.
413      \param v A vector to store intermediate results.
414      \param offset Index in v to begin storing results.
415      \param sampleNo Sample number to evaluate.
416      \param roffset (output parameter) the offset in the return vector where the result begins.
417    
418      The return value will be an existing vector so do not deallocate it.
419      If the result is stored in v it should be stored at the offset given.
420      Everything from offset to the end of v should be considered available for this method to use.
421      */
422      ValueType*
423      resolveUnary(ValueType& v,  size_t offset,int sampleNo,  size_t& roffset) const;
424    
425      /**
426      \brief Compute the value of the expression (reduction operation) for the given sample.
427      \return Vector which stores the value of the subexpression for the given sample.
428      \param v A vector to store intermediate results.
429      \param offset Index in v to begin storing results.
430      \param sampleNo Sample number to evaluate.
431      \param roffset (output parameter) the offset in the return vector where the result begins.
432    
433      The return value will be an existing vector so do not deallocate it.
434      If the result is stored in v it should be stored at the offset given.
435      Everything from offset to the end of v should be considered available for this method to use.
436      */
437      ValueType*
438      resolveReduction(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
439    
440      /**
441      \brief Compute the value of the expression (unary non-pointwise 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      This method differs from the one above in that deals with operations that are not
453      point-wise. That is, the answer cannot just be written on top of the input.
454      Extra buffers are required for these operations.
455      */
456    
457      ValueType*
458      resolveNP1OUT(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
459    
460    /**
461      \brief Compute the value of the expression (unary operation) for the given sample.
462      \return Vector which stores the value of the subexpression for the given sample.
463      \param v A vector to store intermediate results.
464      \param offset Index in v to begin storing results.
465      \param sampleNo Sample number to evaluate.
466      \param roffset (output parameter) the offset in the return vector where the result begins.
467    
468      The return value will be an existing vector so do not deallocate it.
469      If the result is stored in v it should be stored at the offset given.
470      Everything from offset to the end of v should be considered available for this method to use.
471    */
472    DataTypes::ValueType*
473    resolveNP1OUT_P(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
474    
475    /**
476      \brief Compute the value of the expression (unary operation with int params) for the given sample.
477      \return Vector which stores the value of the subexpression for the given sample.
478      \param v A vector to store intermediate results.
479      \param offset Index in v to begin storing results.
480      \param sampleNo Sample number to evaluate.
481      \param roffset (output parameter) the offset in the return vector where the result begins.
482    
483      The return value will be an existing vector so do not deallocate it.
484      If the result is stored in v it should be stored at the offset given.
485      Everything from offset to the end of v should be considered available for this method to use.
486    */
487    DataTypes::ValueType*
488    resolveNP1OUT_2P(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
489    
490    
491      /**
492      \brief Compute the value of the expression (binary operation) for the given sample.
493      \return Vector which stores the value of the subexpression for the given sample.
494      \param v A vector to store intermediate results.
495      \param offset Index in v to begin storing results.
496      \param sampleNo Sample number to evaluate.
497      \param roffset (output parameter) the offset in the return vector where the result begins.
498    
499      The return value will be an existing vector so do not deallocate it.
500      If the result is stored in v it should be stored at the offset given.
501      Everything from offset to the end of v should be considered available for this method to use.
502      */
503      ValueType*
504      resolveBinary(ValueType& v,  size_t offset,int sampleNo,  size_t& roffset) const;
505    
506      /**
507      \brief Compute the value of the expression (tensor product) for the given sample.
508      \return Vector which stores the value of the subexpression for the given sample.
509      \param v A vector to store intermediate results.
510      \param offset Index in v to begin storing results.
511      \param sampleNo Sample number to evaluate.
512      \param roffset (output parameter) the offset in the return vector where the result begins.
513    
514      The return value will be an existing vector so do not deallocate it.
515      If the result is stored in v it should be stored at the offset given.
516      Everything from offset to the end of v should be considered available for this method to use.
517      */
518      DataTypes::ValueType*
519      resolveTProd(ValueType& v,  size_t offset, int sampleNo, size_t& roffset) const;
520    
521  };  };
522    
523  }  }

Legend:
Removed from v.1865  
changed lines
  Added in v.2737

  ViewVC Help
Powered by ViewVC 1.1.26