/[escript]/branches/schroedinger_upto1946/escript/src/DataLazy.cpp
ViewVC logotype

Diff of /branches/schroedinger_upto1946/escript/src/DataLazy.cpp

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

revision 1878 by jfenwick, Thu Oct 9 06:30:49 2008 UTC revision 1879 by jfenwick, Tue Oct 14 03:54:42 2008 UTC
# Line 43  resultFS(DataAbstract_ptr left, DataAbst Line 43  resultFS(DataAbstract_ptr left, DataAbst
43      // maybe we need an interpolate node -      // maybe we need an interpolate node -
44      // that way, if interpolate is required in any other op we can just throw a      // that way, if interpolate is required in any other op we can just throw a
45      // programming error exception.      // programming error exception.
46    
47    
48        if (left->getFunctionSpace()!=right->getFunctionSpace())
49        {
50            throw DataException("FunctionSpaces not equal - interpolation not supported on lazy data.");
51        }
52      return left->getFunctionSpace();      return left->getFunctionSpace();
53  }  }
54    
# Line 50  resultFS(DataAbstract_ptr left, DataAbst Line 56  resultFS(DataAbstract_ptr left, DataAbst
56  DataTypes::ShapeType  DataTypes::ShapeType
57  resultShape(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op)  resultShape(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op)
58  {  {
59      return DataTypes::scalarShape;      if (left->getShape()!=right->getShape())
60        {
61            throw DataException("Shapes not the same - shapes must match for lazy data.");
62        }
63        return left->getShape();
64  }  }
65    
66  size_t  size_t
# Line 58  resultLength(DataAbstract_ptr left, Data Line 68  resultLength(DataAbstract_ptr left, Data
68  {  {
69     switch(op)     switch(op)
70     {     {
71     case IDENTITY: return left->getLength();  //   case IDENTITY: return left->getLength();
72     case ADD:    // the length is preserved in these ops     case ADD:    // the length is preserved in these ops
73     case SUB:     case SUB:
74     case MUL:     case MUL:
# Line 69  resultLength(DataAbstract_ptr left, Data Line 79  resultLength(DataAbstract_ptr left, Data
79     }     }
80  }  }
81    
82    int
83    calcBuffs(const DataLazy_ptr& left, const DataLazy_ptr& right, ES_optype op)
84    {
85       switch(op)
86       {
87       case IDENTITY: return 0;
88       case ADD:    // the length is preserved in these ops
89       case SUB:
90       case MUL:
91       case DIV: return max(left->getBuffsRequired(),right->getBuffsRequired());
92       default:
93        throw DataException("Programmer Error - attempt to calcBuffs() for operator "+opToString(op)+".");
94       }
95    }
96    
97  string ES_opstrings[]={"UNKNOWN","IDENTITY","+","-","*","/"};  string ES_opstrings[]={"UNKNOWN","IDENTITY","+","-","*","/"};
98  int ES_opcount=5;  int ES_opcount=5;
99    
100    // void performOp(ValueType& v, int startOffset, ES_optype op, int m_samplesize)
101    // {
102    //    switch(op)
103    //    {
104    //    case ADD:  DataMaths::binaryOp(v,getShape(),startOffset,v,getShape(),
105    //      startOffset+m_samplesize,plus<double>());
106    //        break;    
107    //    case SUB:  DataMaths::binaryOp(v,getShape(),startOffset,v,getShape(),
108    //      startOffset+m_samplesize,minus<double>());
109    //        break;
110    //    case MUL:  DataMaths::binaryOp(v,getShape(),startOffset,v,getShape(),
111    //      startOffset+m_samplesize,multiplies<double>());
112    //        break;
113    //    case DIV:  DataMaths::binaryOp(v,getShape(),startOffset,v,getShape(),
114    //      startOffset+m_samplesize,divides<double>());
115    //        break;
116    //    default:
117    //  throw DataException("Programmer Error - attempt to performOp() for operator "+opToString(op)+".");
118    //    }
119    //
120    // }
121    
122  }   // end anonymous namespace  }   // end anonymous namespace
123    
124    
# Line 88  opToString(ES_optype op) Line 135  opToString(ES_optype op)
135    
136  DataLazy::DataLazy(DataAbstract_ptr p)  DataLazy::DataLazy(DataAbstract_ptr p)
137      : parent(p->getFunctionSpace(),p->getShape()),      : parent(p->getFunctionSpace(),p->getShape()),
     m_left(p),  
