/[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

temp/escript/src/DataExpanded.cpp revision 1387 by trankine, Fri Jan 11 07:45:26 2008 UTC trunk/escript/src/DataExpanded.cpp revision 3506 by jfenwick, Wed May 11 01:59:45 2011 UTC
# Line 1  Line 1 
1    
 /* $Id$ */  
   
2  /*******************************************************  /*******************************************************
3   *  *
4   *           Copyright 2003-2007 by ACceSS MNRF  * Copyright (c) 2003-2010 by University of Queensland
5   *       Copyright 2007 by University of Queensland  * Earth Systems Science Computational Center (ESSCC)
6   *  * http://www.uq.edu.au/esscc
7   *                http://esscc.uq.edu.au  *
8   *        Primary Business: Queensland, Australia  * Primary Business: Queensland, Australia
9   *  Licensed under the Open Software License version 3.0  * Licensed under the Open Software License version 3.0
10   *     http://www.opensource.org/licenses/osl-3.0.php  * http://www.opensource.org/licenses/osl-3.0.php
11   *  *
12   *******************************************************/  *******************************************************/
13    
14    
15    #include "Data.h"
16  #include "DataExpanded.h"  #include "DataExpanded.h"
17  #include "DataException.h"  #include "DataException.h"
18  #include "DataConstant.h"  #include "DataConstant.h"
19  #include "DataTagged.h"  #include "DataTagged.h"
20    #include <limits>
21    
22    #include "esysUtils/Esys_MPI.h"
23    
24  #ifdef USE_NETCDF  #ifdef USE_NETCDF
25  #include <netcdfcpp.h>  #include <netcdfcpp.h>
26  #endif  #endif
27    
28  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
29    #include "DataMaths.h"
30    
31    //#define MKLRANDOM
32    
33    #ifdef MKLRANDOM
34    #include <mkl_vsl.h>
35    
36    #endif
37    
38  using namespace std;  using namespace std;
39  using namespace boost::python;  using namespace boost::python;
40  using namespace boost;  using namespace boost;
41    using namespace escript::DataTypes;
42    
43    
44    // #define CHECK_FOR_EX_WRITE if (!checkNoSharing()) {throw DataException("Attempt to modify shared object");}
45    
46    #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());}
47    
48  namespace escript {  namespace escript {
49    
50  DataExpanded::DataExpanded(const boost::python::numeric::array& value,  DataExpanded::DataExpanded(const WrappedArray& value,
51                             const FunctionSpace& what)                             const FunctionSpace& what)
52    : DataAbstract(what)    : parent(what,value.getShape())
53  {  {
   DataArrayView::ShapeType tempShape;  
   //  
   // extract the shape of the python numarray  
   for (int i=0; i<value.getrank(); i++) {  
     tempShape.push_back(extract<int>(value.getshape()[i]));  
   }  
54    //    //
55    // initialise the data array for this object    // initialise the data array for this object
56    initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());    initialise(what.getNumSamples(),what.getNumDPPSample());
57    //    //
58    // copy the given value to every data point    // copy the given value to every data point
59    copy(value);    copy(value);
60  }  }
61    
62  DataExpanded::DataExpanded(const DataExpanded& other)  DataExpanded::DataExpanded(const DataExpanded& other)
63    : DataAbstract(other.getFunctionSpace()),    : parent(other.getFunctionSpace(), other.getShape()),
64    m_data(other.m_data)    m_data(other.m_data)
65  {  {
   //  
   // create the view for the data  
   DataArrayView temp(m_data.getData(),other.getPointDataView().getShape());  
   setPointDataView(temp);  
66  }  }
67    
68  DataExpanded::DataExpanded(const DataConstant& other)  DataExpanded::DataExpanded(const DataConstant& other)
69    : DataAbstract(other.getFunctionSpace())    : parent(other.getFunctionSpace(), other.getShape())
70  {  {
71    //    //
72    // initialise the data array for this object    // initialise the data array for this object
73    initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());    initialise(other.getNumSamples(),other.getNumDPPSample());
74    //    //
75    // DataConstant only has one value, copy this to every data point    // DataConstant only has one value, copy this to every data point
76    copy(other.getPointDataView());    copy(other);
77  }  }
78    
79  DataExpanded::DataExpanded(const DataTagged& other)  DataExpanded::DataExpanded(const DataTagged& other)
80    : DataAbstract(other.getFunctionSpace())    : parent(other.getFunctionSpace(), other.getShape())
81  {  {
82    //    //
83    // initialise the data array for this object    // initialise the data array for this object
84    initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());    initialise(other.getNumSamples(),other.getNumDPPSample());
85    //    //
86    // for each data point in this object, extract and copy the corresponding data    // for each data point in this object, extract and copy the corresponding data
87    // value from the given DataTagged object    // value from the given DataTagged object
88    int i,j;    int i,j;
89    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();    DataTypes::ValueType::size_type numRows=m_data.getNumRows();
90    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();    DataTypes::ValueType::size_type numCols=m_data.getNumCols();
91    #pragma omp parallel for private(i,j) schedule(static)    #pragma omp parallel for private(i,j) schedule(static)
92    for (i=0;i<numRows;i++) {    for (i=0;i<numRows;i++) {
93      for (j=0;j<numCols;j++) {      for (j=0;j<numCols;j++) {
94        try {        try {
95          getPointDataView().copy(getPointOffset(i,j),             DataTypes::copyPoint(getVectorRW(), getPointOffset(i,j), getNoValues(),
96                                  other.getPointDataView(),                                  other.getVectorRO(),
97                                  other.getPointOffset(i,j));                                  other.getPointOffset(i,j));
98        }        }
99        catch (std::exception& e) {        catch (std::exception& e) {
# Line 96  DataExpanded::DataExpanded(const DataTag Line 104  DataExpanded::DataExpanded(const DataTag
104  }  }
105    
106  DataExpanded::DataExpanded(const DataExpanded& other,  DataExpanded::DataExpanded(const DataExpanded& other,
107                             const DataArrayView::RegionType& region)                             const DataTypes::RegionType& region)
108    : DataAbstract(other.getFunctionSpace())    : parent(other.getFunctionSpace(),DataTypes::getResultSliceShape(region))
109  {  {
110    //    //
111    // get the shape of the slice    // get the shape of the slice
112    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));  //   DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));
113    //    //
114    // initialise this Data object to the shape of the slice    // initialise this Data object to the shape of the slice
115    initialise(shape,other.getNumSamples(),other.getNumDPPSample());    initialise(other.getNumSamples(),other.getNumDPPSample());
116    //    //
117    // copy the data    // copy the data
118    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);
119    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();    DataTypes::ValueType::size_type numRows=m_data.getNumRows();
120    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();    DataTypes::ValueType::size_type numCols=m_data.getNumCols();
121    int i,j;    int i,j;
122    #pragma omp parallel for private(i,j) schedule(static)    #pragma omp parallel for private(i,j) schedule(static)
123    for (i=0;i<numRows;i++) {    for (i=0;i<numRows;i++) {
124      for (j=0;j<numCols;j++) {      for (j=0;j<numCols;j++) {
125        try {        try {
126          getPointDataView().copySlice(getPointOffset(i,j),          DataTypes::copySlice(getVectorRW(),getShape(),getPointOffset(i,j),
127                                       other.getPointDataView(),                                       other.getVectorRO(),
128                         other.getShape(),
129                                       other.getPointOffset(i,j),                                       other.getPointOffset(i,j),
130                                       region_loop_range);                                       region_loop_range);
131        }        }
# Line 127  DataExpanded::DataExpanded(const DataExp Line 136  DataExpanded::DataExpanded(const DataExp
136    }    }
137  }  }
138    
139  DataExpanded::DataExpanded(const DataArrayView& value,  DataExpanded::DataExpanded(const FunctionSpace& what,
140                             const FunctionSpace& what)                             const DataTypes::ShapeType &shape,
141    : DataAbstract(what)                             const DataTypes::ValueType &data)
142  {    : parent(what,shape)
143    //  {
144    // get the shape of the given data value    EsysAssert(data.size()%getNoValues()==0,
145    DataArrayView::ShapeType tempShape=value.getShape();                   "DataExpanded Constructor - size of supplied data is not a multiple of shape size.");
146    //  
147    // initialise this Data object to the shape of the given data value    if (data.size()==getNoValues())
148    initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());    {
149    //       ValueType& vec=m_data.getData();
150    // copy the given value to every data point       //
151    copy(value);       // create the view of the data
152         initialise(what.getNumSamples(),what.getNumDPPSample());
153         // now we copy this value to all elements
154         for (int i=0;i<getLength();)
155         {
156        for (unsigned int j=0;j<getNoValues();++j,++i)
157        {
158            vec[i]=data[j];
159        }
160         }
161      }
162      else
163      {
164         //
165         // copy the data in the correct format
166         m_data.getData()=data;
167      }
168    
169    
170  }  }
171    
172  DataExpanded::DataExpanded(const FunctionSpace& what,  DataExpanded::DataExpanded(const FunctionSpace& what,
173                             const DataArrayView::ShapeType &shape,                             const DataTypes::ShapeType &shape,
174                             const DataArrayView::ValueType &data)                             const double v)
175    : DataAbstract(what)    : parent(what,shape)
176  {  {
177    //       ValueType& vec=m_data.getData();
178    // create the view of the data       //
179    initialise(shape,what.getNumSamples(),what.getNumDPPSample());       // create the view of the data
180    //       initialise(what.getNumSamples(),what.getNumDPPSample());
181    // copy the data in the correct format       // now we copy this value to all elements
182    m_data.getData()=data;       const int L=getLength();
183         int i;
184         #pragma omp parallel for schedule(static) private(i)
185         for (i=0;i<L;++i)
186         {
187            vec[i]=v;
188         }
189  }  }
190    
191    
192    
193  DataExpanded::~DataExpanded()  DataExpanded::~DataExpanded()
194  {  {
195  }  }
196    
197  DataAbstract*  DataAbstract*
198  DataExpanded::getSlice(const DataArrayView::RegionType& region) const  DataExpanded::deepCopy()
199    {
200      return new DataExpanded(*this);
201    }
202    
203    
204    DataAbstract*
205    DataExpanded::getSlice(const DataTypes::RegionType& region) const
206  {  {
207    return new DataExpanded(*this,region);    return new DataExpanded(*this,region);
208  }  }
209    
210  void  void
211  DataExpanded::setSlice(const DataAbstract* value,  DataExpanded::setSlice(const DataAbstract* value,
212                         const DataArrayView::RegionType& region)                         const DataTypes::RegionType& region)
213  {  {
214    const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);    const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);
215    if (tempDataExp==0) {    if (tempDataExp==0) {
216      throw DataException("Programming error - casting to DataExpanded.");      throw DataException("Programming error - casting to DataExpanded.");
217    }    }
218      CHECK_FOR_EX_WRITE
219    //    //
220    // get shape of slice    // get shape of slice
221    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));
222    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);
223    //    //
224    // check shape    // check shape
225    if (getPointDataView().getRank()!=region.size()) {    if (getRank()!=region.size()) {
226      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
227    }    }
228    if (tempDataExp->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) {    if (tempDataExp->getRank()>0 && !DataTypes::checkShape(value->getShape(), shape)) {
229      throw DataException (value->getPointDataView().createShapeErrorMessage(      throw DataException (DataTypes::createShapeErrorMessage(
230          "Error - Couldn't copy slice due to shape mismatch.",shape));          "Error - Couldn't copy slice due to shape mismatch.",shape, value->getShape()));
231    }    }
232    //    //
233    // copy the data from the slice into this object    // copy the data from the slice into this object
234    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();    DataTypes::ValueType::size_type numRows=m_data.getNumRows();
235    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();    DataTypes::ValueType::size_type numCols=m_data.getNumCols();
236    int i, j;    int i, j;
237      ValueType& vec=getVectorRW();
238      const ShapeType& mshape=getShape();
239      const ValueType& tVec=tempDataExp->getVectorRO();
240      const ShapeType& tShape=tempDataExp->getShape();
241    #pragma omp parallel for private(i,j) schedule(static)    #pragma omp parallel for private(i,j) schedule(static)
242    for (i=0;i<numRows;i++) {    for (i=0;i<numRows;i++) {
243      for (j=0;j<numCols;j++) {      for (j=0;j<numCols;j++) {
244        getPointDataView().copySliceFrom(getPointOffset(i,j),          DataTypes::copySliceFrom(vec,mshape,getPointOffset(i,j),
245                                         tempDataExp->getPointDataView(),                                         tVec,
246                           tShape,
247                                         tempDataExp->getPointOffset(i,j),                                         tempDataExp->getPointOffset(i,j),
248                                         region_loop_range);                                         region_loop_range);
249    
250      }      }
251    }    }
252  }  }
253    
254  void  void
255  DataExpanded::copy(const DataArrayView& value)  DataExpanded::copy(const DataConstant& value)
256  {  {
257      EsysAssert((checkShape(getShape(), value.getShape())),
258                     createShapeErrorMessage("Error - Couldn't copy due to shape mismatch.",value.getShape(),getShape()));
259    
260    //    //
261    // copy a single value to every data point in this object    // copy a single value to every data point in this object
262    int nRows=m_data.getNumRows();    int nRows=m_data.getNumRows();
# Line 213  DataExpanded::copy(const DataArrayView& Line 265  DataExpanded::copy(const DataArrayView&
265    #pragma omp parallel for private(i,j) schedule(static)    #pragma omp parallel for private(i,j) schedule(static)
266    for (i=0;i<nRows;i++) {    for (i=0;i<nRows;i++) {
267      for (j=0;j<nCols;j++) {      for (j=0;j<nCols;j++) {
268        // 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);  
269      }      }
270    }    }
271  }  }
272    
273  void  void
274  DataExpanded::copy(const boost::python::numeric::array& value)  DataExpanded::copy(const WrappedArray& value)
275  {  {
   
   // extract the shape of the numarray  
   DataArrayView::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 = DataArrayView::noValues(tempShape);  
   DataVector temp_data(len, 0.0, len);  
   DataArrayView temp_dataView(temp_data, tempShape);  
   temp_dataView.copy(value);  
   
   //  
276    // check the input shape matches this shape    // check the input shape matches this shape
277    if (!getPointDataView().checkShape(temp_dataView.getShape())) {    if (!DataTypes::checkShape(getShape(),value.getShape())) {
278      throw DataException(getPointDataView().createShapeErrorMessage(      throw DataException(DataTypes::createShapeErrorMessage(
279                          "Error - (DataExpanded) Cannot copy due to shape mismatch.",                          "Error - (DataExpanded) Cannot copy due to shape mismatch.",
280                          temp_dataView.getShape()));                          value.getShape(),getShape()));
281    }    }
282    //    getVectorRW().copyFromArray(value, getNumDPPSample()*getNumSamples());
   // now copy over the data  
   copy(temp_dataView);  
283  }  }
284    
285    
286  void  void
287  DataExpanded::initialise(const DataArrayView::ShapeType& shape,  DataExpanded::initialise(int noSamples,
                          int noSamples,  
288                           int noDataPointsPerSample)                           int noDataPointsPerSample)
289  {  {
290      if (noSamples==0)     //retain the default empty object
291      {
292         return;
293      }
294    //    //
295    // resize data array to the required size    // resize data array to the required size
296    m_data.resize(noSamples,noDataPointsPerSample,DataArrayView::noValues(shape));    m_data.resize(noSamples,noDataPointsPerSample,getNoValues());
297    //  }
298    // create the data view of the data array  
299    DataArrayView temp(m_data.getData(),shape);  bool
300    setPointDataView(temp);  DataExpanded::hasNaN() const
301    {
302        const ValueType& v=m_data.getData();
303        for (ValueType::size_type i=0;i<v.size();++i)
304        {
305            if (nancheck(v[i]))
306            {
307                return true;
308            }
309        }
310        return false;
311  }  }
312    
313    
314  string  string
315  DataExpanded::toString() const  DataExpanded::toString() const
316  {  {
317    stringstream temp;    stringstream temp;
318    FunctionSpace fs=getFunctionSpace();    FunctionSpace fs=getFunctionSpace();
319    //  
320    // create a temporary view as the offset will be changed    int offset=0;
   DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());  
321    for (int i=0;i<m_data.getNumRows();i++) {    for (int i=0;i<m_data.getNumRows();i++) {
322      for (int j=0;j<m_data.getNumCols();j++) {      for (int j=0;j<m_data.getNumCols();j++) {
323        tempView.setOffset(getPointOffset(i,j));        offset=getPointOffset(i,j);
324        stringstream suffix;        stringstream suffix;
325        suffix << "( id: " << i << ", ref: " << fs.getReferenceIDOfSample(i) << ", pnt: " << j << ")";        suffix << "( id: " << i << ", ref: " << fs.getReferenceIDOfSample(i) << ", pnt: " << j << ")";
326        temp << tempView.toString(suffix.str());        temp << DataTypes::pointToString(getVectorRO(),getShape(),offset,suffix.str());
327        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
328          temp << endl;          temp << endl;
329        }        }
330      }      }
331    }    }
332      string result=temp.str();
333      if (result.empty())
334      {
335        return "(data contains no samples)\n";
336      }
337    return temp.str();    return temp.str();
338  }  }
339    
340  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
341  DataExpanded::getPointOffset(int sampleNo,  DataExpanded::getPointOffset(int sampleNo,
342                               int dataPointNo) const                               int dataPointNo) const
343  {  {
344    return m_data.index(sampleNo,dataPointNo);    return m_data.index(sampleNo,dataPointNo);
345  }  }
346    
347  DataArrayView  DataTypes::ValueType::size_type
348  DataExpanded::getDataPoint(int sampleNo,  DataExpanded::getPointOffset(int sampleNo,
349                             int dataPointNo)                               int dataPointNo)
350  {  {
351    DataArrayView temp(m_data.getData(),getPointDataView().getShape(),getPointOffset(sampleNo,dataPointNo));    return m_data.index(sampleNo,dataPointNo);
   return temp;  
352  }  }
353    
354  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
355  DataExpanded::getLength() const  DataExpanded::getLength() const
356  {  {
357    return m_data.size();    return m_data.size();
358  }  }
359    
 int  
 DataExpanded::archiveData(ofstream& archiveFile,  
                           const DataArrayView::ValueType::size_type noValues) const  
 {  
   return(m_data.archiveData(archiveFile, noValues));  
 }  
