/[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 790 by bcumming, Wed Jul 26 23:12:34 2006 UTC revision 1141 by gross, Tue May 15 04:24:51 2007 UTC
# Line 19  Line 19 
19  #include "DataEmpty.h"  #include "DataEmpty.h"
20  #include "DataArray.h"  #include "DataArray.h"
21  #include "DataArrayView.h"  #include "DataArrayView.h"
 #include "DataProf.h"  
22  #include "FunctionSpaceFactory.h"  #include "FunctionSpaceFactory.h"
23  #include "AbstractContinuousDomain.h"  #include "AbstractContinuousDomain.h"
24  #include "UnaryFuncs.h"  #include "UnaryFuncs.h"
# Line 38  using namespace boost::python; Line 37  using namespace boost::python;
37  using namespace boost;  using namespace boost;
38  using namespace escript;  using namespace escript;
39    
 #if defined DOPROF  
 //  
 // global table of profiling data for all Data objects  
 DataProf dataProfTable;  
 #endif  
   
40  Data::Data()  Data::Data()
41  {  {
42    //    //
# Line 52  Data::Data() Line 45  Data::Data()
45    shared_ptr<DataAbstract> temp_data(temp);    shared_ptr<DataAbstract> temp_data(temp);
46    m_data=temp_data;    m_data=temp_data;
47    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
48  }  }
49    
50  Data::Data(double value,  Data::Data(double value,
# Line 70  Data::Data(double value, Line 59  Data::Data(double value,
59    DataArray temp(dataPointShape,value);    DataArray temp(dataPointShape,value);
60    initialise(temp.getView(),what,expanded);    initialise(temp.getView(),what,expanded);
61    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
62  }  }
63    
64  Data::Data(double value,  Data::Data(double value,
# Line 85  Data::Data(double value, Line 70  Data::Data(double value,
70    pair<int,int> dataShape=what.getDataShape();    pair<int,int> dataShape=what.getDataShape();
71    initialise(temp.getView(),what,expanded);    initialise(temp.getView(),what,expanded);
72    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
73  }  }
74    
75  Data::Data(const Data& inData)  Data::Data(const Data& inData)
76  {  {
77    m_data=inData.m_data;    m_data=inData.m_data;
78    m_protected=inData.isProtected();    m_protected=inData.isProtected();
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
79  }  }
80    
81  Data::Data(const Data& inData,  Data::Data(const Data& inData,
# Line 110  Data::Data(const Data& inData, Line 87  Data::Data(const Data& inData,
87    shared_ptr<DataAbstract> temp_data(tmp);    shared_ptr<DataAbstract> temp_data(tmp);
88    m_data=temp_data;    m_data=temp_data;
89    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
90  }  }
91    
92  Data::Data(const Data& inData,  Data::Data(const Data& inData,
93             const FunctionSpace& functionspace)             const FunctionSpace& functionspace)
94  {  {
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
95    if (inData.getFunctionSpace()==functionspace) {    if (inData.getFunctionSpace()==functionspace) {
96      m_data=inData.m_data;      m_data=inData.m_data;
97    } else {    } else {
     #if defined DOPROF  
     profData->interpolate++;  
     #endif  
98      Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);      Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);
99      // Note: Must use a reference or pointer to a derived object      // Note: Must use a reference or pointer to a derived object
100      // in order to get polymorphic behaviour. Shouldn't really      // in order to get polymorphic behaviour. Shouldn't really
# Line 158  Data::Data(const DataTagged::TagListType Line 124  Data::Data(const DataTagged::TagListType
124    if (expanded) {    if (expanded) {
125      expand();      expand();
126    }    }
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
127  }  }
128    
129  Data::Data(const numeric::array& value,  Data::Data(const numeric::array& value,
# Line 170  Data::Data(const numeric::array& value, Line 132  Data::Data(const numeric::array& value,
132  {  {
133    initialise(value,what,expanded);    initialise(value,what,expanded);
134    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
135  }  }
136    
137  Data::Data(const DataArrayView& value,  Data::Data(const DataArrayView& value,
# Line 182  Data::Data(const DataArrayView& value, Line 140  Data::Data(const DataArrayView& value,
140  {  {
141    initialise(value,what,expanded);    initialise(value,what,expanded);
142    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
143  }  }
144    
145  Data::Data(const object& value,  Data::Data(const object& value,
# Line 195  Data::Data(const object& value, Line 149  Data::Data(const object& value,
149    numeric::array asNumArray(value);    numeric::array asNumArray(value);
150    initialise(asNumArray,what,expanded);    initialise(asNumArray,what,expanded);
151    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
152  }  }
153    
154  Data::Data(const object& value,  Data::Data(const object& value,
# Line 220  Data::Data(const object& value, Line 170  Data::Data(const object& value,
170      initialise(temp.getView(),other.getFunctionSpace(),false);      initialise(temp.getView(),other.getFunctionSpace(),false);
171    }    }
172    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
173  }  }
174    
175  Data::~Data()  Data::~Data()
# Line 267  Data::getShapeTuple() const Line 213  Data::getShapeTuple() const
213    }    }
214  }  }
215    
216    const
217    boost::python::str
218    Data::str() const
219    {
220      return boost::python::str(toString().c_str());
221    }
222    
223  void  void
224  Data::copy(const Data& other)  Data::copy(const Data& other)
225  {  {
# Line 319  Data::copy(const Data& other) Line 272  Data::copy(const Data& other)
272    throw DataException("Error - Copy not implemented for this Data type.");    throw DataException("Error - Copy not implemented for this Data type.");
273  }  }
274    
275    
276    void
277    Data::setToZero()
278    {
279      {
280        DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
281        if (temp!=0) {
282           temp->setToZero();
283           return;
284        }
285      }
286      {
287        DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());
288        if (temp!=0) {
289          temp->setToZero();
290          return;
291        }
292      }
293      {
294        DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());
295        if (temp!=0) {
296          temp->setToZero();
297          return;
298        }
299      }
300      throw DataException("Error - Data can not be set to zero.");
301    }
302    
303  void  void
304  Data::copyWithMask(const Data& other,  Data::copyWithMask(const Data& other,
305                     const Data& mask)                     const Data& mask)
# Line 350  Data::isTagged() const Line 331  Data::isTagged() const
331    return (temp!=0);    return (temp!=0);
332  }  }
333    
 /* TODO */  
 /* global reduction -- the local data being empty does not imply that it is empty on other processers*/  
