/[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 474 by jgs, Mon Jan 30 04:23:44 2006 UTC revision 964 by gross, Tue Feb 13 05:10:26 2007 UTC
# Line 1  Line 1 
1  // $Id$  // $Id$
 /*=============================================================================  
   
  ******************************************************************************  
  *                                                                            *  
  *       COPYRIGHT ACcESS 2004 -  All Rights Reserved                         *  
  *                                                                            *  
  * This software is the property of ACcESS.  No part of this code             *  
  * may be copied in any form or by any means without the expressed written    *  
  * consent of ACcESS.  Copying, use or modification of this software          *  
  * by any unauthorised person is illegal unless that                          *  
  * person has a software license agreement with ACcESS.                       *  
  *                                                                            *  
  ******************************************************************************  
   
 ******************************************************************************/  
2    
3    /*
4     ************************************************************
5     *          Copyright 2006 by ACcESS MNRF                   *
6     *                                                          *
7     *              http://www.access.edu.au                    *
8     *       Primary Business: Queensland, Australia            *
9     *  Licensed under the Open Software License version 3.0    *
10     *     http://www.opensource.org/licenses/osl-3.0.php       *
11     *                                                          *
12     ************************************************************
13    */
14  #include "Data.h"  #include "Data.h"
15    
 #include <iostream>  
 #include <fstream>  
 #include <algorithm>  
 #include <vector>  
 #include <exception>  
 #include <functional>  
 #include <math.h>  
   
 #include <boost/python/dict.hpp>  
 #include <boost/python/str.hpp>  
 #include <boost/python/extract.hpp>  
 #include <boost/python/long.hpp>  
 #include <boost/python/tuple.hpp>  
   
 #include "DataException.h"  
16  #include "DataExpanded.h"  #include "DataExpanded.h"
17  #include "DataConstant.h"  #include "DataConstant.h"
18  #include "DataTagged.h"  #include "DataTagged.h"
19  #include "DataEmpty.h"  #include "DataEmpty.h"
20  #include "DataArray.h"  #include "DataArray.h"
21    #include "DataArrayView.h"
22  #include "DataProf.h"  #include "DataProf.h"
23  #include "FunctionSpaceFactory.h"  #include "FunctionSpaceFactory.h"
24  #include "AbstractContinuousDomain.h"  #include "AbstractContinuousDomain.h"
25  #include "UnaryFuncs.h"  #include "UnaryFuncs.h"
26    
27    #include <fstream>
28    #include <algorithm>
29    #include <vector>
30    #include <functional>
31    
32    #include <boost/python/dict.hpp>
33    #include <boost/python/extract.hpp>
34    #include <boost/python/long.hpp>
35    
36  using namespace std;  using namespace std;
37  using namespace boost::python;  using namespace boost::python;
38  using namespace boost;  using namespace boost;
# Line 60  Data::Data() Line 51  Data::Data()
51    DataAbstract* temp=new DataEmpty();    DataAbstract* temp=new DataEmpty();
52    shared_ptr<DataAbstract> temp_data(temp);    shared_ptr<DataAbstract> temp_data(temp);
53    m_data=temp_data;    m_data=temp_data;
54      m_protected=false;
55  #if defined DOPROF  #if defined DOPROF
56    // create entry in global profiling table for this object    // create entry in global profiling table for this object
57    profData = dataProfTable.newData();    profData = dataProfTable.newData();
# Line 77  Data::Data(double value, Line 69  Data::Data(double value,
69    }    }
70    DataArray temp(dataPointShape,value);    DataArray temp(dataPointShape,value);
71    initialise(temp.getView(),what,expanded);    initialise(temp.getView(),what,expanded);
72      m_protected=false;
73  #if defined DOPROF  #if defined DOPROF
74    // create entry in global profiling table for this object    // create entry in global profiling table for this object
75    profData = dataProfTable.newData();    profData = dataProfTable.newData();
# Line 91  Data::Data(double value, Line 84  Data::Data(double value,
84    DataArray temp(dataPointShape,value);    DataArray temp(dataPointShape,value);
85    pair<int,int> dataShape=what.getDataShape();    pair<int,int> dataShape=what.getDataShape();
86    initialise(temp.getView(),what,expanded);    initialise(temp.getView(),what,expanded);
87      m_protected=false;
88  #if defined DOPROF  #if defined DOPROF
89    // create entry in global profiling table for this object    // create entry in global profiling table for this object
90    profData = dataProfTable.newData();    profData = dataProfTable.newData();
# Line 100  Data::Data(double value, Line 94  Data::Data(double value,
94  Data::Data(const Data& inData)  Data::Data(const Data& inData)
95  {  {
96    m_data=inData.m_data;    m_data=inData.m_data;
97      m_protected=inData.isProtected();
98  #if defined DOPROF  #if defined DOPROF
99    // create entry in global profiling table for this object    // create entry in global profiling table for this object
100    profData = dataProfTable.newData();    profData = dataProfTable.newData();
# Line 114  Data::Data(const Data& inData, Line 109  Data::Data(const Data& inData,
109    DataAbstract* tmp = inData.m_data->getSlice(region);    DataAbstract* tmp = inData.m_data->getSlice(region);
110    shared_ptr<DataAbstract> temp_data(tmp);    shared_ptr<DataAbstract> temp_data(tmp);
111    m_data=temp_data;    m_data=temp_data;
112      m_protected=false;
113  #if defined DOPROF  #if defined DOPROF
114    // create entry in global profiling table for this object    // create entry in global profiling table for this object
115    profData = dataProfTable.newData();    profData = dataProfTable.newData();
# Line 123  Data::Data(const Data& inData, Line 119  Data::Data(const Data& inData,
119  Data::Data(const Data& inData,  Data::Data(const Data& inData,
120             const FunctionSpace& functionspace)             const FunctionSpace& functionspace)
121  {  {
122    #if defined DOPROF
123      // create entry in global profiling table for this object
124      profData = dataProfTable.newData();
125    #endif
126    if (inData.getFunctionSpace()==functionspace) {    if (inData.getFunctionSpace()==functionspace) {
127      m_data=inData.m_data;      m_data=inData.m_data;
128    } else {    } else {
129        #if defined DOPROF
130        profData->interpolate++;
131        #endif
132      Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);      Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);
133      // Note: Must use a reference or pointer to a derived object      // Note: Must use a reference or pointer to a derived object
134      // in order to get polymorphic behaviour. Shouldn't really      // in order to get polymorphic behaviour. Shouldn't really
# Line 139  Data::Data(const Data& inData, Line 142  Data::Data(const Data& inData,
142      }      }
143      m_data=tmp.m_data;      m_data=tmp.m_data;
144    }    }
145  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
146  }  }
147    
148  Data::Data(const DataTagged::TagListType& tagKeys,  Data::Data(const DataTagged::TagListType& tagKeys,
# Line 154  Data::Data(const DataTagged::TagListType Line 154  Data::Data(const DataTagged::TagListType
154    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);
155    shared_ptr<DataAbstract> temp_data(temp);    shared_ptr<DataAbstract> temp_data(temp);
156    m_data=temp_data;    m_data=temp_data;
157      m_protected=false;
158    if (expanded) {    if (expanded) {
159      expand();      expand();
160    }    }
# Line 168  Data::Data(const numeric::array& value, Line 169  Data::Data(const numeric::array& value,
169             bool expanded)             bool expanded)
170  {  {
171    initialise(value,what,expanded);    initialise(value,what,expanded);
172      m_protected=false;
173  #if defined DOPROF  #if defined DOPROF
174    // create entry in global profiling table for this object    // create entry in global profiling table for this object
175    profData = dataProfTable.newData();    profData = dataProfTable.newData();
# Line 179  Data::Data(const DataArrayView& value, Line 181  Data::Data(const DataArrayView& value,
181             bool expanded)             bool expanded)
182  {  {
183    initialise(value,what,expanded);    initialise(value,what,expanded);
184      m_protected=false;
185  #if defined DOPROF  #if defined DOPROF
186    // create entry in global profiling table for this object    // create entry in global profiling table for this object
187    profData = dataProfTable.newData();    profData = dataProfTable.newData();
# Line 191  Data::Data(const object& value, Line 194  Data::Data(const object& value,
194  {  {
195    numeric::array asNumArray(value);    numeric::array asNumArray(value);
196    initialise(asNumArray,what,expanded);    initialise(asNumArray,what,expanded);
197      m_protected=false;
198  #if defined DOPROF  #if defined DOPROF
199    // create entry in global profiling table for this object    // create entry in global profiling table for this object
200    profData = dataProfTable.newData();    profData = dataProfTable.newData();
# Line 215  Data::Data(const object& value, Line 219  Data::Data(const object& value,
219      // Create a DataConstant with the same sample shape as other      // Create a DataConstant with the same sample shape as other
220      initialise(temp.getView(),other.getFunctionSpace(),false);      initialise(temp.getView(),other.getFunctionSpace(),false);
221    }    }
222      m_protected=false;
223  #if defined DOPROF  #if defined DOPROF
224    // create entry in global profiling table for this object    // create entry in global profiling table for this object
225    profData = dataProfTable.newData();    profData = dataProfTable.newData();
# Line 360  Data::isConstant() const Line 365  Data::isConstant() const
365  }  }
366    
367  void  void
368    Data::setProtection()
369    {
370       m_protected=true;
371    }
372    
373    bool
374    Data::isProtected() const
375    {
376       return m_protected;
377    }
378    
379    
380    
381    void
382  Data::expand()  Data::expand()
383  {  {
384    if (isConstant()) {    if (isConstant()) {
# Line 401  Data::tag() Line 420  Data::tag()
420    }    }
421  }  }
422    
423  void  Data
424  Data::reshapeDataPoint(const DataArrayView::ShapeType& shape)  Data::oneOver() const
425  {  {
426    m_data->reshapeDataPoint(shape);  #if defined DOPROF
427      profData->where++;
428    #endif
429      return escript::unaryOp(*this,bind1st(divides<double>(),1.));
430  }  }
431    
432  Data  Data
# Line 444  Data::whereNonPositive() const Line 466  Data::whereNonPositive() const
466  }  }
467    
468  Data  Data
469  Data::whereZero() const  Data::whereZero(double tol) const
470  {  {
471  #if defined DOPROF  #if defined DOPROF
472    profData->where++;    profData->where++;
473  #endif  #endif
474    return escript::unaryOp(*this,bind2nd(equal_to<double>(),0.0));    Data dataAbs=abs();
475      return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));
476  }  }
477    
478  Data  Data
479  Data::whereNonZero() const  Data::whereNonZero(double tol) const
480  {  {
481  #if defined DOPROF  #if defined DOPROF
482    profData->where++;    profData->where++;
483  #endif  #endif
484    return escript::unaryOp(*this,bind2nd(not_equal_to<double>(),0.0));    Data dataAbs=abs();
485      return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));
486  }  }
487    
488  Data  Data
# Line 524  Data::getDataPointShape() const Line 548  Data::getDataPointShape() const
548    return getPointDataView().getShape();    return getPointDataView().getShape();
549  }  }
550    
551    
552    
553  void  void
554  Data::fillFromNumArray(const boost::python::numeric::array num_array)  Data::fillFromNumArray(const boost::python::numeric::array num_array)
555  {  {
556      if (isProtected()) {
557            throw DataException("Error - attempt to update protected Data object.");
558      }
559    //    //
560    // check rank    // check rank
561    if (num_array.getrank()<getDataPointRank())    if (num_array.getrank()<getDataPointRank())
# Line 613  Data::convertToNumArray() Line 642  Data::convertToNumArray()
642        }        }
643        if (dataPointRank==1) {        if (dataPointRank==1) {
644          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<dataPointShape[0]; i++) {
645            numArray[dataPoint][i]=dataPointView(i);            numArray[make_tuple(dataPoint,i)]=dataPointView(i);
646          }          }
647        }        }
648        if (dataPointRank==2) {        if (dataPointRank==2) {
649          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<dataPointShape[0]; i++) {
650            for (int j=0; j<dataPointShape[1]; j++) {            for (int j=0; j<dataPointShape[1]; j++) {
651              numArray[dataPoint][i][j] = dataPointView(i,j);              numArray[make_tuple(dataPoint,i,j)] = dataPointView(i,j);
652            }            }
653          }          }
654        }        }
# Line 627  Data::convertToNumArray() Line 656  Data::convertToNumArray()
656          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<dataPointShape[0]; i++) {
657            for (int j=0; j<dataPointShape[1]; j++) {            for (int j=0; j<dataPointShape[1]; j++) {
658              for (int k=0; k<dataPointShape[2]; k++) {              for (int k=0; k<dataPointShape[2]; k++) {
659                numArray[dataPoint][i][j][k]=dataPointView(i,j,k);                numArray[make_tuple(dataPoint,i,j,k)]=dataPointView(i,j,k);
660              }              }
661            }            }
662          }          }
# Line 637  Data::convertToNumArray() Line 666  Data::convertToNumArray()
666            for (int j=0; j<dataPointShape[1]; j++) {            for (int j=0; j<dataPointShape[1]; j++) {
667              for (int k=0; k<dataPointShape[2]; k++) {              for (int k=0; k<dataPointShape[2]; k++) {
668                for (int l=0; l<dataPointShape[3]; l++) {                for (int l=0; l<dataPointShape[3]; l++) {
669                  numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);                  numArray[make_tuple(dataPoint,i,j,k,l)]=dataPointView(i,j,k,l);
670                }                }
671              }              }
672            }            }
# Line 652  Data::convertToNumArray() Line 681  Data::convertToNumArray()
681    return numArray;    return numArray;
682  }  }
683    
684  const  
685    const
686  boost::python::numeric::array  boost::python::numeric::array
687  Data::convertToNumArrayFromSampleNo(int sampleNo)  Data:: getValueOfDataPoint(int dataPointNo)
688  {  {
689    //    size_t length=0;
690    // Check a valid sample number has been supplied    int i, j, k, l;
   if (sampleNo >= getNumSamples()) {  
     throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  
   }  
   
   //  
   // determine the number of data points per sample  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
   
691    //    //
692    // determine the rank and shape of each data point    // determine the rank and shape of each data point
693    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
# Line 676  Data::convertToNumArrayFromSampleNo(int Line 698  Data::convertToNumArrayFromSampleNo(int
698    boost::python::numeric::array numArray(0.0);    boost::python::numeric::array numArray(0.0);
699    
700    //    //
701    // the rank of the returned numeric array will be the rank of    // the shape of the returned numeric array will be the same
702    // the data points, plus one. Where the rank of the array is n,    // as that of the data point
703    // the last n-1 dimensions will be equal to the shape of the    int arrayRank = dataPointRank;
704    // 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(numDataPointsPerSample);  
   for (int d=0; d<dataPointRank; d++) {  
      arrayShape.push_back(dataPointShape[d]);  
   }  
705    
706    //    //
707    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
708      if (arrayRank==0) {
709        numArray.resize(1);
710      }
711    if (arrayRank==1) {    if (arrayRank==1) {
712      numArray.resize(arrayShape[0]);      numArray.resize(arrayShape[0]);
713    }    }
# Line 703  Data::convertToNumArrayFromSampleNo(int Line 720  Data::convertToNumArrayFromSampleNo(int
720    if (arrayRank==4) {    if (arrayRank==4) {
721      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
722    }    }
   if (arrayRank==5) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);  
   }  
