/[escript]/branches/clazy/escriptcore/src/DataLazy.cpp
ViewVC logotype

Diff of /branches/clazy/escriptcore/src/DataLazy.cpp

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

revision 2779 by caltinay, Thu Nov 26 03:51:15 2009 UTC revision 4286 by caltinay, Thu Mar 7 04:28:11 2013 UTC
# Line 1  Line 1 
1    
2  /*******************************************************  /*****************************************************************************
3  *  *
4  * Copyright (c) 2003-2009 by University of Queensland  * Copyright (c) 2003-2013 by University of Queensland
5  * Earth Systems Science Computational Center (ESSCC)  * http://www.uq.edu.au
 * http://www.uq.edu.au/esscc  
6  *  *
7  * Primary Business: Queensland, Australia  * Primary Business: Queensland, Australia
8  * Licensed under the Open Software License version 3.0  * Licensed under the Open Software License version 3.0
9  * http://www.opensource.org/licenses/osl-3.0.php  * http://www.opensource.org/licenses/osl-3.0.php
10  *  *
11  *******************************************************/  * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12    * Development since 2012 by School of Earth Sciences
13    *
14    *****************************************************************************/
15    
16    
17  #include "DataLazy.h"  #include "DataLazy.h"
18  #ifdef USE_NETCDF  #include "esysUtils/Esys_MPI.h"
 #include <netcdfcpp.h>  
 #endif  
 #ifdef PASO_MPI  
 #include <mpi.h>  
 #endif  
