ESYS13  Revision_
DataLazy.h
Go to the documentation of this file.
00001 
00002 /*******************************************************
00003 *
00004 * Copyright (c) 2003-2012 by University of Queensland
00005 * Earth Systems Science Computational Center (ESSCC)
00006 * http://www.uq.edu.au/esscc
00007 *
00008 * Primary Business: Queensland, Australia
00009 * Licensed under the Open Software License version 3.0
00010 * http://www.opensource.org/licenses/osl-3.0.php
00011 *
00012 *******************************************************/
00013 
00014 
00015 #if !defined escript_DataLazy_20081008_H
00016 #define escript_DataLazy_20081008_H
00017 #include "system_dep.h"
00018 
00019 #include "DataAbstract.h"
00020 
00021 #include <string>
00022 #include <functional>
00023 
00024 #include "LocalOps.h"       // for tensor_binary_op
00025 #include "DataVector.h"     // for ElementType
00026 
00027 
00028 //#define LAZY_NODE_STORAGE
00029 
00030 namespace escript {
00031 
00032 // For the purposes of unit testing and maintaining sanity, it is important that this enum be contiguous
00033 enum ES_optype
00034 {
00035     UNKNOWNOP=0,
00036     IDENTITY=1,
00037     ADD=2,
00038     SUB=3,
00039     MUL=4,
00040     DIV=5,
00041     POW=6,
00042     SIN=POW+1,
00043     COS=SIN+1,
00044     TAN=SIN+2,
00045     ASIN=SIN+3,
00046     ACOS=SIN+4,
00047     ATAN=SIN+5,
00048     SINH=SIN+6,
00049     COSH=SIN+7,
00050     TANH=SIN+8,
00051     ERF=SIN+9,
00052     ASINH=SIN+10,
00053     ACOSH=SIN+11,
00054     ATANH=SIN+12,
00055     LOG10=ATANH+1,
00056     LOG=LOG10+1,
00057     SIGN=LOG10+2,
00058     ABS=LOG10+3,
00059     NEG=LOG10+4,
00060     POS=LOG10+5,
00061     EXP=LOG10+6,
00062     SQRT=LOG10+7,
00063     RECIP=LOG10+8,
00064     GZ=RECIP+1,
00065     LZ=GZ+1,
00066     GEZ=GZ+2,
00067     LEZ=GZ+3,
00068     NEZ=GZ+4,
00069     EZ=GZ+5,
00070     SYM=EZ+1,
00071     NSYM=SYM+1,
00072     PROD=NSYM+1,
00073     TRANS=PROD+1,
00074     TRACE=TRANS+1,
00075     SWAP=TRACE+1,
00076     MINVAL=SWAP+1,
00077     MAXVAL=MINVAL+1,
00078     CONDEVAL=MAXVAL+1
00079 };
00080 
00081 ESCRIPT_DLL_API
00082 const std::string&
00083 opToString(ES_optype op);
00084 
00094 class DataLazy;
00095 
00096 typedef POINTER_WRAPPER_CLASS(DataLazy) DataLazy_ptr;
00097 typedef POINTER_WRAPPER_CLASS(const DataLazy) const_DataLazy_ptr;
00098 
00099 class DataLazy : public DataAbstract
00100 {
00101 
00102 typedef DataAbstract parent;
00103 typedef DataTypes::ValueType ValueType;
00104 typedef DataTypes::ShapeType ShapeType;
00105 
00106 public:
00112   ESCRIPT_DLL_API
00113   DataLazy(DataAbstract_ptr p);
00114 
00115 
00123   ESCRIPT_DLL_API
00124   DataLazy(DataAbstract_ptr left, ES_optype op);
00125 
00134   ESCRIPT_DLL_API
00135   DataLazy(DataAbstract_ptr left, ES_optype op, double tol);
00136 
00145   ESCRIPT_DLL_API  
00146   DataLazy(DataAbstract_ptr left, ES_optype op, int axis_offset);
00147 
00148 
00156   ESCRIPT_DLL_API
00157   DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);
00158 
00168   ESCRIPT_DLL_API
00169   DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op, int axis_offset, int transpose);
00170 
00180   ESCRIPT_DLL_API
00181   DataLazy(DataAbstract_ptr left, ES_optype op, const int axis0, const int axis1);
00182 
00189   ESCRIPT_DLL_API
00190   DataLazy(DataAbstract_ptr mask, DataAbstract_ptr left, DataAbstract_ptr right/*, double tol*/);
00191 
00192   ESCRIPT_DLL_API
00193   ~DataLazy();
00194 
00199   ESCRIPT_DLL_API
00200   DataReady_ptr 
00201   resolve();
00202 
00203   ESCRIPT_DLL_API
00204   std::string
00205   toString() const;
00206 
00207   ESCRIPT_DLL_API
00208   DataAbstract* 
00209   deepCopy();
00210 
00211 
00216   ESCRIPT_DLL_API
00217   ValueType::size_type
00218   getLength() const;
00219 
00220 
00221   ESCRIPT_DLL_API
00222   DataAbstract*
00223   getSlice(const DataTypes::RegionType& region) const;
00224 
00225 
00226   DataTypes::ValueType::size_type 
00227   getPointOffset(int sampleNo,
00228                  int dataPointNo) const;
00229 
00230   DataTypes::ValueType::size_type 
00231   getPointOffset(int sampleNo,
00232                  int dataPointNo);
00233 
00237   ESCRIPT_DLL_API
00238   size_t
00239   getMaxSampleSize() const;
00240 
00249   ESCRIPT_DLL_API
00250   const ValueType*
00251   resolveSample(int sampleNo, size_t& roffset); 
00252 
00256   ESCRIPT_DLL_API
00257   bool
00258   actsExpanded() const;
00259 
00264   ESCRIPT_DLL_API
00265   virtual void
00266   setToZero();
00267 
00268   
00269   ESCRIPT_DLL_API
00270   void
00271   resolveGroupWorker(std::vector<DataLazy*>& dats);
00272 
00273 
00274 private:
00275   DataReady_ptr m_id;   //  For IDENTITY nodes, stores a wrapped value.
00276   DataLazy_ptr m_left, m_right, m_mask; // operands for operation.
00277   ES_optype m_op;   // operation to perform.
00278 
00279   size_t m_samplesize;  // number of values required to store a sample
00280 
00281   char m_readytype; // E for expanded, T for tagged, C for constant
00282 
00283   int m_axis_offset;    // required extra info for general tensor product
00284   int m_transpose;  // offset and transpose are used for swapaxes as well
00285   int m_SL, m_SM, m_SR; // computed properties used in general tensor product
00286 
00287 
00288   double m_tol;     // required extra info for <>0 and ==0
00289 
00290   size_t m_children;
00291   size_t m_height;
00292 
00293   int* m_sampleids;     // may be NULL
00294   DataVector m_samples;  
00295 
00299   void LazyNodeSetup();
00300 
00301 
00302   const DataTypes::ValueType*
00303   resolveNodeUnary(int tid, int sampleNo, size_t& roffset);
00304 
00305 
00306   const DataTypes::ValueType*
00307   resolveNodeReduction(int tid, int sampleNo, size_t& roffset);  
00308 
00309   const DataTypes::ValueType*
00310   resolveNodeSample(int tid, int sampleNo, size_t& roffset);
00311 
00312   const DataTypes::ValueType*
00313   resolveNodeBinary(int tid, int sampleNo, size_t& roffset);
00314 
00315   const DataTypes::ValueType*
00316   resolveNodeNP1OUT(int tid, int sampleNo, size_t& roffset);
00317 
00318   const DataTypes::ValueType*
00319   resolveNodeNP1OUT_P(int tid, int sampleNo, size_t& roffset);
00320 
00321   const DataTypes::ValueType*
00322   resolveNodeTProd(int tid, int sampleNo, size_t& roffset);
00323 
00324   const DataTypes::ValueType*
00325   resolveNodeNP1OUT_2P(int tid, int sampleNo, size_t& roffset);
00326 
00327   const DataTypes::ValueType*
00328   resolveNodeCondEval(int tid, int sampleNo, size_t& roffset);
00329 
00333   void
00334   intoString(std::ostringstream& oss) const;
00335 
00339   void
00340   intoTreeString(std::ostringstream& oss,std::string indent) const;
00341 
00348   void
00349   collapse();       // converts the node into an IDENTITY node
00350 
00351 
00357   DataReady_ptr
00358   collapseToReady();
00359 
00364   void
00365   resolveToIdentity();
00366 
00370   void 
00371   makeIdentity(const DataReady_ptr& p);
00372 
00373 
00377   DataReady_ptr
00378   resolveNodeWorker();
00379 
00380 };
00381 
00382 }
00383 #endif