/[escript]/branches/lapack2681/escript/src/DataConstant.cpp
ViewVC logotype

Contents of /branches/lapack2681/escript/src/DataConstant.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2740 - (show annotations)
Tue Nov 10 06:48:24 2009 UTC (9 years, 8 months ago) by jfenwick
File size: 12113 byte(s)


1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2009 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14
15 #include "Data.h"
16 #include "DataConstant.h"
17 #include "DataException.h"
18 #include "esysUtils/EsysAssert.h"
19
20 #include <iostream>
21 #include <boost/python/extract.hpp>
22 #ifdef USE_NETCDF
23 #include <netcdfcpp.h>
24 #endif
25 #ifdef PASO_MPI
26 #include <mpi.h>
27 #endif
28
29 #include <boost/python/extract.hpp>
30 #include <boost/scoped_ptr.hpp>
31 #include "DataMaths.h"
32
33 // #define CHECK_FOR_EX_WRITE if (!checkNoSharing()) {throw DataException("Attempt to modify shared object");}
34
35 #define CHECK_FOR_EX_WRITE if (!checkNoSharing()) {std::ostringstream ss; ss << " Attempt to modify shared object. line " << __LINE__ << " of " << __FILE__; ss << m_owners.size(); cerr << ss << endl; /* *((int*)0)=17; */throw DataException(ss.str());}
36
37 using namespace std;
38 using namespace boost::python;
39
40 namespace escript {
41
42 DataConstant::DataConstant(const WrappedArray& value,
43 const FunctionSpace& what)
44 : parent(what,value.getShape())
45 {
46 m_data.copyFromArray(value,1);
47 }
48
49 DataConstant::DataConstant(const DataConstant& other)
50 : parent(other.getFunctionSpace(),other.getShape())
51 {
52 m_data=other.m_data;
53 }
54
55 DataConstant::DataConstant(const DataConstant& other,
56 const DataTypes::RegionType& region)
57 : parent(other.getFunctionSpace(),DataTypes::getResultSliceShape(region))
58 {
59 //
60 // allocate space for this new DataConstant's data
61 int len = getNoValues();
62 m_data.resize(len,0.,len);
63 //
64 // create a view of the data with the correct shape
65 DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);
66 //
67 // load the view with the data from the slice
68 DataTypes::copySlice(m_data,getShape(),0,other.getVectorRO(),other.getShape(),0,region_loop_range);
69 }
70
71 DataConstant::DataConstant(const FunctionSpace& what,
72 const DataTypes::ShapeType &shape,
73 const DataTypes::ValueType &data)
74 : parent(what,shape)
75 {
76 //
77 // copy the data in the correct format
78 m_data=data;
79 //
80 // create the view of the data
81 // DataArrayView tempView(m_data,shape);
82 // setPointDataView(tempView);
83 }
84
85 string
86 DataConstant::toString() const
87 {
88 return DataTypes::pointToString(m_data,getShape(),0,"");
89 }
90
91
92 DataAbstract*
93 DataConstant::deepCopy()
94 {
95 return new DataConstant(*this);
96 }
97
98
99 DataTypes::ValueType::size_type
100 DataConstant::getPointOffset(int sampleNo,
101 int dataPointNo) const
102 {
103 // We avoid this check for constant data due to issues manipulating
104 // data with no samples.
105
106 // EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
107 // "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
108 //
109 // Whatever the coord's always return the same value as this is constant data.
110 return 0;
111 }
112
113 DataTypes::ValueType::size_type
114 DataConstant::getPointOffset(int sampleNo,
115 int dataPointNo)
116 {
117 // We avoid this check for constant data due to issues manipulating
118 // data with no samples.
119
120 // EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
121 // "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
122 //
123 // Whatever the coord's always return the same value as this is constant data.
124 //
125
126 return 0;
127 }
128
129 DataTypes::ValueType::size_type
130 DataConstant::getLength() const
131 {
132 return m_data.size();
133 }
134
135 DataAbstract*
136 DataConstant::getSlice(const DataTypes::RegionType& region) const
137 {
138 return new DataConstant(*this,region);
139 }
140
141 void
142 DataConstant::setSlice(const DataAbstract* value,
143 const DataTypes::RegionType& region)
144 {
145 const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);
146 if (tempDataConst==0) {
147 throw DataException("Programming error - casting to DataConstant.");
148 }
149 CHECK_FOR_EX_WRITE
150 //
151 DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));
152 DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);
153 //
154 // check shape:
155 if (getRank()!=region.size()) {
156 throw DataException("Error - Invalid slice region.");
157 }
158 if (getRank()>0 && !DataTypes::checkShape(value->getShape(),shape)) {
159 throw DataException (DataTypes::createShapeErrorMessage(
160 "Error - Couldn't copy slice due to shape mismatch.",shape,value->getShape()));
161 }
162 // getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);
163 DataTypes::copySliceFrom(m_data,getShape(),0,tempDataConst->getVectorRO(), tempDataConst->getShape(),0,region_loop_range);
164 }
165
166
167
168 void
169 DataConstant::symmetric(DataAbstract* ev)
170 {
171 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
172 if (temp_ev==0) {
173 throw DataException("Error - DataConstant::symmetric: casting to DataConstant failed (propably a programming error).");
174 }
175 DataMaths::symmetric(m_data,getShape(),0,temp_ev->getVectorRW(), temp_ev->getShape(),0);
176 }
177
178 void
179 DataConstant::nonsymmetric(DataAbstract* ev)
180 {
181 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
182 if (temp_ev==0) {
183 throw DataException("Error - DataConstant::nonsymmetric: casting to DataConstant failed (propably a programming error).");
184 }
185 DataMaths::nonsymmetric(m_data,getShape(),0,temp_ev->getVectorRW(), temp_ev->getShape(),0);
186 }
187
188 void
189 DataConstant::trace(DataAbstract* ev, int axis_offset)
190 {
191 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
192 if (temp_ev==0) {
193 throw DataException("Error - DataConstant::trace: casting to DataConstant failed (propably a programming error).");
194 }
195 ValueType& evVec=temp_ev->getVectorRW();
196 const ShapeType& evShape=temp_ev->getShape();
197 DataMaths::trace(m_data,getShape(),0,evVec,evShape,0,axis_offset);
198 }
199
200 void
201 DataConstant::swapaxes(DataAbstract* ev, int axis0, int axis1)
202 {
203 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
204 if (temp_ev==0) {
205 throw DataException("Error - DataConstant::swapaxes: casting to DataConstant failed (propably a programming error).");
206 }
207 DataMaths::swapaxes(m_data,getShape(),0,temp_ev->getVectorRW(), temp_ev->getShape(),0,axis0,axis1);
208 }
209
210 void
211 DataConstant::transpose(DataAbstract* ev, int axis_offset)
212 {
213 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
214 if (temp_ev==0) {
215 throw DataException("Error - DataConstant::transpose: casting to DataConstant failed (propably a programming error).");
216 }
217 DataMaths::transpose(m_data, getShape(),0, temp_ev->getVectorRW(),temp_ev->getShape(),0,axis_offset);
218 }
219
220 void
221 DataConstant::eigenvalues(DataAbstract* ev)
222 {
223 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
224 if (temp_ev==0) {
225 throw DataException("Error - DataConstant::eigenvalues: casting to DataConstant failed (propably a programming error).");
226 }
227 DataMaths::eigenvalues(m_data,getShape(),0,temp_ev->getVectorRW(), temp_ev->getShape(),0);
228 }
229 void
230 DataConstant::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
231 {
232 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
233 if (temp_ev==0) {
234 throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
235 }
236 DataConstant* temp_V=dynamic_cast<DataConstant*>(V);
237 if (temp_V==0) {
238 throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
239 }
240 DataMaths::eigenvalues_and_eigenvectors(m_data, getShape(),0,temp_ev->getVectorRW(), temp_ev->getShape(),0,temp_V->getVectorRW(), temp_V->getShape(),0,tol);
241 }
242
243
244
245 void
246 DataConstant::matrixInverse(DataAbstract* out) const
247 {
248 DataConstant* temp=dynamic_cast<DataConstant*>(out);
249 if (temp==0)
250 {
251 throw DataException("Error - DataConstant::matrixInverse: casting to DataConstant failed (propably a programming error).");
252 }
253 if (getRank()!=2)
254 {
255 throw DataException("Error - DataExpanded::matrixInverse: input must be rank 2.");
256 }
257 int* p=new int[getShape()[0]];
258 boost::scoped_ptr<int> piv(p);
259 int res=DataMaths::matrix_inverse(m_data, getShape(), 0, temp->getVectorRW(), temp->getShape(), 0, 1, p);
260 if (res)
261 {
262 DataMaths::matrixInverseError(res); // throws exceptions
263 }
264 }
265
266 void
267 DataConstant::setToZero()
268 {
269 CHECK_FOR_EX_WRITE
270 DataTypes::ValueType::size_type n=m_data.size();
271 for (int i=0; i<n ;++i) m_data[i]=0.;
272 }
273
274 void
275 DataConstant::dump(const std::string fileName) const
276 {
277 #ifdef USE_NETCDF
278 const NcDim* ncdims[DataTypes::maxRank];
279 NcVar* var;
280 int rank = getRank();
281 int type= getFunctionSpace().getTypeCode();
282 int ndims =0;
283 long dims[DataTypes::maxRank];
284 const double* d_ptr=&(m_data[0]);
285 DataTypes::ShapeType shape = getShape();
286 int mpi_iam=getFunctionSpace().getDomain()->getMPIRank();
287 int mpi_num=getFunctionSpace().getDomain()->getMPISize();
288 #ifdef PASO_MPI
289 MPI_Status status;
290 #endif
291
292 #ifdef PASO_MPI
293 /* Serialize NetCDF I/O */
294 if (mpi_iam>0) MPI_Recv(&ndims, 0, MPI_INT, mpi_iam-1, 81802, MPI_COMM_WORLD, &status);
295 #endif
296
297 // netCDF error handler
298 NcError err(NcError::verbose_nonfatal);
299 // Create the file.
300 char *newFileName = Escript_MPI_appendRankToFileName(fileName.c_str(), mpi_num, mpi_iam);
301 NcFile dataFile(newFileName, NcFile::Replace);
302 // check if writing was successful
303 if (!dataFile.is_valid())
304 throw DataException("Error - DataConstant:: opening of netCDF file for output failed.");
305 if (!dataFile.add_att("type_id",0) )
306 throw DataException("Error - DataConstant:: appending data type to netCDF file failed.");
307 if (!dataFile.add_att("rank",rank) )
308 throw DataException("Error - DataConstant:: appending rank attribute to netCDF file failed.");
309 if (!dataFile.add_att("function_space_type",type))
310 throw DataException("Error - DataConstant:: appending function space attribute to netCDF file failed.");
311
312 if (rank == 0) {
313 if( ! (ncdims[0] = dataFile.add_dim("l", 1)) )
314 throw DataException("Error - DataConstant:: appending ncdimension 0 to netCDF file failed.");
315 dims[0]=1,
316 ndims=1;
317 } else {
318 ndims=rank;
319 dims[0]=shape[0];
320 if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
321 throw DataException("Error - DataConstant:: appending ncdimension 0 to netCDF file failed.");
322 if ( rank >1 ) {
323 dims[1]=shape[1];
324 if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
325 throw DataException("Error - DataConstant:: appending ncdimension 1 to netCDF file failed.");
326 }
327 if ( rank >2 ) {
328 dims[2]=shape[2];
329 if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
330 throw DataException("Error - DataConstant:: appending ncdimension 2 to netCDF file failed.");
331 }
332 if ( rank >3 ) {
333 dims[3]=shape[3];
334 if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
335 throw DataException("Error - DataConstant:: appending ncdimension 3 to netCDF file failed.");
336 }
337 }
338
339 if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
340 throw DataException("Error - DataConstant:: appending variable to netCDF file failed.");
341 if (! (var->put(d_ptr,dims)) )
342 throw DataException("Error - DataConstant:: copy data to netCDF buffer failed.");
343 #ifdef PASO_MPI
344 if (mpi_iam<mpi_num-1) MPI_Send(&ndims, 0, MPI_INT, mpi_iam+1, 81802, MPI_COMM_WORLD);
345 #endif
346 #else
347 throw DataException("Error - DataConstant:: dump is not configured with netCDF. Please contact your installation manager.");
348 #endif
349 }
350
351 // These used to be marked as inline in DataConstant.
352 // But they are marked virtual in DataReady
353 DataTypes::ValueType&
354 DataConstant::getVectorRW()
355 {
356 CHECK_FOR_EX_WRITE
357 return m_data;
358 }
359
360 const DataTypes::ValueType&
361 DataConstant::getVectorRO() const
362 {
363 return m_data;
364 }
365
366 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26