334  bool  bool
335  Data::isEmpty() const  Data::isEmpty() const
336  {  {
# Line 422  Data::tag() Line 401  Data::tag()
401    }    }
402  }  }
403    
404  void  Data
405  Data::reshapeDataPoint(const DataArrayView::ShapeType& shape)  Data::oneOver() const
406  {  {
407    m_data->reshapeDataPoint(shape);    return escript::unaryOp(*this,bind1st(divides<double>(),1.));
408  }  }
409    
410  Data  Data
411  Data::wherePositive() const  Data::wherePositive() const
412  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
413    return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));    return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));
414  }  }
415    
416  Data  Data
417  Data::whereNegative() const  Data::whereNegative() const
418  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
419    return escript::unaryOp(*this,bind2nd(less<double>(),0.0));    return escript::unaryOp(*this,bind2nd(less<double>(),0.0));
420  }  }
421    
422  Data  Data
423  Data::whereNonNegative() const  Data::whereNonNegative() const
424  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
425    return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));    return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));
426  }  }
427    
428  Data  Data
429  Data::whereNonPositive() const  Data::whereNonPositive() const
430  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
431    return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));    return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));
432  }  }
433    
434  Data  Data
435  Data::whereZero(double tol) const  Data::whereZero(double tol) const
436  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
437    Data dataAbs=abs();    Data dataAbs=abs();
438    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));
439  }  }
# Line 477  Data::whereZero(double tol) const Line 441  Data::whereZero(double tol) const
441  Data  Data
442  Data::whereNonZero(double tol) const  Data::whereNonZero(double tol) const
443  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
444    Data dataAbs=abs();    Data dataAbs=abs();
445    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));
446  }  }
# Line 487  Data::whereNonZero(double tol) const Line 448  Data::whereNonZero(double tol) const
448  Data  Data
449  Data::interpolate(const FunctionSpace& functionspace) const  Data::interpolate(const FunctionSpace& functionspace) const
450  {  {
 #if defined DOPROF  
   profData->interpolate++;  
 #endif  
451    return Data(*this,functionspace);    return Data(*this,functionspace);
452  }  }
453    
# Line 511  Data::probeInterpolation(const FunctionS Line 469  Data::probeInterpolation(const FunctionS
469  Data  Data
470  Data::gradOn(const FunctionSpace& functionspace) const  Data::gradOn(const FunctionSpace& functionspace) const
471  {  {
 #if defined DOPROF  
   profData->grad++;  
 #endif  
472    if (functionspace.getDomain()!=getDomain())    if (functionspace.getDomain()!=getDomain())
473      throw DataException("Error - gradient cannot be calculated on different domains.");      throw DataException("Error - gradient cannot be calculated on different domains.");
474    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();
# Line 547  Data::getDataPointShape() const Line 502  Data::getDataPointShape() const
502    return getPointDataView().getShape();    return getPointDataView().getShape();
503  }  }
504    
 void  
 Data::fillFromNumArray(const boost::python::numeric::array num_array)  
 {  
   if (isProtected()) {  
         throw DataException("Error - attempt to update protected Data object.");  
   }  
   //  
   // check rank  
   if (num_array.getrank()<getDataPointRank())  
       throw DataException("Rank of numarray does not match Data object rank");  
   
   //  
   // 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");  
   }  
505    
   //  
   // make sure data is expanded:  
   if (!isExpanded()) {  
     expand();  
   }  
506    
507    //  const
   // and copy over  
   m_data->copyAll(num_array);  
 }  
   
 const  
508  boost::python::numeric::array  boost::python::numeric::array
509  Data::convertToNumArray()  Data:: getValueOfDataPoint(int dataPointNo)
510  {  {
511    //    size_t length=0;
512    // determine the total number of data points    int i, j, k, l;
   int numSamples = getNumSamples();  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
   int numDataPoints = numSamples * numDataPointsPerSample;  
   
513    //    //
514    // determine the rank and shape of each data point    // determine the rank and shape of each data point
515    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
# Line 596  Data::convertToNumArray() Line 520  Data::convertToNumArray()
520    boost::python::numeric::array numArray(0.0);    boost::python::numeric::array numArray(0.0);
521    
522    //    //
523    // the rank of the returned numeric array will be the rank of    // the shape of the returned numeric array will be the same
524    // the data points, plus one. Where the rank of the array is n,    // as that of the data point
525    // the last n-1 dimensions will be equal to the shape of the    int arrayRank = dataPointRank;
526    // 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]);  
   }  
527    
528    //    //
529    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
530      if (arrayRank==0) {
531        numArray.resize(1);
532      }
533    if (arrayRank==1) {    if (arrayRank==1) {
534      numArray.resize(arrayShape[0]);      numArray.resize(arrayShape[0]);
535    }    }
# Line 623  Data::convertToNumArray() Line 542  Data::convertToNumArray()
542    if (arrayRank==4) {    if (arrayRank==4) {
543      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
544    }    }
   if (arrayRank==5) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);  
   }  
545    
546    //    if (getNumDataPointsPerSample()>0) {
547    // loop through each data point in turn, loading the values for that data point         int sampleNo = dataPointNo/getNumDataPointsPerSample();
548    // into the numeric array.         int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
549    int dataPoint = 0;         //
550    for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {         // Check a valid sample number has been supplied
551      for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
552        DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
553        if (dataPointRank==0) {         }
554          numArray[dataPoint]=dataPointView();                
555        }         //
556        if (dataPointRank==1) {         // Check a valid data point number has been supplied
557          for (int i=0; i<dataPointShape[0]; i++) {         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
558            numArray[dataPoint][i]=dataPointView(i);             throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
559          }         }
560        }         // TODO: global error handling
561        if (dataPointRank==2) {         // create a view of the data if it is stored locally
562          for (int i=0; i<dataPointShape[0]; i++) {         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
563            for (int j=0; j<dataPointShape[1]; j++) {          
564              numArray[dataPoint][i][j] = dataPointView(i,j);         switch( dataPointRank ){
565            }              case 0 :
566          }                  numArray[0] = dataPointView();
567        }                  break;
568        if (dataPointRank==3) {              case 1 :        
569          for (int i=0; i<dataPointShape[0]; i++) {                  for( i=0; i<dataPointShape[0]; i++ )
570            for (int j=0; j<dataPointShape[1]; j++) {                      numArray[i]=dataPointView(i);
571              for (int k=0; k<dataPointShape[2]; k++) {                  break;
572                numArray[dataPoint][i][j][k]=dataPointView(i,j,k);              case 2 :        
573              }                  for( i=0; i<dataPointShape[0]; i++ )
574            }                      for( j=0; j<dataPointShape[1]; j++)
575          }                          numArray[make_tuple(i,j)]=dataPointView(i,j);
576        }                  break;
577        if (dataPointRank==4) {              case 3 :        
578          for (int i=0; i<dataPointShape[0]; i++) {                  for( i=0; i<dataPointShape[0]; i++ )
579            for (int j=0; j<dataPointShape[1]; j++) {                      for( j=0; j<dataPointShape[1]; j++ )
580              for (int k=0; k<dataPointShape[2]; k++) {                          for( k=0; k<dataPointShape[2]; k++)
581                for (int l=0; l<dataPointShape[3]; l++) {                              numArray[make_tuple(i,j,k)]=dataPointView(i,j,k);
582                  numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);                  break;
583                }              case 4 :
584              }                  for( i=0; i<dataPointShape[0]; i++ )
585            }                      for( j=0; j<dataPointShape[1]; j++ )
586          }                          for( k=0; k<dataPointShape[2]; k++ )
587        }                              for( l=0; l<dataPointShape[3]; l++)
588        dataPoint++;                                  numArray[make_tuple(i,j,k,l)]=dataPointView(i,j,k,l);
589      }                  break;
590        }
591    }    }
   
592    //    //
593    // return the loaded array    // return the array
594    return numArray;    return numArray;
 }  