723    
724    //    if (getNumDataPointsPerSample()>0) {
725    // loop through each data point in turn, loading the values for that data point         int sampleNo = dataPointNo/getNumDataPointsPerSample();
726    // into the numeric array.         int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
727    for (int dataPoint = 0; dataPoint < numDataPointsPerSample; dataPoint++) {         //
728      DataArrayView dataPointView = getDataPoint(sampleNo, dataPoint);         // Check a valid sample number has been supplied
729      if (dataPointRank==0) {         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
730        numArray[dataPoint]=dataPointView();             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
731      }         }
732      if (dataPointRank==1) {                
733        for (int i=0; i<dataPointShape[0]; i++) {         //
734          numArray[dataPoint][i]=dataPointView(i);         // Check a valid data point number has been supplied
735        }         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
736      }             throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
737      if (dataPointRank==2) {         }
738        for (int i=0; i<dataPointShape[0]; i++) {         // TODO: global error handling
739          for (int j=0; j<dataPointShape[1]; j++) {         // create a view of the data if it is stored locally
740            numArray[dataPoint][i][j] = dataPointView(i,j);         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
741          }          
742        }         switch( dataPointRank ){
743      }              case 0 :
744      if (dataPointRank==3) {                  numArray[0] = dataPointView();
745        for (int i=0; i<dataPointShape[0]; i++) {                  break;
746          for (int j=0; j<dataPointShape[1]; j++) {              case 1 :        
747            for (int k=0; k<dataPointShape[2]; k++) {                  for( i=0; i<dataPointShape[0]; i++ )
748              numArray[dataPoint][i][j][k]=dataPointView(i,j,k);                      numArray[i]=dataPointView(i);
749            }                  break;
750          }              case 2 :        
751        }                  for( i=0; i<dataPointShape[0]; i++ )
752      }                      for( j=0; j<dataPointShape[1]; j++)
753      if (dataPointRank==4) {                          numArray[make_tuple(i,j)]=dataPointView(i,j);
754        for (int i=0; i<dataPointShape[0]; i++) {                  break;
755          for (int j=0; j<dataPointShape[1]; j++) {              case 3 :        
756            for (int k=0; k<dataPointShape[2]; k++) {                  for( i=0; i<dataPointShape[0]; i++ )
757              for (int l=0; l<dataPointShape[3]; l++) {                      for( j=0; j<dataPointShape[1]; j++ )
758                numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);                          for( k=0; k<dataPointShape[2]; k++)
759              }                              numArray[make_tuple(i,j,k)]=dataPointView(i,j,k);
760            }                  break;
761          }              case 4 :
762        }                  for( i=0; i<dataPointShape[0]; i++ )
763      }                      for( j=0; j<dataPointShape[1]; j++ )
764                            for( k=0; k<dataPointShape[2]; k++ )
765                                for( l=0; l<dataPointShape[3]; l++)
766                                    numArray[make_tuple(i,j,k,l)]=dataPointView(i,j,k,l);
767                    break;
768        }
769    }    }
   
