/[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 2644 by jfenwick, Wed Sep 2 04:14:03 2009 UTC revision 2737 by jfenwick, Tue Nov 3 00:44:00 2009 UTC
# Line 119  enum ES_opgroup Line 119  enum ES_opgroup
119     G_NP1OUT,        // non-pointwise op with one output     G_NP1OUT,        // non-pointwise op with one output
120     G_NP1OUT_P,      // non-pointwise op with one output requiring a parameter     G_NP1OUT_P,      // non-pointwise op with one output requiring a parameter
121     G_TENSORPROD,    // general tensor product     G_TENSORPROD,    // general tensor product
122     G_NP1OUT_2P      // non-pointwise op with one output requiring two params     G_NP1OUT_2P,     // non-pointwise op with one output requiring two params
123       G_REDUCTION      // non-pointwise unary op with a scalar output
124  };  };
125    
126    
# Line 134  string ES_opstrings[]={"UNKNOWN","IDENTI Line 135  string ES_opstrings[]={"UNKNOWN","IDENTI
135              "symmetric","nonsymmetric",              "symmetric","nonsymmetric",
136              "prod",              "prod",
137              "transpose", "trace",              "transpose", "trace",
138              "swapaxes"};              "swapaxes",
139  int ES_opcount=41;              "minval", "maxval"};
140    int ES_opcount=43;
141  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,
142              G_UNARY,G_UNARY,G_UNARY, //10              G_UNARY,G_UNARY,G_UNARY, //10
143              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 145  ES_opgroup opgroups[]={G_UNKNOWN,G_IDENT Line 147  ES_opgroup opgroups[]={G_UNKNOWN,G_IDENT
147              G_NP1OUT,G_NP1OUT,              G_NP1OUT,G_NP1OUT,
148              G_TENSORPROD,              G_TENSORPROD,
149              G_NP1OUT_P, G_NP1OUT_P,              G_NP1OUT_P, G_NP1OUT_P,
150              G_NP1OUT_2P};              G_NP1OUT_2P,
151                G_REDUCTION, G_REDUCTION};
152  inline  inline
153  ES_opgroup  ES_opgroup
154  getOpgroup(ES_optype op)  getOpgroup(ES_optype op)
# Line 190  resultShape(DataAbstract_ptr left, DataA Line 193  resultShape(DataAbstract_ptr left, DataA
193        {        {
194          throw DataException("Shapes not the name - shapes must match for (point)binary operations.");          throw DataException("Shapes not the name - shapes must match for (point)binary operations.");
195        }        }
196    
197        if (left->getRank()==0)   // we need to allow scalar * anything        if (left->getRank()==0)   // we need to allow scalar * anything
198        {        {
199          return right->getShape();          return right->getShape();
# Line 407  calcBuffs(const DataLazy_ptr& left, cons Line 411  calcBuffs(const DataLazy_ptr& left, cons
411     {     {
412     case G_IDENTITY: return 1;     case G_IDENTITY: return 1;
413     case G_BINARY: return 1+max(left->getBuffsRequired(),right->getBuffsRequired()+1);     case G_BINARY: return 1+max(left->getBuffsRequired(),right->getBuffsRequired()+1);
414       case G_REDUCTION:
415     case G_UNARY:     case G_UNARY:
416     case G_UNARY_P: return max(left->getBuffsRequired(),1);     case G_UNARY_P: return max(left->getBuffsRequired(),1);
417     case G_NP1OUT: return 1+max(left->getBuffsRequired(),1);     case G_NP1OUT: return 1+max(left->getBuffsRequired(),1);
# Line 480  LAZYDEBUG(cout << "(1)Lazy created with Line 485  LAZYDEBUG(cout << "(1)Lazy created with
485  }  }
486    
487  DataLazy::DataLazy(DataAbstract_ptr left, ES_optype op)  DataLazy::DataLazy(DataAbstract_ptr left, ES_optype op)
488      : parent(left->getFunctionSpace(),left->getShape()),      : parent(left->getFunctionSpace(),(getOpgroup(op)!=G_REDUCTION)?left->getShape():DataTypes::scalarShape),
489      m_op(op),      m_op(op),
490      m_axis_offset(0),      m_axis_offset(0),
491      m_transpose(0),      m_transpose(0),
492      m_SL(0), m_SM(0), m_SR(0)      m_SL(0), m_SM(0), m_SR(0)
493  {  {
494     if ((getOpgroup(op)!=G_UNARY) && (getOpgroup(op)!=G_NP1OUT))     if ((getOpgroup(op)!=G_UNARY) && (getOpgroup(op)!=G_NP1OUT) && (getOpgroup(op)!=G_REDUCTION))
495     {     {
496      throw DataException("Programmer error - constructor DataLazy(left, op) will only process UNARY operations.");      throw DataException("Programmer error - constructor DataLazy(left, op) will only process UNARY operations.");
497     }     }
# Line 933  DataLazy::collapseToReady() Line 938  DataLazy::collapseToReady()
938      case SWAP:      case SWAP:
939      result=left.swapaxes(m_axis_offset, m_transpose);      result=left.swapaxes(m_axis_offset, m_transpose);
940      break;      break;
941        case MINVAL:
942        result=left.minval();
943        break;
944        case MAXVAL:
945        result=left.minval();
946        break;
947      default:      default:
948      throw DataException("Programmer error - collapseToReady does not know how to resolve operator "+opToString(m_op)+".");      throw DataException("Programmer error - collapseToReady does not know how to resolve operator "+opToString(m_op)+".");
949    }    }
# Line 1099  DataLazy::resolveUnary(ValueType& v, siz Line 1110  DataLazy::resolveUnary(ValueType& v, siz
1110  }  }
1111    
1112    
1113    /*
1114      \brief Compute the value of the expression (reduction operation) for the given sample.
1115      \return Vector which stores the value of the subexpression for the given sample.
1116      \param v A vector to store intermediate results.
1117      \param offset Index in v to begin storing results.
1118      \param sampleNo Sample number to evaluate.
1119      \param roffset (output parameter) the offset in the return vector where the result begins.
1120    
1121      The return value will be an existing vector so do not deallocate it.
1122      If the result is stored in v it should be stored at the offset given.
1123      Everything from offset to the end of v should be considered available for this method to use.
1124    */
1125    DataTypes::ValueType*
1126    DataLazy::resolveReduction(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const
1127    {
1128        // we assume that any collapsing has been done before we get here
1129        // since we only have one argument we don't need to think about only
1130        // processing single points.
1131      if (m_readytype!='E')
1132      {
1133        throw DataException("Programmer error - resolveUnary should only be called on expanded Data.");
1134      }
1135      const ValueType* vleft=m_left->resolveVectorSample(v,offset,sampleNo,roffset);
1136      double* result=&(v[offset]);
1137      roffset=offset;
1138      unsigned int ndpps=getNumDPPSample();
1139      unsigned int psize=DataTypes::noValues(getShape());
1140      switch (m_op)
1141      {
1142        case MINVAL:
1143        {
1144          for (unsigned int z=0;z<ndpps;++z)
1145          {
1146             FMin op;
1147             *result=DataMaths::reductionOp(*vleft, m_left->getShape(), roffset, op, numeric_limits<double>::max());
1148             roffset+=psize;
1149             result++;
1150          }
1151        }
1152        break;
1153        case MAXVAL:
1154        {
1155          for (unsigned int z=0;z<ndpps;++z)
1156          {
1157             FMax op;
1158             *result=DataMaths::reductionOp(*vleft, m_left->getShape(), roffset, op, numeric_limits<double>::max()*-1);
1159             roffset+=psize;
1160             result++;
1161          }
1162        }
1163        break;
1164        default:
1165        throw DataException("Programmer error - resolveReduction can not resolve operator "+opToString(m_op)+".");
1166      }
1167      return &v;
1168    }
1169    
1170    
1171    
# Line 1628  LAZYDEBUG(cout << "Resolve sample " << t Line 1694  LAZYDEBUG(cout << "Resolve sample " << t
1694    case G_NP1OUT_P: return resolveNodeNP1OUT_P(tid, sampleNo, roffset);    case G_NP1OUT_P: return resolveNodeNP1OUT_P(tid, sampleNo, roffset);
1695    case G_TENSORPROD: return resolveNodeTProd(tid, sampleNo, roffset);    case G_TENSORPROD: return resolveNodeTProd(tid, sampleNo, roffset);
1696    case G_NP1OUT_2P: return resolveNodeNP1OUT_2P(tid, sampleNo, roffset);    case G_NP1OUT_2P: return resolveNodeNP1OUT_2P(tid, sampleNo, roffset);
1697      case G_REDUCTION: return resolveNodeReduction(tid, sampleNo, roffset);
1698    default:    default:
1699      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)+".");
1700    }    }
# Line 1766  DataLazy::resolveNodeUnary(int tid, int Line 1833  DataLazy::resolveNodeUnary(int tid, int
1833    
1834    
1835  const DataTypes::ValueType*  const DataTypes::ValueType*
1836    DataLazy::resolveNodeReduction(int tid, int sampleNo, size_t& roffset)
1837    {
1838        // we assume that any collapsing has been done before we get here
1839        // since we only have one argument we don't need to think about only
1840        // processing single points.
1841        // we will also know we won't get identity nodes
1842      if (m_readytype!='E')
1843      {
1844        throw DataException("Programmer error - resolveUnary should only be called on expanded Data.");
1845      }
1846      if (m_op==IDENTITY)
1847      {
1848        throw DataException("Programmer error - resolveNodeUnary should not be called on identity nodes.");
1849      }
1850      size_t loffset=0;
1851      const DataTypes::ValueType* leftres=m_left->resolveNodeSample(tid, sampleNo, loffset);
1852    
1853      roffset=m_samplesize*tid;
1854      unsigned int ndpps=getNumDPPSample();
1855      unsigned int psize=DataTypes::noValues(getShape());
1856      double* result=&(m_samples[roffset]);
1857      switch (m_op)
1858      {
1859        case MINVAL:
1860        {
1861          for (unsigned int z=0;z<ndpps;++z)
1862          {
1863            FMin op;
1864            *result=DataMaths::reductionOp(*leftres, m_left->getShape(), loffset, op, numeric_limits<double>::max());
1865            loffset+=psize;
1866            result++;
1867          }
1868        }
1869        break;
1870        case MAXVAL:
1871        {
1872          for (unsigned int z=0;z<ndpps;++z)
1873          {
1874          FMax op;
1875          *result=DataMaths::reductionOp(*leftres, m_left->getShape(), loffset, op, numeric_limits<double>::max()*-1);
1876          loffset+=psize;
1877          result++;
1878          }
1879        }
1880        break;
1881        default:
1882        throw DataException("Programmer error - resolveUnary can not resolve operator "+opToString(m_op)+".");
1883      }
1884      return &(m_samples);
1885    }
1886    
1887    const DataTypes::ValueType*
1888  DataLazy::resolveNodeNP1OUT(int tid, int sampleNo, size_t& roffset)  DataLazy::resolveNodeNP1OUT(int tid, int sampleNo, size_t& roffset)
1889  {  {
1890      // we assume that any collapsing has been done before we get here      // we assume that any collapsing has been done before we get here
# Line 2172  LAZYDEBUG(cout << "Finish  sample " << t Line 2291  LAZYDEBUG(cout << "Finish  sample " << t
2291    case G_NP1OUT_P: return resolveNP1OUT_P(v, offset, sampleNo,roffset);    case G_NP1OUT_P: return resolveNP1OUT_P(v, offset, sampleNo,roffset);
2292    case G_TENSORPROD: return resolveTProd(v,offset, sampleNo,roffset);    case G_TENSORPROD: return resolveTProd(v,offset, sampleNo,roffset);
2293    case G_NP1OUT_2P: return resolveNP1OUT_2P(v, offset, sampleNo, roffset);    case G_NP1OUT_2P: return resolveNP1OUT_2P(v, offset, sampleNo, roffset);
2294      case G_REDUCTION: return resolveReduction(v, offset, sampleNo, roffset);
2295    default:    default:
2296      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)+".");
2297    }    }
# Line 2340  DataLazy::toString() const Line 2460  DataLazy::toString() const
2460  {  {
2461    ostringstream oss;    ostringstream oss;
2462    oss << "Lazy Data:";    oss << "Lazy Data:";
2463    intoString(oss);    if (escriptParams.getPRINT_LAZY_TREE()==0)
2464      {
2465          intoString(oss);
2466      }
2467      else
2468      {
2469        oss << endl;
2470        intoTreeString(oss,"");
2471      }
2472    return oss.str();    return oss.str();
2473  }  }
2474    
# Line 2381  DataLazy::intoString(ostringstream& oss) Line 2509  DataLazy::intoString(ostringstream& oss)
2509    case G_UNARY_P:    case G_UNARY_P:
2510    case G_NP1OUT:    case G_NP1OUT:
2511    case G_NP1OUT_P:    case G_NP1OUT_P:
2512      case G_REDUCTION:
2513      oss << opToString(m_op) << '(';      oss << opToString(m_op) << '(';
2514      m_left->intoString(oss);      m_left->intoString(oss);
2515      oss << ')';      oss << ')';
# Line 2403  DataLazy::intoString(ostringstream& oss) Line 2532  DataLazy::intoString(ostringstream& oss)
2532    }    }
2533  }  }
2534    
2535    
2536    void
2537    DataLazy::intoTreeString(ostringstream& oss, string indent) const
2538    {
2539      oss << '[' << m_rank << ':' << setw(3) << m_samplesize << "] " << indent;
2540      switch (getOpgroup(m_op))
2541      {
2542      case G_IDENTITY:
2543        if (m_id->isExpanded())
2544        {
2545           oss << "E";
2546        }
2547        else if (m_id->isTagged())
2548        {
2549          oss << "T";
2550        }
2551        else if (m_id->isConstant())
2552        {
2553          oss << "C";
2554        }
2555        else
2556        {
2557          oss << "?";
2558        }
2559        oss << '@' << m_id.get() << endl;
2560        break;
2561      case G_BINARY:
2562        oss << opToString(m_op) << endl;
2563        indent+='.';
2564        m_left->intoTreeString(oss, indent);
2565        m_right->intoTreeString(oss, indent);
2566        break;
2567      case G_UNARY:
2568      case G_UNARY_P:
2569      case G_NP1OUT:
2570      case G_NP1OUT_P:
2571      case G_REDUCTION:
2572        oss << opToString(m_op) << endl;
2573        indent+='.';
2574        m_left->intoTreeString(oss, indent);
2575        break;
2576      case G_TENSORPROD:
2577        oss << opToString(m_op) << endl;
2578        indent+='.';
2579        m_left->intoTreeString(oss, indent);
2580        m_right->intoTreeString(oss, indent);
2581        break;
2582      case G_NP1OUT_2P:
2583        oss << opToString(m_op) << ", " << m_axis_offset << ", " << m_transpose<< endl;
2584        indent+='.';
2585        m_left->intoTreeString(oss, indent);
2586        break;
2587      default:
2588        oss << "UNKNOWN";
2589      }
2590    }
2591    
2592    
2593  DataAbstract*  DataAbstract*
2594  DataLazy::deepCopy()  DataLazy::deepCopy()
2595  {  {
2596    switch (getOpgroup(m_op))    switch (getOpgroup(m_op))
2597    {    {
2598    case G_IDENTITY:  return new DataLazy(m_id->deepCopy()->getPtr());    case G_IDENTITY:  return new DataLazy(m_id->deepCopy()->getPtr());
2599    case G_UNARY: return new DataLazy(m_left->deepCopy()->getPtr(),m_op);    case G_UNARY:
2600      case G_REDUCTION:      return new DataLazy(m_left->deepCopy()->getPtr(),m_op);
2601      case G_UNARY_P:   return new DataLazy(m_left->deepCopy()->getPtr(), m_op, m_tol);
2602    case G_BINARY:    return new DataLazy(m_left->deepCopy()->getPtr(),m_right->deepCopy()->getPtr(),m_op);    case G_BINARY:    return new DataLazy(m_left->deepCopy()->getPtr(),m_right->deepCopy()->getPtr(),m_op);
2603    case G_NP1OUT: return new DataLazy(m_left->deepCopy()->getPtr(), m_right->deepCopy()->getPtr(),m_op);    case G_NP1OUT: return new DataLazy(m_left->deepCopy()->getPtr(), m_right->deepCopy()->getPtr(),m_op);
2604    case G_TENSORPROD: return new DataLazy(m_left->deepCopy()->getPtr(), m_right->deepCopy()->getPtr(), m_op, m_axis_offset, m_transpose);    case G_TENSORPROD: return new DataLazy(m_left->deepCopy()->getPtr(), m_right->deepCopy()->getPtr(), m_op, m_axis_offset, m_transpose);
2605      case G_NP1OUT_P:   return new DataLazy(m_left->deepCopy()->getPtr(),m_op,  m_axis_offset);
2606      case G_NP1OUT_2P:  return new DataLazy(m_left->deepCopy()->getPtr(), m_op, m_axis_offset, m_transpose);
2607    default:    default:
2608      throw DataException("Programmer error - do not know how to deepcopy operator "+opToString(m_op)+".");      throw DataException("Programmer error - do not know how to deepcopy operator "+opToString(m_op)+".");
2609    }    }
2610  }  }
2611    
2612    
2613    
2614  // There is no single, natural interpretation of getLength on DataLazy.  // There is no single, natural interpretation of getLength on DataLazy.
2615  // Instances of DataReady can look at the size of their vectors.  // Instances of DataReady can look at the size of their vectors.
2616  // For lazy though, it could be the size the data would be if it were resolved;  // For lazy though, it could be the size the data would be if it were resolved;

Legend:
Removed from v.2644  
changed lines
  Added in v.2737

  ViewVC Help
Powered by ViewVC 1.1.26