/[escript]/trunk/escript/src/Data.cpp
ViewVC logotype

Diff of /trunk/escript/src/Data.cpp

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

revision 782 by bcumming, Tue Jul 18 00:47:47 2006 UTC revision 1118 by gross, Tue Apr 24 08:55:04 2007 UTC
# Line 38  using namespace boost::python; Line 38  using namespace boost::python;
38  using namespace boost;  using namespace boost;
39  using namespace escript;  using namespace escript;
40    
 #if defined DOPROF  
 //  
 // global table of profiling data for all Data objects  
 DataProf dataProfTable;  
 #endif  
   
41  Data::Data()  Data::Data()
42  {  {
43    //    //
# Line 51  Data::Data() Line 45  Data::Data()
45    DataAbstract* temp=new DataEmpty();    DataAbstract* temp=new DataEmpty();
46    shared_ptr<DataAbstract> temp_data(temp);    shared_ptr<DataAbstract> temp_data(temp);
47    m_data=temp_data;    m_data=temp_data;
48  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
49  }  }
50    
51  Data::Data(double value,  Data::Data(double value,
# Line 68  Data::Data(double value, Line 59  Data::Data(double value,
59    }    }
60    DataArray temp(dataPointShape,value);    DataArray temp(dataPointShape,value);
61    initialise(temp.getView(),what,expanded);    initialise(temp.getView(),what,expanded);
62  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
63  }  }
64    
65  Data::Data(double value,  Data::Data(double value,
# Line 82  Data::Data(double value, Line 70  Data::Data(double value,
70    DataArray temp(dataPointShape,value);    DataArray temp(dataPointShape,value);
71    pair<int,int> dataShape=what.getDataShape();    pair<int,int> dataShape=what.getDataShape();
72    initialise(temp.getView(),what,expanded);    initialise(temp.getView(),what,expanded);
73  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
74  }  }
75    
76  Data::Data(const Data& inData)  Data::Data(const Data& inData)
77  {  {
78    m_data=inData.m_data;    m_data=inData.m_data;
79  #if defined DOPROF    m_protected=inData.isProtected();
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
80  }  }
81    
82  Data::Data(const Data& inData,  Data::Data(const Data& inData,
# Line 105  Data::Data(const Data& inData, Line 87  Data::Data(const Data& inData,
87    DataAbstract* tmp = inData.m_data->getSlice(region);    DataAbstract* tmp = inData.m_data->getSlice(region);
88    shared_ptr<DataAbstract> temp_data(tmp);    shared_ptr<DataAbstract> temp_data(tmp);
89    m_data=temp_data;    m_data=temp_data;
90  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
91  }  }
92    
93  Data::Data(const Data& inData,  Data::Data(const Data& inData,
94             const FunctionSpace& functionspace)             const FunctionSpace& functionspace)
95  {  {
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
96    if (inData.getFunctionSpace()==functionspace) {    if (inData.getFunctionSpace()==functionspace) {
97      m_data=inData.m_data;      m_data=inData.m_data;
98    } else {    } else {
     #if defined DOPROF  
     profData->interpolate++;  
     #endif  
99      Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);      Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);
100      // Note: Must use a reference or pointer to a derived object      // Note: Must use a reference or pointer to a derived object
101      // in order to get polymorphic behaviour. Shouldn't really      // in order to get polymorphic behaviour. Shouldn't really
# Line 137  Data::Data(const Data& inData, Line 109  Data::Data(const Data& inData,
109      }      }
110      m_data=tmp.m_data;      m_data=tmp.m_data;
111    }    }
112      m_protected=false;
113  }  }
114    
115  Data::Data(const DataTagged::TagListType& tagKeys,  Data::Data(const DataTagged::TagListType& tagKeys,
# Line 148  Data::Data(const DataTagged::TagListType Line 121  Data::Data(const DataTagged::TagListType
121    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);
122    shared_ptr<DataAbstract> temp_data(temp);    shared_ptr<DataAbstract> temp_data(temp);
123    m_data=temp_data;    m_data=temp_data;
124      m_protected=false;
125    if (expanded) {    if (expanded) {
126      expand();      expand();
127    }    }
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
128  }  }
129    
130  Data::Data(const numeric::array& value,  Data::Data(const numeric::array& value,
# Line 162  Data::Data(const numeric::array& value, Line 132  Data::Data(const numeric::array& value,
132             bool expanded)             bool expanded)
133  {  {
134    initialise(value,what,expanded);    initialise(value,what,expanded);
135  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
136  }  }
137    
138  Data::Data(const DataArrayView& value,  Data::Data(const DataArrayView& value,
# Line 173  Data::Data(const DataArrayView& value, Line 140  Data::Data(const DataArrayView& value,
140             bool expanded)             bool expanded)
141  {  {
142    initialise(value,what,expanded);    initialise(value,what,expanded);
143  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
144  }  }
145    
146  Data::Data(const object& value,  Data::Data(const object& value,
# Line 185  Data::Data(const object& value, Line 149  Data::Data(const object& value,
149  {  {
150    numeric::array asNumArray(value);    numeric::array asNumArray(value);
151    initialise(asNumArray,what,expanded);    initialise(asNumArray,what,expanded);
152  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
153  }  }
154    
155  Data::Data(const object& value,  Data::Data(const object& value,
# Line 209  Data::Data(const object& value, Line 170  Data::Data(const object& value,
170      // Create a DataConstant with the same sample shape as other      // Create a DataConstant with the same sample shape as other
171      initialise(temp.getView(),other.getFunctionSpace(),false);      initialise(temp.getView(),other.getFunctionSpace(),false);
172    }    }
173  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
174  }  }
175    
176  Data::~Data()  Data::~Data()
# Line 255  Data::getShapeTuple() const Line 213  Data::getShapeTuple() const
213          throw DataException("Error - illegal Data rank.");          throw DataException("Error - illegal Data rank.");
214    }    }
215  }  }
   
216  void  void
217  Data::copy(const Data& other)  Data::copy(const Data& other)
218  {  {
# Line 308  Data::copy(const Data& other) Line 265  Data::copy(const Data& other)
265    throw DataException("Error - Copy not implemented for this Data type.");    throw DataException("Error - Copy not implemented for this Data type.");
266  }  }
267    
268    
269    void
270    Data::setToZero()
271    {
272      {
273        DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
274        if (temp!=0) {
275           temp->setToZero();
276           return;
277        }
278      }
279      {
280        DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());
281        if (temp!=0) {
282          temp->setToZero();
283          return;
284        }
285      }
286      {
287        DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());
288        if (temp!=0) {
289          temp->setToZero();
290          return;
291        }
292      }
293      throw DataException("Error - Data can not be set to zero.");
294    }
295    
296  void  void
297  Data::copyWithMask(const Data& other,  Data::copyWithMask(const Data& other,
298                     const Data& mask)                     const Data& mask)
# Line 339  Data::isTagged() const Line 324  Data::isTagged() const
324    return (temp!=0);    return (temp!=0);
325  }  }
326    
 /* TODO */  
 /* global reduction -- the local data being empty does not imply that it is empty on other processers*/  
