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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 480 - (show annotations)
Wed Feb 1 05:15:12 2006 UTC (13 years, 8 months ago) by jgs
File size: 15060 byte(s)
rationalise #includes and forward declarations

1 // $Id$
2 /*
3 ******************************************************************************
4 * *
5 * COPYRIGHT ACcESS 2004 - All Rights Reserved *
6 * *
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 *
9 * consent of ACcESS. Copying, use or modification of this software *
10 * by any unauthorised person is illegal unless that person has a software *
11 * license agreement with ACcESS. *
12 * *
13 ******************************************************************************
14 */
15
16 #include "DataExpanded.h"
17 #include "DataException.h"
18 #include "DataConstant.h"
19 #include "DataTagged.h"
20
21 #include <boost/python/extract.hpp>
22
23 using namespace std;
24 using namespace boost::python;
25 using namespace boost;
26
27 namespace escript {
28
29 DataExpanded::DataExpanded(const boost::python::numeric::array& value,
30 const FunctionSpace& what)
31 : DataAbstract(what)
32 {
33 DataArrayView::ShapeType tempShape;
34 //
35 // extract the shape of the python numarray
36 for (int i=0; i<value.getrank(); i++) {
37 tempShape.push_back(extract<int>(value.getshape()[i]));
38 }
39 //
40 // initialise the data array for this object
41 initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
42 //
43 // copy the given value to every data point
44 copy(value);
45 }
46
47 DataExpanded::DataExpanded(const DataExpanded& other)
48 : DataAbstract(other.getFunctionSpace()),
49 m_data(other.m_data)
50 {
51 //
52 // create the view for the data
53 DataArrayView temp(m_data.getData(),other.getPointDataView().getShape());
54 setPointDataView(temp);
55 }
56
57 DataExpanded::DataExpanded(const DataConstant& other)
58 : DataAbstract(other.getFunctionSpace())
59 {
60 //
61 // initialise the data array for this object
62 initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());
63 //
64 // DataConstant only has one value, copy this to every data point
65 copy(other.getPointDataView());
66 }
67
68 DataExpanded::DataExpanded(const DataTagged& other)
69 : DataAbstract(other.getFunctionSpace())
70 {
71 //
72 // initialise the data array for this object
73 initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());
74 //
75 // for each data point in this object, extract and copy the corresponding data
76 // value from the given DataTagged object
77 int i,j;
78 DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
79 DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
80 #pragma omp parallel for private(i,j) schedule(static)
81 for (i=0;i<numRows;i++) {
82 for (j=0;j<numCols;j++) {
83 try {
84 getPointDataView().copy(getPointOffset(i,j),
85 other.getPointDataView(),
86 other.getPointOffset(i,j));
87 }
88 catch (std::exception& e) {
89 cout << e.what() << endl;
90 }
91 }
92 }
93 }
94
95 DataExpanded::DataExpanded(const DataExpanded& other,
96 const DataArrayView::RegionType& region)
97 : DataAbstract(other.getFunctionSpace())
98 {
99 //
100 // get the shape of the slice
101 DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
102 //
103 // initialise this Data object to the shape of the slice
104 initialise(shape,other.getNumSamples(),other.getNumDPPSample());
105 //
106 // copy the data
107 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
108 DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
109 DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
110 int i,j;
111 #pragma omp parallel for private(i,j) schedule(static)
112 for (i=0;i<numRows;i++) {
113 for (j=0;j<numCols;j++) {
114 try {
115 getPointDataView().copySlice(getPointOffset(i,j),
116 other.getPointDataView(),
117 other.getPointOffset(i,j),
118 region_loop_range);
119 }
120 catch (std::exception& e) {
121 cout << e.what() << endl;
122 }
123 }
124 }
125 }
126
127 DataExpanded::DataExpanded(const DataArrayView& value,
128 const FunctionSpace& what)
129 : DataAbstract(what)
130 {
131 //
132 // get the shape of the given data value
133 DataArrayView::ShapeType tempShape=value.getShape();
134 //
135 // initialise this Data object to the shape of the given data value
136 initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
137 //
138 // copy the given value to every data point
139 copy(value);
140 }
141
142 DataExpanded::DataExpanded(const FunctionSpace& what,
143 const DataArrayView::ShapeType &shape,
144 const DataArrayView::ValueType &data)
145 : DataAbstract(what)
146 {
147 //
148 // create the view of the data
149 initialise(shape,what.getNumSamples(),what.getNumDPPSample());
150 //
151 // copy the data in the correct format
152 m_data.getData()=data;
153 }
154
155 DataExpanded::~DataExpanded()
156 {
157 }
158
159 void
160 DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)
161 {
162 if (getPointDataView().getRank()!=0) {
163 stringstream temp;
164 temp << "Error - Can only reshape Data with data points of rank 0. "
165 << "This Data has data points with rank: "
166 << getPointDataView().getRank();
167 throw DataException(temp.str());
168 }
169 //
170 // create the new DataBlocks2D data array, and a corresponding DataArrayView
171 DataBlocks2D newData(getNumSamples(),getNumDPPSample(),DataArrayView::noValues(shape));
172 DataArrayView newView(newData.getData(),shape);
173 //
174 // Copy the original data to every value for the new shape
175 int i,j;
176 int nRows=m_data.getNumRows();
177 int nCols=m_data.getNumCols();
178 #pragma omp parallel for private(i,j) schedule(static)
179 for (i=0;i<nRows;i++) {
180 for (j=0;j<nCols;j++) {
181 // NOTE: An exception may be thown from this call if
182 // DOASSERT is on which of course will play
183 // havoc with the omp threads. Run single threaded
184 // if using DOASSERT.
185 newView.copy(newData.index(i,j),m_data(i,j));
186 }
187 }
188 // swap the new data array for the original
189 m_data.Swap(newData);
190 // set the corresponding DataArrayView
191 DataArrayView temp(m_data.getData(),shape);
192 setPointDataView(temp);
193 }
194
195 DataAbstract*
196 DataExpanded::getSlice(const DataArrayView::RegionType& region) const
197 {
198 return new DataExpanded(*this,region);
199 }
200
201 void
202 DataExpanded::setSlice(const DataAbstract* value,
203 const DataArrayView::RegionType& region)
204 {
205 const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);
206 if (tempDataExp==0) {
207 throw DataException("Programming error - casting to DataExpanded.");
208 }
209 //
210 // get shape of slice
211 DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
212 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
213 //
214 // check shape
215 if (getPointDataView().getRank()!=region.size()) {
216 throw DataException("Error - Invalid slice region.");
217 }
218 if (tempDataExp->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {
219 throw DataException (value->getPointDataView().createShapeErrorMessage(
220 "Error - Couldn't copy slice due to shape mismatch.",shape));
221 }
222 //
223 // copy the data from the slice into this object
224 DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
225 DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
226 int i, j;
227 #pragma omp parallel for private(i,j) schedule(static)
228 for (i=0;i<numRows;i++) {
229 for (j=0;j<numCols;j++) {
230 getPointDataView().copySliceFrom(getPointOffset(i,j),
231 tempDataExp->getPointDataView(),
232 tempDataExp->getPointOffset(i,j),
233 region_loop_range);
234 }
235 }
236 }
237
238 void
239 DataExpanded::copy(const DataArrayView& value)
240 {
241 //
242 // copy a single value to every data point in this object
243 int nRows=m_data.getNumRows();
244 int nCols=m_data.getNumCols();
245 int i,j;
246 #pragma omp parallel for private(i,j) schedule(static)
247 for (i=0;i<nRows;i++) {
248 for (j=0;j<nCols;j++) {
249 // NOTE: An exception may be thown from this call if
250 // DOASSERT is on which of course will play
251 // havoc with the omp threads. Run single threaded
252 // if using DOASSERT.
253 getPointDataView().copy(m_data.index(i,j),value);
254 }
255 }
256 }
257
258 void
259 DataExpanded::copy(const boost::python::numeric::array& value)
260 {
261 //
262 // first convert the numarray into a DataArray object
263 DataArray temp(value);
264 //
265 // check the input shape matches this shape
266 if (!getPointDataView().checkShape(temp.getView().getShape())) {
267 throw DataException(getPointDataView().createShapeErrorMessage(
268 "Error - (DataExpanded) Cannot copy due to shape mismatch.",
269 temp.getView().getShape()));
270 }
271 //
272 // now copy over the data
273 copy(temp.getView());
274 }
275
276 void
277 DataExpanded::initialise(const DataArrayView::ShapeType& shape,
278 int noSamples,
279 int noDataPointsPerSample)
280 {
281 //
282 // resize data array to the required size
283 m_data.resize(noSamples,noDataPointsPerSample,DataArrayView::noValues(shape));
284 //
285 // create the data view of the data array
286 DataArrayView temp(m_data.getData(),shape);
287 setPointDataView(temp);
288 }
289
290 string
291 DataExpanded::toString() const
292 {
293 stringstream temp;
294 //
295 // create a temporary view as the offset will be changed
296 DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());
297 for (int i=0;i<m_data.getNumRows();i++) {
298 for (int j=0;j<m_data.getNumCols();j++) {
299 tempView.setOffset(m_data.index(i,j));
300 stringstream suffix;
301 suffix << "(" << i << "," << j << ")";
302 temp << tempView.toString(suffix.str());
303 if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
304 temp << endl;
305 }
306 }
307 }
308 return temp.str();
309 }
310
311 DataArrayView::ValueType::size_type
312 DataExpanded::getPointOffset(int sampleNo,
313 int dataPointNo) const
314 {
315 return m_data.index(sampleNo,dataPointNo);
316 }
317
318 DataArrayView
319 DataExpanded::getDataPoint(int sampleNo,
320 int dataPointNo)
321 {
322 DataArrayView temp(m_data.getData(),getPointDataView().getShape(),m_data.index(sampleNo,dataPointNo));
323 return temp;
324 }
325
326 DataArrayView::ValueType::size_type
327 DataExpanded::getLength() const
328 {
329 return m_data.size();
330 }
331
332 void
333 DataExpanded::setRefValue(int ref,
334 const DataArray& value)
335 {
336 //
337 // Get the number of samples and data-points per sample.
338 int numSamples = getNumSamples();
339 int numDPPSample = getNumDPPSample();
340
341 //
342 // Determine the sample number which corresponds to this reference number.
343 int sampleNo = -1;
344 int tempRef = -1;
345 for (int n=0; n<numSamples; n++) {
346 tempRef = getFunctionSpace().getReferenceNoFromSampleNo(n);
347 if (tempRef == ref) {
348 sampleNo = n;
349 break;
350 }
351 }
352 if (sampleNo == -1) {
353 throw DataException("DataExpanded::setRefValue error: invalid ref number supplied.");
354 }
355
356 for (int n=0; n<numDPPSample; n++) {
357 //
358 // Get *each* data-point in the sample in turn.
359 DataArrayView pointView = getDataPoint(sampleNo, n);
360 //
361 // Assign the values in the DataArray to this data-point.
362 pointView.copy(value.getView());
363 }
364 }
365
366 void
367 DataExpanded::getRefValue(int ref,
368 DataArray& value)
369 {
370 //
371 // Get the number of samples and data-points per sample.
372 int numSamples = getNumSamples();
373 int numDPPSample = getNumDPPSample();
374
375 //
376 // Determine the sample number which corresponds to this reference number
377 int sampleNo = -1;
378 int tempRef = -1;
379 for (int n=0; n<numSamples; n++) {
380 tempRef = getFunctionSpace().getReferenceNoFromSampleNo(n);
381 if (tempRef == ref) {
382 sampleNo = n;
383 break;
384 }
385 }
386 if (sampleNo == -1) {
387 throw DataException("DataExpanded::getRefValue error: invalid ref number supplied.");
388 }
389
390 //
391 // Get the *first* data-point associated with this sample number.
392 DataArrayView pointView = getDataPoint(sampleNo, 0);
393
394 //
395 // Load the values from this data-point into the DataArray
396 value.getView().copy(pointView);
397 }
398
399 int
400 DataExpanded::archiveData(ofstream& archiveFile,
401 const DataArrayView::ValueType::size_type noValues) const
402 {
403 return(m_data.archiveData(archiveFile, noValues));
404 }
405
406 int
407 DataExpanded::extractData(ifstream& archiveFile,
408 const DataArrayView::ValueType::size_type noValues)
409 {
410 return(m_data.extractData(archiveFile, noValues));
411 }
412
413 void
414 DataExpanded::copyAll(const boost::python::numeric::array& value) {
415 //
416 // Get the number of samples and data-points per sample.
417 int numSamples = getNumSamples();
418 int numDataPointsPerSample = getNumDPPSample();
419 int dataPointRank = getPointDataView().getRank();
420 ShapeType dataPointShape = getPointDataView().getShape();
421 //
422 // check rank:
423 if (value.getrank()!=dataPointRank+1)
424 throw DataException("Rank of numarray does not match Data object rank");
425 if (value.getshape()[0]!=numSamples*numDataPointsPerSample)
426 throw DataException("leading dimension of numarray is too small");
427 //
428 int dataPoint = 0;
429 for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {
430 for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
431 DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);
432 if (dataPointRank==0) {
433 dataPointView()=extract<double>(value[dataPoint]);
434 } else if (dataPointRank==1) {
435 for (int i=0; i<dataPointShape[0]; i++) {
436 dataPointView(i)=extract<double>(value[dataPoint][i]);
437 }
438 } else if (dataPointRank==2) {
439 for (int i=0; i<dataPointShape[0]; i++) {
440 for (int j=0; j<dataPointShape[1]; j++) {
441 dataPointView(i,j)=extract<double>(value[dataPoint][i][j]);
442 }
443 }
444 } else if (dataPointRank==3) {
445 for (int i=0; i<dataPointShape[0]; i++) {
446 for (int j=0; j<dataPointShape[1]; j++) {
447 for (int k=0; k<dataPointShape[2]; k++) {
448 dataPointView(i,j,k)=extract<double>(value[dataPoint][i][j][k]);
449 }
450 }
451 }
452 } else if (dataPointRank==4) {
453 for (int i=0; i<dataPointShape[0]; i++) {
454 for (int j=0; j<dataPointShape[1]; j++) {
455 for (int k=0; k<dataPointShape[2]; k++) {
456 for (int l=0; l<dataPointShape[3]; l++) {
457 dataPointView(i,j,k,l)=extract<double>(value[dataPoint][i][j][k][l]);
458 }
459 }
460 }
461 }
462 }
463 dataPoint++;
464 }
465 }
466 }
467 } // end of namespace

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26