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

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

Legend:
Removed from v.126  
changed lines
  Added in v.3675

  ViewVC Help
Powered by ViewVC 1.1.26