327  bool  bool
328  Data::isEmpty() const  Data::isEmpty() const
329  {  {
# Line 356  Data::isConstant() const Line 339  Data::isConstant() const
339  }  }
340    
341  void  void
342    Data::setProtection()
343    {
344       m_protected=true;
345    }
346    
347    bool
348    Data::isProtected() const
349    {
350       return m_protected;
351    }
352    
353    
354    
355    void
356  Data::expand()  Data::expand()
357  {  {
358    if (isConstant()) {    if (isConstant()) {
# Line 397  Data::tag() Line 394  Data::tag()
394    }    }
395  }  }
396    
397  void  Data
398  Data::reshapeDataPoint(const DataArrayView::ShapeType& shape)  Data::oneOver() const
399  {  {
400    m_data->reshapeDataPoint(shape);    return escript::unaryOp(*this,bind1st(divides<double>(),1.));
401  }  }
402    
403  Data  Data
404  Data::wherePositive() const  Data::wherePositive() const
405  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
406    return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));    return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));
407  }  }
408    
409  Data  Data
410  Data::whereNegative() const  Data::whereNegative() const
411  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
412    return escript::unaryOp(*this,bind2nd(less<double>(),0.0));    return escript::unaryOp(*this,bind2nd(less<double>(),0.0));
413  }  }
414    
415  Data  Data
416  Data::whereNonNegative() const  Data::whereNonNegative() const
417  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
418    return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));    return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));
419  }  }
420    
421  Data  Data
422  Data::whereNonPositive() const  Data::whereNonPositive() const
423  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
424    return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));    return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));
425  }  }
426    
427  Data  Data
428  Data::whereZero(double tol) const  Data::whereZero(double tol) const
429  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
430    Data dataAbs=abs();    Data dataAbs=abs();
431    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));
432  }  }
# Line 452  Data::whereZero(double tol) const Line 434  Data::whereZero(double tol) const
434  Data  Data
435  Data::whereNonZero(double tol) const  Data::whereNonZero(double tol) const
436  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
437    Data dataAbs=abs();    Data dataAbs=abs();
438    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));
439  }  }
# Line 462  Data::whereNonZero(double tol) const Line 441  Data::whereNonZero(double tol) const
441  Data  Data
442  Data::interpolate(const FunctionSpace& functionspace) const  Data::interpolate(const FunctionSpace& functionspace) const
443  {  {
 #if defined DOPROF  
   profData->interpolate++;  
 #endif  
444    return Data(*this,functionspace);    return Data(*this,functionspace);
445  }  }
446    
# Line 486  Data::probeInterpolation(const FunctionS Line 462  Data::probeInterpolation(const FunctionS
462  Data  Data
463  Data::gradOn(const FunctionSpace& functionspace) const  Data::gradOn(const FunctionSpace& functionspace) const
464  {  {
 #if defined DOPROF  
   profData->grad++;  
 #endif  
465    if (functionspace.getDomain()!=getDomain())    if (functionspace.getDomain()!=getDomain())
466      throw DataException("Error - gradient cannot be calculated on different domains.");      throw DataException("Error - gradient cannot be calculated on different domains.");
467    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();
# Line 522  Data::getDataPointShape() const Line 495  Data::getDataPointShape() const
495    return getPointDataView().getShape();    return getPointDataView().getShape();
496  }  }
497    
 void  
 Data::fillFromNumArray(const boost::python::numeric::array num_array)  
 {  
   //  
   // check rank  
   if (num_array.getrank()<getDataPointRank())  
       throw DataException("Rank of numarray does not match Data object rank");  
498    
   //  
   // check shape of num_array  
   for (int i=0; i<getDataPointRank(); i++) {  
     if (extract<int>(num_array.getshape()[i+1])!=getDataPointShape()[i])  
        throw DataException("Shape of numarray does not match Data object rank");  
   }  
499    
500    //  const
   // make sure data is expanded:  
   if (!isExpanded()) {  
     expand();  
   }  
   
   //  
   // and copy over  
   m_data->copyAll(num_array);  
 }  
   
 const  
501  boost::python::numeric::array  boost::python::numeric::array
502  Data::convertToNumArray()  Data:: getValueOfDataPoint(int dataPointNo)
503  {  {
504    //    size_t length=0;
505    // determine the total number of data points    int i, j, k, l;
   int numSamples = getNumSamples();  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
   int numDataPoints = numSamples * numDataPointsPerSample;  
   
506    //    //
507    // determine the rank and shape of each data point    // determine the rank and shape of each data point
508    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
# Line 568  Data::convertToNumArray() Line 513  Data::convertToNumArray()
513    boost::python::numeric::array numArray(0.0);    boost::python::numeric::array numArray(0.0);
514    
515    //    //
516    // the rank of the returned numeric array will be the rank of    // the shape of the returned numeric array will be the same
517    // the data points, plus one. Where the rank of the array is n,    // as that of the data point
518    // the last n-1 dimensions will be equal to the shape of the    int arrayRank = dataPointRank;
519    // data points, whilst the first dimension will be equal to the    DataArrayView::ShapeType arrayShape = dataPointShape;
   // total number of data points. Thus the array will consist of  
   // a serial vector of the data points.  
   int arrayRank = dataPointRank + 1;  
   DataArrayView::ShapeType arrayShape;  
   arrayShape.push_back(numDataPoints);  
   for (int d=0; d<dataPointRank; d++) {  
      arrayShape.push_back(dataPointShape[d]);  
   }  
520    
521    //    //
522    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
523      if (arrayRank==0) {
524        numArray.resize(1);
525      }
526    if (arrayRank==1) {    if (arrayRank==1) {
527      numArray.resize(arrayShape[0]);      numArray.resize(arrayShape[0]);
528    }    }
# Line 595  Data::convertToNumArray() Line 535  Data::convertToNumArray()
535    if (arrayRank==4) {    if (arrayRank==4) {
536      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
537    }    }
   if (arrayRank==5) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);  
   }  
538    
539    //    if (getNumDataPointsPerSample()>0) {
540    // loop through each data point in turn, loading the values for that data point         int sampleNo = dataPointNo/getNumDataPointsPerSample();
541    // into the numeric array.         int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
542    int dataPoint = 0;         //
543    for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {         // Check a valid sample number has been supplied
544      for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
545        DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
546        if (dataPointRank==0) {         }
547          numArray[dataPoint]=dataPointView();                
548        }         //
549        if (dataPointRank==1) {         // Check a valid data point number has been supplied
550          for (int i=0; i<dataPointShape[0]; i++) {         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
551            numArray[dataPoint][i]=dataPointView(i);             throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
552          }         }
553        }         // TODO: global error handling
554        if (dataPointRank==2) {         // create a view of the data if it is stored locally
555          for (int i=0; i<dataPointShape[0]; i++) {         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
556            for (int j=0; j<dataPointShape[1]; j++) {          
557              numArray[dataPoint][i][j] = dataPointView(i,j);         switch( dataPointRank ){
558            }              case 0 :
559          }                  numArray[0] = dataPointView();
560        }                  break;
561        if (dataPointRank==3) {              case 1 :        
562          for (int i=0; i<dataPointShape[0]; i++) {                  for( i=0; i<dataPointShape[0]; i++ )
563            for (int j=0; j<dataPointShape[1]; j++) {                      numArray[i]=dataPointView(i);
564              for (int k=0; k<dataPointShape[2]; k++) {                  break;
565                numArray[dataPoint][i][j][k]=dataPointView(i,j,k);              case 2 :        
566              }                  for( i=0; i<dataPointShape[0]; i++ )
567            }                      for( j=0; j<dataPointShape[1]; j++)
568          }                          numArray[make_tuple(i,j)]=dataPointView(i,j);
569        }                  break;
570        if (dataPointRank==4) {              case 3 :        
571          for (int i=0; i<dataPointShape[0]; i++) {                  for( i=0; i<dataPointShape[0]; i++ )
572            for (int j=0; j<dataPointShape[1]; j++) {                      for( j=0; j<dataPointShape[1]; j++ )
573              for (int k=0; k<dataPointShape[2]; k++) {                          for( k=0; k<dataPointShape[2]; k++)
574                for (int l=0; l<dataPointShape[3]; l++) {                              numArray[make_tuple(i,j,k)]=dataPointView(i,j,k);
575                  numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);                  break;
576                }              case 4 :
577              }                  for( i=0; i<dataPointShape[0]; i++ )
578            }                      for( j=0; j<dataPointShape[1]; j++ )
579          }                          for( k=0; k<dataPointShape[2]; k++ )
580        }                              for( l=0; l<dataPointShape[3]; l++)
581        dataPoint++;                                  numArray[make_tuple(i,j,k,l)]=dataPointView(i,j,k,l);
582      }                  break;
583        }
584    }    }
   
585    //    //
586    // return the loaded array    // return the array
587    return numArray;    return numArray;
 }  