19  #ifdef _OPENMP  #ifdef _OPENMP
20  #include <omp.h>  #include <omp.h>
21  #endif  #endif
# Line 30  Line 27 
27    
28  #include "EscriptParams.h"  #include "EscriptParams.h"
29    
30    #ifdef USE_NETCDF
31    #include <netcdfcpp.h>
32    #endif
33    
34  #include <iomanip>      // for some fancy formatting in debug  #include <iomanip>      // for some fancy formatting in debug
35    
36  // #define LAZYDEBUG(X) if (privdebug){X;}  // #define LAZYDEBUG(X) if (privdebug){X;}
# Line 44  bool privdebug=false; Line 45  bool privdebug=false;
45    
46  // #define SIZELIMIT if ((m_height>escript::escriptParams.getTOO_MANY_LEVELS()) || (m_children>escript::escriptParams.getTOO_MANY_NODES())) {cerr << "\n!!!!!!! SIZE LIMIT EXCEEDED " << m_children << ";" << m_height << endl << toString() << endl;resolveToIdentity();}  // #define SIZELIMIT if ((m_height>escript::escriptParams.getTOO_MANY_LEVELS()) || (m_children>escript::escriptParams.getTOO_MANY_NODES())) {cerr << "\n!!!!!!! SIZE LIMIT EXCEEDED " << m_children << ";" << m_height << endl << toString() << endl;resolveToIdentity();}
47    
48  #define SIZELIMIT if ((m_height>escript::escriptParams.getTOO_MANY_LEVELS()) || (m_children>escript::escriptParams.getTOO_MANY_NODES())) {resolveToIdentity();}  // #define SIZELIMIT if ((m_height>escript::escriptParams.getTOO_MANY_LEVELS()) || (m_children>escript::escriptParams.getTOO_MANY_NODES())) {cerr << "SIZE LIMIT EXCEEDED " << m_height << endl;resolveToIdentity();}
49    
50    
51    #define SIZELIMIT if (m_height>escript::escriptParams.getTOO_MANY_LEVELS())  {if (escript::escriptParams.getLAZY_VERBOSE()){cerr << "SIZE LIMIT EXCEEDED height=" << m_height << endl;}resolveToIdentity();}
52    
53  /*  /*
54  How does DataLazy work?  How does DataLazy work?
# Line 58  A special operation, IDENTITY, stores an Line 61  A special operation, IDENTITY, stores an
61  This means that all "internal" nodes in the structure are instances of DataLazy.  This means that all "internal" nodes in the structure are instances of DataLazy.
62    
63  Each operation has a string representation as well as an opgroup - eg G_IDENTITY, G_BINARY, ...  Each operation has a string representation as well as an opgroup - eg G_IDENTITY, G_BINARY, ...
64  Note that IDENITY is not considered a unary operation.  Note that IDENTITY is not considered a unary operation.
65    
66  I am avoiding calling the structure formed a tree because it is not guaranteed to be one (eg c=a+a).  I am avoiding calling the structure formed a tree because it is not guaranteed to be one (eg c=a+a).
67  It must however form a DAG (directed acyclic graph).  It must however form a DAG (directed acyclic graph).
# Line 67  I will refer to individual DataLazy obje Line 70  I will refer to individual DataLazy obje
70  Each node also stores:  Each node also stores:
71  - m_readytype \in {'E','T','C','?'} ~ indicates what sort of DataReady would be produced if the expression was  - m_readytype \in {'E','T','C','?'} ~ indicates what sort of DataReady would be produced if the expression was
72      evaluated.      evaluated.
73  - m_buffsrequired ~ the larged number of samples which would need to be kept simultaneously in order to  - m_buffsrequired ~ the large number of samples which would need to be kept simultaneously in order to
74      evaluate the expression.      evaluate the expression.
75  - m_samplesize ~ the number of doubles stored in a sample.  - m_samplesize ~ the number of doubles stored in a sample.
76    
# Line 140  enum ES_opgroup Line 143  enum ES_opgroup
143     G_NP1OUT_P,      // non-pointwise op with one output requiring a parameter     G_NP1OUT_P,      // non-pointwise op with one output requiring a parameter
144     G_TENSORPROD,    // general tensor product     G_TENSORPROD,    // general tensor product
145     G_NP1OUT_2P,     // non-pointwise op with one output requiring two params     G_NP1OUT_2P,     // non-pointwise op with one output requiring two params
146     G_REDUCTION      // non-pointwise unary op with a scalar output     G_REDUCTION,     // non-pointwise unary op with a scalar output
147       G_CONDEVAL
148  };  };
149    
150    
# Line 156  string ES_opstrings[]={"UNKNOWN","IDENTI Line 160  string ES_opstrings[]={"UNKNOWN","IDENTI
160              "prod",              "prod",
161              "transpose", "trace",              "transpose", "trace",
162              "swapaxes",              "swapaxes",
163              "minval", "maxval"};              "minval", "maxval",
164  int ES_opcount=43;              "condEval"};
165    int ES_opcount=44;
166  ES_opgroup opgroups[]={G_UNKNOWN,G_IDENTITY,G_BINARY,G_BINARY,G_BINARY,G_BINARY, G_BINARY,  ES_opgroup opgroups[]={G_UNKNOWN,G_IDENTITY,G_BINARY,G_BINARY,G_BINARY,G_BINARY, G_BINARY,
167              G_UNARY,G_UNARY,G_UNARY, //10              G_UNARY,G_UNARY,G_UNARY, //10
168              G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,    // 17              G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,    // 17
# Line 168  ES_opgroup opgroups[]={G_UNKNOWN,G_IDENT Line 173  ES_opgroup opgroups[]={G_UNKNOWN,G_IDENT
173              G_TENSORPROD,              G_TENSORPROD,
174              G_NP1OUT_P, G_NP1OUT_P,              G_NP1OUT_P, G_NP1OUT_P,
175              G_NP1OUT_2P,              G_NP1OUT_2P,
176              G_REDUCTION, G_REDUCTION};              G_REDUCTION, G_REDUCTION,
177                G_CONDEVAL};
178  inline  inline
179  ES_opgroup  ES_opgroup
180  getOpgroup(ES_optype op)  getOpgroup(ES_optype op)
# Line 189  resultFS(DataAbstract_ptr left, DataAbst Line 195  resultFS(DataAbstract_ptr left, DataAbst
195    FunctionSpace r=right->getFunctionSpace();    FunctionSpace r=right->getFunctionSpace();
196    if (l!=r)    if (l!=r)
197    {    {
198      if (r.probeInterpolation(l))      signed char res=r.getDomain()->preferredInterpolationOnDomain(r.getTypeCode(), l.getTypeCode());
199        if (res==1)
200      {      {
201      return l;      return l;
202      }      }
203      if (l.probeInterpolation(r))      if (res==-1)
204      {      {
205      return r;      return r;
206      }      }
# Line 333  SwapShape(DataAbstract_ptr left, const i Line 340  SwapShape(DataAbstract_ptr left, const i
340          throw DataException(e.str());          throw DataException(e.str());
341       }       }
342       if (axis1<0 || axis1>rank-1) {       if (axis1<0 || axis1>rank-1) {
343            stringstream e;
344          e << "Error - Data::swapaxes: axis1 must be between 0 and rank-1=" << (rank-1);          e << "Error - Data::swapaxes: axis1 must be between 0 and rank-1=" << (rank-1);
345          throw DataException(e.str());          throw DataException(e.str());
346       }       }
# Line 737  DataLazy::DataLazy(DataAbstract_ptr left Line 745  DataLazy::DataLazy(DataAbstract_ptr left
745  LAZYDEBUG(cout << "(7)Lazy created with " << m_samplesize << endl;)  LAZYDEBUG(cout << "(7)Lazy created with " << m_samplesize << endl;)
746  }  }
747    
748    
749    namespace
750    {
751    
752        inline int max3(int a, int b, int c)
753        {
754        int t=(a>b?a:b);
755        return (t>c?t:c);
756    
757        }
758    }
759    
760    DataLazy::DataLazy(DataAbstract_ptr mask, DataAbstract_ptr left, DataAbstract_ptr right/*, double tol*/)
761        : parent(left->getFunctionSpace(), left->getShape()),
762        m_op(CONDEVAL),
763        m_axis_offset(0),
764        m_transpose(0),
765        m_tol(0)
766    {
767    
768       DataLazy_ptr lmask;
769       DataLazy_ptr lleft;
770       DataLazy_ptr lright;
771       if (!mask->isLazy())
772       {
773        lmask=DataLazy_ptr(new DataLazy(mask));
774       }
775       else
776       {
777        lmask=dynamic_pointer_cast<DataLazy>(mask);
778       }
779       if (!left->isLazy())
780       {
781        lleft=DataLazy_ptr(new DataLazy(left));
782       }
783       else
784       {
785        lleft=dynamic_pointer_cast<DataLazy>(left);
786       }
787       if (!right->isLazy())
788       {
789        lright=DataLazy_ptr(new DataLazy(right));
790       }
791       else
792       {
793        lright=dynamic_pointer_cast<DataLazy>(right);
794       }
795       m_readytype=lmask->m_readytype;
796       if ((lleft->m_readytype!=lright->m_readytype) || (lmask->m_readytype!=lleft->m_readytype))
797       {
798        throw DataException("Programmer Error - condEval arguments must have the same readytype");
799       }
800       m_left=lleft;
801       m_right=lright;
802       m_mask=lmask;
803       m_samplesize=getNumDPPSample()*getNoValues();
804       m_children=m_left->m_children+m_right->m_children+m_mask->m_children+1;
805       m_height=max3(m_left->m_height,m_right->m_height,m_mask->m_height)+1;
806       LazyNodeSetup();
807       SIZELIMIT
808    LAZYDEBUG(cout << "(8)Lazy created with " << m_samplesize << endl;)
809    }
810    
811    
812    
813  DataLazy::~DataLazy()  DataLazy::~DataLazy()
814  {  {
815     delete[] m_sampleids;     delete[] m_sampleids;
# Line 974  if (&x<stackend[omp_get_thread_num()]) Line 1047  if (&x<stackend[omp_get_thread_num()])
1047      return &(m_samples);        // sample is already resolved      return &(m_samples);        // sample is already resolved
1048    }    }
1049    m_sampleids[tid]=sampleNo;    m_sampleids[tid]=sampleNo;
1050    
1051    switch (getOpgroup(m_op))    switch (getOpgroup(m_op))
1052    {    {
1053    case G_UNARY:    case G_UNARY:
# Line 984  if (&x<stackend[omp_get_thread_num()]) Line 1058  if (&x<stackend[omp_get_thread_num()])
1058    case G_TENSORPROD: return resolveNodeTProd(tid, sampleNo, roffset);    case G_TENSORPROD: return resolveNodeTProd(tid, sampleNo, roffset);
1059    case G_NP1OUT_2P: return resolveNodeNP1OUT_2P(tid, sampleNo, roffset);    case G_NP1OUT_2P: return resolveNodeNP1OUT_2P(tid, sampleNo, roffset);
1060    case G_REDUCTION: return resolveNodeReduction(tid, sampleNo, roffset);    case G_REDUCTION: return resolveNodeReduction(tid, sampleNo, roffset);
1061      case G_CONDEVAL: return resolveNodeCondEval(tid, sampleNo, roffset);
1062    default:    default:
1063      throw DataException("Programmer Error - resolveSample does not know how to process "+opToString(m_op)+".");      throw DataException("Programmer Error - resolveSample does not know how to process "+opToString(m_op)+".");
1064    }    }
# Line 1141  DataLazy::resolveNodeReduction(int tid, Line 1216  DataLazy::resolveNodeReduction(int tid,
1216    
1217    roffset=m_samplesize*tid;    roffset=m_samplesize*tid;
1218    unsigned int ndpps=getNumDPPSample();    unsigned int ndpps=getNumDPPSample();
1219    unsigned int psize=DataTypes::noValues(getShape());    unsigned int psize=DataTypes::noValues(m_left->getShape());
1220    double* result=&(m_samples[roffset]);    double* result=&(m_samples[roffset]);
1221    switch (m_op)    switch (m_op)
1222    {    {
# Line 1302  DataLazy::resolveNodeNP1OUT_2P(int tid, Line 1377  DataLazy::resolveNodeNP1OUT_2P(int tid,
1377    return &m_samples;    return &m_samples;
1378  }  }
1379    
1380    const DataTypes::ValueType*
1381    DataLazy::resolveNodeCondEval(int tid, int sampleNo, size_t& roffset)
1382    {
1383      if (m_readytype!='E')
1384      {
1385        throw DataException("Programmer error - resolveNodeCondEval should only be called on expanded Data.");
1386      }
1387      if (m_op!=CONDEVAL)
1388      {
1389        throw DataException("Programmer error - resolveNodeCondEval should only be called on CONDEVAL nodes.");
1390      }
1391      size_t subroffset;
1392    
1393      const ValueType* maskres=m_mask->resolveNodeSample(tid, sampleNo, subroffset);
1394      const ValueType* srcres=0;
1395      if ((*maskres)[subroffset]>0)
1396      {
1397        srcres=m_left->resolveNodeSample(tid, sampleNo, subroffset);
1398      }
1399      else
1400      {
1401        srcres=m_right->resolveNodeSample(tid, sampleNo, subroffset);
1402      }
1403    
1404      // Now we need to copy the result
1405    
1406      roffset=m_samplesize*tid;
1407      for (int i=0;i<m_samplesize;++i)
1408      {
1409        m_samples[roffset+i]=(*srcres)[subroffset+i];  
1410      }
1411    
1412      return &m_samples;
1413    }
1414    
1415  // This method assumes that any subexpressions which evaluate to Constant or Tagged Data  // This method assumes that any subexpressions which evaluate to Constant or Tagged Data
1416  // have already been collapsed to IDENTITY. So we must have at least one expanded child.  // have already been collapsed to IDENTITY. So we must have at least one expanded child.
# Line 1441  LAZYDEBUG(cout << "Right res["<< rroffse Line 1549  LAZYDEBUG(cout << "Right res["<< rroffse
1549    
1550    
1551    roffset=m_samplesize*tid;    roffset=m_samplesize*tid;
1552    double* resultp=&(m_samples[roffset]);        // results are stored at the vector offset we recieved    double* resultp=&(m_samples[roffset]);        // results are stored at the vector offset we received
1553    switch(m_op)    switch(m_op)
1554    {    {
1555      case ADD:      case ADD:
# Line 1503  LAZYDEBUG(cout << "m_samplesize=" << m_s Line 1611  LAZYDEBUG(cout << "m_samplesize=" << m_s
1611  LAZYDEBUG(cout << "outputshape=" << DataTypes::shapeToString(getShape()) << endl;)  LAZYDEBUG(cout << "outputshape=" << DataTypes::shapeToString(getShape()) << endl;)
1612  LAZYDEBUG(cout << "DPPS=" << m_right->getNumDPPSample() <<"."<<endl;)  LAZYDEBUG(cout << "DPPS=" << m_right->getNumDPPSample() <<"."<<endl;)
1613    
1614    double* resultp=&(m_samples[offset]);     // results are stored at the vector offset we recieved    double* resultp=&(m_samples[offset]);     // results are stored at the vector offset we received
1615    switch(m_op)    switch(m_op)
1616    {    {
1617      case PROD:      case PROD:
# Line 1591  DataLazy::resolve() Line 1699  DataLazy::resolve()
1699      return m_id;      return m_id;
1700  }  }
1701    
1702    
1703    /* This is really a static method but I think that caused problems in windows */
1704    void
1705    DataLazy::resolveGroupWorker(std::vector<DataLazy*>& dats)
1706    {
1707      if (dats.empty())
1708      {
1709        return;
1710      }
1711      vector<DataLazy*> work;
1712      FunctionSpace fs=dats[0]->getFunctionSpace();
1713      bool match=true;
1714      for (int i=dats.size()-1;i>=0;--i)
1715      {
1716        if (dats[i]->m_readytype!='E')
1717        {
1718            dats[i]->collapse();
1719        }
1720        if (dats[i]->m_op!=IDENTITY)
1721        {
1722            work.push_back(dats[i]);
1723            if (fs!=dats[i]->getFunctionSpace())
1724            {
1725                match=false;
1726            }
1727        }
1728      }
1729      if (work.empty())
1730      {
1731        return;     // no work to do
1732      }
1733      if (match)    // all functionspaces match.  Yes I realise this is overly strict
1734      {     // it is possible that dats[0] is one of the objects which we discarded and
1735            // all the other functionspaces match.
1736        vector<DataExpanded*> dep;
1737        vector<ValueType*> vecs;
1738        for (int i=0;i<work.size();++i)
1739        {
1740            dep.push_back(new DataExpanded(fs,work[i]->getShape(), ValueType(work[i]->getNoValues())));
1741            vecs.push_back(&(dep[i]->getVectorRW()));
1742        }
1743        int totalsamples=work[0]->getNumSamples();
1744        const ValueType* res=0; // Storage for answer
1745        int sample;
1746        #pragma omp parallel private(sample, res)
1747        {
1748            size_t roffset=0;
1749            #pragma omp for schedule(static)
1750            for (sample=0;sample<totalsamples;++sample)
1751            {
1752            roffset=0;
1753            int j;
1754            for (j=work.size()-1;j>=0;--j)
1755            {
1756    #ifdef _OPENMP
1757                    res=work[j]->resolveNodeSample(omp_get_thread_num(),sample,roffset);
1758    #else
1759                    res=work[j]->resolveNodeSample(0,sample,roffset);
1760    #endif
1761                    DataVector::size_type outoffset=dep[j]->getPointOffset(sample,0);
1762                    memcpy(&((*vecs[j])[outoffset]),&((*res)[roffset]),work[j]->m_samplesize*sizeof(DataVector::ElementType));
1763            }
1764            }
1765        }
1766        // Now we need to load the new results as identity ops into the lazy nodes
1767        for (int i=work.size()-1;i>=0;--i)
1768        {
1769            work[i]->makeIdentity(boost::dynamic_pointer_cast<DataReady>(dep[i]->getPtr()));
1770        }
1771      }
1772      else  // functionspaces do not match
1773      {
1774        for (int i=0;i<work.size();++i)
1775        {
1776            work[i]->resolveToIdentity();
1777        }
1778      }
1779    }
1780    
1781    
1782    
1783  // This version of resolve uses storage in each node to hold results  // This version of resolve uses storage in each node to hold results
1784  DataReady_ptr  DataReady_ptr
1785  DataLazy::resolveNodeWorker()  DataLazy::resolveNodeWorker()
# Line 1653  std::string Line 1842  std::string
1842  DataLazy::toString() const  DataLazy::toString() const
1843  {  {
1844    ostringstream oss;    ostringstream oss;
1845    oss << "Lazy Data:";    oss << "Lazy Data: [depth=" << m_height<< "] ";
1846    if (escriptParams.getPRINT_LAZY_TREE()==0)    switch (escriptParams.getLAZY_STR_FMT())
   {  
       intoString(oss);  
   }  
   else  
1847    {    {
1848      case 1:   // tree format
1849      oss << endl;      oss << endl;
1850      intoTreeString(oss,"");      intoTreeString(oss,"");
1851        break;
1852      case 2:   // just the depth
1853        break;
1854      default:
1855        intoString(oss);
1856        break;
1857    }    }
1858    return oss.str();    return oss.str();
1859  }  }
# Line 1721  DataLazy::intoString(ostringstream& oss) Line 1913  DataLazy::intoString(ostringstream& oss)
1913      oss << ", " << m_axis_offset << ", " << m_transpose;      oss << ", " << m_axis_offset << ", " << m_transpose;
1914      oss << ')';      oss << ')';
1915      break;      break;
1916      case G_CONDEVAL:
1917        oss << opToString(m_op)<< '(' ;
1918        m_mask->intoString(oss);
1919        oss << " ? ";
1920        m_left->intoString(oss);
1921        oss << " : ";
1922        m_right->intoString(oss);
1923        oss << ')';
1924        break;
1925    default:    default:
1926      oss << "UNKNOWN";      oss << "UNKNOWN";
1927    }    }

Legend:
Removed from v.2779  
changed lines
  Added in v.4286

  ViewVC Help
Powered by ViewVC 1.1.26