/[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 2799 by jfenwick, Thu Dec 3 01:35:08 2009 UTC revision 4264 by jfenwick, Thu Feb 28 11:32:57 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 142  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 158  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 170  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 191  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 740  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 977  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 987  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 1144  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 1305  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 1606  DataLazy::resolveGroupWorker(std::vector Line 1711  DataLazy::resolveGroupWorker(std::vector
1711    vector<DataLazy*> work;    vector<DataLazy*> work;
1712    FunctionSpace fs=dats[0]->getFunctionSpace();    FunctionSpace fs=dats[0]->getFunctionSpace();
1713    bool match=true;    bool match=true;
1714    for (int i=dats.size()-1;i>0;--i)    for (int i=dats.size()-1;i>=0;--i)
1715    {    {
1716      if (dats[i]->m_readytype!='E')      if (dats[i]->m_readytype!='E')
1717      {      {
# Line 1646  DataLazy::resolveGroupWorker(std::vector Line 1751  DataLazy::resolveGroupWorker(std::vector
1751          {          {
1752          roffset=0;          roffset=0;
1753          int j;          int j;
1754          for (j=work.size()-1;j>0;--j)          for (j=work.size()-1;j>=0;--j)
1755          {          {
1756  #ifdef _OPENMP  #ifdef _OPENMP
1757                  res=work[j]->resolveNodeSample(omp_get_thread_num(),sample,roffset);                  res=work[j]->resolveNodeSample(omp_get_thread_num(),sample,roffset);
# Line 1659  DataLazy::resolveGroupWorker(std::vector Line 1764  DataLazy::resolveGroupWorker(std::vector
1764          }          }
1765      }      }
1766      // Now we need to load the new results as identity ops into the lazy nodes      // 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)      for (int i=work.size()-1;i>=0;--i)
1768      {      {
1769          work[i]->makeIdentity(boost::dynamic_pointer_cast<DataReady>(dep[i]->getPtr()));          work[i]->makeIdentity(boost::dynamic_pointer_cast<DataReady>(dep[i]->getPtr()));
1770      }      }
# Line 1808  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.2799  
changed lines
  Added in v.4264

  ViewVC Help
Powered by ViewVC 1.1.26