/[escript]/trunk-mpi-branch/escript/src/DataFactory.cpp
ViewVC logotype

Contents of /trunk-mpi-branch/escript/src/DataFactory.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1295 - (show annotations)
Mon Sep 10 06:07:09 2007 UTC (12 years ago) by ksteube
File size: 11285 byte(s)
Have now merged latest trunk features into MPI branch in preparation for
ending the MPI branch.
Compiles but has run time problems in bandwith reduction.

1 // $Id$
2 /*
3 ************************************************************
4 * Copyright 2006 by ACcESS MNRF *
5 * *
6 * http://www.access.edu.au *
7 * Primary Business: Queensland, Australia *
8 * Licensed under the Open Software License version 3.0 *
9 * http://www.opensource.org/licenses/osl-3.0.php *
10 * *
11 ************************************************************
12 */
13
14 #include "DataFactory.h"
15
16 #include <boost/python/extract.hpp>
17 #include <iostream>
18 #ifdef USE_NETCDF
19 #include <netcdfcpp.h>
20 #endif
21
22 using namespace boost::python;
23
24 namespace escript {
25
26 Data
27 Scalar(double value,
28 const FunctionSpace& what,
29 bool expanded)
30 {
31 //
32 // an empty shape is a scalar
33 DataArrayView::ShapeType shape;
34 return Data(value,shape,what,expanded);
35 }
36
37 Data
38 Vector(double value,
39 const FunctionSpace& what,
40 bool expanded)
41 {
42 DataArrayView::ShapeType shape(1,what.getDomain().getDim());
43 return Data(value,shape,what,expanded);
44 }
45
46 Data
47 Tensor(double value,
48 const FunctionSpace& what,
49 bool expanded)
50 {
51 DataArrayView::ShapeType shape(2,what.getDomain().getDim());
52 return Data(value,shape,what,expanded);
53 }
54
55 Data
56 Tensor3(double value,
57 const FunctionSpace& what,
58 bool expanded)
59 {
60 DataArrayView::ShapeType shape(3,what.getDomain().getDim());
61 return Data(value,shape,what,expanded);
62 }
63
64 Data
65 Tensor4(double value,
66 const FunctionSpace& what,
67 bool expanded)
68 {
69 DataArrayView::ShapeType shape(4,what.getDomain().getDim());
70 return Data(value,shape,what,expanded);
71 }
72
73 Data
74 load(const std::string fileName,
75 const AbstractDomain& domain)
76 {
77 #ifdef PASO_MPI
78 throw DataException("Error - load :: not implemented for MPI yet.");
79 #endif
80
81 #ifdef USE_NETCDF
82 NcAtt *type_att, *rank_att, *function_space_type_att;
83 // netCDF error handler
84 NcError err(NcError::silent_nonfatal);
85 // Create the file.
86 NcFile dataFile(fileName.c_str(), NcFile::ReadOnly);
87 if (!dataFile.is_valid())
88 throw DataException("Error - load:: opening of netCDF file for input failed.");
89 /* recover function space */
90 if (! (function_space_type_att=dataFile.get_att("function_space_type")) )
91 throw DataException("Error - load:: cannot recover function_space_type attribute from escript netCDF file.");
92 int function_space_type = function_space_type_att->as_int(0);
93 delete function_space_type_att;
94 /* test if function space id is valid and create function space instance */
95 if (! domain.isValidFunctionSpaceType(function_space_type) )
96 throw DataException("Error - load:: function space type code in netCDF file is invalid for given domain.");
97 FunctionSpace function_space=FunctionSpace(domain, function_space_type);
98 /* recover rank */
99 if (! (rank_att=dataFile.get_att("rank")) )
100 throw DataException("Error - load:: cannot recover rank attribute from escript netCDF file.");
101 int rank = rank_att->as_int(0);
102 delete rank_att;
103 if (rank<0 || rank>DataArrayView::maxRank)
104 throw DataException("Error - load:: rank in escript netCDF file is greater than maximum rank.");
105 /* recover type attribute */
106 int type=-1;
107 if ((type_att=dataFile.get_att("type")) ) {
108 char* type_str = type_att->as_string(0);
109 if (strncmp(type_str, "constant", strlen("constant")) == 0 ) {
110 type =0;
111 } else if (strncmp(type_str, "tagged", strlen("tagged")) == 0 ) {
112 type =1;
113 } else if (strncmp(type_str, "expanded", strlen("expanded")) == 0 ) {
114 type =2;
115 }
116 free(type_str);
117 } else {
118 if (! (type_att=dataFile.get_att("type_id")) )
119 throw DataException("Error - load:: cannot recover type attribute from escript netCDF file.");
120 type=type_att->as_int(0);
121 }
122 delete type_att;
123
124 /* recover dimension */
125 int ndims=dataFile.num_dims();
126 int ntags =0 , num_samples =0 , num_data_points_per_sample =0, d=0, len_data_point=1;
127 NcDim *d_dim, *tags_dim, *num_samples_dim, *num_data_points_per_sample_dim;
128 /* recover shape */
129 DataArrayView::ShapeType shape;
130 long dims[DataArrayView::maxRank+2];
131 if (rank>0) {
132 if (! (d_dim=dataFile.get_dim("d0")) )
133 throw DataException("Error - load:: unable to recover d0 from netCDF file.");
134 d=d_dim->size();
135 shape.push_back(d);
136 dims[0]=d;
137 len_data_point*=d;
138 }
139 if (rank>1) {
140 if (! (d_dim=dataFile.get_dim("d1")) )
141 throw DataException("Error - load:: unable to recover d1 from netCDF file.");
142 d=d_dim->size();
143 shape.push_back(d);
144 dims[1]=d;
145 len_data_point*=d;
146 }
147 if (rank>2) {
148 if (! (d_dim=dataFile.get_dim("d2")) )
149 throw DataException("Error - load:: unable to recover d2 from netCDF file.");
150 d=d_dim->size();
151 shape.push_back(d);
152 dims[2]=d;
153 len_data_point*=d;
154 }
155 if (rank>3) {
156 if (! (d_dim=dataFile.get_dim("d3")) )
157 throw DataException("Error - load:: unable to recover d3 from netCDF file.");
158 d=d_dim->size();
159 shape.push_back(d);
160 dims[3]=d;
161 len_data_point*=d;
162 }
163 /* recover stuff */
164 Data out;
165 NcVar *var, *ids_var, *tags_var;
166 if (type == 0) {
167 /* constant data */
168 if ( ! ( (ndims == rank && rank >0) || ( ndims ==1 && rank == 0 ) ) )
169 throw DataException("Error - load:: illegal number of dimensions for constant data in netCDF file.");
170 if (rank == 0) {
171 if (! (d_dim=dataFile.get_dim("l")) )
172 throw DataException("Error - load:: unable to recover d0 for scalar constant data in netCDF file.");
173 int d0 = d_dim->size();
174 if (! d0 == 1)
175 throw DataException("Error - load:: d0 is expected to be one for scalar constant data in netCDF file.");
176 dims[0]=1;
177 }
178 out=Data(0,shape,function_space);
179 if (!(var = dataFile.get_var("data")))
180 throw DataException("Error - load:: unable to find data in netCDF file.");
181 if (! var->get(&(out.getDataPoint(0,0).getData()[0]), dims) )
182 throw DataException("Error - load:: unable to recover data from netCDF file.");
183 } else if (type == 1) {
184 /* tagged data */
185 if ( ! (ndims == rank + 1) )
186 throw DataException("Error - load:: illegal number of dimensions for tagged data in netCDF file.");
187 if (! (tags_dim=dataFile.get_dim("num_tags")) )
188 throw DataException("Error - load:: unable to recover number of tags from netCDF file.");
189 ntags=tags_dim->size();
190 dims[rank]=ntags;
191 int *tags = (int *) malloc(ntags*sizeof(int));
192 if (! ( tags_var = dataFile.get_var("tags")) )
193 {
194 free(tags);
195 throw DataException("Error - load:: unable to find tags in netCDF file.");
196 }
197 if (! tags_var->get(tags, ntags) )
198 {
199 free(tags);
200 throw DataException("Error - load:: unable to recover tags from netCDF file.");
201 }
202
203 DataVector data(len_data_point * ntags, 0., len_data_point * ntags);
204 if (!(var = dataFile.get_var("data")))
205 {
206 free(tags);
207 throw DataException("Error - load:: unable to find data in netCDF file.");
208 }
209 if (! var->get(&(data[0]), dims) )
210 {
211 free(tags);
212 throw DataException("Error - load:: unable to recover data from netCDF file.");
213 }
214 out=Data(DataArrayView(data,shape,0),function_space);
215 for (int t=1; t<ntags; ++t) {
216 out.setTaggedValueFromCPP(tags[t],DataArrayView(data,shape,t*len_data_point));
217 }
218 free(tags);
219 } else if (type == 2) {
220 /* expanded data */
221 if ( ! (ndims == rank + 2) )
222 throw DataException("Error - load:: illegal number of dimensions for exanded data in netCDF file.");
223 if ( ! (num_samples_dim = dataFile.get_dim("num_samples") ) )
224 throw DataException("Error - load:: unable to recover number of samples from netCDF file.");
225 num_samples = num_samples_dim->size();
226 if ( ! (num_data_points_per_sample_dim = dataFile.get_dim("num_data_points_per_sample") ) )
227 throw DataException("Error - load:: unable to recover number of data points per sample from netCDF file.");
228 num_data_points_per_sample=num_data_points_per_sample_dim->size();
229 // check shape:
230 if ( ! (num_samples == function_space.getNumSamples() && num_data_points_per_sample == function_space.getNumDataPointsPerSample()) )
231 throw DataException("Error - load:: data sample layout of file does not match data layout of function space.");
232 // get ids
233 if (! ( ids_var = dataFile.get_var("id")) )
234 throw DataException("Error - load:: unable to find reference ids in netCDF file.");
235 const int* ids_p=function_space.borrowSampleReferenceIDs();
236 int *ids_of_nc = (int *)malloc(num_samples*sizeof(int));
237 if (! ids_var->get(ids_of_nc, (long) num_samples) )
238 {
239 free(ids_of_nc);
240 throw DataException("Error - load:: unable to recover ids from netCDF file.");
241 }
242 // check order:
243 int failed=-1, local_failed=-1, i;
244 #pragma omp parallel private(local_failed)
245 {
246 local_failed=-1;
247 #pragma omp for private(i) schedule(static)
248 for (i=0;i < num_samples; ++i) {
249 if (ids_of_nc[i]!=ids_p[i]) local_failed=i;
250 }
251 #pragma omp critical
252 if (local_failed>=0) failed = local_failed;
253 }
254 if (failed>=0)
255 {
256 free(ids_of_nc);
257 throw DataException("Error - load:: data ordering in netCDF file does not match ordering of FunctionSpace.");
258 }
259 // get the data:
260 dims[rank]=num_data_points_per_sample;
261 dims[rank+1]=num_samples;
262 out=Data(0,shape,function_space,true);
263 if (!(var = dataFile.get_var("data")))
264 {
265 free(ids_of_nc);
266 throw DataException("Error - load:: unable to find data in netCDF file.");
267 }
268 if (! var->get(&(out.getDataPoint(0,0).getData()[0]), dims) )
269 {
270 free(ids_of_nc);
271 throw DataException("Error - load:: unable to recover data from netCDF file.");
272 }
273 // if (failed==-1)
274 // out->m_data.reorderByReferenceIDs(ids_of_nc)
275 free(ids_of_nc);
276 } else {
277 throw DataException("Error - load:: unknown escript data type in netCDF file.");
278 }
279 return out;
280 #else
281 throw DataException("Error - load:: is not compiled with netCFD. Please contact your insatllation manager.");
282 #endif
283 }
284
285 bool
286 loadConfigured()
287 {
288 #ifdef USE_NETCDF
289 return true;
290 #else
291 return false;
292 #endif
293 }
294
295 Data
296 convertToData(const boost::python::object& value,
297 const FunctionSpace& what)
298 {
299 // first we try to extract a Data object from value
300 extract<Data> value_data(value);
301 if (value_data.check()) {
302 Data extracted_data=value_data();
303 if (extracted_data.isEmpty()) {
304 return extracted_data;
305 } else {
306 return Data(extracted_data,what);
307 }
308 } else {
309 return Data(value,what);
310 }
311 }
312
313 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26