/[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 1903 by jfenwick, Wed Oct 22 06:11:01 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 22  Line 22 
22  #include <functional>  #include <functional>
23    
24  #include "LocalOps.h"       // for tensor_binary_op  #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,
# Line 33  enum ES_optype Line 39  enum ES_optype
39      SUB=3,      SUB=3,
40      MUL=4,      MUL=4,
41      DIV=5,      DIV=5,
42      SIN=6,      POW=6,
43      COS=7,      SIN=POW+1,
44      TAN=8,      COS=SIN+1,
45      ASIN=9,      TAN=SIN+2,
46      ACOS=10,      ASIN=SIN+3,
47      ATAN=11,      ACOS=SIN+4,
48      SINH=12,      ATAN=SIN+5,
49      COSH=13,      SINH=SIN+6,
50      TANH=14,      COSH=SIN+7,
51      ERF=15,      TANH=SIN+8,
52      ASINH=16,      ERF=SIN+9,
53      ACOSH=17,      ASINH=SIN+10,
54      ATANH=18,      ACOSH=SIN+11,
55      LOG10=19,      ATANH=SIN+12,
56      LOG=20,      LOG10=ATANH+1,
57      SIGN=21,      LOG=LOG10+1,
58      ABS=22,      SIGN=LOG10+2,
59      NEG=23,      ABS=LOG10+3,
60      POS=24,      NEG=LOG10+4,
61      EXP=25,      POS=LOG10+5,
62      SQRT=26,      EXP=LOG10+6,
63      RECIP=27,      SQRT=LOG10+7,
64      GZ=28,      RECIP=LOG10+8,
65      LZ=29,      GZ=RECIP+1,
66      GEZ=30,      LZ=GZ+1,
67      LEZ=31      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    
# Line 106  public: Line 124  public:
124    DataLazy(DataAbstract_ptr left, ES_optype op);    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.    \brief Produce a DataLazy for a binary operation.
151    \param left left operand    \param left left operand
152    \param right right operand    \param right right operand
# Line 115  public: Line 156  public:
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
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    ESCRIPT_DLL_API    ESCRIPT_DLL_API
184    ~DataLazy();    ~DataLazy();
185    
# Line 137  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 153  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.      \return the number of samples which need to be stored to evaluate the expression.
# Line 162  public: Line 231  public:
231    getBuffsRequired() const;    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.       \brief Produces an IDENTITY DataLazy containing zero.
269       The result will have the same shape and functionspace as before.       The result will have the same shape and functionspace as before.
270    */    */
# Line 173  private: Line 276  private:
276    DataReady_ptr m_id;   //  For IDENTITY nodes, stores a wrapped value.    DataReady_ptr m_id;   //  For IDENTITY nodes, stores a wrapped value.
277    DataLazy_ptr m_left, m_right; // operands for operation.    DataLazy_ptr m_left, m_right; // operands for operation.
278    ES_optype m_op;   // operation to perform.    ES_optype m_op;   // operation to perform.
   size_t m_length;  // number of values represented by the operation  
279    
280    int m_buffsRequired;  // how many samples are required to evaluate this expression    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    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    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.    Does the work for toString.
# Line 188  private: Line 342  private:
342    intoString(std::ostringstream& oss) const;    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.     \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.     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     For this reason, it should not be used on DataExpanded instances. (To do so would defeat
# Line 206  private: Line 366  private:
366    collapseToReady();    collapseToReady();
367    
368    /**    /**
369    \brief Compute the value of the expression for the given sample.    \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.    \return Vector which stores the value of the subexpression for the given sample.
398    \param v A vector to store intermediate results.    \param v A vector to store intermediate results.
399    \param offset Index in v to begin storing results.    \param offset Index in v to begin storing results.
# Line 215  private: Line 402  private:
402    
403    The return value will be an existing vector so do not deallocate it.    The return value will be an existing vector so do not deallocate it.
404    */    */
405      ESCRIPT_DLL_API
406    const ValueType*    const ValueType*
407    resolveSample(ValueType& v,  size_t offset ,int sampleNo, size_t& roffset);    resolveVectorSample(ValueType& v,  size_t offset, int sampleNo, size_t& roffset);
408    
409    
410    /**    /**
411    \brief Compute the value of the expression (binary operation) for the given sample.    \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.    \return Vector which stores the value of the subexpression for the given sample.
413    \param v A vector to store intermediate results.    \param v A vector to store intermediate results.
414    \param offset Index in v to begin storing results.    \param offset Index in v to begin storing results.
# Line 234  private: Line 423  private:
423    resolveUnary(ValueType& v,  size_t offset,int sampleNo,  size_t& roffset) const;    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.    \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.    \return Vector which stores the value of the subexpression for the given sample.
494    \param v A vector to store intermediate results.    \param v A vector to store intermediate results.
# Line 248  private: Line 503  private:
503    ValueType*    ValueType*
504    resolveBinary(ValueType& v,  size_t offset,int sampleNo,  size_t& roffset) const;    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.1903  
changed lines
  Added in v.2737

  ViewVC Help
Powered by ViewVC 1.1.26