588    
589  const  }
590  boost::python::numeric::array  void
591  Data::convertToNumArrayFromSampleNo(int sampleNo)  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
592  {  {
593    //      // this will throw if the value cannot be represented
594    // Check a valid sample number has been supplied      boost::python::numeric::array num_array(py_object);
595    if (sampleNo >= getNumSamples()) {      setValueOfDataPointToArray(dataPointNo,num_array);
     throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  
   }  
596    
   //  
   // determine the number of data points per sample  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
597    
598    //  }
   // determine the rank and shape of each data point  
   int dataPointRank = getDataPointRank();  
   DataArrayView::ShapeType dataPointShape = getDataPointShape();  
599    
600    void
601    Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array& num_array)
602    {
603      if (isProtected()) {
604            throw DataException("Error - attempt to update protected Data object.");
605      }
606    //    //
607    // create the numeric array to be returned    // check rank
608    boost::python::numeric::array numArray(0.0);    if (num_array.getrank()<getDataPointRank())
609          throw DataException("Rank of numarray does not match Data object rank");
610    
611    //    //
612    // the rank of the returned numeric array will be the rank of    // check shape of num_array
613    // the data points, plus one. Where the rank of the array is n,    for (int i=0; i<getDataPointRank(); i++) {
614    // the last n-1 dimensions will be equal to the shape of the      if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])
615    // data points, whilst the first dimension will be equal to the         throw DataException("Shape of numarray does not match Data object rank");
   // total number of data points. Thus the array will consist of  
   // a serial vector of the data points.  
   int arrayRank = dataPointRank + 1;  
   DataArrayView::ShapeType arrayShape;  
   arrayShape.push_back(numDataPointsPerSample);  
   for (int d=0; d<dataPointRank; d++) {  
      arrayShape.push_back(dataPointShape[d]);  
616    }    }
   
617    //    //
618    // resize the numeric array to the shape just calculated    // make sure data is expanded:
619    if (arrayRank==1) {    if (!isExpanded()) {
620      numArray.resize(arrayShape[0]);      expand();
   }  
   if (arrayRank==2) {  
     numArray.resize(arrayShape[0],arrayShape[1]);  
   }  
   if (arrayRank==3) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2]);  
   }  
   if (arrayRank==4) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);  
   }  
   if (arrayRank==5) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);  
621    }    }
622      if (getNumDataPointsPerSample()>0) {
623    //         int sampleNo = dataPointNo/getNumDataPointsPerSample();
624    // loop through each data point in turn, loading the values for that data point         int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
625    // into the numeric array.         m_data->copyToDataPoint(sampleNo, dataPointNoInSample,num_array);
626    for (int dataPoint = 0; dataPoint < numDataPointsPerSample; dataPoint++) {    } else {
627      DataArrayView dataPointView = getDataPoint(sampleNo, dataPoint);         m_data->copyToDataPoint(-1, 0,num_array);
     if (dataPointRank==0) {  
       numArray[dataPoint]=dataPointView();  
     }  
     if (dataPointRank==1) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         numArray[dataPoint][i]=dataPointView(i);  
       }  
     }  
     if (dataPointRank==2) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         for (int j=0; j<dataPointShape[1]; j++) {  
           numArray[dataPoint][i][j] = dataPointView(i,j);  
         }  
       }  
     }  
     if (dataPointRank==3) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         for (int j=0; j<dataPointShape[1]; j++) {  
           for (int k=0; k<dataPointShape[2]; k++) {  
             numArray[dataPoint][i][j][k]=dataPointView(i,j,k);  
           }  
         }  
       }  
     }  
     if (dataPointRank==4) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         for (int j=0; j<dataPointShape[1]; j++) {  
           for (int k=0; k<dataPointShape[2]; k++) {  
             for (int l=0; l<dataPointShape[3]; l++) {  
               numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);  
             }  
           }  
         }  
       }  
     }  
628    }    }
   
   //  
   // return the loaded array  
   return numArray;  
629  }  }
630    
631  const  void
632  boost::python::numeric::array  Data::setValueOfDataPoint(int dataPointNo, const double value)
 #ifdef PASO_MPI  
 Data::convertToNumArrayFromDPNo(int procNo,  
                                 int sampleNo,  
                                                                 int dataPointNo)  
   
 #else  
 Data::convertToNumArrayFromDPNo(int sampleNo,  
                                 int dataPointNo)  
 #endif  
633  {  {
634    //    if (isProtected()) {
635    // Check a valid sample number has been supplied          throw DataException("Error - attempt to update protected Data object.");
   if (sampleNo >= getNumSamples()) {  
     throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  
636    }    }
   
637    //    //
638    // Check a valid data point number has been supplied    // make sure data is expanded:
639    if (dataPointNo >= getNumDataPointsPerSample()) {    if (!isExpanded()) {
640      throw DataException("Error - Data::convertToNumArray: invalid dataPointNo.");      expand();
641    }    }
642      if (getNumDataPointsPerSample()>0) {
643           int sampleNo = dataPointNo/getNumDataPointsPerSample();
644           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
645           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,value);
646      } else {
647           m_data->copyToDataPoint(-1, 0,value);
648      }
649    }
650    
651    const
652    boost::python::numeric::array
653    Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
654    {
655      size_t length=0;
656      int i, j, k, l, pos;
657    //    //
658    // determine the rank and shape of each data point    // determine the rank and shape of each data point
659    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
# Line 809  Data::convertToNumArrayFromDPNo(int samp Line 687  Data::convertToNumArrayFromDPNo(int samp
687      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
688    }    }
689    
690      // added for the MPI communication
691      length=1;
692      for( i=0; i<arrayRank; i++ ) length *= arrayShape[i];
693      double *tmpData = new double[length];
694    
695    //    //
696    // load the values for the data point into the numeric array.    // load the values for the data point into the numeric array.
   DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);  
   if (dataPointRank==0) {  
     numArray[0]=dataPointView();  
   }  
   if (dataPointRank==1) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       numArray[i]=dataPointView(i);  
     }  
   }  
   if (dataPointRank==2) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         numArray[i][j] = dataPointView(i,j);  
       }  
     }  
   }  
   if (dataPointRank==3) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         for (int k=0; k<dataPointShape[2]; k++) {  
           numArray[i][j][k]=dataPointView(i,j,k);  
         }  
       }  
     }  
   }  
   if (dataPointRank==4) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         for (int k=0; k<dataPointShape[2]; k++) {  
           for (int l=0; l<dataPointShape[3]; l++) {  
             numArray[i][j][k][l]=dataPointView(i,j,k,l);  
           }  
         }  
       }  
     }  
   }  
