/[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 2500 by jfenwick, Tue Jun 30 00:42:38 2009 UTC
# 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  };  };
78    
79    ESCRIPT_DLL_API
80  const std::string&  const std::string&
81  opToString(ES_optype op);  opToString(ES_optype op);
82    
# Line 106  public: Line 122  public:
122    DataLazy(DataAbstract_ptr left, ES_optype op);    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.    \brief Produce a DataLazy for a binary operation.
149    \param left left operand    \param left left operand
150    \param right right operand    \param right right operand
# Line 115  public: Line 154  public:
154    ESCRIPT_DLL_API    ESCRIPT_DLL_API
155    DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);    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    ESCRIPT_DLL_API
182    ~DataLazy();    ~DataLazy();
183    
# Line 137  public: Line 200  public:
200    
201    /**    /**
202       \brief       \brief
203       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.
204    */    */
205    ESCRIPT_DLL_API    ESCRIPT_DLL_API
206    ValueType::size_type    ValueType::size_type
# Line 153  public: Line 216  public:
216    getPointOffset(int sampleNo,    getPointOffset(int sampleNo,
217                   int dataPointNo) const;                   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.      \return the number of samples which need to be stored to evaluate the expression.
# Line 162  public: Line 229  public:
229    getBuffsRequired() const;    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 v A vector to store intermediate results.
249      \param offset Index in v to begin storing results.
250      \param sampleNo Sample number to evaluate.
251      \param roffset (output parameter) the offset in the return vector where the result begins.
252    
253      The return value will be an existing vector so do not deallocate it.
254      */
255      ESCRIPT_DLL_API
256      const ValueType*
257      resolveSample(ValueType& v,  size_t offset, int sampleNo, size_t& roffset);
258    
259       /**
260      \brief Compute the value of the expression for the given sample.
261      \return Vector which stores the value of the subexpression for the given sample.
262      \param v A BufferGroup to store intermediate results.
263      \param sampleNo Sample number to evaluate.
264      \param roffset (output parameter) the offset in the return vector where the result begins.
265    
266      The return value will be an existing vector so do not deallocate it.
267      */
268      ESCRIPT_DLL_API
269      const ValueType*
270      resolveSample(BufferGroup& bg, int sampleNo, size_t& roffset);
271    
272      /**
273      \brief if resolve() was called would it produce expanded data.
274      */
275      ESCRIPT_DLL_API
276      bool
277      actsExpanded() const;
278    
279      /**
280       \brief Produces an IDENTITY DataLazy containing zero.       \brief Produces an IDENTITY DataLazy containing zero.
281       The result will have the same shape and functionspace as before.       The result will have the same shape and functionspace as before.
282    */    */
# Line 173  private: Line 288  private:
288    DataReady_ptr m_id;   //  For IDENTITY nodes, stores a wrapped value.    DataReady_ptr m_id;   //  For IDENTITY nodes, stores a wrapped value.
289    DataLazy_ptr m_left, m_right; // operands for operation.    DataLazy_ptr m_left, m_right; // operands for operation.
290    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  
291    
292    int m_buffsRequired;  // how many samples are required to evaluate this expression    int m_buffsRequired;  // how many samples are required to evaluate this expression
293    size_t m_samplesize;  // number of values required to store a sample    size_t m_samplesize;  // number of values required to store a sample
294    
295    char m_readytype; // E for expanded, T for tagged, C for constant    char m_readytype; // E for expanded, T for tagged, C for constant
296    
297      int m_axis_offset;    // required extra info for general tensor product
298      int m_transpose;  // offset and transpose are used for swapaxes as well
299      int m_SL, m_SM, m_SR; // computed properties used in general tensor product
300    
301    
302      double m_tol;     // required extra info for <>0 and ==0
303    
304      size_t m_maxsamplesize;   // largest samplesize required by any node in the expression
305      size_t m_children;
306      size_t m_height;
307    
308    #ifdef LAZY_NODE_STORAGE
309    
310      int* m_sampleids;     // may be NULL
311      DataVector m_samples;  
312    
313    #endif // LAZY_NODE_STORAGE
314    
315    
316    #ifdef LAZY_NODE_STORAGE
317      /**
318      Allocates sample storage at each node
319      */
320      void LazyNodeSetup();
321    
322    
323      const DataTypes::ValueType*
324      resolveNodeUnary(int tid, int sampleNo, size_t& roffset);
325    
326      const DataTypes::ValueType*
327      resolveNodeSample(int tid, int sampleNo, size_t& roffset);
328    
329      const DataTypes::ValueType*
330      resolveNodeBinary(int tid, int sampleNo, size_t& roffset);
331    
332      const DataTypes::ValueType*
333      resolveNodeNP1OUT(int tid, int sampleNo, size_t& roffset);
334    
335      const DataTypes::ValueType*
336      resolveNodeNP1OUT_P(int tid, int sampleNo, size_t& roffset);
337    
338      const DataTypes::ValueType*
339      resolveNodeTProd(int tid, int sampleNo, size_t& roffset);
340    
341      const DataTypes::ValueType*
342      resolveNodeNP1OUT_2P(int tid, int sampleNo, size_t& roffset);
343      
344    #endif
345    
346    /**    /**
347    Does the work for toString.    Does the work for toString.
# Line 206  private: Line 368  private:
368    collapseToReady();    collapseToReady();
369    
370    /**    /**
371    \brief Compute the value of the expression for the given sample.    \brief resolve the expression can store it in the current node
372      The current node will be converted to an identity node.
373      */
374      void
375      resolveToIdentity();
376    
377      /**
378      \brief helper method for resolveToIdentity and the identity constructor
379      */
380      void
381      makeIdentity(const DataReady_ptr& p);
382    
383      /**
384      \brief resolve to a ReadyData object using a vector buffer.
385      */
386      DataReady_ptr
387      resolveVectorWorker();
388    
389    #ifdef LAZY_NODE_STORAGE
390      /**
391      \brief resolve to a ReadyData object using storage at nodes
392      */
393      DataReady_ptr
394      resolveNodeWorker();
395    #endif
396    
397      /**
398      \brief Compute the value of the expression (unary operation) for the given sample.
399    \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.
400    \param v A vector to store intermediate results.    \param v A vector to store intermediate results.
401    \param offset Index in v to begin storing results.    \param offset Index in v to begin storing results.
# Line 214  private: Line 403  private:
403    \param roffset (output parameter) the offset in the return vector where the result begins.    \param roffset (output parameter) the offset in the return vector where the result begins.
404    
405    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.
406      If the result is stored in v it should be stored at the offset given.
407      Everything from offset to the end of v should be considered available for this method to use.
408    */    */
409    const ValueType*    ValueType*
410    resolveSample(ValueType& v,  size_t offset ,int sampleNo, size_t& roffset);    resolveUnary(ValueType& v,  size_t offset,int sampleNo,  size_t& roffset) const;
411    
412    /**    /**
413    \brief Compute the value of the expression (binary operation) for the given sample.    \brief Compute the value of the expression (unary non-pointwise operation) for the given sample.
414    \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.
415    \param v A vector to store intermediate results.    \param v A vector to store intermediate results.
416    \param offset Index in v to begin storing results.    \param offset Index in v to begin storing results.
# Line 229  private: Line 420  private:
420    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.
421    If the result is stored in v it should be stored at the offset given.    If the result is stored in v it should be stored at the offset given.
422    Everything from offset to the end of v should be considered available for this method to use.    Everything from offset to the end of v should be considered available for this method to use.
423    
424      This method differs from the one above in that deals with operations that are not
425      point-wise. That is, the answer cannot just be written on top of the input.
426      Extra buffers are required for these operations.
427    */    */
428    
429    ValueType*    ValueType*
430    resolveUnary(ValueType& v,  size_t offset,int sampleNo,  size_t& roffset) const;    resolveNP1OUT(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
431    
432    /**
433      \brief Compute the value of the expression (unary operation) for the given sample.
434      \return Vector which stores the value of the subexpression for the given sample.
435      \param v A vector to store intermediate results.
436      \param offset Index in v to begin storing results.
437      \param sampleNo Sample number to evaluate.
438      \param roffset (output parameter) the offset in the return vector where the result begins.
439    
440      The return value will be an existing vector so do not deallocate it.
441      If the result is stored in v it should be stored at the offset given.
442      Everything from offset to the end of v should be considered available for this method to use.
443    */
444    DataTypes::ValueType*
445    resolveNP1OUT_P(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
446    
447    /**
448      \brief Compute the value of the expression (unary operation with int params) for the given sample.
449      \return Vector which stores the value of the subexpression for the given sample.
450      \param v A vector to store intermediate results.
451      \param offset Index in v to begin storing results.
452      \param sampleNo Sample number to evaluate.
453      \param roffset (output parameter) the offset in the return vector where the result begins.
454    
455      The return value will be an existing vector so do not deallocate it.
456      If the result is stored in v it should be stored at the offset given.
457      Everything from offset to the end of v should be considered available for this method to use.
458    */
459    DataTypes::ValueType*
460    resolveNP1OUT_2P(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const;
461    
462    
463    /**    /**
464    \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.
# Line 248  private: Line 475  private:
475    ValueType*    ValueType*
476    resolveBinary(ValueType& v,  size_t offset,int sampleNo,  size_t& roffset) const;    resolveBinary(ValueType& v,  size_t offset,int sampleNo,  size_t& roffset) const;
477    
478      /**
479      \brief Compute the value of the expression (tensor product) for the given sample.
480      \return Vector which stores the value of the subexpression for the given sample.
481      \param v A vector to store intermediate results.
482      \param offset Index in v to begin storing results.
483      \param sampleNo Sample number to evaluate.
484      \param roffset (output parameter) the offset in the return vector where the result begins.
485    
486      The return value will be an existing vector so do not deallocate it.
487      If the result is stored in v it should be stored at the offset given.
488      Everything from offset to the end of v should be considered available for this method to use.
489      */
490      DataTypes::ValueType*
491      resolveTProd(ValueType& v,  size_t offset, int sampleNo, size_t& roffset) const;
492    
493  };  };
494    
495  }  }

Legend:
Removed from v.1903  
changed lines
  Added in v.2500

  ViewVC Help
Powered by ViewVC 1.1.26