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

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

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

revision 97 by jgs, Tue Dec 14 05:39:33 2004 UTC revision 100 by jgs, Wed Dec 15 03:48:48 2004 UTC
# Line 1  Line 1 
 // $Id$  
1  /*  /*
2   ******************************************************************************   ******************************************************************************
3   *                                                                            *   *                                                                            *
# Line 29  using namespace boost; Line 28  using namespace boost;
28    
29  namespace escript {  namespace escript {
30    
31  DataExpanded::DataExpanded(const boost::python::numeric::array& value,    DataExpanded::DataExpanded(const boost::python::numeric::array& value, const FunctionSpace& what) : DataAbstract(what)
32                             const FunctionSpace& what)    {
33    : DataAbstract(what)      //cout << "Calling DataExpanded constructor 1." << endl;
34  {      DataArrayView::ShapeType tempShape;
35    DataArrayView::ShapeType tempShape;      //
36    //      // extract the shape from the python array
37    // extract the shape from the python array      for (int i=0; i<value.getrank(); ++i) {
38    for (int i=0; i<value.getrank(); ++i) {        cout << extract<int>(value.getshape()[i]) << endl;
39      //cout << extract<int>(value.getshape()[i]) << endl;        tempShape.push_back(extract<int>(value.getshape()[i]));
40      tempShape.push_back(extract<int>(value.getshape()[i]));      }
41    }      initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
42    initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());      //
43    //      // copy the given value to every data point
44    // copy the given value to every data point      copy(value);
45    copy(value);    }
46  }  
47      DataExpanded::DataExpanded(const DataExpanded& other) : DataAbstract(other.getFunctionSpace()), m_data(other.m_data)
48  DataExpanded::DataExpanded(const DataExpanded& other)    {    
49    : DataAbstract(other.getFunctionSpace()),      //cout << "Calling DataExpanded copy constructor." << endl;
50    m_data(other.m_data)      //
51  {          // create the view for the data
52    //      DataArrayView temp(m_data.getData(),other.getPointDataView().getShape());
53    // create the view for the data      setPointDataView(temp);
54    DataArrayView temp(m_data.getData(),other.getPointDataView().getShape());    }
55    setPointDataView(temp);  
56  }    DataExpanded::DataExpanded(const DataConstant& other) : DataAbstract(other.getFunctionSpace())
57      {    
58  DataExpanded::DataExpanded(const DataConstant& other)      //cout << "Creating DataExpanded from DataConstant." << endl;
59    : DataAbstract(other.getFunctionSpace())      initialise(other.getPointDataView().getShape(),other.getNumSamples(), other.getNumDPPSample());
60  {          //
61    initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());      // DataConstant only has one value
62    //      copy(other.getPointDataView());
63    // DataConstant only has one value    }
64    copy(other.getPointDataView());  
65  }    DataExpanded::DataExpanded(const DataTagged& other) : DataAbstract(other.getFunctionSpace())
66      {    
67  DataExpanded::DataExpanded(const DataTagged& other)      //cout << "Creating DataExpanded from DataTagged." << endl;
68    : DataAbstract(other.getFunctionSpace())      initialise(other.getPointDataView().getShape(),other.getNumSamples(), other.getNumDPPSample());
69  {          int i,j;
70    initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());      DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
71    int i,j;      DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
   DataArrayView::ValueType::size_type numRows=m_data.getNumRows();  
   DataArrayView::ValueType::size_type numCols=m_data.getNumCols();  
72  #pragma omp parallel for private(i,j) schedule(static)  #pragma omp parallel for private(i,j) schedule(static)
73    for (i=0;i<numRows;++i) {      for (i=0;i<numRows;++i) {
74      for (j=0;j<numCols;++j) {        for (j=0;j<numCols;++j) {
75        try {      try {
76          getPointDataView().copy(getPointOffset(i,j), other.getPointDataView(), other.getPointOffset(i,j));        getPointDataView().copy(getPointOffset(i,j), other.getPointDataView(), other.getPointOffset(i,j));
77        }      }
78        catch (std::exception& e) {      catch (std::exception& e) {
79          cout << e.what() << endl;        cout << e.what() << endl;
80        }
81        }        }
82      }      }
83    }    }
 }  
84    
85  DataExpanded::DataExpanded(const DataExpanded& other,    DataExpanded::DataExpanded(const DataExpanded& other, const DataArrayView::RegionType& region) : DataAbstract(other.getFunctionSpace())
86                             const DataArrayView::RegionType& region)    {
87    : DataAbstract(other.getFunctionSpace())      //cout << "Creating DataExpanded by slicing from another DataExpanded." << endl;
88  {      //
89    //      // get the shape of the slice
90    // get the shape of the slice      DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
91    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));      initialise(shape,other.getNumSamples(),other.getNumDPPSample());
92    //      //
93    // initialise this Data object to the shape of the slice      // copy the data
94    initialise(shape,other.getNumSamples(),other.getNumDPPSample());      DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
95    //      DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
96    // copy the data      int i,j;
   DataArrayView::ValueType::size_type numRows=m_data.getNumRows();  
   DataArrayView::ValueType::size_type numCols=m_data.getNumCols();  
   int i,j;  
97  #pragma omp parallel for private(i,j) schedule(static)  #pragma omp parallel for private(i,j) schedule(static)
98    for (i=0;i<numRows;++i) {      for (i=0;i<numRows;++i) {
99      for (j=0;j<numCols;++j) {        for (j=0;j<numCols;++j) {
100        try {      try {
101          getPointDataView().copySlice(getPointOffset(i,j),other.getPointDataView(),other.getPointOffset(i,j),region);        getPointDataView().copySlice(getPointOffset(i,j), other.getPointDataView(), other.getPointOffset(i,j), region);
102        }
103        catch (std::exception& e) {
104          cout << e.what() << endl;
105        }
106        }        }
107        catch (std::exception& e) {      }
108          cout << e.what() << endl;    }
109    
110      DataExpanded::DataExpanded(const DataArrayView& value,const FunctionSpace& what): DataAbstract(what)
111      {
112        //cout << "Calling DataExpanded constructor 2." << endl;
113        DataArrayView::ShapeType tempShape=value.getShape();
114        initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
115        copy(value);
116      }
117    
118      DataExpanded::~DataExpanded()
119      {
120        //cout << "Destructing DataExpanded." << endl;
121      }
122    
123      DataAbstract* DataExpanded::getSlice(const DataArrayView::RegionType& region) const
124      {
125        return new DataExpanded(*this,region);
126      }
127    
128      void DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)
129      {
130        //
131        // reshape a rank zero data point
132        if (getPointDataView().getRank()!=0) {
133          stringstream temp;
134          temp << "Error - Can only reshape Data with data points of rank 0. "
135           << "This Data has data points with rank: "
136           << getPointDataView().getRank();
137          throw DataException(temp.str());
138        }
139        DataBlocks2D newData(getNumSamples(),getNumDPPSample(), DataArrayView::noValues(shape));
140        DataArrayView newView(newData.getData(),shape);
141        //
142        // Copy the original data to every value for the new shape
143        int i,j;
144        int nRows=m_data.getNumRows();
145        int nCols=m_data.getNumCols();
146        #pragma omp parallel for private(i,j) schedule(static)
147        for (i=0;i<nRows;++i) {
148          for (j=0;j<nCols;++j) {
149        //
150        // Copy the data into the specified offset
151        // NOTE: An exception may be thown from this call if
152        // DOASSERT is on which of course will play
153        // havoc with the omp threads. Run single threaded
154        // if using DOASSERT.
155        newView.copy(newData.index(i,j),m_data.getData()[m_data.index(i,j)]);
156        }        }
157      }      }
158        m_data.Swap(newData);
159        DataArrayView temp(m_data.getData(),shape);
160        setPointDataView(temp);
161    }    }
 }  
162    
163  DataExpanded::DataExpanded(const DataArrayView& value,    void DataExpanded::setSlice(const DataAbstract* value, const DataArrayView::RegionType& region)
164                             const FunctionSpace& what)    {
165    : DataAbstract(what)      const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);
166  {      if (tempDataExp==0) {
167    DataArrayView::ShapeType tempShape=value.getShape();        throw DataException("Programming error - casting to DataExpanded.");
168    initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());      }
169    copy(value);      if (getPointDataView().getRank()!=region.size()) {
170  }        throw DataException("Error - Invalid slice region.");
171        }
172  DataExpanded::~DataExpanded()      if (!value->getPointDataView().checkShape(DataArrayView::getResultSliceShape(region))) {
173  {        throw DataException (value->getPointDataView().createShapeErrorMessage(
174  }                  "Error - Couldn't copy slice due to shape mismatch.",DataArrayView::getResultSliceShape(region)));
175        }
176  void      //
177  DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)      // copy the data
178  {      DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
179    //      DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
180    // reshape a rank zero data point      int i,j;
   if (getPointDataView().getRank()!=0) {  
     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());  
   }  
   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) {  
       //  
       // Copy the data into the specified offset  
       // 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.getData()[m_data.index(i,j)]);  
     }  
   }  
   m_data.Swap(newData);  
   DataArrayView temp(m_data.getData(),shape);  
   setPointDataView(temp);  
 }  
   
 DataAbstract*  
 DataExpanded::getSlice(const DataArrayView::RegionType& region) const  
 {  
   return new DataExpanded(*this,region);  
 }  
   
 void  
 DataExpanded::setSlice(const DataAbstract* value,  
                        const DataArrayView::RegionType& region)  
 {  
   const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);  
   if (tempDataExp==0) {  
     throw DataException("Programming error - casting to DataExpanded.");  
   }  
   if (getPointDataView().getRank()!=region.size()) {  
     throw DataException("Error - Invalid slice region.");  
   }  
   if (!value->getPointDataView().checkShape(DataArrayView::getResultSliceShape(region))) {  
     throw DataException (value->getPointDataView().createShapeErrorMessage(  
         "Error - Couldn't copy slice due to shape mismatch.",DataArrayView::getResultSliceShape(region)));  
   }  
   //  
   // copy the data  
   DataArrayView::ValueType::size_type numRows=m_data.getNumRows();  
   DataArrayView::ValueType::size_type numCols=m_data.getNumCols();  
   int i, j;  
181  #pragma omp parallel for private(i,j) schedule(static)  #pragma omp parallel for private(i,j) schedule(static)
182    for (i=0;i<numRows;i++) {      for (i=0;i<numRows;++i) {
183      for (j=0;j<numCols;j++) {        for (j=0;j<numCols;++j) {
184        getPointDataView().copySliceFrom(getPointOffset(i,j),tempDataExp->getPointDataView(),tempDataExp->getPointOffset(i,j),region);      getPointDataView().copySliceFrom(getPointOffset(i,j), tempDataExp->getPointDataView(), tempDataExp->getPointOffset(i,j), region);
185          }
186      }      }
187    }    }
188  }  
189      void DataExpanded::copy(const DataArrayView& value)
190  void    {
191  DataExpanded::copy(const DataArrayView& value)      //
192  {      // Copy a single value to every data point
193    //      int i,j;
194    // Copy a single value to every data point      int nRows=m_data.getNumRows();
195    int nRows=m_data.getNumRows();      int nCols=m_data.getNumCols();
196    int nCols=m_data.getNumCols();      #pragma omp parallel for private(i,j) schedule(static)
197    int i, j;      for (i=0;i<nRows;++i) {
198  #pragma omp parallel for private(i,j) schedule(static)        for (j=0;j<nCols;++j) {
199    for (i=0;i<nRows;++i) {      //
200      for (j=0;j<nCols;++j) {      // Copy the data into the specified offset
201        //      // NOTE: An exception may be thown from this call if
202        // Copy the data into the specified offset      // DOASSERT is on which of course will play
203        // NOTE: An exception may be thown from this call if      // havoc with the omp threads. Run single threaded
204        // DOASSERT is on which of course will play      // if using DOASSERT.
205        // havoc with the omp threads. Run single threaded      getPointDataView().copy(m_data.index(i,j),value);
       // if using DOASSERT.  
       getPointDataView().copy(m_data.index(i,j),value);  
     }  
   }  
 }  
   
 void  
 DataExpanded::copy(const boost::python::numeric::array& value)  
 {  
   //  
   // first convert the numarray into a DataArrayView format  
   DataArray temp(value);  
   //  
   // check the input shape matches this shape, this will throw an exception  
   if (!getPointDataView().checkShape(temp.getView().getShape())) {  
     throw DataException(getPointDataView().createShapeErrorMessage(  
                         "Error - (DataExpanded) Cannot copy due to shape mismatch.",  
                         temp.getView().getShape()));  
   }  
   //  
   // now copy over the entire data structure  
   copy(temp.getView());  
 }  
   
 void  
 DataExpanded::initialise(const DataArrayView::ShapeType& shape,  
                          int noSamples,  
                          int noDataPointsPerSample)  
 {  
   //  
   // resize data to the required size  
   m_data.resize(noSamples,noDataPointsPerSample,DataArrayView::noValues(shape));  
   //  
   // create a point data viewer of the data  
   DataArrayView temp(m_data.getData(),shape);  
   setPointDataView(temp);  
 }  
   
 string  
 DataExpanded::toString() const  
 {  
   stringstream temp;  
   //  
   // create a temporary view as the offset will be changed  
   DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());  
   for (int i=0;i<m_data.getNumRows();++i) {  
     for (int j=0;j<m_data.getNumCols();++j) {  
       tempView.setOffset(m_data.index(i,j));  
       stringstream suffix;  
       suffix << "(" << i << "," << j << ")";  
       temp << tempView.toString(suffix.str());  
       if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {  
         temp << endl;  
206        }        }
207      }      }
208    }    }
   return temp.str();  
 }  
209    
210  DataArrayView::ValueType::size_type    void DataExpanded::copy(const boost::python::numeric::array& value)
211  DataExpanded::getPointOffset(int sampleNo,    {
212                               int dataPointNo) const      //
213  {      // first convert the numarray into a DataArrayView format
214    return m_data.index(sampleNo,dataPointNo);      DataArray temp(value);
215  }      //
216        // check the input shape matches this shape, this will throw an exception
217  DataArrayView      if (!getPointDataView().checkShape(temp.getView().getShape())) {
218  DataExpanded::getDataPoint(int sampleNo,        throw DataException(getPointDataView().createShapeErrorMessage(
219                             int dataPointNo)           "Error - (DataExpanded) Cannot copy due to shape mismatch.",
220  {           temp.getView().getShape()));
221    DataArrayView temp(m_data.getData(),getPointDataView().getShape(),m_data.index(sampleNo,dataPointNo));      }
222    return temp;      //
223  }      // now copy over the entire data structure
224        copy(temp.getView());
225  DataArrayView::ValueType::size_type    }
226  DataExpanded::getLength() const  
227  {    void DataExpanded::initialise(const DataArrayView::ShapeType& shape, int noSamples, int noDataPointsPerSample)
228    return m_data.getData().size();    {
229  }      //
230        // resize data to the required size
231        //cout << "noDataPointsPerSample=" << noDataPointsPerSample
232        //     << " noSamples=" << noSamples
233        //     << " noValuesPerDataPoint=" << DataArrayView::noValues(shape) << endl;
234        m_data.resize(noSamples,noDataPointsPerSample, DataArrayView::noValues(shape));
235        //
236        // create a point data viewer of the data
237        DataArrayView temp(m_data.getData(),shape);
238        setPointDataView(temp);
239        //cout << "leaving initialise." << endl;
240      }
241    
242      string DataExpanded::toString() const
243      {
244        stringstream temp;
245        //
246        // create a temporary view as the offset will be changed
247        DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape(), getPointDataView().getOffset());
248        for (int i=0;i<m_data.getNumRows();++i) {
249          for (int j=0;j<m_data.getNumCols();++j) {
250            tempView.setOffset(m_data.index(i,j));
251        stringstream suffix;
252        suffix << "(" << i << "," << j << ")";
253        temp << tempView.toString(suffix.str());
254        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
255          temp << endl;
256        }
257          }
258        }
259        return temp.str();
260      }
261    
262      DataArrayView::ValueType::size_type DataExpanded::getPointOffset(int sampleNo, int dataPointNo) const
263      {
264        return m_data.index(sampleNo,dataPointNo);
265      }
266    
267      DataArrayView DataExpanded::getDataPoint(int sampleNo, int dataPointNo)
268      {
269        DataArrayView temp(m_data.getData(), getPointDataView().getShape(), m_data.index(sampleNo,dataPointNo));
270        return temp;
271      }
272    
273      DataArrayView::ValueType::size_type DataExpanded::getLength() const
274      {
275        return m_data.getData().size();
276      }
277    
278  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.97  
changed lines
  Added in v.100

  ViewVC Help
Powered by ViewVC 1.1.26