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

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

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

revision 1827 by ksteube, Thu Oct 2 04:28:07 2008 UTC revision 3981 by jfenwick, Fri Sep 21 02:47:54 2012 UTC
# Line 1  Line 1 
1    
2  /*******************************************************  /*****************************************************************************
3  *  *
4  * Copyright (c) 2003-2008 by University of Queensland  * Copyright (c) 2003-2012 by University of Queensland
5  * Earth Systems Science Computational Center (ESSCC)  * http://www.uq.edu.au
 * http://www.uq.edu.au/esscc  
6  *  *
7  * Primary Business: Queensland, Australia  * Primary Business: Queensland, Australia
8  * Licensed under the Open Software License version 3.0  * Licensed under the Open Software License version 3.0
9  * http://www.opensource.org/licenses/osl-3.0.php  * http://www.opensource.org/licenses/osl-3.0.php
10  *  *
11  *******************************************************/  * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12    * Development since 2012 by School of Earth Sciences
13    *
14    *****************************************************************************/
15    
16    
17  #include "Data.h"  #include "Data.h"
# Line 17  Line 19 
19  #include "DataException.h"  #include "DataException.h"
20  #include "DataConstant.h"  #include "DataConstant.h"
21  #include "DataTagged.h"  #include "DataTagged.h"
22    #include <limits>
23    
24    #include "esysUtils/Esys_MPI.h"
25    
26  #ifdef USE_NETCDF  #ifdef USE_NETCDF
27  #include <netcdfcpp.h>  #include <netcdfcpp.h>
28  #endif  #endif
 #ifdef PASO_MPI  
 #include <mpi.h>  
 #endif  
