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

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

  ViewVC Help
Powered by ViewVC 1.1.26