697    
698        // updated for the MPI case
699        if( get_MPIRank()==procNo ){
700                 if (getNumDataPointsPerSample()>0) {
701                    int sampleNo = dataPointNo/getNumDataPointsPerSample();
702                    int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
703                    //
704                    // Check a valid sample number has been supplied
705                    if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
706                      throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
707                    }
708                  
709                    //
710                    // Check a valid data point number has been supplied
711                    if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
712                      throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
713                    }
714                    // TODO: global error handling
715            // create a view of the data if it is stored locally
716            DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
717            
718            // pack the data from the view into tmpData for MPI communication
719            pos=0;
720            switch( dataPointRank ){
721                case 0 :
722                    tmpData[0] = dataPointView();
723                    break;
724                case 1 :        
725                    for( i=0; i<dataPointShape[0]; i++ )
726                        tmpData[i]=dataPointView(i);
727                    break;
728                case 2 :        
729                    for( i=0; i<dataPointShape[0]; i++ )
730                        for( j=0; j<dataPointShape[1]; j++, pos++ )
731                            tmpData[pos]=dataPointView(i,j);
732                    break;
733                case 3 :        
734                    for( i=0; i<dataPointShape[0]; i++ )
735                        for( j=0; j<dataPointShape[1]; j++ )
736                            for( k=0; k<dataPointShape[2]; k++, pos++ )
737                                tmpData[pos]=dataPointView(i,j,k);
738                    break;
739                case 4 :
740                    for( i=0; i<dataPointShape[0]; i++ )
741                        for( j=0; j<dataPointShape[1]; j++ )
742                            for( k=0; k<dataPointShape[2]; k++ )
743                                for( l=0; l<dataPointShape[3]; l++, pos++ )
744                                    tmpData[pos]=dataPointView(i,j,k,l);
745                    break;
746            }
747                }
748        }
749            #ifdef PASO_MPI
750            // broadcast the data to all other processes
751        MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
752            #endif
753    
754        // unpack the data
755        switch( dataPointRank ){
756            case 0 :
757                numArray[0]=tmpData[0];
758                break;
759            case 1 :        
760                for( i=0; i<dataPointShape[0]; i++ )
761                    numArray[i]=tmpData[i];
762                break;
763            case 2 :        
764                for( i=0; i<dataPointShape[0]; i++ )
765                    for( j=0; j<dataPointShape[1]; j++ )
766                       numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
767                break;
768            case 3 :        
769                for( i=0; i<dataPointShape[0]; i++ )
770                    for( j=0; j<dataPointShape[1]; j++ )
771                        for( k=0; k<dataPointShape[2]; k++ )
772                            numArray[make_tuple(i,j,k)]=tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];
773                break;
774            case 4 :
775                for( i=0; i<dataPointShape[0]; i++ )
776                    for( j=0; j<dataPointShape[1]; j++ )
777                        for( k=0; k<dataPointShape[2]; k++ )
778                            for( l=0; l<dataPointShape[3]; l++ )
779                                    numArray[make_tuple(i,j,k,l)]=tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];
780                break;
781        }
782    
783        delete [] tmpData;  
784    //    //
785    // return the loaded array    // return the loaded array
786    return numArray;    return numArray;
787  }  }
788    
789    
790    
791  boost::python::numeric::array  boost::python::numeric::array
792  Data::integrate() const  Data::integrate() const
793  {  {
# Line 860  Data::integrate() const Line 795  Data::integrate() const
795    int rank = getDataPointRank();    int rank = getDataPointRank();
796    DataArrayView::ShapeType shape = getDataPointShape();    DataArrayView::ShapeType shape = getDataPointShape();
797    
 #if defined DOPROF  
   profData->integrate++;  
 #endif  
798    
799    //    //
800    // calculate the integral values    // calculate the integral values
# Line 927  Data::integrate() const Line 859  Data::integrate() const
859  Data  Data
860  Data::sin() const  Data::sin() const
861  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
862    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);
863  }  }
864    
865  Data  Data
866  Data::cos() const  Data::cos() const
867  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
868    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);
869  }  }
870    
871  Data  Data
872  Data::tan() const  Data::tan() const
873  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
874    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);
875  }  }
876    
877  Data  Data
878  Data::asin() const  Data::asin() const
879  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
880    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);
881  }  }
882    
883  Data  Data
884  Data::acos() const  Data::acos() const
885  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
886    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);
887  }  }
888    
889    
890  Data  Data
891  Data::atan() const  Data::atan() const
892  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
893    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);
894  }  }
895    
896  Data  Data
897  Data::sinh() const  Data::sinh() const
898  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
899    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);
900  }  }
901    
902  Data  Data
903  Data::cosh() const  Data::cosh() const
904  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
905    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);
906  }  }
907    
908  Data  Data
909  Data::tanh() const  Data::tanh() const
910  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
911    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);
912  }  }
913    
914    
915  Data  Data
916  Data::asinh() const  Data::erf() const
917  {  {
918  #if defined DOPROF  #ifdef _WIN32
919    profData->unary++;    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
920    #else
921      return escript::unaryOp(*this,(Data::UnaryDFunPtr)::erf);
922  #endif  #endif
923    }
924    
925    Data
926    Data::asinh() const
927    {
928    #ifdef _WIN32
929      return escript::unaryOp(*this,escript::asinh_substitute);
930    #else
931    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);
932    #endif
933  }  }
934    
935  Data  Data
936  Data::acosh() const  Data::acosh() const
937  {  {
938  #if defined DOPROF  #ifdef _WIN32
939    profData->unary++;    return escript::unaryOp(*this,escript::acosh_substitute);
940  #endif  #else
941    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);
942    #endif
943  }  }
944    
945  Data  Data
946  Data::atanh() const  Data::atanh() const
947  {  {
948  #if defined DOPROF  #ifdef _WIN32
949    profData->unary++;    return escript::unaryOp(*this,escript::atanh_substitute);
950  #endif  #else
951    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);
952    #endif
953  }  }
954    
955  Data  Data
956  Data::log10() const  Data::log10() const
957  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
958    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);
959  }  }
960    
961  Data  Data
962  Data::log() const  Data::log() const
963  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
964    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);
965  }  }
966    
967  Data  Data
968  Data::sign() const  Data::sign() const
969  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
970    return escript::unaryOp(*this,escript::fsign);    return escript::unaryOp(*this,escript::fsign);
971  }  }
972    
973  Data  Data
974  Data::abs() const  Data::abs() const
975  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
976    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);
977  }  }
978    
979  Data  Data
980  Data::neg() const  Data::neg() const
981  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
982    return escript::unaryOp(*this,negate<double>());    return escript::unaryOp(*this,negate<double>());
983  }  }
984    
985  Data  Data
986  Data::pos() const  Data::pos() const
987  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
988    Data result;    Data result;
989    // perform a deep copy    // perform a deep copy
990    result.copy(*this);    result.copy(*this);
# Line 1092  Data::pos() const Line 994  Data::pos() const
994  Data  Data
995  Data::exp() const  Data::exp() const
996  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
997    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);
998  }  }
999    
1000  Data  Data
1001  Data::sqrt() const  Data::sqrt() const
1002  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
1003    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);
1004  }  }
1005    
# Line 1111  double Line 1007  double
1007  Data::Lsup() const  Data::Lsup() const
1008  {  {
1009    double localValue, globalValue;    double localValue, globalValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1010    //    //
1011    // set the initial absolute maximum value to zero    // set the initial absolute maximum value to zero
1012    
# Line 1131  double Line 1024  double
1024  Data::Linf() const  Data::Linf() const
1025  {  {
1026    double localValue, globalValue;    double localValue, globalValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1027    //    //
1028    // set the initial absolute minimum value to max double    // set the initial absolute minimum value to max double
1029    AbsMin abs_min_func;    AbsMin abs_min_func;
# Line 1151  double Line 1041  double
1041  Data::sup() const  Data::sup() const
1042  {  {
1043    double localValue, globalValue;    double localValue, globalValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1044    //    //
1045    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1046    FMax fmax_func;    FMax fmax_func;
# Line 1170  double Line 1057  double
1057  Data::inf() const  Data::inf() const
1058  {  {
1059    double localValue, globalValue;    double localValue, globalValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1060    //    //
1061    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1062    FMin fmin_func;    FMin fmin_func;
# Line 1190  Data::inf() const Line 1074  Data::inf() const
1074  Data  Data
1075  Data::maxval() const  Data::maxval() const
1076  {  {
 #if defined DOPROF  
   profData->reduction2++;  
 #endif  
1077    //    //
1078    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1079    FMax fmax_func;    FMax fmax_func;
# Line 1202  Data::maxval() const Line 1083  Data::maxval() const
1083  Data  Data
1084  Data::minval() const  Data::minval() const
1085  {  {
 #if defined DOPROF  
   profData->reduction2++;  
 #endif  
1086    //    //
1087    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1088    FMin fmin_func;    FMin fmin_func;
# Line 1212  Data::minval() const Line 1090  Data::minval() const
1090  }  }
1091    
1092  Data  Data
1093  Data::trace() const  Data::swapaxes(const int axis0, const int axis1) const
1094  {  {
1095  #if defined DOPROF       int axis0_tmp,axis1_tmp;
1096    profData->reduction2++;       DataArrayView::ShapeType s=getDataPointShape();
1097  #endif       DataArrayView::ShapeType ev_shape;
1098    Trace trace_func;       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1099    return dp_algorithm(trace_func,0);       // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1100         int rank=getDataPointRank();
1101         if (rank<2) {
1102            throw DataException("Error - Data::swapaxes argument must have at least rank 2.");
1103         }
1104         if (axis0<0 || axis0>rank-1) {
1105            throw DataException("Error - Data::swapaxes: axis0 must be between 0 and rank-1=" + rank-1);
1106         }
1107         if (axis1<0 || axis1>rank-1) {
1108             throw DataException("Error - Data::swapaxes: axis1 must be between 0 and rank-1=" + rank-1);
1109         }
1110         if (axis0 == axis1) {
1111             throw DataException("Error - Data::swapaxes: axis indices must be different.");
1112         }
1113         if (axis0 > axis1) {
1114             axis0_tmp=axis1;
1115             axis1_tmp=axis0;
1116         } else {
1117             axis0_tmp=axis0;
1118             axis1_tmp=axis1;
1119         }
1120         for (int i=0; i<rank; i++) {
1121           if (i == axis0_tmp) {
1122              ev_shape.push_back(s[axis1_tmp]);
1123           } else if (i == axis1_tmp) {
1124              ev_shape.push_back(s[axis0_tmp]);
1125           } else {
1126              ev_shape.push_back(s[i]);
1127           }
1128         }
1129         Data ev(0.,ev_shape,getFunctionSpace());
1130         ev.typeMatchRight(*this);
1131         m_data->swapaxes(ev.m_data.get(), axis0_tmp, axis1_tmp);
1132         return ev;
1133    
1134  }  }
1135    
1136  Data  Data
1137  Data::symmetric() const  Data::symmetric() const
1138  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1139       // check input       // check input
1140       DataArrayView::ShapeType s=getDataPointShape();       DataArrayView::ShapeType s=getDataPointShape();
1141       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
# Line 1249  Data::symmetric() const Line 1158  Data::symmetric() const
1158  Data  Data
1159  Data::nonsymmetric() const  Data::nonsymmetric() const
1160  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1161       // check input       // check input
1162       DataArrayView::ShapeType s=getDataPointShape();       DataArrayView::ShapeType s=getDataPointShape();
1163       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
# Line 1284  Data::nonsymmetric() const Line 1190  Data::nonsymmetric() const
1190  }  }
1191    
1192  Data  Data
1193  Data::matrixtrace(int axis_offset) const  Data::trace(int axis_offset) const
1194  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1195       DataArrayView::ShapeType s=getDataPointShape();       DataArrayView::ShapeType s=getDataPointShape();
1196       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1197          DataArrayView::ShapeType ev_shape;          DataArrayView::ShapeType ev_shape;
1198          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1199          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1200          m_data->matrixtrace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1201          return ev;          return ev;
1202       }       }
1203       if (getDataPointRank()==3) {       if (getDataPointRank()==3) {
# Line 1309  Data::matrixtrace(int axis_offset) const Line 1212  Data::matrixtrace(int axis_offset) const
1212          }          }
1213          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1214          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1215          m_data->matrixtrace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1216          return ev;          return ev;
1217       }       }
1218       if (getDataPointRank()==4) {       if (getDataPointRank()==4) {
# Line 1328  Data::matrixtrace(int axis_offset) const Line 1231  Data::matrixtrace(int axis_offset) const
1231      }      }
1232          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1233          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1234      m_data->matrixtrace(ev.m_data.get(), axis_offset);      m_data->trace(ev.m_data.get(), axis_offset);
1235          return ev;          return ev;
1236       }       }
1237       else {       else {
1238          throw DataException("Error - Data::matrixtrace can only be calculated for rank 2, 3 or 4 object.");          throw DataException("Error - Data::trace can only be calculated for rank 2, 3 or 4 object.");
1239       }       }
1240  }  }
1241    
1242  Data  Data
1243  Data::transpose(int axis_offset) const  Data::transpose(int axis_offset) const
1244  {  {
 #if defined DOPROF  
      profData->reduction2++;  
 #endif  
1245       DataArrayView::ShapeType s=getDataPointShape();       DataArrayView::ShapeType s=getDataPointShape();
1246       DataArrayView::ShapeType ev_shape;       DataArrayView::ShapeType ev_shape;
1247       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
# Line 1363  Data::transpose(int axis_offset) const Line 1263  Data::transpose(int axis_offset) const
1263  Data  Data
1264  Data::eigenvalues() const  Data::eigenvalues() const
1265  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1266       // check input       // check input
1267       DataArrayView::ShapeType s=getDataPointShape();       DataArrayView::ShapeType s=getDataPointShape();
1268       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
# Line 1383  Data::eigenvalues() const Line 1280  Data::eigenvalues() const
1280  const boost::python::tuple  const boost::python::tuple
1281  Data::eigenvalues_and_eigenvectors(const double tol) const  Data::eigenvalues_and_eigenvectors(const double tol) const
1282  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1283       DataArrayView::ShapeType s=getDataPointShape();       DataArrayView::ShapeType s=getDataPointShape();
1284       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1285          throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for rank 2 object.");          throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for rank 2 object.");
# Line 1403  Data::eigenvalues_and_eigenvectors(const Line 1297  Data::eigenvalues_and_eigenvectors(const
1297  }  }
1298    
1299  const boost::python::tuple  const boost::python::tuple
1300  Data::mindp() const  Data::minGlobalDataPoint() const
1301  {  {
1302    // NB: calc_mindp had to be split off from mindp as boost::make_tuple causes an    // NB: calc_minGlobalDataPoint( had to be split off from minGlobalDataPoint( as boost::make_tuple causes an
1303    // abort (for unknown reasons) if there are openmp directives with it in the    // abort (for unknown reasons) if there are openmp directives with it in the
1304    // surrounding function    // surrounding function
1305    
   int SampleNo;  
1306    int DataPointNo;    int DataPointNo;
1307  #ifdef PASO_MPI    int ProcNo;
1308      int ProcNo;    calc_minGlobalDataPoint(ProcNo,DataPointNo);
1309    calc_mindp(ProcNo,SampleNo,DataPointNo);    return make_tuple(ProcNo,DataPointNo);
   return make_tuple(ProcNo,SampleNo,DataPointNo);  
 #else  
   calc_mindp(SampleNo,DataPointNo);  
   return make_tuple(SampleNo,DataPointNo);  
 #endif  
1310  }  }
1311    
1312  void  void
1313  #ifdef PASO_MPI  Data::calc_minGlobalDataPoint(int& ProcNo,
1314  Data::calc_mindp(   int& ProcNo,                          int& DataPointNo) const
                 int& SampleNo,  
         int& DataPointNo) const  
 #else  
 Data::calc_mindp(   int& SampleNo,  
         int& DataPointNo) const  
 #endif  