360    
 int  
 DataExpanded::extractData(ifstream& archiveFile,  
                           const DataArrayView::ValueType::size_type noValues)  
 {  
   return(m_data.extractData(archiveFile, noValues));  
 }  
361    
362  void  void
363  DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const double value) {  DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const double value) {
364      CHECK_FOR_EX_WRITE
365    //    //
366    // Get the number of samples and data-points per sample.    // Get the number of samples and data-points per sample.
367    int numSamples = getNumSamples();    int numSamples = getNumSamples();
368    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
369    int dataPointRank = getPointDataView().getRank();    int dataPointRank = getRank();
370    ShapeType dataPointShape = getPointDataView().getShape();    ShapeType dataPointShape = getShape();
371    if (numSamples*numDataPointsPerSample > 0) {    if (numSamples*numDataPointsPerSample > 0) {
372       //TODO: global error handling       //TODO: global error handling
373       if ((sampleNo >= numSamples) || (sampleNo < 0 )) {       if ((sampleNo >= numSamples) || (sampleNo < 0 )) {
# Line 337  DataExpanded::copyToDataPoint(const int Line 376  DataExpanded::copyToDataPoint(const int
376       if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {       if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
377             throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");             throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
378       }       }
379       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);       ValueType::size_type offset = getPointOffset(sampleNo, dataPointNo);
380         ValueType& vec=getVectorRW();
381       if (dataPointRank==0) {       if (dataPointRank==0) {
382           dataPointView()=value;           vec[offset]=value;
383       } else if (dataPointRank==1) {       } else if (dataPointRank==1) {
384          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<dataPointShape[0]; i++) {
385              dataPointView(i)=value;              vec[offset+i]=value;
386          }          }
387       } else if (dataPointRank==2) {       } else if (dataPointRank==2) {
388          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<dataPointShape[0]; i++) {
389             for (int j=0; j<dataPointShape[1]; j++) {             for (int j=0; j<dataPointShape[1]; j++) {
390                dataPointView(i,j)=value;                vec[offset+getRelIndex(dataPointShape,i,j)]=value;
391             }             }
392          }          }
393       } else if (dataPointRank==3) {       } else if (dataPointRank==3) {
394          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<dataPointShape[0]; i++) {
395             for (int j=0; j<dataPointShape[1]; j++) {             for (int j=0; j<dataPointShape[1]; j++) {
396                for (int k=0; k<dataPointShape[2]; k++) {                for (int k=0; k<dataPointShape[2]; k++) {
397                   dataPointView(i,j,k)=value;                   vec[offset+getRelIndex(dataPointShape,i,j,k)]=value;
398                }                }
399             }             }
400          }          }
# Line 363  DataExpanded::copyToDataPoint(const int Line 403  DataExpanded::copyToDataPoint(const int
403             for (int j=0; j<dataPointShape[1]; j++) {             for (int j=0; j<dataPointShape[1]; j++) {
404               for (int k=0; k<dataPointShape[2]; k++) {               for (int k=0; k<dataPointShape[2]; k++) {
405                 for (int l=0; l<dataPointShape[3]; l++) {                 for (int l=0; l<dataPointShape[3]; l++) {
406                    dataPointView(i,j,k,l)=value;                    vec[offset+getRelIndex(dataPointShape,i,j,k,l)]=value;
407                 }                 }
408               }               }
409             }             }
# Line 371  DataExpanded::copyToDataPoint(const int Line 411  DataExpanded::copyToDataPoint(const int
411       }       }
412    }    }
413  }  }
414    
415  void  void
416  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) {
417      CHECK_FOR_EX_WRITE
418    //    //
419    // Get the number of samples and data-points per sample.    // Get the number of samples and data-points per sample.
420    int numSamples = getNumSamples();    int numSamples = getNumSamples();
421    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
   int dataPointRank = getPointDataView().getRank();  
   ShapeType dataPointShape = getPointDataView().getShape();  
422    //    //
423    // check rank:    // check rank:
424    if (value.getrank()!=dataPointRank)    if (value.getRank()!=getRank())
425         throw DataException("Rank of numarray does not match Data object rank");         throw DataException("Rank of value does not match Data object rank");
426    if (numSamples*numDataPointsPerSample > 0) {    if (numSamples*numDataPointsPerSample > 0) {
427       //TODO: global error handling       //TODO: global error handling
428       if ((sampleNo >= numSamples) || (sampleNo < 0 )) {       if ((sampleNo >= numSamples) || (sampleNo < 0 )) {
# Line 391  DataExpanded::copyToDataPoint(const int Line 431  DataExpanded::copyToDataPoint(const int
431       if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {       if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
432             throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");             throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
433       }       }
434       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);       ValueType::size_type offset = getPointOffset(sampleNo, dataPointNo);
435       if (dataPointRank==0) {       ValueType& vec=getVectorRW();
436           dataPointView()=extract<double>(value[0]);       vec.copyFromArrayToOffset(value,offset,1);
      } else if (dataPointRank==1) {  
         for (int i=0; i<dataPointShape[0]; i++) {  
             dataPointView(i)=extract<double>(value[i]);  
         }  
      } else if (dataPointRank==2) {  
         for (int i=0; i<dataPointShape[0]; i++) {  
            for (int j=0; j<dataPointShape[1]; j++) {  
               dataPointView(i,j)=extract<double>(value[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++) {  
                  dataPointView(i,j,k)=extract<double>(value[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++) {  
                   dataPointView(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 = getPointDataView().getRank();  
   ShapeType dataPointShape = getPointDataView().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");  
   //  
   int dataPoint = 0;  
   for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {  
     for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {  
       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);  
       if (dataPointRank==0) {  
          dataPointView()=extract<double>(value[dataPoint]);  
       } else if (dataPointRank==1) {  
          for (int i=0; i<dataPointShape[0]; i++) {  
             dataPointView(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++) {  
              dataPointView(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++) {  
                  dataPointView(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++) {  
                  dataPointView(i,j,k,l)=extract<double>(value[dataPoint][i][j][k][l]);  
                }  
              }  
            }  
          }  
       }  
       dataPoint++;  
     }  
437    }    }
438  }  }
439    
440  void  void
441  DataExpanded::symmetric(DataAbstract* ev)  DataExpanded::symmetric(DataAbstract* ev)
442  {  {
# Line 489  DataExpanded::symmetric(DataAbstract* ev Line 447  DataExpanded::symmetric(DataAbstract* ev
447    if (temp_ev==0) {    if (temp_ev==0) {
448      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).");
449    }    }
450    DataArrayView& thisView=getPointDataView();    const ValueType& vec=getVectorRO();
451    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
452      ValueType& evVec=temp_ev->getVectorRW();
453      const ShapeType& evShape=temp_ev->getShape();
454    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
455    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
456      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
457           DataArrayView::symmetric(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::symmetric(vec,shape,getPointOffset(sampleNo,dataPointNo),
458                                      evView,ev->getPointOffset(sampleNo,dataPointNo));                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo));
459      }      }
460    }    }
461  }  }
# Line 509  DataExpanded::nonsymmetric(DataAbstract* Line 469  DataExpanded::nonsymmetric(DataAbstract*
469    if (temp_ev==0) {    if (temp_ev==0) {
470      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).");
471    }    }
472    DataArrayView& thisView=getPointDataView();    const ValueType& vec=getVectorRO();
473    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
474      ValueType& evVec=temp_ev->getVectorRW();
475      const ShapeType& evShape=temp_ev->getShape();
476    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
477    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
478      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
479           DataArrayView::nonsymmetric(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::nonsymmetric(vec,shape,getPointOffset(sampleNo,dataPointNo),
480                                      evView,ev->getPointOffset(sampleNo,dataPointNo));                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo));
481      }      }
482    }    }
483  }  }
# Line 529  DataExpanded::trace(DataAbstract* ev, in Line 491  DataExpanded::trace(DataAbstract* ev, in
491    if (temp_ev==0) {    if (temp_ev==0) {
492      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).");
493    }    }
494    DataArrayView& thisView=getPointDataView();    const ValueType& vec=getVectorRO();
495    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
496      ValueType& evVec=temp_ev->getVectorRW();
497      const ShapeType& evShape=temp_ev->getShape();
498    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
499    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
500      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
501           DataArrayView::trace(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::trace(vec,shape,getPointOffset(sampleNo,dataPointNo),
502                                      evView,ev->getPointOffset(sampleNo,dataPointNo),axis_offset);                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo),axis_offset);
503      }      }
504    }    }
505  }  }
# Line 550  DataExpanded::transpose(DataAbstract* ev Line 514  DataExpanded::transpose(DataAbstract* ev
514    if (temp_ev==0) {    if (temp_ev==0) {
515      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).");
516    }    }
517    DataArrayView& thisView=getPointDataView();    const ValueType& vec=getVectorRO();
518    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
519      ValueType& evVec=temp_ev->getVectorRW();
520      const ShapeType& evShape=temp_ev->getShape();
521    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
522    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
523      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
524           DataArrayView::transpose(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::transpose(vec,shape,getPointOffset(sampleNo,dataPointNo),
525                                      evView,ev->getPointOffset(sampleNo,dataPointNo),axis_offset);                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo),axis_offset);
526      }      }
527    }    }
528  }  }
# Line 571  DataExpanded::swapaxes(DataAbstract* ev, Line 537  DataExpanded::swapaxes(DataAbstract* ev,
537    if (temp_ev==0) {    if (temp_ev==0) {
538      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).");
539    }    }
540    DataArrayView& thisView=getPointDataView();    const ValueType& vec=getVectorRO();
541    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
542      ValueType& evVec=temp_ev->getVectorRW();
543      const ShapeType& evShape=temp_ev->getShape();
544    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
545    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
546      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
547           DataArrayView::swapaxes(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::swapaxes(vec,shape,getPointOffset(sampleNo,dataPointNo),
548                                      evView,ev->getPointOffset(sampleNo,dataPointNo),axis0,axis1);                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo),axis0,axis1);
549      }      }
550    }    }
551  }  }
# Line 591  DataExpanded::eigenvalues(DataAbstract* Line 559  DataExpanded::eigenvalues(DataAbstract*
559    if (temp_ev==0) {    if (temp_ev==0) {
560      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).");
561    }    }
562    DataArrayView& thisView=getPointDataView();    const ValueType& vec=getVectorRO();
563    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
564      ValueType& evVec=temp_ev->getVectorRW();
565      const ShapeType& evShape=temp_ev->getShape();
566    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
567    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
568      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
569           DataArrayView::eigenvalues(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::eigenvalues(vec,shape,getPointOffset(sampleNo,dataPointNo),
570                                      evView,ev->getPointOffset(sampleNo,dataPointNo));                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo));
571      }      }
572    }    }
573  }  }
# Line 615  DataExpanded::eigenvalues_and_eigenvecto Line 585  DataExpanded::eigenvalues_and_eigenvecto
585    if (temp_V==0) {    if (temp_V==0) {
586      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).");
587    }    }
588    DataArrayView& thisView=getPointDataView();    const ValueType& vec=getVectorRO();
589    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
590    DataArrayView& VView=V->getPointDataView();    ValueType& evVec=temp_ev->getVectorRW();
591      const ShapeType& evShape=temp_ev->getShape();
592      ValueType& VVec=temp_V->getVectorRW();
593      const ShapeType& VShape=temp_V->getShape();
594    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
595    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
596      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
597           DataArrayView::eigenvalues_and_eigenvectors(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::eigenvalues_and_eigenvectors(vec,shape,getPointOffset(sampleNo,dataPointNo),
598                                                       evView,ev->getPointOffset(sampleNo,dataPointNo),                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo),
599                                                       VView,V->getPointOffset(sampleNo,dataPointNo),                                      VVec, VShape,V->getPointOffset(sampleNo,dataPointNo),tol);
                                                      tol);  