595    
596  const  }
597  boost::python::numeric::array  void
598  Data::convertToNumArrayFromSampleNo(int sampleNo)  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
599  {  {
600    //      // this will throw if the value cannot be represented
601    // Check a valid sample number has been supplied      boost::python::numeric::array num_array(py_object);
602    if (sampleNo >= getNumSamples()) {      setValueOfDataPointToArray(dataPointNo,num_array);
     throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  
   }  
603    
   //  
   // determine the number of data points per sample  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
604    
605    //  }
   // determine the rank and shape of each data point  
   int dataPointRank = getDataPointRank();  
   DataArrayView::ShapeType dataPointShape = getDataPointShape();  
606    
607    void
608    Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array& num_array)
609    {
610      if (isProtected()) {
611            throw DataException("Error - attempt to update protected Data object.");
612      }
613    //    //
614    // create the numeric array to be returned    // check rank
615    boost::python::numeric::array numArray(0.0);    if (num_array.getrank()<getDataPointRank())
616          throw DataException("Rank of numarray does not match Data object rank");
617    
618    //    //
619    // the rank of the returned numeric array will be the rank of    // check shape of num_array
620    // the data points, plus one. Where the rank of the array is n,    for (int i=0; i<getDataPointRank(); i++) {
621    // the last n-1 dimensions will be equal to the shape of the      if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])
622    // 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]);  
623    }    }
   
624    //    //
625    // resize the numeric array to the shape just calculated    // make sure data is expanded:
626    if (arrayRank==1) {    if (!isExpanded()) {
627      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]);  
628    }    }
629      if (getNumDataPointsPerSample()>0) {
630    //         int sampleNo = dataPointNo/getNumDataPointsPerSample();
631    // loop through each data point in turn, loading the values for that data point         int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
632    // into the numeric array.         m_data->copyToDataPoint(sampleNo, dataPointNoInSample,num_array);
633    for (int dataPoint = 0; dataPoint < numDataPointsPerSample; dataPoint++) {    } else {
634      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);  
             }  
           }  
         }  
       }  
     }  
635    }    }
   
   //  
   // return the loaded array  
   return numArray;  
636  }  }
637    
638  const  void
639  boost::python::numeric::array  Data::setValueOfDataPoint(int dataPointNo, const double value)
 Data::convertToNumArrayFromDPNo(int procNo,  
                                 int sampleNo,  
                                                                 int dataPointNo)  
   
640  {  {
641      size_t length=0;    if (isProtected()) {
642      int i, j, k, l, pos;          throw DataException("Error - attempt to update protected Data object.");
   
   //  
   // Check a valid sample number has been supplied  
   if (sampleNo >= getNumSamples()) {  
     throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  
643    }    }
   
644    //    //
645    // Check a valid data point number has been supplied    // make sure data is expanded:
646    if (dataPointNo >= getNumDataPointsPerSample()) {    if (!isExpanded()) {
647      throw DataException("Error - Data::convertToNumArray: invalid dataPointNo.");      expand();
648      }
649      if (getNumDataPointsPerSample()>0) {
650           int sampleNo = dataPointNo/getNumDataPointsPerSample();
651           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
652           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,value);
653      } else {
654           m_data->copyToDataPoint(-1, 0,value);
655    }    }
656    }
657    
658    const
659    boost::python::numeric::array
660    Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
661    {
662      size_t length=0;
663      int i, j, k, l, pos;
664    //    //
665    // determine the rank and shape of each data point    // determine the rank and shape of each data point
666    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
# Line 835  Data::convertToNumArrayFromDPNo(int proc Line 694  Data::convertToNumArrayFromDPNo(int proc
694      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
695    }    }
696    
697      // added for the MPI communication    // added for the MPI communication
698      length=1;    length=1;
699      for( i=0; i<arrayRank; i++ )    for( i=0; i<arrayRank; i++ ) length *= arrayShape[i];
700          length *= arrayShape[i];    double *tmpData = new double[length];
     double *tmpData = new double[length];  
701    
702    //    //
703    // load the values for the data point into the numeric array.    // load the values for the data point into the numeric array.
704    
705      // updated for the MPI case      // updated for the MPI case
706      if( get_MPIRank()==procNo ){      if( get_MPIRank()==procNo ){
707                 if (getNumDataPointsPerSample()>0) {
708                    int sampleNo = dataPointNo/getNumDataPointsPerSample();
709                    int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
710                    //
711                    // Check a valid sample number has been supplied
712                    if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
713                      throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
714                    }
715                  
716                    //
717                    // Check a valid data point number has been supplied
718                    if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
719                      throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
720                    }
721                    // TODO: global error handling
722          // create a view of the data if it is stored locally          // create a view of the data if it is stored locally
723          DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);          DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
724                    
725          // pack the data from the view into tmpData for MPI communication          // pack the data from the view into tmpData for MPI communication
726          pos=0;          pos=0;
# Line 878  Data::convertToNumArrayFromDPNo(int proc Line 751  Data::convertToNumArrayFromDPNo(int proc
751                                  tmpData[pos]=dataPointView(i,j,k,l);                                  tmpData[pos]=dataPointView(i,j,k,l);
752                  break;                  break;
753          }          }
754                }
755      }      }
756  #ifdef PASO_MPI          #ifdef PASO_MPI
757          // broadcast the data to all other processes          // broadcast the data to all other processes
758          MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );      MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
759  #endif          #endif
760    
761      // unpack the data      // unpack the data
762      switch( dataPointRank ){      switch( dataPointRank ){
763          case 0 :          case 0 :
764              numArray[i]=tmpData[0];              numArray[0]=tmpData[0];
765              break;              break;
766          case 1 :                  case 1 :        
767              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
# Line 896  Data::convertToNumArrayFromDPNo(int proc Line 770  Data::convertToNumArrayFromDPNo(int proc
770          case 2 :                  case 2 :        
771              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
772                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
773                      tmpData[i+j*dataPointShape[0]];                     numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
774              break;              break;
775          case 3 :                  case 3 :        
776              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
777                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
778                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
779                          tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];                          numArray[make_tuple(i,j,k)]=tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];
780              break;              break;
781          case 4 :          case 4 :
782              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
783                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
784                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
785                          for( l=0; l<dataPointShape[3]; l++ )                          for( l=0; l<dataPointShape[3]; l++ )
786                              tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];                                  numArray[make_tuple(i,j,k,l)]=tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];
787              break;              break;
788      }      }
789    
790      delete [] tmpData;        delete [] tmpData;  
 /*  
   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);  
           }  
         }  
       }  
     }  
   }  
 */  
   