1315  {  {
1316    int i,j;    int i,j;
1317    int lowi=0,lowj=0;    int lowi=0,lowj=0;
# Line 1484  Data::calc_mindp(  int& SampleNo, Line 1366  Data::calc_mindp(  int& SampleNo,
1366    
1367      delete [] globalMins;      delete [] globalMins;
1368      ProcNo = lowProc;      ProcNo = lowProc;
1369    #else
1370        ProcNo = 0;
1371  #endif  #endif
1372    SampleNo = lowi;    DataPointNo = lowj + lowi * numDPPSample;
   DataPointNo = lowj;  
1373  }  }
1374    
1375  void  void
# Line 1510  Data::saveVTK(std::string fileName) cons Line 1393  Data::saveVTK(std::string fileName) cons
1393  Data&  Data&
1394  Data::operator+=(const Data& right)  Data::operator+=(const Data& right)
1395  {  {
1396  #if defined DOPROF    if (isProtected()) {
1397    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1398  #endif    }
1399    binaryOp(right,plus<double>());    binaryOp(right,plus<double>());
1400    return (*this);    return (*this);
1401  }  }
# Line 1520  Data::operator+=(const Data& right) Line 1403  Data::operator+=(const Data& right)
1403  Data&  Data&
1404  Data::operator+=(const boost::python::object& right)  Data::operator+=(const boost::python::object& right)
1405  {  {
1406  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1407    profData->binary++;    binaryOp(tmp,plus<double>());
 #endif  
   binaryOp(right,plus<double>());  
1408    return (*this);    return (*this);
1409  }  }
1410    
1411  Data&  Data&
1412  Data::operator-=(const Data& right)  Data::operator-=(const Data& right)
1413  {  {
1414  #if defined DOPROF    if (isProtected()) {
1415    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1416  #endif    }
1417    binaryOp(right,minus<double>());    binaryOp(right,minus<double>());
1418    return (*this);    return (*this);
1419  }  }
# Line 1540  Data::operator-=(const Data& right) Line 1421  Data::operator-=(const Data& right)
1421  Data&  Data&
1422  Data::operator-=(const boost::python::object& right)  Data::operator-=(const boost::python::object& right)
1423  {  {
1424  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1425    profData->binary++;    binaryOp(tmp,minus<double>());
 #endif  
   binaryOp(right,minus<double>());  
1426    return (*this);    return (*this);
1427  }  }
1428    
1429  Data&  Data&
1430  Data::operator*=(const Data& right)  Data::operator*=(const Data& right)
1431  {  {
1432  #if defined DOPROF    if (isProtected()) {
1433    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1434  #endif    }
1435    binaryOp(right,multiplies<double>());    binaryOp(right,multiplies<double>());
1436    return (*this);    return (*this);
1437  }  }
# Line 1560  Data::operator*=(const Data& right) Line 1439  Data::operator*=(const Data& right)
1439  Data&  Data&
1440  Data::operator*=(const boost::python::object& right)  Data::operator*=(const boost::python::object& right)
1441  {  {
1442  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1443    profData->binary++;    binaryOp(tmp,multiplies<double>());
 #endif  
   binaryOp(right,multiplies<double>());  
1444    return (*this);    return (*this);
1445  }  }
1446    
1447  Data&  Data&
1448  Data::operator/=(const Data& right)  Data::operator/=(const Data& right)
1449  {  {
1450  #if defined DOPROF    if (isProtected()) {
1451    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1452  #endif    }
1453    binaryOp(right,divides<double>());    binaryOp(right,divides<double>());
1454    return (*this);    return (*this);
1455  }  }
# Line 1580  Data::operator/=(const Data& right) Line 1457  Data::operator/=(const Data& right)
1457  Data&  Data&
1458  Data::operator/=(const boost::python::object& right)  Data::operator/=(const boost::python::object& right)
1459  {  {
1460  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1461    profData->binary++;    binaryOp(tmp,divides<double>());
 #endif  
   binaryOp(right,divides<double>());  
1462    return (*this);    return (*this);
1463  }  }
1464    
1465  Data  Data
1466  Data::rpowO(const boost::python::object& left) const  Data::rpowO(const boost::python::object& left) const
1467  {  {
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1468    Data left_d(left,*this);    Data left_d(left,*this);
1469    return left_d.powD(*this);    return left_d.powD(*this);
1470  }  }
# Line 1600  Data::rpowO(const boost::python::object& Line 1472  Data::rpowO(const boost::python::object&
1472  Data  Data
1473  Data::powO(const boost::python::object& right) const  Data::powO(const boost::python::object& right) const
1474  {  {
1475  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1476    profData->binary++;    return powD(tmp);
 #endif  
   Data result;  
   result.copy(*this);  
   result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   return result;  
1477  }  }
1478    
1479  Data  Data
1480  Data::powD(const Data& right) const  Data::powD(const Data& right) const
1481  {  {
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1482    Data result;    Data result;
1483    result.copy(*this);    if (getDataPointRank()<right.getDataPointRank()) {
1484    result.binaryOp(right,(Data::BinaryDFunPtr)::pow);       result.copy(right);
1485         result.binaryOp(*this,escript::rpow);
1486      } else {
1487         result.copy(*this);
1488         result.binaryOp(right,(Data::BinaryDFunPtr)::pow);
1489      }
1490    return result;    return result;
1491  }  }
1492    
# Line 1630  escript::operator+(const Data& left, con Line 1499  escript::operator+(const Data& left, con
1499    Data result;    Data result;
1500    //    //
1501    // perform a deep copy    // perform a deep copy
1502    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1503    result+=right;       result.copy(right);
1504         result+=left;
1505      } else {
1506         result.copy(left);
1507         result+=right;
1508      }
1509    return result;    return result;
1510  }  }
1511    
# Line 1643  escript::operator-(const Data& left, con Line 1517  escript::operator-(const Data& left, con
1517    Data result;    Data result;
1518    //    //
1519    // perform a deep copy    // perform a deep copy
1520    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1521    result-=right;       result=right.neg();
1522         result+=left;
1523      } else {
1524         result.copy(left);
1525         result-=right;
1526      }
1527    return result;    return result;
1528  }  }
1529    
# Line 1656  escript::operator*(const Data& left, con Line 1535  escript::operator*(const Data& left, con
1535    Data result;    Data result;
1536    //    //
1537    // perform a deep copy    // perform a deep copy
1538    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1539    result*=right;       result.copy(right);
1540         result*=left;
1541      } else {
1542         result.copy(left);
1543         result*=right;
1544      }
1545    return result;    return result;
1546  }  }
1547    
# Line 1669  escript::operator/(const Data& left, con Line 1553  escript::operator/(const Data& left, con
1553    Data result;    Data result;
1554    //    //
1555    // perform a deep copy    // perform a deep copy
1556    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1557    result/=right;       result=right.oneOver();
1558         result*=left;
1559      } else {
1560         result.copy(left);
1561         result/=right;
1562      }
1563    return result;    return result;
1564  }  }
1565    
# Line 1679  escript::operator/(const Data& left, con Line 1568  escript::operator/(const Data& left, con
1568  Data  Data
1569  escript::operator+(const Data& left, const boost::python::object& right)  escript::operator+(const Data& left, const boost::python::object& right)
1570  {  {
1571    //    return left+Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result+=right;  
   return result;  
1572  }  }
1573    
1574  //  //
# Line 1695  escript::operator+(const Data& left, con Line 1576  escript::operator+(const Data& left, con
1576  Data  Data
1577  escript::operator-(const Data& left, const boost::python::object& right)  escript::operator-(const Data& left, const boost::python::object& right)
1578  {  {
1579    //    return left-Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result-=right;  
   return result;  
1580  }  }
1581    
1582  //  //
# Line 1711  escript::operator-(const Data& left, con Line 1584  escript::operator-(const Data& left, con
1584  Data  Data
1585  escript::operator*(const Data& left, const boost::python::object& right)  escript::operator*(const Data& left, const boost::python::object& right)
1586  {  {
1587    //    return left*Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result*=right;  
   return result;  
1588  }  }
1589    
1590  //  //
# Line 1727  escript::operator*(const Data& left, con Line 1592  escript::operator*(const Data& left, con
1592  Data  Data
1593  escript::operator/(const Data& left, const boost::python::object& right)  escript::operator/(const Data& left, const boost::python::object& right)
1594  {  {
1595    //    return left/Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result/=right;  
   return result;  
1596  }  }
1597    
1598  //  //
# Line 1743  escript::operator/(const Data& left, con Line 1600  escript::operator/(const Data& left, con
1600  Data  Data
1601  escript::operator+(const boost::python::object& left, const Data& right)  escript::operator+(const boost::python::object& left, const Data& right)
1602  {  {
1603    //    return Data(left,right.getFunctionSpace(),false)+right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result+=right;  
   return result;  
1604  }  }
1605    
1606  //  //
# Line 1756  escript::operator+(const boost::python:: Line 1608  escript::operator+(const boost::python::
1608  Data  Data
1609  escript::operator-(const boost::python::object& left, const Data& right)  escript::operator-(const boost::python::object& left, const Data& right)
1610  {  {
1611    //    return Data(left,right.getFunctionSpace(),false)-right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result-=right;  
   return result;  
1612  }  }
1613    
1614  //  //
# Line 1769  escript::operator-(const boost::python:: Line 1616  escript::operator-(const boost::python::
1616  Data  Data
1617  escript::operator*(const boost::python::object& left, const Data& right)  escript::operator*(const boost::python::object& left, const Data& right)
1618  {  {
1619    //    return Data(left,right.getFunctionSpace(),false)*right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result*=right;  
   return result;  
1620  }  }
1621    
1622  //  //
# Line 1782  escript::operator*(const boost::python:: Line 1624  escript::operator*(const boost::python::
1624  Data  Data
1625  escript::operator/(const boost::python::object& left, const Data& right)  escript::operator/(const boost::python::object& left, const Data& right)
1626  {  {
1627    //    return Data(left,right.getFunctionSpace(),false)/right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result/=right;  
   return result;  
1628  }  }
1629    
1630  //  //
# Line 1856  Data::getItem(const boost::python::objec Line 1693  Data::getItem(const boost::python::objec
1693  Data  Data
1694  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataArrayView::RegionType& region) const
1695  {  {
 #if defined DOPROF  
   profData->slicing++;  
 #endif  
1696    return Data(*this,region);    return Data(*this,region);
1697  }  }
1698    
# Line 1897  void Line 1731  void
1731  Data::setSlice(const Data& value,  Data::setSlice(const Data& value,
1732                 const DataArrayView::RegionType& region)                 const DataArrayView::RegionType& region)
1733  {  {
1734  #if defined DOPROF    if (isProtected()) {
1735    profData->slicing++;          throw DataException("Error - attempt to update protected Data object.");
1736  #endif    }
1737    Data tempValue(value);    Data tempValue(value);
1738    typeMatchLeft(tempValue);    typeMatchLeft(tempValue);
1739    typeMatchRight(tempValue);    typeMatchRight(tempValue);
# Line 1934  Data::typeMatchRight(const Data& right) Line 1768  Data::typeMatchRight(const Data& right)
1768    }    }
1769  }  }
1770    
1771  /* TODO */  void
1772  /* global reduction */  Data::setTaggedValueByName(std::string name,
1773                               const boost::python::object& value)
1774    {
1775         if (getFunctionSpace().getDomain().isValidTagName(name)) {
1776            int tagKey=getFunctionSpace().getDomain().getTag(name);
1777            setTaggedValue(tagKey,value);
1778         }
1779    }
1780  void  void
1781  Data::setTaggedValue(int tagKey,  Data::setTaggedValue(int tagKey,
1782                       const boost::python::object& value)                       const boost::python::object& value)
1783  {  {
1784      if (isProtected()) {
1785            throw DataException("Error - attempt to update protected Data object.");
1786      }
1787    //    //
1788    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1789    tag();    tag();
# Line 1957  Data::setTaggedValue(int tagKey, Line 1801  Data::setTaggedValue(int tagKey,
1801    m_data->setTaggedValue(tagKey,valueDataArray.getView());    m_data->setTaggedValue(tagKey,valueDataArray.getView());
1802  }  }
1803    
 /* TODO */  
 /* global reduction */  