770    //    //
771    // return the loaded array    // return the array
772    return numArray;    return numArray;
 }  
773    
774  const  }
775  boost::python::numeric::array  void
776  Data::convertToNumArrayFromDPNo(int sampleNo,  Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array num_array)
                                 int dataPointNo)  
777  {  {
778      if (isProtected()) {
779            throw DataException("Error - attempt to update protected Data object.");
780      }
781      //
782      // check rank
783      if (num_array.getrank()<getDataPointRank())
784          throw DataException("Rank of numarray does not match Data object rank");
785    
786      //
787      // check shape of num_array
788      for (int i=0; i<getDataPointRank(); i++) {
789        if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])
790           throw DataException("Shape of numarray does not match Data object rank");
791      }
792    //    //
793    // Check a valid sample number has been supplied    // make sure data is expanded:
794    if (sampleNo >= getNumSamples()) {    if (!isExpanded()) {
795      throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");      expand();
796      }
797      if (getNumDataPointsPerSample()>0) {
798           int sampleNo = dataPointNo/getNumDataPointsPerSample();
799           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
800           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,num_array);
801      } else {
802           m_data->copyToDataPoint(-1, 0,num_array);
803    }    }
804    }
805    
806    void
807    Data::setValueOfDataPoint(int dataPointNo, const double value)
808    {
809      if (isProtected()) {
810            throw DataException("Error - attempt to update protected Data object.");
811      }
812    //    //
813    // Check a valid data point number has been supplied    // make sure data is expanded:
814    if (dataPointNo >= getNumDataPointsPerSample()) {    if (!isExpanded()) {
815      throw DataException("Error - Data::convertToNumArray: invalid dataPointNo.");      expand();
816    }    }
817      if (getNumDataPointsPerSample()>0) {
818           int sampleNo = dataPointNo/getNumDataPointsPerSample();
819           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
820           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,value);
821      } else {
822           m_data->copyToDataPoint(-1, 0,value);
823      }
824    }
825    
826    const
827    boost::python::numeric::array
828    Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
829    {
830      size_t length=0;
831      int i, j, k, l, pos;
832    //    //
833    // determine the rank and shape of each data point    // determine the rank and shape of each data point
834    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
# Line 804  Data::convertToNumArrayFromDPNo(int samp Line 862  Data::convertToNumArrayFromDPNo(int samp
862      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
863    }    }
864    
865      // added for the MPI communication
866      length=1;
867      for( i=0; i<arrayRank; i++ ) length *= arrayShape[i];
868      double *tmpData = new double[length];
869    
870    //    //
871    // load the values for the data point into the numeric array.    // load the values for the data point into the numeric array.
   DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);  
   if (dataPointRank==0) {  
     numArray[0]=dataPointView();  
   }  
   if (dataPointRank==1) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       numArray[i]=dataPointView(i);  
     }  
   }  
   if (dataPointRank==2) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         numArray[i][j] = dataPointView(i,j);  
       }  
     }  
   }  
   if (dataPointRank==3) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         for (int k=0; k<dataPointShape[2]; k++) {  
           numArray[i][j][k]=dataPointView(i,j,k);  
         }  
       }  
     }  
   }  
   if (dataPointRank==4) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         for (int k=0; k<dataPointShape[2]; k++) {  
           for (int l=0; l<dataPointShape[3]; l++) {  
             numArray[i][j][k][l]=dataPointView(i,j,k,l);  
           }  
         }  
       }  
     }  
   }  