29    
30  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
31  #include "DataMaths.h"  #include "DataMaths.h"
32    
33    //#define MKLRANDOM
34    
35    #ifdef MKLRANDOM
36    #include <mkl_vsl.h>
37    #else
38    #include <boost/random/mersenne_twister.hpp>
39    #endif
40    
41  using namespace std;  using namespace std;
42  using namespace boost::python;  using namespace boost::python;
43  using namespace boost;  using namespace boost;
44  using namespace escript::DataTypes;  using namespace escript::DataTypes;
45    
46    
47    // #define CHECK_FOR_EX_WRITE if (!checkNoSharing()) {throw DataException("Attempt to modify shared object");}
48    
49    #define CHECK_FOR_EX_WRITE if (!checkNoSharing()) {std::ostringstream ss; ss << " Attempt to modify shared object. line " << __LINE__ << " of " << __FILE__; *((int*)0)=17;throw DataException(ss.str());}
50    
51  namespace escript {  namespace escript {
52    
53  DataExpanded::DataExpanded(const boost::python::numeric::array& value,  DataExpanded::DataExpanded(const WrappedArray& value,
54                             const FunctionSpace& what)                             const FunctionSpace& what)
55    : DataAbstract(what,DataTypes::shapeFromNumArray(value))    : parent(what,value.getShape())
56  {  {
57    //    //
58    // initialise the data array for this object    // initialise the data array for this object
# Line 47  DataExpanded::DataExpanded(const boost:: Line 63  DataExpanded::DataExpanded(const boost::
63  }  }
64    
65  DataExpanded::DataExpanded(const DataExpanded& other)  DataExpanded::DataExpanded(const DataExpanded& other)
66    : DataAbstract(other.getFunctionSpace(), other.getShape()),    : parent(other.getFunctionSpace(), other.getShape()),
67    m_data(other.m_data)    m_data(other.m_data)
68  {  {
69  }  }
70    
71  DataExpanded::DataExpanded(const DataConstant& other)  DataExpanded::DataExpanded(const DataConstant& other)
72    : DataAbstract(other.getFunctionSpace(), other.getShape())    : parent(other.getFunctionSpace(), other.getShape())
73  {  {
74    //    //
75    // initialise the data array for this object    // initialise the data array for this object
# Line 64  DataExpanded::DataExpanded(const DataCon Line 80  DataExpanded::DataExpanded(const DataCon
80  }  }
81    
82  DataExpanded::DataExpanded(const DataTagged& other)  DataExpanded::DataExpanded(const DataTagged& other)
83    : DataAbstract(other.getFunctionSpace(), other.getShape())    : parent(other.getFunctionSpace(), other.getShape())
84  {  {
85    //    //
86    // initialise the data array for this object    // initialise the data array for this object
# Line 79  DataExpanded::DataExpanded(const DataTag Line 95  DataExpanded::DataExpanded(const DataTag
95    for (i=0;i<numRows;i++) {    for (i=0;i<numRows;i++) {
96      for (j=0;j<numCols;j++) {      for (j=0;j<numCols;j++) {
97        try {        try {
98             DataTypes::copyPoint(getVector(), getPointOffset(i,j), getNoValues(),             DataTypes::copyPoint(getVectorRW(), getPointOffset(i,j), getNoValues(),
99                                  other.getVector(),                                  other.getVectorRO(),
100                                  other.getPointOffset(i,j));                                  other.getPointOffset(i,j));
101        }        }
102        catch (std::exception& e) {        catch (std::exception& e) {
# Line 92  DataExpanded::DataExpanded(const DataTag Line 108  DataExpanded::DataExpanded(const DataTag
108    
109  DataExpanded::DataExpanded(const DataExpanded& other,  DataExpanded::DataExpanded(const DataExpanded& other,
110                             const DataTypes::RegionType& region)                             const DataTypes::RegionType& region)
111    : DataAbstract(other.getFunctionSpace(),DataTypes::getResultSliceShape(region))    : parent(other.getFunctionSpace(),DataTypes::getResultSliceShape(region))
112  {  {
113    //    //
114    // get the shape of the slice    // get the shape of the slice
# Line 110  DataExpanded::DataExpanded(const DataExp Line 126  DataExpanded::DataExpanded(const DataExp
126    for (i=0;i<numRows;i++) {    for (i=0;i<numRows;i++) {
127      for (j=0;j<numCols;j++) {      for (j=0;j<numCols;j++) {
128        try {        try {
129  //         getPointDataView().copySlice(getPointOffset(i,j),          DataTypes::copySlice(getVectorRW(),getShape(),getPointOffset(i,j),
130  //                                      other.getPointDataView(),                                       other.getVectorRO(),
 //                                      other.getPointOffset(i,j),  
 //                                      region_loop_range);  
         DataTypes::copySlice(getVector(),getShape(),getPointOffset(i,j),  
                                      other.getVector(),  
131                       other.getShape(),                       other.getShape(),
132                                       other.getPointOffset(i,j),                                       other.getPointOffset(i,j),
133                                       region_loop_range);                                       region_loop_range);
# Line 127  DataExpanded::DataExpanded(const DataExp Line 139  DataExpanded::DataExpanded(const DataExp
139    }    }
140  }  }
141    
 // DataExpanded::DataExpanded(const DataArrayView& value,  
 //                            const FunctionSpace& what)  
 //   : DataAbstract(what)  
 // {  
 //   //  
 //   // get the shape of the given data value  
 //   DataTypes::ShapeType tempShape=value.getShape();  
 //   //  
 //   // initialise this Data object to the shape of the given data value  
 //   initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());  
 //   //  
 //   // copy the given value to every data point  
 //   copy(value);  
 // }  
   
142  DataExpanded::DataExpanded(const FunctionSpace& what,  DataExpanded::DataExpanded(const FunctionSpace& what,
143                             const DataTypes::ShapeType &shape,                             const DataTypes::ShapeType &shape,
144                             const DataTypes::ValueType &data)                             const DataTypes::ValueType &data)
145    : DataAbstract(what,shape)    : parent(what,shape)
146  {  {
147    EsysAssert(data.size()%getNoValues()==0,    EsysAssert(data.size()%getNoValues()==0,
148                   "DataExpanded Constructor - size of supplied data is not a multiple of shape size.");                   "DataExpanded Constructor - size of supplied data is not a multiple of shape size.");
# Line 159  DataExpanded::DataExpanded(const Functio Line 156  DataExpanded::DataExpanded(const Functio
156       // now we copy this value to all elements       // now we copy this value to all elements
157       for (int i=0;i<getLength();)       for (int i=0;i<getLength();)
158       {       {
159      for (int j=0;j<getNoValues();++j,++i)      for (unsigned int j=0;j<getNoValues();++j,++i)
160      {      {
161          vec[i]=data[j];          vec[i]=data[j];
162      }      }
# Line 175  DataExpanded::DataExpanded(const Functio Line 172  DataExpanded::DataExpanded(const Functio
172    
173  }  }
174    
175    DataExpanded::DataExpanded(const FunctionSpace& what,
176                               const DataTypes::ShapeType &shape,
177                               const double v)
178      : parent(what,shape)
179    {
180         ValueType& vec=m_data.getData();
181         //
182         // create the view of the data
183         initialise(what.getNumSamples(),what.getNumDPPSample());
184         // now we copy this value to all elements
185         const int L=getLength();
186         int i;
187         #pragma omp parallel for schedule(static) private(i)
188         for (i=0;i<L;++i)
189         {
190            vec[i]=v;
191         }
192    }
193    
194    
195    
196  DataExpanded::~DataExpanded()  DataExpanded::~DataExpanded()
197  {  {
198  }  }
# Line 200  DataExpanded::setSlice(const DataAbstrac Line 218  DataExpanded::setSlice(const DataAbstrac
218    if (tempDataExp==0) {    if (tempDataExp==0) {
219      throw DataException("Programming error - casting to DataExpanded.");      throw DataException("Programming error - casting to DataExpanded.");
220    }    }
221      CHECK_FOR_EX_WRITE
222    //    //
223    // get shape of slice    // get shape of slice
224    DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));    DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));
# Line 218  DataExpanded::setSlice(const DataAbstrac Line 237  DataExpanded::setSlice(const DataAbstrac
237    DataTypes::ValueType::size_type numRows=m_data.getNumRows();    DataTypes::ValueType::size_type numRows=m_data.getNumRows();
238    DataTypes::ValueType::size_type numCols=m_data.getNumCols();    DataTypes::ValueType::size_type numCols=m_data.getNumCols();
239    int i, j;    int i, j;
240    ValueType& vec=getVector();    ValueType& vec=getVectorRW();
241    const ShapeType& mshape=getShape();    const ShapeType& mshape=getShape();
242    const ValueType& tVec=tempDataExp->getVector();    const ValueType& tVec=tempDataExp->getVectorRO();
243    const ShapeType& tShape=tempDataExp->getShape();    const ShapeType& tShape=tempDataExp->getShape();
244    #pragma omp parallel for private(i,j) schedule(static)    #pragma omp parallel for private(i,j) schedule(static)
245    for (i=0;i<numRows;i++) {    for (i=0;i<numRows;i++) {
246      for (j=0;j<numCols;j++) {      for (j=0;j<numCols;j++) {
 /*      getPointDataView().copySliceFrom(getPointOffset(i,j),  
                                        tempDataExp->getPointDataView(),  
                                        tempDataExp->getPointOffset(i,j),  
                                        region_loop_range);*/  
247          DataTypes::copySliceFrom(vec,mshape,getPointOffset(i,j),          DataTypes::copySliceFrom(vec,mshape,getPointOffset(i,j),
248                                         tVec,                                         tVec,
249                         tShape,                         tShape,
# Line 253  DataExpanded::copy(const DataConstant& v Line 268  DataExpanded::copy(const DataConstant& v
268    #pragma omp parallel for private(i,j) schedule(static)    #pragma omp parallel for private(i,j) schedule(static)
269    for (i=0;i<nRows;i++) {    for (i=0;i<nRows;i++) {
270      for (j=0;j<nCols;j++) {      for (j=0;j<nCols;j++) {
271        // NOTE: An exception may be thown from this call if        DataTypes::copyPoint(getVectorRW(), getPointOffset(i,j), getNoValues(), value.getVectorRO(), 0);
       // DOASSERT is on which of course will play  
       // havoc with the omp threads. Run single threaded  
       // if using DOASSERT.  
       //getPointDataView().copy(getPointOffset(i,j),value);  
       DataTypes::copyPoint(getVector(), getPointOffset(i,j), getNoValues(), value.getVector(), 0);  
272      }      }
273    }    }
274  }  }
275    
276    void
277  // void  DataExpanded::copy(const WrappedArray& value)
278  // DataExpanded::copy(const DataArrayView& value)  {
 // {  
 //   //  
 //   // copy a single value to every data point in this object  
 //   int nRows=m_data.getNumRows();  
 //   int nCols=m_data.getNumCols();  
 //   int i,j;  
 //   #pragma omp parallel for private(i,j) schedule(static)  
 //   for (i=0;i<nRows;i++) {  
 //     for (j=0;j<nCols;j++) {  
 //       // NOTE: An exception may be thown from this call if  
 //       // DOASSERT is on which of course will play  
 //       // havoc with the omp threads. Run single threaded  
 //       // if using DOASSERT.  
 //       getPointDataView().copy(getPointOffset(i,j),value);  
 //     }  
 //   }  
 // }  
   
 void  
 DataExpanded::copy(const boost::python::numeric::array& value)  
 {  
   
   // extract the shape of the numarray  
   DataTypes::ShapeType tempShape;  
   for (int i=0; i < value.getrank(); i++) {  
     tempShape.push_back(extract<int>(value.getshape()[i]));  
   }  
   
   // get the space for the data vector  
 //   int len = DataTypes::noValues(tempShape);  
 //   DataVector temp_data(len, 0.0, len);  
 //   DataArrayView temp_dataView(temp_data, tempShape);  
 //   temp_dataView.copy(value);  
   
   //  
279    // check the input shape matches this shape    // check the input shape matches this shape
280    if (!DataTypes::checkShape(getShape(),tempShape)) {    if (!DataTypes::checkShape(getShape(),value.getShape())) {
281      throw DataException(DataTypes::createShapeErrorMessage(      throw DataException(DataTypes::createShapeErrorMessage(
282                          "Error - (DataExpanded) Cannot copy due to shape mismatch.",                          "Error - (DataExpanded) Cannot copy due to shape mismatch.",
283                          tempShape,getShape()));                          value.getShape(),getShape()));
284    }    }
285    //    getVectorRW().copyFromArray(value, getNumDPPSample()*getNumSamples());
   // now copy over the data  
   //copy(temp_dataView);  
   getVector().copyFromNumArray(value);  
286  }  }
287    
288    
# Line 327  DataExpanded::initialise(int noSamples, Line 299  DataExpanded::initialise(int noSamples,
299    m_data.resize(noSamples,noDataPointsPerSample,getNoValues());    m_data.resize(noSamples,noDataPointsPerSample,getNoValues());
300  }  }
301    
302    bool
303    DataExpanded::hasNaN() const
304    {
305        const ValueType& v=m_data.getData();
306        for (ValueType::size_type i=0;i<v.size();++i)
307        {
308            if (nancheck(v[i]))
309            {
310                return true;
311            }
312        }
313        return false;
314    }
315    
316    
317  string  string
318  DataExpanded::toString() const  DataExpanded::toString() const
319  {  {
# Line 339  DataExpanded::toString() const Line 326  DataExpanded::toString() const
326        offset=getPointOffset(i,j);        offset=getPointOffset(i,j);
327        stringstream suffix;        stringstream suffix;
328        suffix << "( id: " << i << ", ref: " << fs.getReferenceIDOfSample(i) << ", pnt: " << j << ")";        suffix << "( id: " << i << ", ref: " << fs.getReferenceIDOfSample(i) << ", pnt: " << j << ")";
329        temp << DataTypes::pointToString(getVector(),getShape(),offset,suffix.str());        temp << DataTypes::pointToString(getVectorRO(),getShape(),offset,suffix.str());
330        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
331          temp << endl;          temp << endl;
332        }        }
# Line 361  DataExpanded::getPointOffset(int sampleN Line 348  DataExpanded::getPointOffset(int sampleN
348  }  }
349    
350  DataTypes::ValueType::size_type  DataTypes::ValueType::size_type
351    DataExpanded::getPointOffset(int sampleNo,
352                                 int dataPointNo)
353    {
354      return m_data.index(sampleNo,dataPointNo);
355    }
356    
357    DataTypes::ValueType::size_type
358  DataExpanded::getLength() const  DataExpanded::getLength() const
359  {  {
360    return m_data.size();    return m_data.size();
# Line 370  DataExpanded::getLength() const Line 364  DataExpanded::getLength() const
364    
365  void  void
366  DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const double value) {  DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const double value) {
367      CHECK_FOR_EX_WRITE
368    //    //
369    // Get the number of samples and data-points per sample.    // Get the number of samples and data-points per sample.
370    int numSamples = getNumSamples();    int numSamples = getNumSamples();
# Line 385  DataExpanded::copyToDataPoint(const int Line 380  DataExpanded::copyToDataPoint(const int
380             throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");             throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
381       }       }
382       ValueType::size_type offset = getPointOffset(sampleNo, dataPointNo);       ValueType::size_type offset = getPointOffset(sampleNo, dataPointNo);
383       ValueType& vec=getVector();       ValueType& vec=getVectorRW();
384       if (dataPointRank==0) {       if (dataPointRank==0) {
385           vec[0]=value;           vec[offset]=value;
386       } else if (dataPointRank==1) {       } else if (dataPointRank==1) {
387          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<dataPointShape[0]; i++) {
388              vec[offset+i]=value;              vec[offset+i]=value;
# Line 419  DataExpanded::copyToDataPoint(const int Line 414  DataExpanded::copyToDataPoint(const int
414       }       }
415    }    }
416  }  }
417    
418  void  void
419  DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const boost::python::numeric::array& value) {  DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const WrappedArray& value) {
420      CHECK_FOR_EX_WRITE
421    //    //
422    // Get the number of samples and data-points per sample.    // Get the number of samples and data-points per sample.
423    int numSamples = getNumSamples();    int numSamples = getNumSamples();
424    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
   int dataPointRank = getRank();  
   const ShapeType& shape = getShape();  
425    //    //
426    // check rank:    // check rank:
427    if (value.getrank()!=dataPointRank)    if (value.getRank()!=getRank())
428         throw DataException("Rank of numarray does not match Data object rank");         throw DataException("Rank of value does not match Data object rank");
429    if (numSamples*numDataPointsPerSample > 0) {    if (numSamples*numDataPointsPerSample > 0) {
430       //TODO: global error handling       //TODO: global error handling
431       if ((sampleNo >= numSamples) || (sampleNo < 0 )) {       if ((sampleNo >= numSamples) || (sampleNo < 0 )) {
# Line 439  DataExpanded::copyToDataPoint(const int Line 434  DataExpanded::copyToDataPoint(const int
434       if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {       if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
435             throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");             throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
436       }       }
437       ValueType& vec=getVector();       ValueType::size_type offset = getPointOffset(sampleNo, dataPointNo);
438       if (dataPointRank==0) {       ValueType& vec=getVectorRW();
439           vec[0]=extract<double>(value[0]);       vec.copyFromArrayToOffset(value,offset,1);
      } else if (dataPointRank==1) {  
         for (int i=0; i<shape[0]; i++) {  
             vec[i]=extract<double>(value[i]);  
         }  
      } else if (dataPointRank==2) {  
         for (int i=0; i<shape[0]; i++) {  
            for (int j=0; j<shape[1]; j++) {  
               vec[getRelIndex(shape,i,j)]=extract<double>(value[i][j]);  
            }  
         }  
      } else if (dataPointRank==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++) {  
                  vec[getRelIndex(shape,i,j,k)]=extract<double>(value[i][j][k]);  
               }  
            }  
         }  
      } else if (dataPointRank==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++) {  
                   vec[getRelIndex(shape,i,j,k,l)]=extract<double>(value[i][j][k][l]);  
                }  
              }  
            }  
          }  
      }  
   }  
 }  
 void  
 DataExpanded::copyAll(const boost::python::numeric::array& value) {  
   //  
   // Get the number of samples and data-points per sample.  
   int numSamples = getNumSamples();  
   int numDataPointsPerSample = getNumDPPSample();  
   int dataPointRank = getRank();  
   const ShapeType& dataPointShape = getShape();  
   //  
   // check rank:  
   if (value.getrank()!=dataPointRank+1)  
        throw DataException("Rank of numarray does not match Data object rank");  
   if (value.getshape()[0]!=numSamples*numDataPointsPerSample)  
        throw DataException("leading dimension of numarray is too small");  
   //  
   ValueType& vec=getVector();  
   int dataPoint = 0;  
   for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {  
     for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {  
       ValueType::size_type offset=getPointOffset(sampleNo, dataPointNo);  
       if (dataPointRank==0) {  
          vec[offset]=extract<double>(value[dataPoint]);  
       } else if (dataPointRank==1) {  
          for (int i=0; i<dataPointShape[0]; i++) {  
             vec[offset+i]=extract<double>(value[dataPoint][i]);  
          }  
       } else if (dataPointRank==2) {  
          for (int i=0; i<dataPointShape[0]; i++) {  
            for (int j=0; j<dataPointShape[1]; j++) {  
          vec[offset+getRelIndex(dataPointShape,i,j)]=extract<double>(value[dataPoint][i][j]);  
            }  
          }  
        } else 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++) {  
          vec[offset+getRelIndex(dataPointShape,i,j,k)]=extract<double>(value[dataPoint][i][j][k]);  
              }  
            }  
          }  
        } else 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++) {  
                  vec[offset+getRelIndex(dataPointShape,i,j,k,l)]=extract<double>(value[dataPoint][i][j][k][l]);  
                }  
              }  
            }  
          }  
       }  
       dataPoint++;  
     }  