1804  void  void
1805  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
1806                              const DataArrayView& value)                              const DataArrayView& value)
1807  {  {
1808      if (isProtected()) {
1809            throw DataException("Error - attempt to update protected Data object.");
1810      }
1811    //    //
1812    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1813    tag();    tag();
# Line 1976  Data::setTaggedValueFromCPP(int tagKey, Line 1821  Data::setTaggedValueFromCPP(int tagKey,
1821    m_data->setTaggedValue(tagKey,value);    m_data->setTaggedValue(tagKey,value);
1822  }  }
1823    
 /* TODO */  
 /* global reduction */  
1824  int  int
1825  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
1826  {  {
1827    return m_data->getTagNumber(dpno);    return m_data->getTagNumber(dpno);
1828  }  }
1829    
 /* TODO */  
 /* global reduction */  
 void  
 Data::setRefValue(int ref,  
                   const boost::python::numeric::array& value)  
 {  
   //  
   // Construct DataArray from boost::python::object input value  
   DataArray valueDataArray(value);  
   
   //  
   // Call DataAbstract::setRefValue  
   m_data->setRefValue(ref,valueDataArray);  
 }  
   
 /* TODO */  
 /* global reduction */  
 void  
 Data::getRefValue(int ref,  
                   boost::python::numeric::array& value)  
 {  
   //  
   // Construct DataArray for boost::python::object return value  
   DataArray valueDataArray(value);  
   
   //  
   // Load DataArray with values from data-points specified by ref  
   m_data->getRefValue(ref,valueDataArray);  
   
   //  
   // Load values from valueDataArray into return numarray  
   
   // extract the shape of the numarray  
   int rank = value.getrank();  
   DataArrayView::ShapeType shape;  
   for (int i=0; i < rank; i++) {  
     shape.push_back(extract<int>(value.getshape()[i]));  
   }  
   
   // and load the numarray with the data from the DataArray  
   DataArrayView valueView = valueDataArray.getView();  
   
   if (rank==0) {  
       boost::python::numeric::array temp_numArray(valueView());  
       value = temp_numArray;  
   }  
   if (rank==1) {  
     for (int i=0; i < shape[0]; i++) {  
       value[i] = valueView(i);  
     }  
   }  
   if (rank==2) {  
     for (int i=0; i < shape[0]; i++) {  
       for (int j=0; j < shape[1]; j++) {  
         value[i][j] = valueView(i,j);  
       }  
     }  
   }  
   if (rank==3) {  
     for (int i=0; i < shape[0]; i++) {  
       for (int j=0; j < shape[1]; j++) {  
         for (int k=0; k < shape[2]; k++) {  
           value[i][j][k] = valueView(i,j,k);  
         }  
       }  
     }  
   }  
   if (rank==4) {  
     for (int i=0; i < shape[0]; i++) {  
       for (int j=0; j < shape[1]; j++) {  
         for (int k=0; k < shape[2]; k++) {  
           for (int l=0; l < shape[3]; l++) {  
             value[i][j][k][l] = valueView(i,j,k,l);  
           }  
         }  
       }  
     }  
   }  
   
 }  
   