872    
873        // updated for the MPI case
874        if( get_MPIRank()==procNo ){
875                 if (getNumDataPointsPerSample()>0) {
876                    int sampleNo = dataPointNo/getNumDataPointsPerSample();
877                    int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
878                    //
879                    // Check a valid sample number has been supplied
880                    if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
881                      throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
882                    }
883                  
884                    //
885                    // Check a valid data point number has been supplied
886                    if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
887                      throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
888                    }
889                    // TODO: global error handling
890            // create a view of the data if it is stored locally
891            DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
892            
893            // pack the data from the view into tmpData for MPI communication
894            pos=0;
895            switch( dataPointRank ){
896                case 0 :
897                    tmpData[0] = dataPointView();
898                    break;
899                case 1 :        
900                    for( i=0; i<dataPointShape[0]; i++ )
901                        tmpData[i]=dataPointView(i);
902                    break;
903                case 2 :        
904                    for( i=0; i<dataPointShape[0]; i++ )
905                        for( j=0; j<dataPointShape[1]; j++, pos++ )
906                            tmpData[pos]=dataPointView(i,j);
907                    break;
908                case 3 :        
909                    for( i=0; i<dataPointShape[0]; i++ )
910                        for( j=0; j<dataPointShape[1]; j++ )
911                            for( k=0; k<dataPointShape[2]; k++, pos++ )
912                                tmpData[pos]=dataPointView(i,j,k);
913                    break;
914                case 4 :
915                    for( i=0; i<dataPointShape[0]; i++ )
916                        for( j=0; j<dataPointShape[1]; j++ )
917                            for( k=0; k<dataPointShape[2]; k++ )
918                                for( l=0; l<dataPointShape[3]; l++, pos++ )
919                                    tmpData[pos]=dataPointView(i,j,k,l);
920                    break;
921            }
922                }
923        }
924            #ifdef PASO_MPI
925            // broadcast the data to all other processes
926        MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
927            #endif
928    
929        // unpack the data
930        switch( dataPointRank ){
931            case 0 :
932                numArray[0]=tmpData[0];
933                break;
934            case 1 :        
935                for( i=0; i<dataPointShape[0]; i++ )
936                    numArray[i]=tmpData[i];
937                break;
938            case 2 :        
939                for( i=0; i<dataPointShape[0]; i++ )
940                    for( j=0; j<dataPointShape[1]; j++ )
941                       numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
942                break;
943            case 3 :        
944                for( i=0; i<dataPointShape[0]; i++ )
945                    for( j=0; j<dataPointShape[1]; j++ )
946                        for( k=0; k<dataPointShape[2]; k++ )
947                            numArray[make_tuple(i,j,k)]=tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];
948                break;
949            case 4 :
950                for( i=0; i<dataPointShape[0]; i++ )
951                    for( j=0; j<dataPointShape[1]; j++ )
952                        for( k=0; k<dataPointShape[2]; k++ )
953                            for( l=0; l<dataPointShape[3]; l++ )
954                                    numArray[make_tuple(i,j,k,l)]=tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];
955                break;
956        }
957    
958        delete [] tmpData;  
959    //    //
960    // return the loaded array    // return the loaded array
961    return numArray;    return numArray;
962  }  }
963    
964    
965    
966  boost::python::numeric::array  boost::python::numeric::array
967  Data::integrate() const  Data::integrate() const
968  {  {
# Line 920  Data::integrate() const Line 1035  Data::integrate() const
1035  }  }
1036    
1037  Data  Data
1038    Data::erf() const
1039    {
1040    #if defined DOPROF
1041      profData->unary++;
1042    #endif
1043      return escript::unaryOp(*this,(Data::UnaryDFunPtr)::erf);
1044    }
1045    
1046    Data
1047  Data::sin() const  Data::sin() const
1048  {  {
1049  #if defined DOPROF  #if defined DOPROF
# Line 1105  Data::sqrt() const Line 1229  Data::sqrt() const
1229  double  double
1230  Data::Lsup() const  Data::Lsup() const
1231  {  {
1232      double localValue, globalValue;
1233  #if defined DOPROF  #if defined DOPROF
1234    profData->reduction1++;    profData->reduction1++;
1235  #endif  #endif
1236    //    //
1237    // set the initial absolute maximum value to zero    // set the initial absolute maximum value to zero
1238    
1239    AbsMax abs_max_func;    AbsMax abs_max_func;
1240    return algorithm(abs_max_func,0);    localValue = algorithm(abs_max_func,0);
1241    #ifdef PASO_MPI
1242      MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1243      return globalValue;
1244    #else
1245      return localValue;
1246    #endif
1247  }  }
1248    
1249  double  double
1250  Data::Linf() const  Data::Linf() const
1251  {  {
1252      double localValue, globalValue;
1253  #if defined DOPROF  #if defined DOPROF
1254    profData->reduction1++;    profData->reduction1++;
1255  #endif  #endif
1256    //    //
1257    // set the initial absolute minimum value to max double    // set the initial absolute minimum value to max double
1258    AbsMin abs_min_func;    AbsMin abs_min_func;
1259    return algorithm(abs_min_func,numeric_limits<double>::max());    localValue = algorithm(abs_min_func,numeric_limits<double>::max());
1260    
1261    #ifdef PASO_MPI
1262      MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1263      return globalValue;
1264    #else
1265      return localValue;
1266    #endif
1267  }  }
1268    
1269  double  double
1270  Data::sup() const  Data::sup() const
1271  {  {
1272      double localValue, globalValue;
1273  #if defined DOPROF  #if defined DOPROF
1274    profData->reduction1++;    profData->reduction1++;
1275  #endif  #endif
1276    //    //
1277    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1278    FMax fmax_func;    FMax fmax_func;
1279    return algorithm(fmax_func,numeric_limits<double>::max()*-1);    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1280    #ifdef PASO_MPI
1281      MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1282      return globalValue;
1283    #else
1284      return localValue;
1285    #endif
1286  }  }
1287    
1288  double  double
1289  Data::inf() const  Data::inf() const
1290  {  {
1291      double localValue, globalValue;
1292  #if defined DOPROF  #if defined DOPROF
1293    profData->reduction1++;    profData->reduction1++;
1294  #endif  #endif
1295    //    //
1296    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1297    FMin fmin_func;    FMin fmin_func;
1298    return algorithm(fmin_func,numeric_limits<double>::max());    localValue = algorithm(fmin_func,numeric_limits<double>::max());
1299    #ifdef PASO_MPI
1300      MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1301      return globalValue;
1302    #else
1303      return localValue;
1304    #endif
1305  }  }
1306    
1307    /* TODO */
1308    /* global reduction */
1309  Data  Data
1310  Data::maxval() const  Data::maxval() const
1311  {  {
# Line 1175  Data::minval() const Line 1331  Data::minval() const
1331  }  }
1332    
1333  Data  Data
1334  Data::trace() const  Data::swapaxes(const int axis0, const int axis1) const
1335  {  {
1336  #if defined DOPROF       int axis0_tmp,axis1_tmp;
1337    profData->reduction2++;       #if defined DOPROF
1338  #endif       profData->unary++;
1339    Trace trace_func;       #endif
1340    return dp_algorithm(trace_func,0);       DataArrayView::ShapeType s=getDataPointShape();
1341         DataArrayView::ShapeType ev_shape;
1342         // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1343         // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1344         int rank=getDataPointRank();
1345         if (rank<2) {
1346            throw DataException("Error - Data::swapaxes argument must have at least rank 2.");
1347         }
1348         if (axis0<0 || axis0>rank-1) {
1349            throw DataException("Error - Data::swapaxes: axis0 must be between 0 and rank-1=" + rank-1);
1350         }
1351         if (axis1<0 || axis1>rank-1) {
1352             throw DataException("Error - Data::swapaxes: axis1 must be between 0 and rank-1=" + rank-1);
1353         }
1354         if (axis0 == axis1) {
1355             throw DataException("Error - Data::swapaxes: axis indices must be different.");
1356         }
1357         if (axis0 > axis1) {
1358             axis0_tmp=axis1;
1359             axis1_tmp=axis0;
1360         } else {
1361             axis0_tmp=axis0;
1362             axis1_tmp=axis1;
1363         }
1364         for (int i=0; i<rank; i++) {
1365           if (i == axis0_tmp) {
1366              ev_shape.push_back(s[axis1_tmp]);
1367           } else if (i == axis1_tmp) {
1368              ev_shape.push_back(s[axis0_tmp]);
1369           } else {
1370              ev_shape.push_back(s[i]);
1371           }
1372         }
1373         Data ev(0.,ev_shape,getFunctionSpace());
1374         ev.typeMatchRight(*this);
1375         m_data->swapaxes(ev.m_data.get(), axis0_tmp, axis1_tmp);
1376         return ev;
1377    
1378    }
1379    
1380    Data
1381    Data::symmetric() const
1382    {
1383         #if defined DOPROF
1384            profData->unary++;
1385         #endif
1386         // check input
1387         DataArrayView::ShapeType s=getDataPointShape();
1388         if (getDataPointRank()==2) {
1389            if(s[0] != s[1])
1390               throw DataException("Error - Data::symmetric can only be calculated for rank 2 object with equal first and second dimension.");
1391         }
1392         else if (getDataPointRank()==4) {
1393            if(!(s[0] == s[2] && s[1] == s[3]))
1394               throw DataException("Error - Data::symmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1395         }
1396         else {
1397            throw DataException("Error - Data::symmetric can only be calculated for rank 2 or 4 object.");
1398         }
1399         Data ev(0.,getDataPointShape(),getFunctionSpace());
1400         ev.typeMatchRight(*this);
1401         m_data->symmetric(ev.m_data.get());
1402         return ev;
1403    }
1404    
1405    Data
1406    Data::nonsymmetric() const
1407    {
1408         #if defined DOPROF
1409            profData->unary++;
1410         #endif
1411         // check input
1412         DataArrayView::ShapeType s=getDataPointShape();
1413         if (getDataPointRank()==2) {
1414            if(s[0] != s[1])
1415               throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 object with equal first and second dimension.");
1416            DataArrayView::ShapeType ev_shape;
1417            ev_shape.push_back(s[0]);
1418            ev_shape.push_back(s[1]);
1419            Data ev(0.,ev_shape,getFunctionSpace());
1420            ev.typeMatchRight(*this);
1421            m_data->nonsymmetric(ev.m_data.get());
1422            return ev;
1423         }
1424         else if (getDataPointRank()==4) {
1425            if(!(s[0] == s[2] && s[1] == s[3]))
1426               throw DataException("Error - Data::nonsymmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1427            DataArrayView::ShapeType ev_shape;
1428            ev_shape.push_back(s[0]);
1429            ev_shape.push_back(s[1]);
1430            ev_shape.push_back(s[2]);
1431            ev_shape.push_back(s[3]);
1432            Data ev(0.,ev_shape,getFunctionSpace());
1433            ev.typeMatchRight(*this);
1434            m_data->nonsymmetric(ev.m_data.get());
1435            return ev;
1436         }
1437         else {
1438            throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 or 4 object.");
1439         }
1440    }
1441    
1442    Data
1443    Data::trace(int axis_offset) const
1444    {
1445         #if defined DOPROF
1446            profData->unary++;
1447         #endif
1448         DataArrayView::ShapeType s=getDataPointShape();
1449         if (getDataPointRank()==2) {
1450            DataArrayView::ShapeType ev_shape;
1451            Data ev(0.,ev_shape,getFunctionSpace());
1452            ev.typeMatchRight(*this);
1453            m_data->trace(ev.m_data.get(), axis_offset);
1454            return ev;
1455         }
1456         if (getDataPointRank()==3) {
1457            DataArrayView::ShapeType ev_shape;
1458            if (axis_offset==0) {
1459              int s2=s[2];
1460              ev_shape.push_back(s2);
1461            }
1462            else if (axis_offset==1) {
1463              int s0=s[0];
1464              ev_shape.push_back(s0);
1465            }
1466            Data ev(0.,ev_shape,getFunctionSpace());
1467            ev.typeMatchRight(*this);
1468            m_data->trace(ev.m_data.get(), axis_offset);
1469            return ev;
1470         }
1471         if (getDataPointRank()==4) {
1472            DataArrayView::ShapeType ev_shape;
1473            if (axis_offset==0) {
1474              ev_shape.push_back(s[2]);
1475              ev_shape.push_back(s[3]);
1476            }
1477            else if (axis_offset==1) {
1478              ev_shape.push_back(s[0]);
1479              ev_shape.push_back(s[3]);
1480            }
1481        else if (axis_offset==2) {
1482          ev_shape.push_back(s[0]);
1483          ev_shape.push_back(s[1]);
1484        }
1485            Data ev(0.,ev_shape,getFunctionSpace());
1486            ev.typeMatchRight(*this);
1487        m_data->trace(ev.m_data.get(), axis_offset);
1488            return ev;
1489         }
1490         else {
1491            throw DataException("Error - Data::trace can only be calculated for rank 2, 3 or 4 object.");
1492         }
1493    }
1494    
1495    Data
1496    Data::transpose(int axis_offset) const
1497    {
1498         #if defined DOPROF
1499         profData->unary++;
1500         #endif
1501         DataArrayView::ShapeType s=getDataPointShape();
1502         DataArrayView::ShapeType ev_shape;
1503         // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1504         // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1505         int rank=getDataPointRank();
1506         if (axis_offset<0 || axis_offset>rank) {
1507            throw DataException("Error - Data::transpose must have 0 <= axis_offset <= rank=" + rank);
1508         }
1509         for (int i=0; i<rank; i++) {
1510           int index = (axis_offset+i)%rank;
1511           ev_shape.push_back(s[index]); // Append to new shape
1512         }
1513         Data ev(0.,ev_shape,getFunctionSpace());
1514         ev.typeMatchRight(*this);
1515         m_data->transpose(ev.m_data.get(), axis_offset);
1516         return ev;
1517  }  }
1518    
1519  Data  Data
1520  Data::transpose(int axis) const  Data::eigenvalues() const
1521    {
1522         #if defined DOPROF
1523            profData->unary++;
1524         #endif
1525         // check input
1526         DataArrayView::ShapeType s=getDataPointShape();
1527         if (getDataPointRank()!=2)
1528            throw DataException("Error - Data::eigenvalues can only be calculated for rank 2 object.");
1529         if(s[0] != s[1])
1530            throw DataException("Error - Data::eigenvalues can only be calculated for object with equal first and second dimension.");
1531         // create return
1532         DataArrayView::ShapeType ev_shape(1,s[0]);
1533         Data ev(0.,ev_shape,getFunctionSpace());
1534         ev.typeMatchRight(*this);
1535         m_data->eigenvalues(ev.m_data.get());
1536         return ev;
1537    }
1538    
1539    const boost::python::tuple
1540    Data::eigenvalues_and_eigenvectors(const double tol) const
1541  {  {
1542  #if defined DOPROF       #if defined DOPROF
1543    profData->reduction2++;          profData->unary++;
1544  #endif       #endif
1545    // not implemented       DataArrayView::ShapeType s=getDataPointShape();
1546    throw DataException("Error - Data::transpose not implemented yet.");       if (getDataPointRank()!=2)
1547    return Data();          throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for rank 2 object.");
1548         if(s[0] != s[1])
1549            throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for object with equal first and second dimension.");
1550         // create return
1551         DataArrayView::ShapeType ev_shape(1,s[0]);
1552         Data ev(0.,ev_shape,getFunctionSpace());
1553         ev.typeMatchRight(*this);
1554         DataArrayView::ShapeType V_shape(2,s[0]);
1555         Data V(0.,V_shape,getFunctionSpace());
1556         V.typeMatchRight(*this);
1557         m_data->eigenvalues_and_eigenvectors(ev.m_data.get(),V.m_data.get(),tol);
1558         return make_tuple(boost::python::object(ev),boost::python::object(V));
1559  }  }
1560    
1561  const boost::python::tuple  const boost::python::tuple
1562  Data::mindp() const  Data::minGlobalDataPoint() const
1563  {  {
1564    // 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
1565    // 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
1566    // surrounding function    // surrounding function
1567    
   int SampleNo;  
1568    int DataPointNo;    int DataPointNo;
1569      int ProcNo;
1570    calc_mindp(SampleNo,DataPointNo);    calc_minGlobalDataPoint(ProcNo,DataPointNo);
1571      return make_tuple(ProcNo,DataPointNo);
   return make_tuple(SampleNo,DataPointNo);  
1572  }  }
1573    
1574  void  void
1575  Data::calc_mindp(int& SampleNo,  Data::calc_minGlobalDataPoint(int& ProcNo,
1576                   int& DataPointNo) const                          int& DataPointNo) const
1577  {  {
1578    int i,j;    int i,j;
1579    int lowi=0,lowj=0;    int lowi=0,lowj=0;
# Line 1248  Data::calc_mindp(int& SampleNo, Line 1609  Data::calc_mindp(int& SampleNo,
1609      }      }
1610    }    }
1611    
1612    SampleNo = lowi;  #ifdef PASO_MPI
1613    DataPointNo = lowj;      // determine the processor on which the minimum occurs
1614        next = temp.getDataPoint(lowi,lowj)();
1615        int lowProc = 0;
1616        double *globalMins = new double[get_MPISize()+1];
1617        int error = MPI_Gather ( &next, 1, MPI_DOUBLE, globalMins, 1, MPI_DOUBLE, 0, get_MPIComm() );
1618        
1619        if( get_MPIRank()==0 ){
1620            next = globalMins[lowProc];
1621            for( i=1; i<get_MPISize(); i++ )
1622                if( next>globalMins[i] ){
1623                    lowProc = i;
1624                    next = globalMins[i];
1625                }
1626        }
1627        MPI_Bcast( &lowProc, 1, MPI_DOUBLE, 0, get_MPIComm() );
1628    
1629        delete [] globalMins;
1630        ProcNo = lowProc;
1631    #else
1632        ProcNo = 0;
1633    #endif
1634      DataPointNo = lowj + lowi * numDPPSample;
1635  }  }
1636    
1637  void  void
# Line 1273  Data::saveVTK(std::string fileName) cons Line 1655  Data::saveVTK(std::string fileName) cons
1655  Data&  Data&
1656  Data::operator+=(const Data& right)  Data::operator+=(const Data& right)
1657  {  {
1658  #if defined DOPROF    if (isProtected()) {
1659    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1660  #endif    }
1661    binaryOp(right,plus<double>());    binaryOp(right,plus<double>());
1662    return (*this);    return (*this);
1663  }  }
# Line 1283  Data::operator+=(const Data& right) Line 1665  Data::operator+=(const Data& right)
1665  Data&  Data&
1666  Data::operator+=(const boost::python::object& right)  Data::operator+=(const boost::python::object& right)
1667  {  {
1668  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1669    profData->binary++;    binaryOp(tmp,plus<double>());
 #endif  
   binaryOp(right,plus<double>());  
1670    return (*this);    return (*this);
1671  }  }
1672    
1673  Data&  Data&
1674  Data::operator-=(const Data& right)  Data::operator-=(const Data& right)
1675  {  {
1676  #if defined DOPROF    if (isProtected()) {
1677    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1678  #endif    }
1679    binaryOp(right,minus<double>());    binaryOp(right,minus<double>());
1680    return (*this);    return (*this);
1681  }  }
# Line 1303  Data::operator-=(const Data& right) Line 1683  Data::operator-=(const Data& right)
1683  Data&  Data&
1684  Data::operator-=(const boost::python::object& right)  Data::operator-=(const boost::python::object& right)
1685  {  {
1686  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1687    profData->binary++;    binaryOp(tmp,minus<double>());
 #endif  
   binaryOp(right,minus<double>());  
1688    return (*this);    return (*this);
1689  }  }
1690    
1691  Data&  Data&
1692  Data::operator*=(const Data& right)  Data::operator*=(const Data& right)
1693  {  {
1694  #if defined DOPROF    if (isProtected()) {
1695    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1696  #endif    }
1697    binaryOp(right,multiplies<double>());    binaryOp(right,multiplies<double>());
1698    return (*this);    return (*this);
1699  }  }
# Line 1323  Data::operator*=(const Data& right) Line 1701  Data::operator*=(const Data& right)
1701  Data&  Data&
1702  Data::operator*=(const boost::python::object& right)  Data::operator*=(const boost::python::object& right)
1703  {  {
1704  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1705    profData->binary++;    binaryOp(tmp,multiplies<double>());
 #endif  
   binaryOp(right,multiplies<double>());  
1706    return (*this);    return (*this);
1707  }  }
1708    
1709  Data&  Data&
1710  Data::operator/=(const Data& right)  Data::operator/=(const Data& right)
1711  {  {
1712  #if defined DOPROF    if (isProtected()) {
1713    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1714  #endif    }
1715    binaryOp(right,divides<double>());    binaryOp(right,divides<double>());
1716    return (*this);    return (*this);
1717  }  }
# Line 1343  Data::operator/=(const Data& right) Line 1719  Data::operator/=(const Data& right)
1719  Data&  Data&
1720  Data::operator/=(const boost::python::object& right)  Data::operator/=(const boost::python::object& right)
1721  {  {
1722  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1723    profData->binary++;    binaryOp(tmp,divides<double>());
 #endif  
   binaryOp(right,divides<double>());  
1724    return (*this);    return (*this);
1725  }  }
1726    
1727  Data  Data
1728    Data::rpowO(const boost::python::object& left) const
1729    {
1730      Data left_d(left,*this);
1731      return left_d.powD(*this);
1732    }
1733    
1734    Data
1735  Data::powO(const boost::python::object& right) const  Data::powO(const boost::python::object& right) const
1736  {  {
1737  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1738    profData->binary++;    return powD(tmp);
 #endif  
   Data result;  
   result.copy(*this);  
   result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   return result;  
1739  }  }
1740    
1741  Data  Data
1742  Data::powD(const Data& right) const  Data::powD(const Data& right) const
1743  {  {
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1744    Data result;    Data result;
1745    result.copy(*this);    if (getDataPointRank()<right.getDataPointRank()) {
1746    result.binaryOp(right,(Data::BinaryDFunPtr)::pow);       result.copy(right);
1747         result.binaryOp(*this,escript::rpow);
1748      } else {
1749         result.copy(*this);
1750         result.binaryOp(right,(Data::BinaryDFunPtr)::pow);
1751      }
1752    return result;    return result;
1753  }  }
1754    
1755    
1756  //  //
1757  // NOTE: It is essential to specify the namespace this operator belongs to  // NOTE: It is essential to specify the namespace this operator belongs to
1758  Data  Data
# Line 1382  escript::operator+(const Data& left, con Line 1761  escript::operator+(const Data& left, con
1761    Data result;    Data result;
1762    //    //
1763    // perform a deep copy    // perform a deep copy
1764    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1765    result+=right;       result.copy(right);
1766         result+=left;
1767      } else {
1768         result.copy(left);
1769         result+=right;
1770      }
1771    return result;    return result;
1772  }  }
1773    
# Line 1395  escript::operator-(const Data& left, con Line 1779  escript::operator-(const Data& left, con
1779    Data result;    Data result;
1780    //    //
1781    // perform a deep copy    // perform a deep copy
1782    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1783    result-=right;       result=right.neg();
1784         result+=left;
1785      } else {
1786         result.copy(left);
1787         result-=right;
1788      }
1789    return result;    return result;
1790  }  }
1791    
# Line 1408  escript::operator*(const Data& left, con Line 1797  escript::operator*(const Data& left, con
1797    Data result;    Data result;
1798    //    //
1799    // perform a deep copy    // perform a deep copy
1800    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1801    result*=right;       result.copy(right);
1802         result*=left;
1803      } else {
1804         result.copy(left);
1805         result*=right;
1806      }
1807    return result;    return result;
1808  }  }
1809    
# Line 1421  escript::operator/(const Data& left, con Line 1815  escript::operator/(const Data& left, con
1815    Data result;    Data result;
1816    //    //
1817    // perform a deep copy    // perform a deep copy
1818    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1819    result/=right;       result=right.oneOver();
1820         result*=left;
1821      } else {
1822         result.copy(left);
1823         result/=right;
1824      }
1825    return result;    return result;
1826  }  }
1827    
# Line 1431  escript::operator/(const Data& left, con Line 1830  escript::operator/(const Data& left, con
1830  Data  Data
1831  escript::operator+(const Data& left, const boost::python::object& right)  escript::operator+(const Data& left, const boost::python::object& right)
1832  {  {
1833    //    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;  
1834  }  }
1835    
1836  //  //
# Line 1447  escript::operator+(const Data& left, con Line 1838  escript::operator+(const Data& left, con
1838  Data  Data
1839  escript::operator-(const Data& left, const boost::python::object& right)  escript::operator-(const Data& left, const boost::python::object& right)
1840  {  {
1841    //    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;  
1842  }  }
1843    
1844  //  //
# Line 1463  escript::operator-(const Data& left, con Line 1846  escript::operator-(const Data& left, con
1846  Data  Data
1847  escript::operator*(const Data& left, const boost::python::object& right)  escript::operator*(const Data& left, const boost::python::object& right)
1848  {  {
1849    //    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;  
1850  }  }
1851    
1852  //  //
# Line 1479  escript::operator*(const Data& left, con Line 1854  escript::operator*(const Data& left, con
1854  Data  Data
1855  escript::operator/(const Data& left, const boost::python::object& right)  escript::operator/(const Data& left, const boost::python::object& right)
1856  {  {
1857    //    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;  
1858  }  }
1859    
1860  //  //
# Line 1495  escript::operator/(const Data& left, con Line 1862  escript::operator/(const Data& left, con
1862  Data  Data
1863  escript::operator+(const boost::python::object& left, const Data& right)  escript::operator+(const boost::python::object& left, const Data& right)
1864  {  {
1865    //    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;  
1866  }  }
1867    
1868  //  //
# Line 1508  escript::operator+(const boost::python:: Line 1870  escript::operator+(const boost::python::
1870  Data  Data
1871  escript::operator-(const boost::python::object& left, const Data& right)  escript::operator-(const boost::python::object& left, const Data& right)
1872  {  {
1873    //    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;  
1874  }  }
1875    
1876  //  //
# Line 1521  escript::operator-(const boost::python:: Line 1878  escript::operator-(const boost::python::
1878  Data  Data
1879  escript::operator*(const boost::python::object& left, const Data& right)  escript::operator*(const boost::python::object& left, const Data& right)
1880  {  {
1881    //    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;  
1882  }  }
1883    
1884  //  //
# Line 1534  escript::operator*(const boost::python:: Line 1886  escript::operator*(const boost::python::
1886  Data  Data
1887  escript::operator/(const boost::python::object& left, const Data& right)  escript::operator/(const boost::python::object& left, const Data& right)
1888  {  {
1889    //    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;  
1890  }  }
1891    
1892  //  //
# Line 1587  escript::operator/(const boost::python:: Line 1934  escript::operator/(const boost::python::
1934  //  return ret;  //  return ret;
1935  //}  //}
1936    
1937    /* TODO */
1938    /* global reduction */
1939  Data  Data
1940  Data::getItem(const boost::python::object& key) const  Data::getItem(const boost::python::object& key) const
1941  {  {
# Line 1601  Data::getItem(const boost::python::objec Line 1950  Data::getItem(const boost::python::objec
1950    return getSlice(slice_region);    return getSlice(slice_region);
1951  }  }
1952    
1953    /* TODO */
1954    /* global reduction */
1955  Data  Data
1956  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataArrayView::RegionType& region) const
1957  {  {
# Line 1610  Data::getSlice(const DataArrayView::Regi Line 1961  Data::getSlice(const DataArrayView::Regi
1961    return Data(*this,region);    return Data(*this,region);
1962  }  }
1963    
1964    /* TODO */
1965    /* global reduction */
1966  void  void
1967  Data::setItemO(const boost::python::object& key,  Data::setItemO(const boost::python::object& key,
1968                 const boost::python::object& value)                 const boost::python::object& value)
# Line 1618  Data::setItemO(const boost::python::obje Line 1971  Data::setItemO(const boost::python::obje
1971    setItemD(key,tempData);    setItemD(key,tempData);
1972  }  }
1973    
1974    /* TODO */
1975    /* global reduction */
1976  void  void
1977  Data::setItemD(const boost::python::object& key,  Data::setItemD(const boost::python::object& key,
1978                 const Data& value)                 const Data& value)
# Line 1635  Data::setItemD(const boost::python::obje Line 1990  Data::setItemD(const boost::python::obje
1990    }    }
1991  }  }
1992    
1993    /* TODO */
1994    /* global reduction */
1995  void  void
1996  Data::setSlice(const Data& value,  Data::setSlice(const Data& value,
1997                 const DataArrayView::RegionType& region)                 const DataArrayView::RegionType& region)
1998  {  {
1999      if (isProtected()) {
2000            throw DataException("Error - attempt to update protected Data object.");
2001      }
2002  #if defined DOPROF  #if defined DOPROF
2003    profData->slicing++;    profData->slicing++;
2004  #endif  #endif
# Line 1676  Data::typeMatchRight(const Data& right) Line 2036  Data::typeMatchRight(const Data& right)
2036    }    }
2037  }  }
2038    
2039    /* TODO */
2040    /* global reduction */
2041  void  void
2042  Data::setTaggedValue(int tagKey,  Data::setTaggedValue(int tagKey,
2043                       const boost::python::object& value)                       const boost::python::object& value)
2044  {  {
2045      if (isProtected()) {
2046            throw DataException("Error - attempt to update protected Data object.");
2047      }
2048    //    //
2049    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2050    tag();    tag();
# Line 1697  Data::setTaggedValue(int tagKey, Line 2062  Data::setTaggedValue(int tagKey,
2062    m_data->setTaggedValue(tagKey,valueDataArray.getView());    m_data->setTaggedValue(tagKey,valueDataArray.getView());
2063  }  }
2064    
2065    /* TODO */
2066    /* global reduction */
2067  void  void
2068  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
2069                              const DataArrayView& value)                              const DataArrayView& value)
2070  {  {
2071      if (isProtected()) {
2072            throw DataException("Error - attempt to update protected Data object.");
2073      }
2074    //    //
2075    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2076    tag();    tag();
# Line 1714  Data::setTaggedValueFromCPP(int tagKey, Line 2084  Data::setTaggedValueFromCPP(int tagKey,
2084    m_data->setTaggedValue(tagKey,value);    m_data->setTaggedValue(tagKey,value);
2085  }  }
2086    
2087    /* TODO */
2088    /* global reduction */
2089  int  int
2090  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
2091  {  {
# Line 1721  Data::getTagNumber(int dpno) Line 2093  Data::getTagNumber(int dpno)
2093  }  }
2094    
2095  void  void
 Data::setRefValue(int ref,  
                   const boost::python::numeric::array& value)  
 {  
   //  
   // Construct DataArray from boost::python::object input value  
   DataArray valueDataArray(value);  
   
   //  
   // Call DataAbstract::setRefValue  
   m_data->setRefValue(ref,valueDataArray);  
 }  
   
 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);  
           }  
         }  
       }  
     }  
   }  
   
 }  
   
 void  