440    }    }
441  }  }
442    
443  void  void
444  DataExpanded::symmetric(DataAbstract* ev)  DataExpanded::symmetric(DataAbstract* ev)
445  {  {
# Line 538  DataExpanded::symmetric(DataAbstract* ev Line 450  DataExpanded::symmetric(DataAbstract* ev
450    if (temp_ev==0) {    if (temp_ev==0) {
451      throw DataException("Error - DataExpanded::symmetric: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::symmetric: casting to DataExpanded failed (propably a programming error).");
452    }    }
453    ValueType& vec=getVector();    const ValueType& vec=getVectorRO();
454    const ShapeType& shape=getShape();    const ShapeType& shape=getShape();
455    ValueType& evVec=temp_ev->getVector();    ValueType& evVec=temp_ev->getVectorRW();
456    const ShapeType& evShape=temp_ev->getShape();    const ShapeType& evShape=temp_ev->getShape();
457    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
458    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
# Line 560  DataExpanded::nonsymmetric(DataAbstract* Line 472  DataExpanded::nonsymmetric(DataAbstract*
472    if (temp_ev==0) {    if (temp_ev==0) {
473      throw DataException("Error - DataExpanded::nonsymmetric: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::nonsymmetric: casting to DataExpanded failed (propably a programming error).");
474    }    }
475    ValueType& vec=getVector();    const ValueType& vec=getVectorRO();
476    const ShapeType& shape=getShape();    const ShapeType& shape=getShape();
477    ValueType& evVec=temp_ev->getVector();    ValueType& evVec=temp_ev->getVectorRW();
478    const ShapeType& evShape=temp_ev->getShape();    const ShapeType& evShape=temp_ev->getShape();
479    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
480    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
# Line 582  DataExpanded::trace(DataAbstract* ev, in Line 494  DataExpanded::trace(DataAbstract* ev, in
494    if (temp_ev==0) {    if (temp_ev==0) {
495      throw DataException("Error - DataExpanded::trace: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::trace: casting to DataExpanded failed (propably a programming error).");
496    }    }
497    ValueType& vec=getVector();    const ValueType& vec=getVectorRO();
498    const ShapeType& shape=getShape();    const ShapeType& shape=getShape();
499    ValueType& evVec=temp_ev->getVector();    ValueType& evVec=temp_ev->getVectorRW();
500    const ShapeType& evShape=temp_ev->getShape();    const ShapeType& evShape=temp_ev->getShape();
501    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
502    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
# Line 605  DataExpanded::transpose(DataAbstract* ev Line 517  DataExpanded::transpose(DataAbstract* ev
517    if (temp_ev==0) {    if (temp_ev==0) {
518      throw DataException("Error - DataExpanded::transpose: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::transpose: casting to DataExpanded failed (propably a programming error).");
519    }    }
520    ValueType& vec=getVector();    const ValueType& vec=getVectorRO();
521    const ShapeType& shape=getShape();    const ShapeType& shape=getShape();
522    ValueType& evVec=temp_ev->getVector();    ValueType& evVec=temp_ev->getVectorRW();
523    const ShapeType& evShape=temp_ev->getShape();    const ShapeType& evShape=temp_ev->getShape();
524    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
525    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
# Line 628  DataExpanded::swapaxes(DataAbstract* ev, Line 540  DataExpanded::swapaxes(DataAbstract* ev,
540    if (temp_ev==0) {    if (temp_ev==0) {
541      throw DataException("Error - DataExpanded::swapaxes: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::swapaxes: casting to DataExpanded failed (propably a programming error).");
542    }    }
543    ValueType& vec=getVector();    const ValueType& vec=getVectorRO();
544    const ShapeType& shape=getShape();    const ShapeType& shape=getShape();
545    ValueType& evVec=temp_ev->getVector();    ValueType& evVec=temp_ev->getVectorRW();
546    const ShapeType& evShape=temp_ev->getShape();    const ShapeType& evShape=temp_ev->getShape();
547    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
548    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
# Line 650  DataExpanded::eigenvalues(DataAbstract* Line 562  DataExpanded::eigenvalues(DataAbstract*
562    if (temp_ev==0) {    if (temp_ev==0) {
563      throw DataException("Error - DataExpanded::eigenvalues: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::eigenvalues: casting to DataExpanded failed (propably a programming error).");
564    }    }
565    ValueType& vec=getVector();    const ValueType& vec=getVectorRO();
566    const ShapeType& shape=getShape();    const ShapeType& shape=getShape();
567    ValueType& evVec=temp_ev->getVector();    ValueType& evVec=temp_ev->getVectorRW();
568    const ShapeType& evShape=temp_ev->getShape();    const ShapeType& evShape=temp_ev->getShape();
569    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
570    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
# Line 676  DataExpanded::eigenvalues_and_eigenvecto Line 588  DataExpanded::eigenvalues_and_eigenvecto
588    if (temp_V==0) {    if (temp_V==0) {
589      throw DataException("Error - DataExpanded::eigenvalues_and_eigenvectors: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::eigenvalues_and_eigenvectors: casting to DataExpanded failed (propably a programming error).");
590    }    }
591    ValueType& vec=getVector();    const ValueType& vec=getVectorRO();
592    const ShapeType& shape=getShape();    const ShapeType& shape=getShape();
593    ValueType& evVec=temp_ev->getVector();    ValueType& evVec=temp_ev->getVectorRW();
594    const ShapeType& evShape=temp_ev->getShape();    const ShapeType& evShape=temp_ev->getShape();
595    ValueType& VVec=temp_V->getVector();    ValueType& VVec=temp_V->getVectorRW();
596    const ShapeType& VShape=temp_V->getShape();    const ShapeType& VShape=temp_V->getShape();
597    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
598    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
# Line 692  DataExpanded::eigenvalues_and_eigenvecto Line 604  DataExpanded::eigenvalues_and_eigenvecto
604    }    }
605  }  }
606    
607    
608    int
609    DataExpanded::matrixInverse(DataAbstract* out) const
610    {
611      DataExpanded* temp=dynamic_cast<DataExpanded*>(out);
612      if (temp==0)
613      {
614        throw DataException("Error - DataExpanded::matrixInverse: casting to DataExpanded failed (propably a programming error).");
615      }
616    
617      if (getRank()!=2)
618      {
619        throw DataException("Error - DataExpanded::matrixInverse: input must be rank 2.");
620    
621      }
622      int  sampleNo;
623      const int numdpps=getNumDPPSample();
624      const int numSamples = getNumSamples();
625      const ValueType& vec=m_data.getData();
626      int errcode=0;
627      #pragma omp parallel private(sampleNo)
628      {
629         int errorcode=0;
630         LapackInverseHelper h(getShape()[0]);
631         #pragma omp for schedule(static)
632         for (sampleNo = 0; sampleNo < numSamples; sampleNo++)
633         {
634                // not sure I like all those virtual calls to getPointOffset
635            DataTypes::ValueType::size_type offset=getPointOffset(sampleNo,0);
636            int res=DataMaths::matrix_inverse(vec, getShape(), offset, temp->getVectorRW(), temp->getShape(), offset, numdpps, h);
637        if (res>errorcode)
638        {
639            errorcode=res;
640            #pragma omp critical
641            {
642              errcode=errorcode;    // I'm not especially concerned which error gets reported as long as one is
643            }
644        }
645         }
646      }
647      return errcode;
648      if (errcode)
649      {
650        DataMaths::matrixInverseError(errcode); // throws exceptions
651      }
652    }
653    
654  void  void
655  DataExpanded::setToZero(){  DataExpanded::setToZero(){
656  // TODO: Surely there is a more efficient way to do this????  // TODO: Surely there is a more efficient way to do this????
657  // Why is there no memset here? Parallel issues?  // Why is there no memset here? Parallel issues?
658    // A: This ensures that memory is touched by the correct thread.
659      CHECK_FOR_EX_WRITE
660    int numSamples = getNumSamples();    int numSamples = getNumSamples();
661    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
662    DataTypes::ValueType::size_type n = getNoValues();    DataTypes::ValueType::size_type n = getNoValues();
# Line 732  DataExpanded::dump(const std::string fil Line 693  DataExpanded::dump(const std::string fil
693     long dims[ldims];     long dims[ldims];
694     const double* d_ptr=&(m_data[0]);     const double* d_ptr=&(m_data[0]);
695     const DataTypes::ShapeType& shape = getShape();     const DataTypes::ShapeType& shape = getShape();
696     int mpi_iam=getFunctionSpace().getDomain().getMPIRank();     int mpi_iam=getFunctionSpace().getDomain()->getMPIRank();
697     int mpi_num=getFunctionSpace().getDomain().getMPISize();     int mpi_num=getFunctionSpace().getDomain()->getMPISize();
698  #ifdef PASO_MPI  #ifdef ESYS_MPI
699     MPI_Status status;     MPI_Status status;
700  #endif  #endif
701    
702  #ifdef PASO_MPI  #ifdef ESYS_MPI
703     /* Serialize NetCDF I/O */     /* Serialize NetCDF I/O */
704     if (mpi_iam>0) MPI_Recv(&ndims, 0, MPI_INT, mpi_iam-1, 81801, MPI_COMM_WORLD, &status);     if (mpi_iam>0) MPI_Recv(&ndims, 0, MPI_INT, mpi_iam-1, 81801, MPI_COMM_WORLD, &status);
705  #endif  #endif
# Line 798  DataExpanded::dump(const std::string fil Line 759  DataExpanded::dump(const std::string fil
759       if (! (var->put(d_ptr,dims)) )       if (! (var->put(d_ptr,dims)) )
760          throw DataException("Error - DataExpanded:: copy data to netCDF buffer failed.");          throw DataException("Error - DataExpanded:: copy data to netCDF buffer failed.");
761     }     }
762  #ifdef PASO_MPI  #ifdef ESYS_MPI
763     if (mpi_iam<mpi_num-1) MPI_Send(&ndims, 0, MPI_INT, mpi_iam+1, 81801, MPI_COMM_WORLD);     if (mpi_iam<mpi_num-1) MPI_Send(&ndims, 0, MPI_INT, mpi_iam+1, 81801, MPI_COMM_WORLD);
764  #endif  #endif
765     #else     #else
# Line 812  DataExpanded::setTaggedValue(int tagKey, Line 773  DataExpanded::setTaggedValue(int tagKey,
773                 const DataTypes::ValueType& value,                 const DataTypes::ValueType& value,
774             int dataOffset)             int dataOffset)
775  {  {
776      CHECK_FOR_EX_WRITE
777    int numSamples = getNumSamples();    int numSamples = getNumSamples();
778    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
779    int sampleNo,dataPointNo, i;    int sampleNo,dataPointNo, i;
# Line 838  DataExpanded::setTaggedValue(int tagKey, Line 800  DataExpanded::setTaggedValue(int tagKey,
800  void  void
801  DataExpanded::reorderByReferenceIDs(int *reference_ids)  DataExpanded::reorderByReferenceIDs(int *reference_ids)
802  {  {
803      CHECK_FOR_EX_WRITE
804    int numSamples = getNumSamples();    int numSamples = getNumSamples();
805    DataTypes::ValueType::size_type n = getNoValues() * getNumDPPSample();    DataTypes::ValueType::size_type n = getNoValues() * getNumDPPSample();
806    int sampleNo, sampleNo2,i;    int sampleNo, sampleNo2,i;
# Line 873  DataExpanded::reorderByReferenceIDs(int Line 836  DataExpanded::reorderByReferenceIDs(int
836  }  }
837    
838  DataTypes::ValueType&  DataTypes::ValueType&
839  DataExpanded::getVector()  DataExpanded::getVectorRW()
840  {  {
841        CHECK_FOR_EX_WRITE
842      return m_data.getData();      return m_data.getData();
843  }  }
844    
845  const DataTypes::ValueType&  const DataTypes::ValueType&
846  DataExpanded::getVector() const  DataExpanded::getVectorRO() const
847  {  {
848      return m_data.getData();      return m_data.getData();
849  }  }
850    
851    
852    #ifndef MKLRANDOM
853    namespace {
854        
855    boost::mt19937 base;        // used to seed all the other generators  
856    vector<boost::mt19937> gens;
857    
858    
859    void seedGens(long seed)
860    {
861    #ifdef _OPENMP
862        int numthreads=omp_get_max_threads();
863    #else
864        int numthreads=1;
865    #endif
866        if (gens.size()==0)     // we haven't instantiated the generators yet  
867        {
868            gens.resize(numthreads);    // yes this means all the generators will be owned by one thread in a NUMA sense      
869        }                   // I don't think it is worth a more complicated solution at this point
870        if (seed!=0)
871        {
872           base.seed((uint32_t)seed);   // without this cast, icc gets confused    
873           for (int i=0;i<numthreads;++i)
874           {
875            uint32_t b=base();
876                gens[i].seed(b);    // initialise each generator with successive random values      
877           }      
878        }
879    }
880      
881      
882    }
883    #endif
884    
885    // Idea here is to create an array of seeds by feeding the original seed into the random generator
886    // The code at the beginning of the function to compute the seed if one is given is
887    // just supposed to introduce some variety (and ensure that multiple ranks don't get the same seed).
888    // I make no claim about how well these initial seeds are distributed
889    void DataExpanded::randomFill(long seed)
890    {
891        CHECK_FOR_EX_WRITE
892        static unsigned prevseed=0; // So if we create a bunch of objects we don't get the same start seed
893        if (seed==0)        // for each one
894        {
895        if (prevseed==0)
896        {
897            time_t s=time(0);
898            seed=s;
899        }
900        else
901        {
902            seed=prevseed+419;  // these numbers are arbitrary
903            if (seed>3040101)       // I want to avoid overflow on 32bit systems
904            {
905            seed=((int)(seed)%0xABCD)+1;
906            }
907        }
908        }
909        // now we need to consider MPI since we don't want each rank to start with the same seed
910        seed+=getFunctionSpace().getDomain()->getMPIRank()*getFunctionSpace().getDomain()->getMPISize()*3;
911        prevseed=seed;
912    
913    #ifdef MKLRANDOM
914    
915    #ifdef _OPENMP
916        int numthreads=omp_get_max_threads();
917    #else
918        int numthreads=1;
919    #endif
920        double* seeds=new double[numthreads];
921        VSLStreamStatePtr sstream;
922    
923        int status=vslNewStream(&sstream, VSL_BRNG_MT19937, seed);  // use a Mersenne Twister
924        numeric_limits<double> dlim;
925        vdRngUniform(VSL_METHOD_DUNIFORM_STD, sstream , numthreads, seeds, -1, 1);
926        vslDeleteStream(&sstream);
927        DataVector& dv=getVectorRW();
928        size_t dvsize=dv.size();
929        #pragma omp parallel
930        {
931        int tnum=0;
932        #ifdef _OPENMP
933        tnum=omp_get_thread_num();
934        #endif
935        VSLStreamStatePtr stream;
936        // the 12345 is a hack to give us a better chance of getting different integer seeds.
937            int status=vslNewStream(&stream, VSL_BRNG_MT19937, seeds[tnum]*12345);  // use a Mersenne Twister
938        int bigchunk=(dvsize/numthreads+1);
939        int smallchunk=dvsize-bigchunk*(numthreads-1);
940        int chunksize=(tnum<(numthreads-1))?bigchunk:smallchunk;
941            vdRngUniform(VSL_METHOD_DUNIFORM_STD, stream, chunksize, &(dv[bigchunk*tnum]), 0,1);
942            vslDeleteStream(&stream);
943        }
944        delete[] seeds;
945    #else
946        boost::mt19937::result_type RMAX=base.max();
947        seedGens(seed);
948        DataVector&  dv=getVectorRW();
949        long i;
950        const size_t dvsize=dv.size();
951        
952        #pragma omp parallel private(i)
953        {
954        int tnum=0;
955        #ifdef _OPENMP
956        tnum=omp_get_thread_num();
957        #endif
958        boost::mt19937& generator=gens[tnum];
959        
960            #pragma omp for schedule(static)
961            for (i=0;i<dvsize;++i)
962            {
963          
964          
965        
966          
967          
968    #ifdef _WIN32
969            dv[i]=((double)generator())/RMAX;
970    #else
971            dv[i]=((double)generator())/RMAX;
972    #endif
973            }
974        }
975    #endif
976    }
977    
978  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.1827  
changed lines
  Added in v.3981

  ViewVC Help
Powered by ViewVC 1.1.26