1830  void  void
1831  Data::archiveData(const std::string fileName)  Data::archiveData(const std::string fileName)
1832  {  {
# Line 2106  Data::archiveData(const std::string file Line 1868  Data::archiveData(const std::string file
1868    DataArrayView::ShapeType dataPointShape = getDataPointShape();    DataArrayView::ShapeType dataPointShape = getDataPointShape();
1869    vector<int> referenceNumbers(noSamples);    vector<int> referenceNumbers(noSamples);
1870    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
1871      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceNoFromSampleNo(sampleNo);      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceIDOfSample(sampleNo);
1872    }    }
1873    vector<int> tagNumbers(noSamples);    vector<int> tagNumbers(noSamples);
1874    if (isTagged()) {    if (isTagged()) {
# Line 2315  Data::extractData(const std::string file Line 2077  Data::extractData(const std::string file
2077      throw DataException("extractData Error: incompatible FunctionSpace");      throw DataException("extractData Error: incompatible FunctionSpace");
2078    }    }
2079    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2080      if (referenceNumbers[sampleNo] != fspace.getReferenceNoFromSampleNo(sampleNo)) {      if (referenceNumbers[sampleNo] != fspace.getReferenceIDOfSample(sampleNo)) {
2081        throw DataException("extractData Error: incompatible FunctionSpace");        throw DataException("extractData Error: incompatible FunctionSpace");
2082      }      }
2083    }    }
# Line 2406  ostream& escript::operator<<(ostream& o, Line 2168  ostream& escript::operator<<(ostream& o,
2168    return o;    return o;
2169  }  }
2170    
2171    Data
2172    escript::C_GeneralTensorProduct(Data& arg_0,
2173                         Data& arg_1,
2174                         int axis_offset,
2175                         int transpose)
2176    {
2177      // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2178      // SM is the product of the last axis_offset entries in arg_0.getShape().
2179    
2180    
2181      // Interpolate if necessary and find an appropriate function space
2182      Data arg_0_Z, arg_1_Z;
2183      if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2184        if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2185          arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2186          arg_1_Z = Data(arg_1);
2187        }
2188        else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2189          arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2190          arg_0_Z =Data(arg_0);
2191        }
2192        else {
2193          throw DataException("Error - C_GeneralTensorProduct: arguments have incompatible function spaces.");
2194        }
2195      } else {
2196          arg_0_Z = Data(arg_0);
2197          arg_1_Z = Data(arg_1);
2198      }
2199      // Get rank and shape of inputs
2200      int rank0 = arg_0_Z.getDataPointRank();
2201      int rank1 = arg_1_Z.getDataPointRank();
2202      DataArrayView::ShapeType shape0 = arg_0_Z.getDataPointShape();
2203      DataArrayView::ShapeType shape1 = arg_1_Z.getDataPointShape();
2204    
2205      // Prepare for the loops of the product and verify compatibility of shapes
2206      int start0=0, start1=0;
2207      if (transpose == 0)       {}
2208      else if (transpose == 1)  { start0 = axis_offset; }
2209      else if (transpose == 2)  { start1 = rank1-axis_offset; }
2210      else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2211    
2212      // Adjust the shapes for transpose
2213      DataArrayView::ShapeType tmpShape0;
2214      DataArrayView::ShapeType tmpShape1;
2215      for (int i=0; i<rank0; i++)   { tmpShape0.push_back( shape0[(i+start0)%rank0] ); }
2216      for (int i=0; i<rank1; i++)   { tmpShape1.push_back( shape1[(i+start1)%rank1] ); }
2217    
2218    #if 0
2219      // For debugging: show shape after transpose
2220      char tmp[100];
2221      std::string shapeStr;
2222      shapeStr = "(";
2223      for (int i=0; i<rank0; i++)   { sprintf(tmp, "%d,", tmpShape0[i]); shapeStr += tmp; }
2224      shapeStr += ")";
2225      cout << "C_GeneralTensorProduct: Shape of arg0 is " << shapeStr << endl;
2226      shapeStr = "(";
2227      for (int i=0; i<rank1; i++)   { sprintf(tmp, "%d,", tmpShape1[i]); shapeStr += tmp; }
2228      shapeStr += ")";
2229      cout << "C_GeneralTensorProduct: Shape of arg1 is " << shapeStr << endl;
2230    #endif
2231    
2232      // Prepare for the loops of the product
2233      int SL=1, SM=1, SR=1;
2234      for (int i=0; i<rank0-axis_offset; i++)   {
2235        SL *= tmpShape0[i];
2236      }
2237      for (int i=rank0-axis_offset; i<rank0; i++)   {
2238        if (tmpShape0[i] != tmpShape1[i-(rank0-axis_offset)]) {
2239          throw DataException("C_GeneralTensorProduct: Error - incompatible shapes");
2240        }
2241        SM *= tmpShape0[i];
2242      }
2243      for (int i=axis_offset; i<rank1; i++)     {
2244        SR *= tmpShape1[i];
2245      }
2246    
2247      // Define the shape of the output
2248      DataArrayView::ShapeType shape2;
2249      for (int i=0; i<rank0-axis_offset; i++) { shape2.push_back(tmpShape0[i]); } // First part of arg_0_Z
2250      for (int i=axis_offset; i<rank1; i++)   { shape2.push_back(tmpShape1[i]); } // Last part of arg_1_Z
2251    
2252      // Declare output Data object
2253      Data res;
2254    
2255      if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2256        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2257        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[0]);
2258        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[0]);
2259        double *ptr_2 = &((res.getPointDataView().getData())[0]);
2260        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2261      }
2262      else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2263    
2264        // Prepare the DataConstant input
2265        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2266        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2267    
2268        // Borrow DataTagged input from Data object
2269        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2270        if (tmp_1==0) { throw DataException("GTP_1 Programming error - casting to DataTagged."); }
2271    
2272        // Prepare a DataTagged output 2
2273        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataTagged output
2274        res.tag();
2275        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2276        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2277    
2278        // Prepare offset into DataConstant
2279        int offset_0 = tmp_0->getPointOffset(0,0);
2280        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2281        // Get the views
2282        DataArrayView view_1 = tmp_1->getDefaultValue();
2283        DataArrayView view_2 = tmp_2->getDefaultValue();
2284        // Get the pointers to the actual data
2285        double *ptr_1 = &((view_1.getData())[0]);
2286        double *ptr_2 = &((view_2.getData())[0]);
2287        // Compute an MVP for the default
2288        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2289        // Compute an MVP for each tag
2290        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2291        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2292        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2293          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2294          DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2295          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2296          double *ptr_1 = &view_1.getData(0);
2297          double *ptr_2 = &view_2.getData(0);
2298          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2299        }
2300    
2301      }
2302      else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2303    
2304        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2305        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2306        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2307        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2308        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2309        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2310        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2311        int sampleNo_1,dataPointNo_1;
2312        int numSamples_1 = arg_1_Z.getNumSamples();
2313        int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2314        int offset_0 = tmp_0->getPointOffset(0,0);
2315        #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2316        for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2317          for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2318            int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2319            int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2320            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2321            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2322            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2323            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2324          }
2325        }
2326    
2327      }
2328      else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2329    
2330        // Borrow DataTagged input from Data object
2331        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2332        if (tmp_0==0) { throw DataException("GTP_0 Programming error - casting to DataTagged."); }
2333    
2334        // Prepare the DataConstant input
2335        DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2336        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2337    
2338        // Prepare a DataTagged output 2
2339        res = Data(0.0, shape2, arg_0_Z.getFunctionSpace());    // DataTagged output
2340        res.tag();
2341        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2342        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2343    
2344        // Prepare offset into DataConstant
2345        int offset_1 = tmp_1->getPointOffset(0,0);
2346        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2347        // Get the views
2348        DataArrayView view_0 = tmp_0->getDefaultValue();
2349        DataArrayView view_2 = tmp_2->getDefaultValue();
2350        // Get the pointers to the actual data
2351        double *ptr_0 = &((view_0.getData())[0]);
2352        double *ptr_2 = &((view_2.getData())[0]);
2353        // Compute an MVP for the default
2354        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2355        // Compute an MVP for each tag
2356        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2357        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2358        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2359          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2360          DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2361          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2362          double *ptr_0 = &view_0.getData(0);
2363          double *ptr_2 = &view_2.getData(0);
2364          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2365        }
2366    
2367      }
2368      else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2369    
2370        // Borrow DataTagged input from Data object
2371        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2372        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2373    
2374        // Borrow DataTagged input from Data object
2375        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2376        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2377    
2378        // Prepare a DataTagged output 2
2379        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());
2380        res.tag();  // DataTagged output
2381        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2382        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2383    
2384        // Get the views
2385        DataArrayView view_0 = tmp_0->getDefaultValue();
2386        DataArrayView view_1 = tmp_1->getDefaultValue();
2387        DataArrayView view_2 = tmp_2->getDefaultValue();
2388        // Get the pointers to the actual data
2389        double *ptr_0 = &((view_0.getData())[0]);
2390        double *ptr_1 = &((view_1.getData())[0]);
2391        double *ptr_2 = &((view_2.getData())[0]);
2392        // Compute an MVP for the default
2393        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2394        // Merge the tags
2395        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2396        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2397        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2398        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2399          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue()); // use tmp_2 to get correct shape
2400        }
2401        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2402          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2403        }
2404        // Compute an MVP for each tag
2405        const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2406        for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2407          DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2408          DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2409          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2410          double *ptr_0 = &view_0.getData(0);
2411          double *ptr_1 = &view_1.getData(0);
2412          double *ptr_2 = &view_2.getData(0);
2413          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2414        }
2415    
2416      }
2417      else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2418    
2419        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2420        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2421        DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2422        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2423        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2424        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2425        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2426        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2427        int sampleNo_0,dataPointNo_0;
2428        int numSamples_0 = arg_0_Z.getNumSamples();
2429        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2430        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2431        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2432          int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2433          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2434          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2435            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2436            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2437            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2438            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2439            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2440          }
2441        }
2442    
2443      }
2444      else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2445    
2446        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2447        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2448        DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2449        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2450        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2451        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2452        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2453        int sampleNo_0,dataPointNo_0;
2454        int numSamples_0 = arg_0_Z.getNumSamples();
2455        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2456        int offset_1 = tmp_1->getPointOffset(0,0);
2457        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2458        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2459          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2460            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2461            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2462            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2463            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2464            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2465            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2466          }
2467        }
2468    
2469    
2470      }
2471      else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2472    
2473        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2474        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2475        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2476        DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2477        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2478        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2479        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2480        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2481        int sampleNo_0,dataPointNo_0;
2482        int numSamples_0 = arg_0_Z.getNumSamples();
2483        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2484        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2485        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2486          int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2487          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2488          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2489            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2490            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2491            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2492            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2493            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2494          }
2495        }
2496    
2497      }
2498      else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2499    
2500        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2501        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2502        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2503        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2504        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2505        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2506        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2507        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2508        int sampleNo_0,dataPointNo_0;
2509        int numSamples_0 = arg_0_Z.getNumSamples();
2510        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2511        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2512        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2513          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2514            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2515            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2516            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2517            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2518            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2519            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2520            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2521          }
2522        }
2523    
2524      }
2525      else {
2526        throw DataException("Error - C_GeneralTensorProduct: unknown combination of inputs");
2527      }
2528    
2529      return res;
2530    }
2531    
2532    DataAbstract*
2533    Data::borrowData() const
2534    {
2535      return m_data.get();
2536    }
2537    
2538  /* Member functions specific to the MPI implementation */  /* Member functions specific to the MPI implementation */
 #ifdef PASO_MPI  