600      }      }
601    }    }
602  }  }
603    
604    
605    int
606    DataExpanded::matrixInverse(DataAbstract* out) const
607    {
608      DataExpanded* temp=dynamic_cast<DataExpanded*>(out);
609      if (temp==0)
610      {
611        throw DataException("Error - DataExpanded::matrixInverse: casting to DataExpanded failed (propably a programming error).");
612      }
613    
614      if (getRank()!=2)
615      {
616        throw DataException("Error - DataExpanded::matrixInverse: input must be rank 2.");
617    
618      }
619      int  sampleNo;
620      const int numdpps=getNumDPPSample();
621      const int numSamples = getNumSamples();
622      const ValueType& vec=m_data.getData();
623      int errcode=0;
624      #pragma omp parallel private(sampleNo)
625      {
626         int errorcode=0;
627         LapackInverseHelper h(getShape()[0]);
628         #pragma omp for schedule(static)
629         for (sampleNo = 0; sampleNo < numSamples; sampleNo++)
630         {
631                // not sure I like all those virtual calls to getPointOffset
632            DataTypes::ValueType::size_type offset=getPointOffset(sampleNo,0);
633            int res=DataMaths::matrix_inverse(vec, getShape(), offset, temp->getVectorRW(), temp->getShape(), offset, numdpps, h);
634        if (res>errorcode)
635        {
636            errorcode=res;
637            #pragma omp critical
638            {
639              errcode=errorcode;    // I'm not especially concerned which error gets reported as long as one is
640            }
641        }
642         }
643      }
644      return errcode;
645      if (errcode)
646      {
647        DataMaths::matrixInverseError(errcode); // throws exceptions
648      }
649    }
650    
651  void  void
652  DataExpanded::setToZero(){  DataExpanded::setToZero(){
653    // TODO: Surely there is a more efficient way to do this????
654    // Why is there no memset here? Parallel issues?
655    // A: This ensures that memory is touched by the correct thread.
656      CHECK_FOR_EX_WRITE
657    int numSamples = getNumSamples();    int numSamples = getNumSamples();
658    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
659    DataArrayView& thisView=getPointDataView();    DataTypes::ValueType::size_type n = getNoValues();
   DataArrayView::ValueType::size_type n = thisView.noValues();  
660    double* p;    double* p;
661    int  sampleNo,dataPointNo, i;    int  sampleNo,dataPointNo, i;
662    #pragma omp parallel for private(sampleNo,dataPointNo,p,i) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo,p,i) schedule(static)
663    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
664      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
665          p=&(m_data[getPointOffset(sampleNo,dataPointNo)]);          p=&(m_data[getPointOffset(sampleNo,dataPointNo)]);
666          for (int i=0; i<n ;++i) p[i]=0.;          for (i=0; i<n ;++i) p[i]=0.;
667      }      }
668    }    }
669  }  }
670    
671    /* Append MPI rank to file name if multiple MPI processes */
672    char *Escript_MPI_appendRankToFileName(const char *fileName, int mpi_size, int mpi_rank) {
673      /* Make plenty of room for the mpi_rank number and terminating '\0' */
674      char *newFileName = (char *)malloc(strlen(fileName)+20);
675      strncpy(newFileName, fileName, strlen(fileName)+1);
676      if (mpi_size>1) sprintf(newFileName+strlen(newFileName), ".%04d", mpi_rank);
677      return(newFileName);
678    }
679    
680  void  void
681  DataExpanded::dump(const std::string fileName) const  DataExpanded::dump(const std::string fileName) const
682  {  {
    #ifdef PASO_MPI  
    throw DataException("Error - DataExpanded:: dump is not implemented for MPI yet.");  
    #endif  
683     #ifdef USE_NETCDF     #ifdef USE_NETCDF
684     const int ldims=2+DataArrayView::maxRank;     const int ldims=2+DataTypes::maxRank;
685     const NcDim* ncdims[ldims];     const NcDim* ncdims[ldims];
686     NcVar *var, *ids;     NcVar *var, *ids;
687     int rank = getPointDataView().getRank();     int rank = getRank();
688     int type=  getFunctionSpace().getTypeCode();     int type=  getFunctionSpace().getTypeCode();
689     int ndims =0;     int ndims =0;
690     long dims[ldims];     long dims[ldims];
691     const double* d_ptr=&(m_data[0]);     const double* d_ptr=&(m_data[0]);
692     DataArrayView::ShapeType shape = getPointDataView().getShape();     const DataTypes::ShapeType& shape = getShape();
693       int mpi_iam=getFunctionSpace().getDomain()->getMPIRank();
694       int mpi_num=getFunctionSpace().getDomain()->getMPISize();
695    #ifdef ESYS_MPI
696       MPI_Status status;
697    #endif
698    
699    #ifdef ESYS_MPI
700       /* Serialize NetCDF I/O */
701       if (mpi_iam>0) MPI_Recv(&ndims, 0, MPI_INT, mpi_iam-1, 81801, MPI_COMM_WORLD, &status);
702    #endif
703    
704     // netCDF error handler     // netCDF error handler
705     NcError err(NcError::verbose_nonfatal);     NcError err(NcError::verbose_nonfatal);
706     // Create the file.     // Create the file.
707     NcFile dataFile(fileName.c_str(), NcFile::Replace);     char *newFileName = Escript_MPI_appendRankToFileName(fileName.c_str(), mpi_num, mpi_iam);
708       NcFile dataFile(newFileName, NcFile::Replace);
709     // check if writing was successful     // check if writing was successful
710     if (!dataFile.is_valid())     if (!dataFile.is_valid())
711          throw DataException("Error - DataExpanded:: opening of netCDF file for output failed.");          throw DataException("Error - DataExpanded:: opening of netCDF file for output failed.");
# Line 681  DataExpanded::dump(const std::string fil Line 719  DataExpanded::dump(const std::string fil
719     if ( rank >0 ) {     if ( rank >0 ) {
720         dims[0]=shape[0];         dims[0]=shape[0];
721         if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )         if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
722              throw DataException("Error - DataExpanded:: appending ncdimsion 0 to netCDF file failed.");              throw DataException("Error - DataExpanded:: appending ncdimension 0 to netCDF file failed.");
723     }     }
724     if ( rank >1 ) {     if ( rank >1 ) {
725         dims[1]=shape[1];         dims[1]=shape[1];
726         if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )         if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
727              throw DataException("Error - DataExpanded:: appending ncdimsion 1 to netCDF file failed.");              throw DataException("Error - DataExpanded:: appending ncdimension 1 to netCDF file failed.");
728     }     }
729     if ( rank >2 ) {     if ( rank >2 ) {
730         dims[2]=shape[2];         dims[2]=shape[2];
731         if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )         if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
732              throw DataException("Error - DataExpanded:: appending ncdimsion 2 to netCDF file failed.");              throw DataException("Error - DataExpanded:: appending ncdimension 2 to netCDF file failed.");
733     }     }
734     if ( rank >3 ) {     if ( rank >3 ) {
735         dims[3]=shape[3];         dims[3]=shape[3];
736         if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )         if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
737              throw DataException("Error - DataExpanded:: appending ncdimsion 3 to netCDF file failed.");              throw DataException("Error - DataExpanded:: appending ncdimension 3 to netCDF file failed.");
738     }     }
739     dims[rank]=getFunctionSpace().getNumDataPointsPerSample();     dims[rank]=getFunctionSpace().getNumDataPointsPerSample();
740     if (! (ncdims[rank] = dataFile.add_dim("num_data_points_per_sample", dims[rank])) )     if (! (ncdims[rank] = dataFile.add_dim("num_data_points_per_sample", dims[rank])) )
# Line 705  DataExpanded::dump(const std::string fil Line 743  DataExpanded::dump(const std::string fil
743     if (! (ncdims[rank+1] = dataFile.add_dim("num_samples", dims[rank+1])) )     if (! (ncdims[rank+1] = dataFile.add_dim("num_samples", dims[rank+1])) )
744              throw DataException("Error - DataExpanded:: appending num_sample to netCDF file failed.");              throw DataException("Error - DataExpanded:: appending num_sample to netCDF file failed.");
745    
746     if (! ( ids = dataFile.add_var("id", ncInt, ncdims[rank+1])) )     if (getFunctionSpace().getNumSamples()>0)
747       {
748    
749         if (! ( ids = dataFile.add_var("id", ncInt, ncdims[rank+1])) )
750          throw DataException("Error - DataExpanded:: appending reference id to netCDF file failed.");          throw DataException("Error - DataExpanded:: appending reference id to netCDF file failed.");
751     const int* ids_p=getFunctionSpace().borrowSampleReferenceIDs();       const int* ids_p=getFunctionSpace().borrowSampleReferenceIDs();
752     if (! (ids->put(ids_p,dims[rank+1])) )       if (! (ids->put(ids_p,dims[rank+1])) )
753          throw DataException("Error - DataExpanded:: copy reference id  to netCDF buffer failed.");          throw DataException("Error - DataExpanded:: copy reference id  to netCDF buffer failed.");
754         if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
    if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )  