791    //    //
792    // return the loaded array    // return the loaded array
793    return numArray;    return numArray;
794  }  }
795    
796    
797    
798  boost::python::numeric::array  boost::python::numeric::array
799  Data::integrate() const  Data::integrate() const
800  {  {
# Line 964  Data::integrate() const Line 802  Data::integrate() const
802    int rank = getDataPointRank();    int rank = getDataPointRank();
803    DataArrayView::ShapeType shape = getDataPointShape();    DataArrayView::ShapeType shape = getDataPointShape();
804    
 #if defined DOPROF  
   profData->integrate++;  
 #endif  
805    
806    //    //
807    // calculate the integral values    // calculate the integral values
# Line 1031  Data::integrate() const Line 866  Data::integrate() const
866  Data  Data
867  Data::sin() const  Data::sin() const
868  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
869    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);
870  }  }
871    
872  Data  Data
873  Data::cos() const  Data::cos() const
874  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
875    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);
876  }  }
877    
878  Data  Data
879  Data::tan() const  Data::tan() const
880  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
881    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);
882  }  }
883    
884  Data  Data
885  Data::asin() const  Data::asin() const
886  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
887    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);
888  }  }
889    
890  Data  Data
891  Data::acos() const  Data::acos() const
892  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
893    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);
894  }  }
895    
896    
897  Data  Data
898  Data::atan() const  Data::atan() const
899  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
900    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);
901  }  }
902    
903  Data  Data
904  Data::sinh() const  Data::sinh() const
905  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
906    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);
907  }  }
908    
909  Data  Data
910  Data::cosh() const  Data::cosh() const
911  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
912    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);
913  }  }
914    
915  Data  Data
916  Data::tanh() const  Data::tanh() const
917  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
918    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);
919  }  }
920    
921    
922  Data  Data
923  Data::asinh() const  Data::erf() const
924  {  {
925  #if defined DOPROF  #ifdef _WIN32
926    profData->unary++;    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
927    #else
928      return escript::unaryOp(*this,(Data::UnaryDFunPtr)::erf);
929  #endif  #endif
930    }
931    
932    Data
933    Data::asinh() const
934    {
935    #ifdef _WIN32
936      return escript::unaryOp(*this,escript::asinh_substitute);
937    #else
938    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);
939    #endif
940  }  }
941    
942  Data  Data
943  Data::acosh() const  Data::acosh() const
944  {  {
945  #if defined DOPROF  #ifdef _WIN32
946    profData->unary++;    return escript::unaryOp(*this,escript::acosh_substitute);
947  #endif  #else
948    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);
949    #endif
950  }  }
951    
952  Data  Data
953  Data::atanh() const  Data::atanh() const
954  {  {
955  #if defined DOPROF  #ifdef _WIN32
956    profData->unary++;    return escript::unaryOp(*this,escript::atanh_substitute);
957  #endif  #else
958    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);
959    #endif
960  }  }
961    
962  Data  Data
963  Data::log10() const  Data::log10() const
964  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
965    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);
966  }  }
967    
968  Data  Data
969  Data::log() const  Data::log() const
970  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
971    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);
972  }  }
973    
974  Data  Data
975  Data::sign() const  Data::sign() const
976  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
977    return escript::unaryOp(*this,escript::fsign);    return escript::unaryOp(*this,escript::fsign);
978  }  }
979    
980  Data  Data
981  Data::abs() const  Data::abs() const
982  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
983    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);
984  }  }
985    
986  Data  Data
987  Data::neg() const  Data::neg() const
988  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
989    return escript::unaryOp(*this,negate<double>());    return escript::unaryOp(*this,negate<double>());
990  }  }
991    
992  Data  Data
993  Data::pos() const  Data::pos() const
994  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
995    Data result;    Data result;
996    // perform a deep copy    // perform a deep copy
997    result.copy(*this);    result.copy(*this);
# Line 1196  Data::pos() const Line 1001  Data::pos() const
1001  Data  Data
1002  Data::exp() const  Data::exp() const
1003  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
1004    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);
1005  }  }
1006    
1007  Data  Data
1008  Data::sqrt() const  Data::sqrt() const
1009  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
1010    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);
1011  }  }
1012    
# Line 1215  double Line 1014  double
1014  Data::Lsup() const  Data::Lsup() const
1015  {  {
1016    double localValue, globalValue;    double localValue, globalValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1017    //    //
1018    // set the initial absolute maximum value to zero    // set the initial absolute maximum value to zero
1019    
# Line 1225  Data::Lsup() const Line 1021  Data::Lsup() const
1021    localValue = algorithm(abs_max_func,0);    localValue = algorithm(abs_max_func,0);
1022  #ifdef PASO_MPI  #ifdef PASO_MPI
1023    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1024    return globalValue;   return globalValue;
1025  #else  #else
1026    return localValue;    return localValue;
1027  #endif  #endif
# Line 1235  double Line 1031  double
1031  Data::Linf() const  Data::Linf() const
1032  {  {
1033    double localValue, globalValue;    double localValue, globalValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1034    //    //
1035    // set the initial absolute minimum value to max double    // set the initial absolute minimum value to max double
1036    AbsMin abs_min_func;    AbsMin abs_min_func;
# Line 1255  double Line 1048  double
1048  Data::sup() const  Data::sup() const
1049  {  {
1050    double localValue, globalValue;    double localValue, globalValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1051    //    //
1052    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1053    FMax fmax_func;    FMax fmax_func;
# Line 1274  double Line 1064  double
1064  Data::inf() const  Data::inf() const
1065  {  {
1066    double localValue, globalValue;    double localValue, globalValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1067    //    //
1068    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1069    FMin fmin_func;    FMin fmin_func;
# Line 1294  Data::inf() const Line 1081  Data::inf() const
1081  Data  Data
1082  Data::maxval() const  Data::maxval() const
1083  {  {
 #if defined DOPROF  
   profData->reduction2++;  
 #endif  
1084    //    //
1085    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1086    FMax fmax_func;    FMax fmax_func;
# Line 1306  Data::maxval() const Line 1090  Data::maxval() const
1090  Data  Data
1091  Data::minval() const  Data::minval() const
1092  {  {
 #if defined DOPROF  
   profData->reduction2++;  
 #endif  
1093    //    //
1094    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1095    FMin fmin_func;    FMin fmin_func;
# Line 1316  Data::minval() const Line 1097  Data::minval() const
1097  }  }
1098    
1099  Data  Data
1100  Data::trace() const  Data::swapaxes(const int axis0, const int axis1) const
1101  {  {
1102  #if defined DOPROF       int axis0_tmp,axis1_tmp;
1103    profData->reduction2++;       DataArrayView::ShapeType s=getDataPointShape();
1104  #endif       DataArrayView::ShapeType ev_shape;
1105    Trace trace_func;       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1106    return dp_algorithm(trace_func,0);       // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1107         int rank=getDataPointRank();
1108         if (rank<2) {
1109            throw DataException("Error - Data::swapaxes argument must have at least rank 2.");
1110         }
1111         if (axis0<0 || axis0>rank-1) {
1112            throw DataException("Error - Data::swapaxes: axis0 must be between 0 and rank-1=" + rank-1);
1113         }
1114         if (axis1<0 || axis1>rank-1) {
1115             throw DataException("Error - Data::swapaxes: axis1 must be between 0 and rank-1=" + rank-1);
1116         }
1117         if (axis0 == axis1) {
1118             throw DataException("Error - Data::swapaxes: axis indices must be different.");
1119         }
1120         if (axis0 > axis1) {
1121             axis0_tmp=axis1;
1122             axis1_tmp=axis0;
1123         } else {
1124             axis0_tmp=axis0;
1125             axis1_tmp=axis1;
1126         }
1127         for (int i=0; i<rank; i++) {
1128           if (i == axis0_tmp) {
1129              ev_shape.push_back(s[axis1_tmp]);
1130           } else if (i == axis1_tmp) {
1131              ev_shape.push_back(s[axis0_tmp]);
1132           } else {
1133              ev_shape.push_back(s[i]);
1134           }
1135         }
1136         Data ev(0.,ev_shape,getFunctionSpace());
1137         ev.typeMatchRight(*this);
1138         m_data->swapaxes(ev.m_data.get(), axis0_tmp, axis1_tmp);
1139         return ev;
1140    
1141  }  }
1142    
1143  Data  Data
1144  Data::symmetric() const  Data::symmetric() const
1145  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1146       // check input       // check input
1147       DataArrayView::ShapeType s=getDataPointShape();       DataArrayView::ShapeType s=getDataPointShape();
1148       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
# Line 1353  Data::symmetric() const Line 1165  Data::symmetric() const
1165  Data  Data
1166  Data::nonsymmetric() const  Data::nonsymmetric() const
1167  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1168       // check input       // check input
1169       DataArrayView::ShapeType s=getDataPointShape();       DataArrayView::ShapeType s=getDataPointShape();
1170       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
# Line 1388  Data::nonsymmetric() const Line 1197  Data::nonsymmetric() const
1197  }  }
1198    
1199  Data  Data
1200  Data::matrixtrace(int axis_offset) const  Data::trace(int axis_offset) const
1201  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1202       DataArrayView::ShapeType s=getDataPointShape();       DataArrayView::ShapeType s=getDataPointShape();
1203       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1204          DataArrayView::ShapeType ev_shape;          DataArrayView::ShapeType ev_shape;
1205          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1206          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1207          m_data->matrixtrace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1208          return ev;          return ev;
1209       }       }
1210       if (getDataPointRank()==3) {       if (getDataPointRank()==3) {
# Line 1413  Data::matrixtrace(int axis_offset) const Line 1219  Data::matrixtrace(int axis_offset) const
1219          }          }
1220          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1221          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1222          m_data->matrixtrace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1223          return ev;          return ev;
1224       }       }
1225       if (getDataPointRank()==4) {       if (getDataPointRank()==4) {
# Line 1432  Data::matrixtrace(int axis_offset) const Line 1238  Data::matrixtrace(int axis_offset) const
1238      }      }
1239          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1240          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1241      m_data->matrixtrace(ev.m_data.get(), axis_offset);      m_data->trace(ev.m_data.get(), axis_offset);
1242          return ev;          return ev;
1243       }       }
1244       else {       else {
1245          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.");
1246       }       }
1247  }  }
1248    
1249  Data  Data
1250  Data::transpose(int axis_offset) const  Data::transpose(int axis_offset) const
1251  {  {
 #if defined DOPROF  
      profData->reduction2++;  
 #endif  
1252       DataArrayView::ShapeType s=getDataPointShape();       DataArrayView::ShapeType s=getDataPointShape();
1253       DataArrayView::ShapeType ev_shape;       DataArrayView::ShapeType ev_shape;
1254       // 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 1467  Data::transpose(int axis_offset) const Line 1270  Data::transpose(int axis_offset) const
1270  Data  Data
1271  Data::eigenvalues() const  Data::eigenvalues() const
1272  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1273       // check input       // check input
1274       DataArrayView::ShapeType s=getDataPointShape();       DataArrayView::ShapeType s=getDataPointShape();
1275       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
# Line 1487  Data::eigenvalues() const Line 1287  Data::eigenvalues() const
1287  const boost::python::tuple  const boost::python::tuple
1288  Data::eigenvalues_and_eigenvectors(const double tol) const  Data::eigenvalues_and_eigenvectors(const double tol) const
1289  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1290       DataArrayView::ShapeType s=getDataPointShape();       DataArrayView::ShapeType s=getDataPointShape();
1291       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1292          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 1507  Data::eigenvalues_and_eigenvectors(const Line 1304  Data::eigenvalues_and_eigenvectors(const
1304  }  }
1305    
1306  const boost::python::tuple  const boost::python::tuple
1307  Data::mindp() const  Data::minGlobalDataPoint() const
1308  {  {
1309    // 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
1310    // 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
1311    // surrounding function    // surrounding function
1312    
   int SampleNo;  
1313    int DataPointNo;    int DataPointNo;
1314      int ProcNo;    int ProcNo;
1315    calc_mindp(ProcNo,SampleNo,DataPointNo);    calc_minGlobalDataPoint(ProcNo,DataPointNo);
1316    return make_tuple(ProcNo,SampleNo,DataPointNo);    return make_tuple(ProcNo,DataPointNo);
1317  }  }
1318    
1319  void  void
1320  Data::calc_mindp(   int& ProcNo,  Data::calc_minGlobalDataPoint(int& ProcNo,
1321                  int& SampleNo,                          int& DataPointNo) const
         int& DataPointNo) const  
1322  {  {
1323    int i,j;    int i,j;
1324    int lowi=0,lowj=0;    int lowi=0,lowj=0;
# Line 1581  Data::calc_mindp(  int& ProcNo, Line 1376  Data::calc_mindp(  int& ProcNo,
1376  #else  #else
1377      ProcNo = 0;      ProcNo = 0;
1378  #endif  #endif
1379    SampleNo = lowi;    DataPointNo = lowj + lowi * numDPPSample;
   DataPointNo = lowj;  
1380  }  }
1381    
1382  void  void
# Line 1609  Data::operator+=(const Data& right) Line 1403  Data::operator+=(const Data& right)
1403    if (isProtected()) {    if (isProtected()) {
1404          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1405    }    }
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1406    binaryOp(right,plus<double>());    binaryOp(right,plus<double>());
1407    return (*this);    return (*this);
1408  }  }
# Line 1619  Data::operator+=(const Data& right) Line 1410  Data::operator+=(const Data& right)
1410  Data&  Data&
1411  Data::operator+=(const boost::python::object& right)  Data::operator+=(const boost::python::object& right)
1412  {  {
1413    if (isProtected()) {    Data tmp(right,getFunctionSpace(),false);
1414          throw DataException("Error - attempt to update protected Data object.");    binaryOp(tmp,plus<double>());
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
   binaryOp(right,plus<double>());  
1415    return (*this);    return (*this);
1416  }  }
1417    
# Line 1635  Data::operator-=(const Data& right) Line 1421  Data::operator-=(const Data& right)
1421    if (isProtected()) {    if (isProtected()) {
1422          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1423    }    }
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1424    binaryOp(right,minus<double>());    binaryOp(right,minus<double>());
1425    return (*this);    return (*this);
1426  }  }
# Line 1645  Data::operator-=(const Data& right) Line 1428  Data::operator-=(const Data& right)
1428  Data&  Data&
1429  Data::operator-=(const boost::python::object& right)  Data::operator-=(const boost::python::object& right)
1430  {  {
1431    if (isProtected()) {    Data tmp(right,getFunctionSpace(),false);
1432          throw DataException("Error - attempt to update protected Data object.");    binaryOp(tmp,minus<double>());
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
   binaryOp(right,minus<double>());  
1433    return (*this);    return (*this);
1434  }  }
1435    
# Line 1661  Data::operator*=(const Data& right) Line 1439  Data::operator*=(const Data& right)
1439    if (isProtected()) {    if (isProtected()) {
1440          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1441    }    }
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1442    binaryOp(right,multiplies<double>());    binaryOp(right,multiplies<double>());
1443    return (*this);    return (*this);
1444  }  }
# Line 1671  Data::operator*=(const Data& right) Line 1446  Data::operator*=(const Data& right)
1446  Data&  Data&
1447  Data::operator*=(const boost::python::object& right)  Data::operator*=(const boost::python::object& right)
1448  {  {
1449    if (isProtected()) {    Data tmp(right,getFunctionSpace(),false);
1450          throw DataException("Error - attempt to update protected Data object.");    binaryOp(tmp,multiplies<double>());
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
   binaryOp(right,multiplies<double>());  
1451    return (*this);    return (*this);
1452  }  }
1453    
# Line 1687  Data::operator/=(const Data& right) Line 1457  Data::operator/=(const Data& right)
1457    if (isProtected()) {    if (isProtected()) {
1458          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1459    }    }
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1460    binaryOp(right,divides<double>());    binaryOp(right,divides<double>());
1461    return (*this);    return (*this);
1462  }  }
# Line 1697  Data::operator/=(const Data& right) Line 1464  Data::operator/=(const Data& right)
1464  Data&  Data&
1465  Data::operator/=(const boost::python::object& right)  Data::operator/=(const boost::python::object& right)
1466  {  {
1467    if (isProtected()) {    Data tmp(right,getFunctionSpace(),false);
1468          throw DataException("Error - attempt to update protected Data object.");    binaryOp(tmp,divides<double>());
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
   binaryOp(right,divides<double>());  
1469    return (*this);    return (*this);
1470  }  }
1471    
1472  Data  Data
1473  Data::rpowO(const boost::python::object& left) const  Data::rpowO(const boost::python::object& left) const
1474  {  {
   if (isProtected()) {  
         throw DataException("Error - attempt to update protected Data object.");  
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1475    Data left_d(left,*this);    Data left_d(left,*this);
1476    return left_d.powD(*this);    return left_d.powD(*this);
1477  }  }
# Line 1723  Data::rpowO(const boost::python::object& Line 1479  Data::rpowO(const boost::python::object&
1479  Data  Data
1480  Data::powO(const boost::python::object& right) const  Data::powO(const boost::python::object& right) const
1481  {  {
1482  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1483    profData->binary++;    return powD(tmp);
 #endif  
   Data result;  
   result.copy(*this);  
   result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   return result;  
1484  }  }
1485    
1486  Data  Data
1487  Data::powD(const Data& right) const  Data::powD(const Data& right) const
1488  {  {
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1489    Data result;    Data result;
1490    result.copy(*this);    if (getDataPointRank()<right.getDataPointRank()) {
1491    result.binaryOp(right,(Data::BinaryDFunPtr)::pow);       result.copy(right);
1492         result.binaryOp(*this,escript::rpow);
1493      } else {
1494         result.copy(*this);
1495         result.binaryOp(right,(Data::BinaryDFunPtr)::pow);
1496      }
1497    return result;    return result;
1498  }  }
1499    
# Line 1753  escript::operator+(const Data& left, con Line 1506  escript::operator+(const Data& left, con
1506    Data result;    Data result;
1507    //    //
1508    // perform a deep copy    // perform a deep copy
1509    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1510    result+=right;       result.copy(right);
1511         result+=left;
1512      } else {
1513         result.copy(left);
1514         result+=right;
1515      }
1516    return result;    return result;
1517  }  }
1518    
# Line 1766  escript::operator-(const Data& left, con Line 1524  escript::operator-(const Data& left, con
1524    Data result;    Data result;
1525    //    //
1526    // perform a deep copy    // perform a deep copy
1527    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1528    result-=right;       result=right.neg();
1529         result+=left;
1530      } else {
1531         result.copy(left);
1532         result-=right;
1533      }
1534    return result;    return result;
1535  }  }
1536    
# Line 1779  escript::operator*(const Data& left, con Line 1542  escript::operator*(const Data& left, con
1542    Data result;    Data result;
1543    //    //
1544    // perform a deep copy    // perform a deep copy
1545    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1546    result*=right;       result.copy(right);
1547         result*=left;
1548      } else {
1549         result.copy(left);
1550         result*=right;
1551      }
1552    return result;    return result;
1553  }  }
1554    
# Line 1792  escript::operator/(const Data& left, con Line 1560  escript::operator/(const Data& left, con
1560    Data result;    Data result;
1561    //    //
1562    // perform a deep copy    // perform a deep copy
1563    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1564    result/=right;       result=right.oneOver();
1565         result*=left;
1566      } else {
1567         result.copy(left);
1568         result/=right;
1569      }
1570    return result;    return result;
1571  }  }
1572    
# Line 1802  escript::operator/(const Data& left, con Line 1575  escript::operator/(const Data& left, con
1575  Data  Data
1576  escript::operator+(const Data& left, const boost::python::object& right)  escript::operator+(const Data& left, const boost::python::object& right)
1577  {  {
1578    //    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;  
1579  }  }
1580    
1581  //  //
# Line 1818  escript::operator+(const Data& left, con Line 1583  escript::operator+(const Data& left, con
1583  Data  Data
1584  escript::operator-(const Data& left, const boost::python::object& right)  escript::operator-(const Data& left, const boost::python::object& right)
1585  {  {
1586    //    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;  
1587  }  }
1588    
1589  //  //
# Line 1834  escript::operator-(const Data& left, con Line 1591  escript::operator-(const Data& left, con
1591  Data  Data
1592  escript::operator*(const Data& left, const boost::python::object& right)  escript::operator*(const Data& left, const boost::python::object& right)
1593  {  {
1594    //    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;  
1595  }  }
1596    
1597  //  //
# Line 1850  escript::operator*(const Data& left, con Line 1599  escript::operator*(const Data& left, con
1599  Data  Data
1600  escript::operator/(const Data& left, const boost::python::object& right)  escript::operator/(const Data& left, const boost::python::object& right)
1601  {  {
1602    //    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;  
1603  }  }
1604    
1605  //  //
# Line 1866  escript::operator/(const Data& left, con Line 1607  escript::operator/(const Data& left, con
1607  Data  Data
1608  escript::operator+(const boost::python::object& left, const Data& right)  escript::operator+(const boost::python::object& left, const Data& right)
1609  {  {
1610    //    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;  
1611  }  }
1612    
1613  //  //
# Line 1879  escript::operator+(const boost::python:: Line 1615  escript::operator+(const boost::python::
1615  Data  Data
1616  escript::operator-(const boost::python::object& left, const Data& right)  escript::operator-(const boost::python::object& left, const Data& right)
1617  {  {
1618    //    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;  
1619  }  }
1620    
1621  //  //
# Line 1892  escript::operator-(const boost::python:: Line 1623  escript::operator-(const boost::python::
1623  Data  Data
1624  escript::operator*(const boost::python::object& left, const Data& right)  escript::operator*(const boost::python::object& left, const Data& right)
1625  {  {
1626    //    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;  
1627  }  }
1628    
1629  //  //
# Line 1905  escript::operator*(const boost::python:: Line 1631  escript::operator*(const boost::python::
1631  Data  Data
1632  escript::operator/(const boost::python::object& left, const Data& right)  escript::operator/(const boost::python::object& left, const Data& right)
1633  {  {
1634    //    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;  
1635  }  }
1636    
1637  //  //
# Line 1979  Data::getItem(const boost::python::objec Line 1700  Data::getItem(const boost::python::objec
1700  Data  Data
1701  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataArrayView::RegionType& region) const
1702  {  {
 #if defined DOPROF  
   profData->slicing++;  
 #endif  
1703    return Data(*this,region);    return Data(*this,region);
1704  }  }
1705    
# Line 2023  Data::setSlice(const Data& value, Line 1741  Data::setSlice(const Data& value,
1741    if (isProtected()) {    if (isProtected()) {
1742          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1743    }    }
 #if defined DOPROF  
   profData->slicing++;  
 #endif  