138      m_op(IDENTITY)      m_op(IDENTITY)
139  {  {
140     length=resultLength(m_left,m_right,m_op);     if (p->isLazy())
141       {
142        // TODO: fix this.   We could make the new node a copy of p?
143        // I don't want identity of Lazy.
144        // Question: Why would that be so bad?
145        // Answer: We assume that the child of ID is something we can call getVector on
146        throw DataException("Programmer error - attempt to create identity from a DataLazy.");
147       }
148       else
149       {
150        m_id=dynamic_pointer_cast<DataReady>(p);
151       }
152       m_length=p->getLength();
153       m_buffsRequired=0;
154       m_samplesize=getNumDPPSample()*getNoValues();
155    cout << "(1)Lazy created with " << m_samplesize << endl;
156  }  }
157    
158  DataLazy::DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op)  DataLazy::DataLazy(DataLazy_ptr left, DataLazy_ptr right, ES_optype op)
159      : parent(resultFS(left,right,op), resultShape(left,right,op)),      : parent(resultFS(left,right,op), resultShape(left,right,op)),
160      m_left(left),      m_left(left),
161      m_right(right),      m_right(right),
162      m_op(op)      m_op(op)
163  {  {
164     length=resultLength(m_left,m_right,m_op);     m_length=resultLength(m_left,m_right,m_op);
165       m_samplesize=getNumDPPSample()*getNoValues();
166       m_buffsRequired=calcBuffs(m_left, m_right, m_op);
167    cout << "(2)Lazy created with " << m_samplesize << endl;
168  }  }
169    
170    DataLazy::DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op)
171        : parent(resultFS(left,right,op), resultShape(left,right,op)),
172        m_op(op)
173    {
174       if (left->isLazy())
175       {
176        m_left=dynamic_pointer_cast<DataLazy>(left);
177       }
178       else
179       {
180        m_left=DataLazy_ptr(new DataLazy(left));
181       }
182       if (right->isLazy())
183       {
184        m_right=dynamic_pointer_cast<DataLazy>(right);
185       }
186       else
187       {
188        m_right=DataLazy_ptr(new DataLazy(right));
189       }
190    
191       m_length=resultLength(m_left,m_right,m_op);
192       m_samplesize=getNumDPPSample()*getNoValues();
193       m_buffsRequired=calcBuffs(m_left, m_right,m_op);
194    cout << "(3)Lazy created with " << m_samplesize << endl;
195    }
196    
197    
198  DataLazy::~DataLazy()  DataLazy::~DataLazy()
199  {  {
200  }  }
201    
202  // If resolving records a pointer to the resolved Data we may need to rethink the const on this method  
203  DataReady_ptr  int
204  DataLazy::resolve()  DataLazy::getBuffsRequired() const
205  {  {
206    DataReady_ptr left;      return m_buffsRequired;
207    DataReady_ptr right;  }
208    if (m_left.get()!=0)  
209    {  void
210      left=m_left->resolve();  DataLazy::resolveSample(ValueType& v,int sampleNo,  size_t offset ) const
211    }  {
212    if (m_right.get()!=0)    if (m_op==IDENTITY)   // copy the contents into the vector
213    {    {
214      right=m_right->resolve();  cout << "Begin ID" << endl;
215    cout << "dpps=" << getNumDPPSample() << " novals=" << getNoValues() << endl;
216        const ValueType& vec=m_id->getVector();
217        size_t srcOffset=m_id->getPointOffset(sampleNo, 0);
218    cout << "v.size()=" << v.size() << " vec=" << vec.size() << endl;
219        for (size_t i=0;i<m_samplesize;++i,++srcOffset,++offset)
220        {
221    cout << "Trying offset=" << offset << " srcOffset=" << srcOffset << endl;
222        v[offset]=vec[srcOffset];  
223        }
224    cout << "End ID" << endl;
225        return;
226    }    }
227    switch (m_op)    size_t rightoffset=offset+m_samplesize;
228      m_left->resolveSample(v,sampleNo,offset);
229      m_right->resolveSample(v,sampleNo,rightoffset);
230    //  for (int i=0;i<getNumDPPSample();++i)
231    {    {
232      case IDENTITY: return left;      switch(m_op)
233      case ADD:      {
234      // Hmm we could get interpolation here, better be careful      case ADD:       // since these are pointwise ops, pretend each sample is one point
235        return C_TensorBinaryOperation(Data(left),Data(right),plus<double>()).borrowReadyPtr();      tensor_binary_operation(m_samplesize,&(v[offset]),&(v[rightoffset]),&(v[offset]),plus<double>());
236      case SUB:      break;
237        return C_TensorBinaryOperation(Data(left),Data(right),minus<double>()).borrowReadyPtr();      case SUB:      
238      case MUL:      tensor_binary_operation(m_samplesize,&(v[offset]),&(v[rightoffset]),&(v[offset]),minus<double>());
239        return C_TensorBinaryOperation(Data(left),Data(right),multiplies<double>()).borrowReadyPtr();      break;
240      case DIV:      case MUL:      
241        return C_TensorBinaryOperation(Data(left),Data(right),divides<double>()).borrowReadyPtr();      tensor_binary_operation(m_samplesize,&(v[offset]),&(v[rightoffset]),&(v[offset]),multiplies<double>());
242        break;
243        case DIV:      
244        tensor_binary_operation(m_samplesize,&(v[offset]),&(v[rightoffset]),&(v[offset]),divides<double>());
245        break;
246      default:      default:
247      throw DataException("Programmer error - do not know how to resolve operator "+opToString(m_op)+".");      throw DataException("Programmer error - do not know how to resolve operator "+opToString(m_op)+".");
248        }
249    }    }
250  }  }
251    
252    DataReady_ptr
253    DataLazy::resolve()
254    {
255      // This is broken!     We need to have a buffer per thread!
256      // so the allocation of v needs to move inside the loop somehow
257    
258    cout << "Sample size=" << m_samplesize << endl;
259    cout << "Buffers=" << m_buffsRequired << endl;
260    
261      ValueType v(m_samplesize*max(1,m_buffsRequired));
262    cout << "Buffer created with size=" << v.size() << endl;
263      ValueType dummy(getNoValues());
264      DataExpanded* result=new DataExpanded(getFunctionSpace(),getShape(),dummy);
265      ValueType& resvec=result->getVector();
266      DataReady_ptr resptr=DataReady_ptr(result);
267      int sample;
268      #pragma omp parallel for private(sample) schedule(static)
269      for (sample=0;sample<getNumSamples();++sample)
270      {
271        resolveSample(v,sample,0);
272        for (int i=0;i<m_samplesize;++i)    // copy values into the output vector
273        {
274        resvec[i]=v[i];
275        }
276      }
277      return resptr;
278    }
279    
280  std::string  std::string
281  DataLazy::toString() const  DataLazy::toString() const
282  {  {
# Line 160  DataLazy::deepCopy() Line 299  DataLazy::deepCopy()
299  DataTypes::ValueType::size_type  DataTypes::ValueType::size_type
300  DataLazy::getLength() const  DataLazy::getLength() const
301  {  {
302    return length;    return m_length;
303  }  }
304    
305    
306  DataAbstract*  DataAbstract*
307  DataLazy::getSlice(const DataTypes::RegionType& region) const  DataLazy::getSlice(const DataTypes::RegionType& region) const
308  {  {
309    // this seems like a really good one to include I just haven't added it yet    throw DataException("getSlice - not implemented for Lazy objects.");
   throw DataException("getSlice - not implemented for Lazy objects - yet.");  
310  }  }
311    
312  DataTypes::ValueType::size_type  DataTypes::ValueType::size_type
# Line 178  DataLazy::getPointOffset(int sampleNo, Line 316  DataLazy::getPointOffset(int sampleNo,
316    throw DataException("getPointOffset - not implemented for Lazy objects - yet.");    throw DataException("getPointOffset - not implemented for Lazy objects - yet.");
317  }  }
318    
319    // // The startOffset is where to write results in the output vector v
320    // void
321    // DataLazy::processSample(ValueType& v, int sampleNo, size_t startOffset)
322    // {
323    //     m_left.processSample(v,sampleNo,startOffset);
324    //     m_right.processSample(v,sampleNo,startOffset+getSampleSize());
325    //     int i;
326    //     #pragma omp parallel for private(i) schedule(static)
327    //     for (i=0;i<getSampleSize();++i)
328    //     {
329    //      performOp(v,startOffset+i*m_pointsize,ES_optype,m_samplesize);
330    //     }
331    // }
332    
333    
334  }   // end namespace  }   // end namespace

Legend:
Removed from v.1878  
changed lines
  Added in v.1879

  ViewVC Help
Powered by ViewVC 1.1.26