755          throw DataException("Error - DataExpanded:: appending variable to netCDF file failed.");          throw DataException("Error - DataExpanded:: appending variable to netCDF file failed.");
756     if (! (var->put(d_ptr,dims)) )       if (! (var->put(d_ptr,dims)) )
757          throw DataException("Error - DataExpanded:: copy data to netCDF buffer failed.");          throw DataException("Error - DataExpanded:: copy data to netCDF buffer failed.");
758       }
759    #ifdef ESYS_MPI
760       if (mpi_iam<mpi_num-1) MPI_Send(&ndims, 0, MPI_INT, mpi_iam+1, 81801, MPI_COMM_WORLD);
761    #endif
762     #else     #else
763     throw DataException("Error - DataExpanded:: dump is not configured with netCDF. Please contact your installation manager.");     throw DataException("Error - DataExpanded:: dump is not configured with netCDF. Please contact your installation manager.");
764     #endif     #endif
765  }  }
766    
767  void  void  
768  DataExpanded::setTaggedValue(int tagKey,  DataExpanded::setTaggedValue(int tagKey,
769                               const DataArrayView& value)             const DataTypes::ShapeType& pointshape,
770                   const DataTypes::ValueType& value,
771               int dataOffset)
772  {  {
773      CHECK_FOR_EX_WRITE
774    int numSamples = getNumSamples();    int numSamples = getNumSamples();
775    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
776    int sampleNo,dataPointNo, i;    int sampleNo,dataPointNo, i;
777    DataArrayView& thisView=getPointDataView();    DataTypes::ValueType::size_type n = getNoValues();
778    DataArrayView::ValueType::size_type n = thisView.noValues();    double* p;
779    double* p,*in=&(value.getData()[0]);    const double* in=&value[0+dataOffset];
780        
781    if (value.noValues() != n) {    if (value.size() != n) {
782      throw DataException("Error - DataExpanded::setTaggedValue: number of input values does not match number of values per data points.");      throw DataException("Error - DataExpanded::setTaggedValue: number of input values does not match number of values per data points.");
783    }    }
784    
# Line 740  DataExpanded::setTaggedValue(int tagKey, Line 787  DataExpanded::setTaggedValue(int tagKey,
787      if (getFunctionSpace().getTagFromSampleNo(sampleNo) == tagKey ) {      if (getFunctionSpace().getTagFromSampleNo(sampleNo) == tagKey ) {
788          for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {          for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
789              p=&(m_data[getPointOffset(sampleNo,dataPointNo)]);              p=&(m_data[getPointOffset(sampleNo,dataPointNo)]);
790              for (int i=0; i<n ;++i) p[i]=in[i];              for (i=0; i<n ;++i) p[i]=in[i];
791          }          }
792      }      }
793    }    }
794  }  }
795    
796    
797    void
798    DataExpanded::reorderByReferenceIDs(int *reference_ids)
799    {
800      CHECK_FOR_EX_WRITE
801      int numSamples = getNumSamples();
802      DataTypes::ValueType::size_type n = getNoValues() * getNumDPPSample();
803      int sampleNo, sampleNo2,i;
804      double* p,*p2;
805      register double rtmp;
806      FunctionSpace fs=getFunctionSpace();
807    
808      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
809         const int id_in=reference_ids[sampleNo];
810         const int id=fs.getReferenceIDOfSample(sampleNo);
811         if (id!=id_in) {
812             bool matched=false;
813             for (sampleNo2 = sampleNo+1; sampleNo2 < numSamples; sampleNo2++) {
814                  if (id == reference_ids[sampleNo2]) {
815                     p=&(m_data[getPointOffset(sampleNo,0)]);
816                     p2=&(m_data[getPointOffset(sampleNo2,0)]);
817                     for (i=0; i<n ;i++) {
818                             rtmp=p[i];
819                             p[i]=p2[i];
820                             p2[i]=rtmp;
821                     }
822                     reference_ids[sampleNo]=id;
823                     reference_ids[sampleNo2]=id_in;
824                     matched=true;
825                     break;
826                  }
827             }
828             if (! matched) {
829                throw DataException("Error - DataExpanded::reorderByReferenceIDs: unable to reorder sample data by reference ids");
830             }
831         }
832       }
833    }
834    
835    DataTypes::ValueType&
836    DataExpanded::getVectorRW()
837    {
838        CHECK_FOR_EX_WRITE
839        return m_data.getData();
840    }
841    
842    const DataTypes::ValueType&
843    DataExpanded::getVectorRO() const
844    {
845        return m_data.getData();
846    }
847    
848    
849    // Idea here is to create an array of seeds by feeding the original seed into the random generator
850    // The code at the beginning of the function to compute the seed if one is given is
851    // just supposed to introduce some variety (and ensure that multiple ranks don't get the same seed).
852    // I make no claim about how well these initial seeds are distributed
853    void DataExpanded::randomFill(long seed)
854    {
855        CHECK_FOR_EX_WRITE
856        static unsigned prevseed=0; // So if we create a bunch of objects we don't get the same start seed
857        if (seed==0)        // for each one
858        {
859        if (prevseed==0)
860        {
861            time_t s=time(0);
862            seed=s;
863        }
864        else
865        {
866            seed=prevseed+419;  // these numbers are arbitrary
867            if (seed>3040101)       // I want to avoid overflow on 32bit systems
868            {
869            seed=((int)(seed)%0xABCD)+1;
870            }
871        }
872        }
873        // now we need to consider MPI since we don't want each rank to start with the same seed
874        seed+=getFunctionSpace().getDomain()->getMPIRank()*getFunctionSpace().getDomain()->getMPISize()*3;
875        prevseed=seed;
876    #ifdef _OPENMP
877        int numthreads=omp_get_max_threads();
878    #else
879        int numthreads=1;
880    #endif
881    
882    #ifdef MKLRANDOM
883        double* seeds=new double[numthreads];
884        VSLStreamStatePtr sstream;
885    
886        int status=vslNewStream(&sstream, VSL_BRNG_MT19937, seed);  // use a Mersenne Twister
887        numeric_limits<double> dlim;
888        vdRngUniform(VSL_METHOD_DUNIFORM_STD, sstream , numthreads, seeds, -1, 1);
889        vslDeleteStream(&sstream);
890        DataVector& dv=getVectorRW();
891        size_t dvsize=dv.size();
892        #pragma omp parallel
893        {
894        int tnum=0;
895        #ifdef _OPENMP
896        tnum=omp_get_thread_num();
897        #endif
898        VSLStreamStatePtr stream;
899        // the 12345 is a hack to give us a better chance of getting different integer seeds.
900            int status=vslNewStream(&stream, VSL_BRNG_MT19937, seeds[tnum]*12345);  // use a Mersenne Twister
901        int bigchunk=(dvsize/numthreads+1);
902        int smallchunk=dvsize-bigchunk*(numthreads-1);
903        int chunksize=(tnum<(numthreads-1))?bigchunk:smallchunk;
904            vdRngUniform(VSL_METHOD_DUNIFORM_STD, stream, chunksize, &(dv[bigchunk*tnum]), 0,1);
905            vslDeleteStream(&stream);
906        }
907        delete[] seeds;
908    #else
909        srand(seed);
910        unsigned* seeds=new unsigned[numthreads];
911        for (int i=0;i<numthreads;++i)
912        {
913        seeds[i]=rand();
914        }
915        DataVector&  dv=getVectorRW();
916        long i;
917        const size_t dvsize=dv.size();
918        #pragma omp parallel private(i)
919        {
920        int tnum=0;
921        #ifdef _OPENMP
922        tnum=omp_get_thread_num();
923        #endif
924        unsigned info=seeds[tnum];
925        
926            #pragma omp for schedule(static)
927            for (i=0;i<dvsize;++i)
928            {
929            dv[i]=(double)rand_r(&info)/RAND_MAX;
930            }
931        }
932        delete[] seeds;
933    #endif
934    }
935    
936  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.1387  
changed lines
  Added in v.3506

  ViewVC Help
Powered by ViewVC 1.1.26