/[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 2083 by caltinay, Fri Nov 21 01:46:05 2008 UTC revision 2084 by jfenwick, Fri Nov 21 05:20:42 2008 UTC
# Line 97  enum ES_opgroup Line 97  enum ES_opgroup
97     G_BINARY,        // pointwise operations with two arguments     G_BINARY,        // pointwise operations with two arguments
98     G_UNARY,     // pointwise operations with one argument     G_UNARY,     // pointwise operations with one argument
99     G_NP1OUT,        // non-pointwise op with one output     G_NP1OUT,        // non-pointwise op with one output
100       G_NP1OUT_P,      // non-pointwise op with one output requiring a parameter
101     G_TENSORPROD     // general tensor product     G_TENSORPROD     // general tensor product
102  };  };
103    
# Line 110  string ES_opstrings[]={"UNKNOWN","IDENTI Line 111  string ES_opstrings[]={"UNKNOWN","IDENTI
111              "log10","log","sign","abs","neg","pos","exp","sqrt",              "log10","log","sign","abs","neg","pos","exp","sqrt",
112              "1/","where>0","where<0","where>=0","where<=0",              "1/","where>0","where<0","where>=0","where<=0",
113              "symmetric","nonsymmetric",              "symmetric","nonsymmetric",
114              "prod"};              "prod",
115  int ES_opcount=36;              "transpose",
116                "trace"};
117    int ES_opcount=38;
118  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,
119              G_UNARY,G_UNARY,G_UNARY, //10              G_UNARY,G_UNARY,G_UNARY, //10
120              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 119  ES_opgroup opgroups[]={G_UNKNOWN,G_IDENT Line 122  ES_opgroup opgroups[]={G_UNKNOWN,G_IDENT
122              G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,        // 28              G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,        // 28
123              G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,            // 33              G_UNARY,G_UNARY,G_UNARY,G_UNARY,G_UNARY,            // 33
124              G_NP1OUT,G_NP1OUT,              G_NP1OUT,G_NP1OUT,
125              G_TENSORPROD};              G_TENSORPROD,
126                G_NP1OUT_P, G_NP1OUT_P};
127  inline  inline
128  ES_opgroup  ES_opgroup
129  getOpgroup(ES_optype op)  getOpgroup(ES_optype op)
# Line 177  resultShape(DataAbstract_ptr left, DataA Line 181  resultShape(DataAbstract_ptr left, DataA
181      return left->getShape();      return left->getShape();
182  }  }
183    
184    // return the shape for "op left"
185    
186    DataTypes::ShapeType
187    resultShape(DataAbstract_ptr left, ES_optype op)
188    {
189        switch(op)
190        {
191            case TRANS:
192            return left->getShape();
193        break;
194        case TRACE:
195            return DataTypes::scalarShape;
196        break;
197            default:
198    cout << op << endl;
199        throw DataException("Programmer error - resultShape(left,op) can't compute shapes for operator "+opToString(op)+".");
200        }
201    }
202    
203  // determine the output shape for the general tensor product operation  // determine the output shape for the general tensor product operation
204  // the additional parameters return information required later for the product  // the additional parameters return information required later for the product
205  // the majority of this code is copy pasted from C_General_Tensor_Product  // the majority of this code is copy pasted from C_General_Tensor_Product
# Line 257  calcBuffs(const DataLazy_ptr& left, cons Line 280  calcBuffs(const DataLazy_ptr& left, cons
280     case G_BINARY: return max(left->getBuffsRequired(),right->getBuffsRequired()+1);     case G_BINARY: return max(left->getBuffsRequired(),right->getBuffsRequired()+1);
281     case G_UNARY: return max(left->getBuffsRequired(),1);     case G_UNARY: return max(left->getBuffsRequired(),1);
282     case G_NP1OUT: return 1+max(left->getBuffsRequired(),1);     case G_NP1OUT: return 1+max(left->getBuffsRequired(),1);
283       case G_NP1OUT_P: return 1+max(left->getBuffsRequired(),1);
284     case G_TENSORPROD: return 1+max(left->getBuffsRequired(),right->getBuffsRequired()+1);     case G_TENSORPROD: return 1+max(left->getBuffsRequired(),right->getBuffsRequired()+1);
285     default:     default:
286      throw DataException("Programmer Error - attempt to calcBuffs() for operator "+opToString(op)+".");      throw DataException("Programmer Error - attempt to calcBuffs() for operator "+opToString(op)+".");
# Line 466  cout << "(4)Lazy created with " << m_sam Line 490  cout << "(4)Lazy created with " << m_sam
490  }  }
491    
492    
493    DataLazy::DataLazy(DataAbstract_ptr left, ES_optype op, int axis_offset)
494        : parent(left->getFunctionSpace(), resultShape(left,op)),
495        m_op(op),
496        m_axis_offset(axis_offset),
497        m_transpose(0)
498    {
499       if ((getOpgroup(op)!=G_NP1OUT_P))
500       {
501        throw DataException("Programmer error - constructor DataLazy(left, op, ax) will only process UNARY operations which require parameters.");
502       }
503       DataLazy_ptr lleft;
504       if (!left->isLazy())
505       {
506        lleft=DataLazy_ptr(new DataLazy(left));
507       }
508       else
509       {
510        lleft=dynamic_pointer_cast<DataLazy>(left);
511       }
512       m_readytype=lleft->m_readytype;
513       m_left=lleft;
514       m_buffsRequired=calcBuffs(m_left, m_right,m_op); // yeah m_right will be null at this point
515       m_samplesize=getNumDPPSample()*getNoValues();
516       m_maxsamplesize=max(m_samplesize,m_left->getMaxSampleSize());
517    cout << "(5)Lazy created with " << m_samplesize << endl;
518    }
519    
520    
521  DataLazy::~DataLazy()  DataLazy::~DataLazy()
522  {  {
523  }  }
# Line 611  DataLazy::collapseToReady() Line 663  DataLazy::collapseToReady()
663      case PROD:      case PROD:
664      result=C_GeneralTensorProduct(left,right,m_axis_offset, m_transpose);      result=C_GeneralTensorProduct(left,right,m_axis_offset, m_transpose);
665      break;      break;
666        case TRANS:
667        result=left.transpose(m_axis_offset);
668        break;
669        case TRACE:
670        result=left.trace(m_axis_offset);
671        break;
672      default:      default:
673      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)+".");
674    }    }
# Line 810  DataLazy::resolveNP1OUT(ValueType& v, si Line 868  DataLazy::resolveNP1OUT(ValueType& v, si
868    return &v;    return &v;
869  }  }
870    
871    /*
872      \brief Compute the value of the expression (unary operation) for the given sample.
873      \return Vector which stores the value of the subexpression for the given sample.
874      \param v A vector to store intermediate results.
875      \param offset Index in v to begin storing results.
876      \param sampleNo Sample number to evaluate.
877      \param roffset (output parameter) the offset in the return vector where the result begins.
878    
879      The return value will be an existing vector so do not deallocate it.
880      If the result is stored in v it should be stored at the offset given.
881      Everything from offset to the end of v should be considered available for this method to use.
882    */
883    DataTypes::ValueType*
884    DataLazy::resolveNP1OUT_P(ValueType& v, size_t offset, int sampleNo, size_t& roffset) const
885    {
886        // we assume that any collapsing has been done before we get here
887        // since we only have one argument we don't need to think about only
888        // processing single points.
889      if (m_readytype!='E')
890      {
891        throw DataException("Programmer error - resolveNP1OUT_P should only be called on expanded Data.");
892      }
893        // since we can't write the result over the input, we need a result offset further along
894      size_t subroffset=roffset+m_samplesize;
895      const ValueType* vleft=m_left->resolveSample(v,offset,sampleNo,subroffset);
896      roffset=offset;
897      switch (m_op)
898      {
899        case TRACE:
900             DataMaths::trace(*vleft,m_left->getShape(),subroffset, v,getShape(),offset,m_axis_offset);
901        break;
902        case TRANS:
903             DataMaths::transpose(*vleft,m_left->getShape(),subroffset, v,getShape(),offset,m_axis_offset);
904        break;
905        default:
906        throw DataException("Programmer error - resolveNP1OUTP can not resolve operator "+opToString(m_op)+".");
907      }
908      return &v;
909    }
910    
911    
912  #define PROC_OP(TYPE,X)                               \  #define PROC_OP(TYPE,X)                               \
# Line 851  cout << "Resolve binary: " << toString() Line 947  cout << "Resolve binary: " << toString()
947      // first work out which of the children are expanded      // first work out which of the children are expanded
948    bool leftExp=(m_left->m_readytype=='E');    bool leftExp=(m_left->m_readytype=='E');
949    bool rightExp=(m_right->m_readytype=='E');    bool rightExp=(m_right->m_readytype=='E');
950      if (!leftExp && !rightExp)
951      {
952        throw DataException("Programmer Error - please use collapse if neither argument has type 'E'.");
953      }
954      bool leftScalar=(m_left->getRank()==0);
955      bool rightScalar=(m_right->getRank()==0);
956    bool bigloops=((leftExp && rightExp) || (!leftExp && !rightExp)); // is processing in single step?    bool bigloops=((leftExp && rightExp) || (!leftExp && !rightExp)); // is processing in single step?
957    int steps=(bigloops?1:getNumDPPSample());    int steps=(bigloops?1:getNumDPPSample());
958    size_t chunksize=(bigloops? m_samplesize : getNoValues());    // if bigloops, pretend the whole sample is a datapoint    size_t chunksize=(bigloops? m_samplesize : getNoValues());    // if bigloops, pretend the whole sample is a datapoint
959    if (m_left->getRank()!=m_right->getRank())    // need to deal with scalar * ? ops    if (m_left->getRank()!=m_right->getRank())    // need to deal with scalar * ? ops
960    {    {
961      EsysAssert((m_left->getRank()==0) || (m_right->getRank()==0), "Error - Ranks must match unless one is 0.");      if (!leftScalar && !rightScalar)
962        {
963           throw DataException("resolveBinary - ranks of arguments must match unless one of them is scalar.");
964        }
965      steps=getNumDPPSample()*max(m_left->getNoValues(),m_right->getNoValues());      steps=getNumDPPSample()*max(m_left->getNoValues(),m_right->getNoValues());
966      chunksize=1;    // for scalar      chunksize=1;    // for scalar
967    }        }    
968    int leftStep=((leftExp && !rightExp)? m_right->getNoValues() : 0);    int leftStep=((leftExp && (!rightExp || rightScalar))? m_right->getNoValues() : 0);
969    int rightStep=((rightExp && !leftExp)? m_left->getNoValues() : 0);    int rightStep=((rightExp && (!leftExp || leftScalar))? m_left->getNoValues() : 0);
970    int resultStep=max(leftStep,rightStep);   // only one (at most) should be !=0    int resultStep=max(leftStep,rightStep);   // only one (at most) should be !=0
971      // Get the values of sub-expressions      // Get the values of sub-expressions
972    const ValueType* left=m_left->resolveSample(v,offset,sampleNo,lroffset);    const ValueType* left=m_left->resolveSample(v,offset,sampleNo,lroffset);
# Line 991  cout << "Resolve sample " << toString() Line 1096  cout << "Resolve sample " << toString()
1096    case G_UNARY: return resolveUnary(v, offset,sampleNo,roffset);    case G_UNARY: return resolveUnary(v, offset,sampleNo,roffset);
1097    case G_BINARY: return resolveBinary(v, offset,sampleNo,roffset);    case G_BINARY: return resolveBinary(v, offset,sampleNo,roffset);
1098    case G_NP1OUT: return resolveNP1OUT(v, offset, sampleNo,roffset);    case G_NP1OUT: return resolveNP1OUT(v, offset, sampleNo,roffset);
1099      case G_NP1OUT_P: return resolveNP1OUT_P(v, offset, sampleNo,roffset);
1100    case G_TENSORPROD: return resolveTProd(v,offset, sampleNo,roffset);    case G_TENSORPROD: return resolveTProd(v,offset, sampleNo,roffset);
1101    default:    default:
1102      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)+".");
# Line 1096  DataLazy::intoString(ostringstream& oss) Line 1202  DataLazy::intoString(ostringstream& oss)
1202      break;      break;
1203    case G_UNARY:    case G_UNARY:
1204    case G_NP1OUT:    case G_NP1OUT:
1205      case G_NP1OUT_P:
1206      oss << opToString(m_op) << '(';      oss << opToString(m_op) << '(';
1207      m_left->intoString(oss);      m_left->intoString(oss);
1208      oss << ')';      oss << ')';

Legend:
Removed from v.2083  
changed lines
  Added in v.2084

  ViewVC Help
Powered by ViewVC 1.1.26