1744    Data tempValue(value);    Data tempValue(value);
1745    typeMatchLeft(tempValue);    typeMatchLeft(tempValue);
1746    typeMatchRight(tempValue);    typeMatchRight(tempValue);
# Line 2060  Data::typeMatchRight(const Data& right) Line 1775  Data::typeMatchRight(const Data& right)
1775    }    }
1776  }  }
1777    
1778  /* TODO */  void
1779  /* global reduction */  Data::setTaggedValueByName(std::string name,
1780                               const boost::python::object& value)
1781    {
1782         if (getFunctionSpace().getDomain().isValidTagName(name)) {
1783            int tagKey=getFunctionSpace().getDomain().getTag(name);
1784            setTaggedValue(tagKey,value);
1785         }
1786    }
1787  void  void
1788  Data::setTaggedValue(int tagKey,  Data::setTaggedValue(int tagKey,
1789                       const boost::python::object& value)                       const boost::python::object& value)
# Line 2086  Data::setTaggedValue(int tagKey, Line 1808  Data::setTaggedValue(int tagKey,
1808    m_data->setTaggedValue(tagKey,valueDataArray.getView());    m_data->setTaggedValue(tagKey,valueDataArray.getView());
1809  }  }
1810    
 /* TODO */  
 /* global reduction */  