2096  Data::archiveData(const std::string fileName)  Data::archiveData(const std::string fileName)
2097  {  {
2098    cout << "Archiving Data object to: " << fileName << endl;    cout << "Archiving Data object to: " << fileName << endl;
# Line 1836  Data::archiveData(const std::string file Line 2131  Data::archiveData(const std::string file
2131    int dataPointSize = getDataPointSize();    int dataPointSize = getDataPointSize();
2132    int dataLength = getLength();    int dataLength = getLength();
2133    DataArrayView::ShapeType dataPointShape = getDataPointShape();    DataArrayView::ShapeType dataPointShape = getDataPointShape();
2134    int referenceNumbers[noSamples];    vector<int> referenceNumbers(noSamples);
2135    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2136      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceNoFromSampleNo(sampleNo);      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceIDOfSample(sampleNo);
2137    }    }
2138    int tagNumbers[noSamples];    vector<int> tagNumbers(noSamples);
2139    if (isTagged()) {    if (isTagged()) {
2140      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2141        tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);        tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);
# Line 1994  Data::extractData(const std::string file Line 2289  Data::extractData(const std::string file
2289        dataPointShape.push_back(flatShape[dim]);        dataPointShape.push_back(flatShape[dim]);
2290      }      }
2291    }    }
2292    int referenceNumbers[noSamples];    vector<int> referenceNumbers(noSamples);
2293    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2294      archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));      archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));
2295    }    }
2296    int tagNumbers[noSamples];    vector<int> tagNumbers(noSamples);
2297    if (dataType==2) {    if (dataType==2) {
2298      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2299        archiveFile.read(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));        archiveFile.read(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));
# Line 2047  Data::extractData(const std::string file Line 2342  Data::extractData(const std::string file
2342      throw DataException("extractData Error: incompatible FunctionSpace");      throw DataException("extractData Error: incompatible FunctionSpace");
2343    }    }
2344    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2345      if (referenceNumbers[sampleNo] != fspace.getReferenceNoFromSampleNo(sampleNo)) {      if (referenceNumbers[sampleNo] != fspace.getReferenceIDOfSample(sampleNo)) {
2346        throw DataException("extractData Error: incompatible FunctionSpace");        throw DataException("extractData Error: incompatible FunctionSpace");
2347      }      }
2348    }    }
# Line 2137  ostream& escript::operator<<(ostream& o, Line 2432  ostream& escript::operator<<(ostream& o,
2432    o << data.toString();    o << data.toString();
2433    return o;    return o;
2434  }  }
2435    
2436    Data
2437    escript::C_GeneralTensorProduct(Data& arg_0,
2438                         Data& arg_1,
2439                         int axis_offset,
2440                         int transpose)
2441    {
2442      // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2443      // SM is the product of the last axis_offset entries in arg_0.getShape().
2444    
2445      #if defined DOPROF
2446        // profData->binary++;
2447      #endif
2448    
2449      // Interpolate if necessary and find an appropriate function space
2450      Data arg_0_Z, arg_1_Z;
2451      if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2452        if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2453          arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2454          arg_1_Z = Data(arg_1);
2455        }
2456        else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2457          arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2458          arg_0_Z =Data(arg_0);
2459        }
2460        else {
2461          throw DataException("Error - C_GeneralTensorProduct: arguments have incompatible function spaces.");
2462        }
2463      } else {
2464          arg_0_Z = Data(arg_0);
2465          arg_1_Z = Data(arg_1);
2466      }
2467      // Get rank and shape of inputs
2468      int rank0 = arg_0_Z.getDataPointRank();
2469      int rank1 = arg_1_Z.getDataPointRank();
2470      DataArrayView::ShapeType shape0 = arg_0_Z.getDataPointShape();
2471      DataArrayView::ShapeType shape1 = arg_1_Z.getDataPointShape();
2472    
2473      // Prepare for the loops of the product and verify compatibility of shapes
2474      int start0=0, start1=0;
2475      if (transpose == 0)       {}
2476      else if (transpose == 1)  { start0 = axis_offset; }
2477      else if (transpose == 2)  { start1 = rank1-axis_offset; }
2478      else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2479    
2480      // Adjust the shapes for transpose
2481      DataArrayView::ShapeType tmpShape0;
2482      DataArrayView::ShapeType tmpShape1;
2483      for (int i=0; i<rank0; i++)   { tmpShape0.push_back( shape0[(i+start0)%rank0] ); }
2484      for (int i=0; i<rank1; i++)   { tmpShape1.push_back( shape1[(i+start1)%rank1] ); }
2485    
2486    #if 0
2487      // For debugging: show shape after transpose
2488      char tmp[100];
2489      std::string shapeStr;
2490      shapeStr = "(";
2491      for (int i=0; i<rank0; i++)   { sprintf(tmp, "%d,", tmpShape0[i]); shapeStr += tmp; }
2492      shapeStr += ")";
2493      cout << "C_GeneralTensorProduct: Shape of arg0 is " << shapeStr << endl;
2494      shapeStr = "(";
2495      for (int i=0; i<rank1; i++)   { sprintf(tmp, "%d,", tmpShape1[i]); shapeStr += tmp; }
2496      shapeStr += ")";
2497      cout << "C_GeneralTensorProduct: Shape of arg1 is " << shapeStr << endl;
2498    #endif
2499    
2500      // Prepare for the loops of the product
2501      int SL=1, SM=1, SR=1;
2502      for (int i=0; i<rank0-axis_offset; i++)   {
2503        SL *= tmpShape0[i];
2504      }
2505      for (int i=rank0-axis_offset; i<rank0; i++)   {
2506        if (tmpShape0[i] != tmpShape1[i-(rank0-axis_offset)]) {
2507          throw DataException("C_GeneralTensorProduct: Error - incompatible shapes");
2508        }
2509        SM *= tmpShape0[i];
2510      }
2511      for (int i=axis_offset; i<rank1; i++)     {
2512        SR *= tmpShape1[i];
2513      }
2514    
2515      // Define the shape of the output
2516      DataArrayView::ShapeType shape2;
2517      for (int i=0; i<rank0-axis_offset; i++) { shape2.push_back(tmpShape0[i]); } // First part of arg_0_Z
2518      for (int i=axis_offset; i<rank1; i++)   { shape2.push_back(tmpShape1[i]); } // Last part of arg_1_Z
2519    
2520      // Declare output Data object
2521      Data res;
2522    
2523      if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2524        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2525        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[0]);
2526        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[0]);
2527        double *ptr_2 = &((res.getPointDataView().getData())[0]);
2528        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2529      }
2530      else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2531    
2532        // Prepare the DataConstant input
2533        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2534        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2535    
2536        // Borrow DataTagged input from Data object
2537        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2538        if (tmp_1==0) { throw DataException("GTP_1 Programming error - casting to DataTagged."); }
2539    
2540        // Prepare a DataTagged output 2
2541        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataTagged output
2542        res.tag();
2543        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2544        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2545    
2546        // Prepare offset into DataConstant
2547        int offset_0 = tmp_0->getPointOffset(0,0);
2548        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2549        // Get the views
2550        DataArrayView view_1 = tmp_1->getDefaultValue();
2551        DataArrayView view_2 = tmp_2->getDefaultValue();
2552        // Get the pointers to the actual data
2553        double *ptr_1 = &((view_1.getData())[0]);
2554        double *ptr_2 = &((view_2.getData())[0]);
2555        // Compute an MVP for the default
2556        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2557        // Compute an MVP for each tag
2558        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2559        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2560        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2561          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2562          DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2563          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2564          double *ptr_1 = &view_1.getData(0);
2565          double *ptr_2 = &view_2.getData(0);
2566          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2567        }
2568    
2569      }
2570      else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2571    
2572        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2573        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2574        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2575        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2576        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2577        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2578        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2579        int sampleNo_1,dataPointNo_1;
2580        int numSamples_1 = arg_1_Z.getNumSamples();
2581        int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2582        int offset_0 = tmp_0->getPointOffset(0,0);
2583        #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2584        for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2585          for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2586            int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2587            int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2588            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2589            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2590            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2591            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2592          }
2593        }
2594    
2595      }
2596      else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2597    
2598        // Borrow DataTagged input from Data object
2599        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2600        if (tmp_0==0) { throw DataException("GTP_0 Programming error - casting to DataTagged."); }
2601    
2602        // Prepare the DataConstant input
2603        DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2604        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2605    
2606        // Prepare a DataTagged output 2
2607        res = Data(0.0, shape2, arg_0_Z.getFunctionSpace());    // DataTagged output
2608        res.tag();
2609        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2610        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2611    
2612        // Prepare offset into DataConstant
2613        int offset_1 = tmp_1->getPointOffset(0,0);
2614        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2615        // Get the views
2616        DataArrayView view_0 = tmp_0->getDefaultValue();
2617        DataArrayView view_2 = tmp_2->getDefaultValue();
2618        // Get the pointers to the actual data
2619        double *ptr_0 = &((view_0.getData())[0]);
2620        double *ptr_2 = &((view_2.getData())[0]);
2621        // Compute an MVP for the default
2622        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2623        // Compute an MVP for each tag
2624        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2625        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2626        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2627          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2628          DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2629          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2630          double *ptr_0 = &view_0.getData(0);
2631          double *ptr_2 = &view_2.getData(0);
2632          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2633        }
2634    
2635      }
2636      else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2637    
2638        // Borrow DataTagged input from Data object
2639        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2640        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2641    
2642        // Borrow DataTagged input from Data object
2643        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2644        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2645    
2646        // Prepare a DataTagged output 2
2647        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());
2648        res.tag();  // DataTagged output
2649        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2650        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2651    
2652        // Get the views
2653        DataArrayView view_0 = tmp_0->getDefaultValue();
2654        DataArrayView view_1 = tmp_1->getDefaultValue();
2655        DataArrayView view_2 = tmp_2->getDefaultValue();
2656        // Get the pointers to the actual data
2657        double *ptr_0 = &((view_0.getData())[0]);
2658        double *ptr_1 = &((view_1.getData())[0]);
2659        double *ptr_2 = &((view_2.getData())[0]);
2660        // Compute an MVP for the default
2661        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2662        // Merge the tags
2663        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2664        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2665        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2666        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2667          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue()); // use tmp_2 to get correct shape
2668        }
2669        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2670          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2671        }
2672        // Compute an MVP for each tag
2673        const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2674        for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2675          DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2676          DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2677          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2678          double *ptr_0 = &view_0.getData(0);
2679          double *ptr_1 = &view_1.getData(0);
2680          double *ptr_2 = &view_2.getData(0);
2681          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2682        }
2683    
2684      }
2685      else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2686    
2687        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2688        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2689        DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2690        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2691        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2692        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2693        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2694        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2695        int sampleNo_0,dataPointNo_0;
2696        int numSamples_0 = arg_0_Z.getNumSamples();
2697        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2698        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2699        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2700          int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2701          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2702          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2703            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2704            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2705            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2706            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2707            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2708          }
2709        }
2710    
2711      }
2712      else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2713    
2714        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2715        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2716        DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2717        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2718        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2719        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2720        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2721        int sampleNo_0,dataPointNo_0;
2722        int numSamples_0 = arg_0_Z.getNumSamples();
2723        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2724        int offset_1 = tmp_1->getPointOffset(0,0);
2725        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2726        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2727          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2728            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2729            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2730            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2731            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2732            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2733            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2734          }
2735        }
2736    
2737    
2738      }
2739      else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2740    
2741        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2742        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2743        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2744        DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2745        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2746        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2747        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2748        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2749        int sampleNo_0,dataPointNo_0;
2750        int numSamples_0 = arg_0_Z.getNumSamples();
2751        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2752        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2753        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2754          int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2755          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2756          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2757            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2758            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2759            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2760            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2761            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2762          }
2763        }
2764    
2765      }
2766      else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2767    
2768        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2769        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2770        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2771        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2772        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2773        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2774        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2775        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2776        int sampleNo_0,dataPointNo_0;
2777        int numSamples_0 = arg_0_Z.getNumSamples();
2778        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2779        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2780        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2781          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2782            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2783            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2784            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2785            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2786            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2787            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2788            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2789          }
2790        }
2791    
2792      }
2793      else {
2794        throw DataException("Error - C_GeneralTensorProduct: unknown combination of inputs");
2795      }
2796    
2797      return res;
2798    }
2799    
2800    DataAbstract*
2801    Data::borrowData() const
2802    {
2803      return m_data.get();
2804    }
2805    
2806    /* Member functions specific to the MPI implementation */
2807    
2808    void
2809    Data::print()
2810    {
2811      int i,j;
2812      
2813      printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );
2814      for( i=0; i<getNumSamples(); i++ )
2815      {
2816        printf( "[%6d]", i );
2817        for( j=0; j<getNumDataPointsPerSample(); j++ )
2818          printf( "\t%10.7g", (getSampleData(i))[j] );
2819        printf( "\n" );
2820      }
2821    }
2822    
2823    int
2824    Data::get_MPISize() const
2825    {
2826        int error, size;
2827    #ifdef PASO_MPI
2828        error = MPI_Comm_size( get_MPIComm(), &size );
2829    #else
2830        size = 1;
2831    #endif
2832        return size;
2833    }
2834    
2835    int
2836    Data::get_MPIRank() const
2837    {
2838        int error, rank;
2839    #ifdef PASO_MPI
2840        error = MPI_Comm_rank( get_MPIComm(), &rank );
2841    #else
2842        rank = 0;
2843    #endif
2844        return rank;
2845    }
2846    
2847    MPI_Comm
2848    Data::get_MPIComm() const
2849    {
2850    #ifdef PASO_MPI
2851        return MPI_COMM_WORLD;
2852    #else
2853        return -1;
2854    #endif
2855    }
2856    

Legend:
Removed from v.474  
changed lines
  Added in v.964

  ViewVC Help
Powered by ViewVC 1.1.26