2539    
2540  void  void
2541  Data::print()  Data::print()
# Line 2428  int Line 2556  int
2556  Data::get_MPISize() const  Data::get_MPISize() const
2557  {  {
2558      int error, size;      int error, size;
2559    #ifdef PASO_MPI
2560      error = MPI_Comm_size( get_MPIComm(), &size );      error = MPI_Comm_size( get_MPIComm(), &size );
2561    #else
2562        size = 1;
2563    #endif
2564      return size;      return size;
2565  }  }
2566    
# Line 2437  int Line 2568  int
2568  Data::get_MPIRank() const  Data::get_MPIRank() const
2569  {  {
2570      int error, rank;      int error, rank;
2571    #ifdef PASO_MPI
2572      error = MPI_Comm_rank( get_MPIComm(), &rank );      error = MPI_Comm_rank( get_MPIComm(), &rank );
2573    #else
2574        rank = 0;
2575    #endif
2576      return rank;      return rank;
2577  }  }
2578    
2579  MPI_Comm  MPI_Comm
2580  Data::get_MPIComm() const  Data::get_MPIComm() const
2581  {  {
2582    #ifdef PASO_MPI
2583      return MPI_COMM_WORLD;      return MPI_COMM_WORLD;
2584  }  #else
2585        return -1;
2586  #endif  #endif
2587    }
2588    

Legend:
Removed from v.782  
changed lines
  Added in v.1118

  ViewVC Help
Powered by ViewVC 1.1.26