1811  void  void
1812  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
1813                              const DataArrayView& value)                              const DataArrayView& value)
# Line 2108  Data::setTaggedValueFromCPP(int tagKey, Line 1828  Data::setTaggedValueFromCPP(int tagKey,
1828    m_data->setTaggedValue(tagKey,value);    m_data->setTaggedValue(tagKey,value);
1829  }  }
1830    
 /* TODO */  
 /* global reduction */  
1831  int  int
1832  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
1833  {  {
1834    return m_data->getTagNumber(dpno);    return m_data->getTagNumber(dpno);
1835  }  }
1836    
 /* TODO */  
 /* global reduction */  
 void  
 Data::setRefValue(int ref,  
                   const boost::python::numeric::array& value)  
 {  
   if (isProtected()) {  
         throw DataException("Error - attempt to update protected Data object.");  
   }  
   //  
   // 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);  
           }  
         }  
       }  
     }  
   }  
   
 }  
   
1837  void  void
1838  Data::archiveData(const std::string fileName)  Data::archiveData(const std::string fileName)
1839  {  {
# Line 2241  Data::archiveData(const std::string file Line 1875  Data::archiveData(const std::string file
1875    DataArrayView::ShapeType dataPointShape = getDataPointShape();    DataArrayView::ShapeType dataPointShape = getDataPointShape();
1876    vector<int> referenceNumbers(noSamples);    vector<int> referenceNumbers(noSamples);
1877    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
1878      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceNoFromSampleNo(sampleNo);      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceIDOfSample(sampleNo);
1879    }    }
1880    vector<int> tagNumbers(noSamples);    vector<int> tagNumbers(noSamples);
1881    if (isTagged()) {    if (isTagged()) {
# Line 2450  Data::extractData(const std::string file Line 2084  Data::extractData(const std::string file
2084      throw DataException("extractData Error: incompatible FunctionSpace");      throw DataException("extractData Error: incompatible FunctionSpace");
2085    }    }
2086    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2087      if (referenceNumbers[sampleNo] != fspace.getReferenceNoFromSampleNo(sampleNo)) {      if (referenceNumbers[sampleNo] != fspace.getReferenceIDOfSample(sampleNo)) {
2088        throw DataException("extractData Error: incompatible FunctionSpace");        throw DataException("extractData Error: incompatible FunctionSpace");
2089      }      }
2090    }    }
# Line 2541  ostream& escript::operator<<(ostream& o, Line 2175  ostream& escript::operator<<(ostream& o,
2175    return o;    return o;
2176  }  }
2177    
2178    Data
2179    escript::C_GeneralTensorProduct(Data& arg_0,
2180                         Data& arg_1,
2181                         int axis_offset,
2182                         int transpose)
2183    {
2184      // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2185      // SM is the product of the last axis_offset entries in arg_0.getShape().
2186    
2187    
2188      // Interpolate if necessary and find an appropriate function space
2189      Data arg_0_Z, arg_1_Z;
2190      if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2191        if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2192          arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2193          arg_1_Z = Data(arg_1);
2194        }
2195        else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2196          arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2197          arg_0_Z =Data(arg_0);
2198        }
2199        else {
2200          throw DataException("Error - C_GeneralTensorProduct: arguments have incompatible function spaces.");
2201        }
2202      } else {
2203          arg_0_Z = Data(arg_0);
2204          arg_1_Z = Data(arg_1);
2205      }
2206      // Get rank and shape of inputs
2207      int rank0 = arg_0_Z.getDataPointRank();
2208      int rank1 = arg_1_Z.getDataPointRank();
2209      DataArrayView::ShapeType shape0 = arg_0_Z.getDataPointShape();
2210      DataArrayView::ShapeType shape1 = arg_1_Z.getDataPointShape();
2211    
2212      // Prepare for the loops of the product and verify compatibility of shapes
2213      int start0=0, start1=0;
2214      if (transpose == 0)       {}
2215      else if (transpose == 1)  { start0 = axis_offset; }
2216      else if (transpose == 2)  { start1 = rank1-axis_offset; }
2217      else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2218    
2219      // Adjust the shapes for transpose
2220      DataArrayView::ShapeType tmpShape0;
2221      DataArrayView::ShapeType tmpShape1;
2222      for (int i=0; i<rank0; i++)   { tmpShape0.push_back( shape0[(i+start0)%rank0] ); }
2223      for (int i=0; i<rank1; i++)   { tmpShape1.push_back( shape1[(i+start1)%rank1] ); }
2224    
2225    #if 0
2226      // For debugging: show shape after transpose
2227      char tmp[100];
2228      std::string shapeStr;
2229      shapeStr = "(";
2230      for (int i=0; i<rank0; i++)   { sprintf(tmp, "%d,", tmpShape0[i]); shapeStr += tmp; }
2231      shapeStr += ")";
2232      cout << "C_GeneralTensorProduct: Shape of arg0 is " << shapeStr << endl;
2233      shapeStr = "(";
2234      for (int i=0; i<rank1; i++)   { sprintf(tmp, "%d,", tmpShape1[i]); shapeStr += tmp; }
2235      shapeStr += ")";
2236      cout << "C_GeneralTensorProduct: Shape of arg1 is " << shapeStr << endl;
2237    #endif
2238    
2239      // Prepare for the loops of the product
2240      int SL=1, SM=1, SR=1;
2241      for (int i=0; i<rank0-axis_offset; i++)   {
2242        SL *= tmpShape0[i];
2243      }
2244      for (int i=rank0-axis_offset; i<rank0; i++)   {
2245        if (tmpShape0[i] != tmpShape1[i-(rank0-axis_offset)]) {
2246          throw DataException("C_GeneralTensorProduct: Error - incompatible shapes");
2247        }
2248        SM *= tmpShape0[i];
2249      }
2250      for (int i=axis_offset; i<rank1; i++)     {
2251        SR *= tmpShape1[i];
2252      }
2253    
2254      // Define the shape of the output
2255      DataArrayView::ShapeType shape2;
2256      for (int i=0; i<rank0-axis_offset; i++) { shape2.push_back(tmpShape0[i]); } // First part of arg_0_Z
2257      for (int i=axis_offset; i<rank1; i++)   { shape2.push_back(tmpShape1[i]); } // Last part of arg_1_Z
2258    
2259      // Declare output Data object
2260      Data res;
2261    
2262      if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2263        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2264        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[0]);
2265        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[0]);
2266        double *ptr_2 = &((res.getPointDataView().getData())[0]);
2267        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2268      }
2269      else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2270    
2271        // Prepare the DataConstant input
2272        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2273        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2274    
2275        // Borrow DataTagged input from Data object
2276        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2277        if (tmp_1==0) { throw DataException("GTP_1 Programming error - casting to DataTagged."); }
2278    
2279        // Prepare a DataTagged output 2
2280        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataTagged output
2281        res.tag();
2282        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2283        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2284    
2285        // Prepare offset into DataConstant
2286        int offset_0 = tmp_0->getPointOffset(0,0);
2287        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2288        // Get the views
2289        DataArrayView view_1 = tmp_1->getDefaultValue();
2290        DataArrayView view_2 = tmp_2->getDefaultValue();
2291        // Get the pointers to the actual data
2292        double *ptr_1 = &((view_1.getData())[0]);
2293        double *ptr_2 = &((view_2.getData())[0]);
2294        // Compute an MVP for the default
2295        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2296        // Compute an MVP for each tag
2297        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2298        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2299        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2300          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2301          DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2302          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2303          double *ptr_1 = &view_1.getData(0);
2304          double *ptr_2 = &view_2.getData(0);
2305          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2306        }
2307    
2308      }
2309      else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2310    
2311        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2312        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2313        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2314        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2315        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2316        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2317        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2318        int sampleNo_1,dataPointNo_1;
2319        int numSamples_1 = arg_1_Z.getNumSamples();
2320        int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2321        int offset_0 = tmp_0->getPointOffset(0,0);
2322        #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2323        for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2324          for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2325            int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2326            int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2327            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2328            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2329            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2330            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2331          }
2332        }
2333    
2334      }
2335      else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2336    
2337        // Borrow DataTagged input from Data object
2338        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2339        if (tmp_0==0) { throw DataException("GTP_0 Programming error - casting to DataTagged."); }
2340    
2341        // Prepare the DataConstant input
2342        DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2343        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2344    
2345        // Prepare a DataTagged output 2
2346        res = Data(0.0, shape2, arg_0_Z.getFunctionSpace());    // DataTagged output
2347        res.tag();
2348        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2349        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2350    
2351        // Prepare offset into DataConstant
2352        int offset_1 = tmp_1->getPointOffset(0,0);
2353        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2354        // Get the views
2355        DataArrayView view_0 = tmp_0->getDefaultValue();
2356        DataArrayView view_2 = tmp_2->getDefaultValue();
2357        // Get the pointers to the actual data
2358        double *ptr_0 = &((view_0.getData())[0]);
2359        double *ptr_2 = &((view_2.getData())[0]);
2360        // Compute an MVP for the default
2361        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2362        // Compute an MVP for each tag
2363        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2364        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2365        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2366          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2367          DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2368          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2369          double *ptr_0 = &view_0.getData(0);
2370          double *ptr_2 = &view_2.getData(0);
2371          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2372        }
2373    
2374      }
2375      else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2376    
2377        // Borrow DataTagged input from Data object
2378        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2379        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2380    
2381        // Borrow DataTagged input from Data object
2382        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2383        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2384    
2385        // Prepare a DataTagged output 2
2386        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());
2387        res.tag();  // DataTagged output
2388        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2389        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2390    
2391        // Get the views
2392        DataArrayView view_0 = tmp_0->getDefaultValue();
2393        DataArrayView view_1 = tmp_1->getDefaultValue();
2394        DataArrayView view_2 = tmp_2->getDefaultValue();
2395        // Get the pointers to the actual data
2396        double *ptr_0 = &((view_0.getData())[0]);
2397        double *ptr_1 = &((view_1.getData())[0]);
2398        double *ptr_2 = &((view_2.getData())[0]);
2399        // Compute an MVP for the default
2400        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2401        // Merge the tags
2402        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2403        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2404        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2405        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2406          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue()); // use tmp_2 to get correct shape
2407        }
2408        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2409          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2410        }
2411        // Compute an MVP for each tag
2412        const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2413        for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2414          DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2415          DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2416          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2417          double *ptr_0 = &view_0.getData(0);
2418          double *ptr_1 = &view_1.getData(0);
2419          double *ptr_2 = &view_2.getData(0);
2420          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2421        }
2422    
2423      }
2424      else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2425    
2426        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2427        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2428        DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2429        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2430        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2431        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2432        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2433        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2434        int sampleNo_0,dataPointNo_0;
2435        int numSamples_0 = arg_0_Z.getNumSamples();
2436        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2437        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2438        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2439          int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2440          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2441          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2442            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2443            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2444            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2445            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2446            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2447          }
2448        }
2449    
2450      }
2451      else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2452    
2453        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2454        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2455        DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2456        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2457        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2458        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2459        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2460        int sampleNo_0,dataPointNo_0;
2461        int numSamples_0 = arg_0_Z.getNumSamples();
2462        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2463        int offset_1 = tmp_1->getPointOffset(0,0);
2464        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2465        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2466          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2467            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2468            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2469            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2470            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2471            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2472            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2473          }
2474        }
2475    
2476    
2477      }
2478      else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2479    
2480        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2481        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2482        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2483        DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2484        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2485        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2486        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2487        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2488        int sampleNo_0,dataPointNo_0;
2489        int numSamples_0 = arg_0_Z.getNumSamples();
2490        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2491        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2492        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2493          int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2494          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2495          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2496            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2497            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2498            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2499            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2500            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2501          }
2502        }
2503    
2504      }
2505      else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2506    
2507        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2508        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2509        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2510        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2511        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2512        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2513        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2514        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2515        int sampleNo_0,dataPointNo_0;
2516        int numSamples_0 = arg_0_Z.getNumSamples();
2517        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2518        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2519        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2520          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2521            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2522            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2523            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2524            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2525            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2526            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2527            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2528          }
2529        }
2530    
2531      }
2532      else {
2533        throw DataException("Error - C_GeneralTensorProduct: unknown combination of inputs");
2534      }
2535    
2536      return res;
2537    }
2538    
2539    DataAbstract*
2540    Data::borrowData() const
2541    {
2542      return m_data.get();
2543    }
2544    
2545  /* Member functions specific to the MPI implementation */  /* Member functions specific to the MPI implementation */
2546    
2547  void  void
# Line 2557  Data::print() Line 2558  Data::print()
2558      printf( "\n" );      printf( "\n" );
2559    }    }
2560  }  }
2561    void
2562    Data::dump(const std::string fileName) const
2563    {
2564      try
2565         {
2566            return m_data->dump(fileName);
2567         }
2568         catch (exception& e)
2569         {
2570            cout << e.what() << endl;
2571         }
2572    }
2573    
2574  int  int
2575  Data::get_MPISize() const  Data::get_MPISize() const

Legend:
Removed from v.790  
changed lines
  Added in v.1141

  ViewVC Help
Powered by ViewVC 1.1.26