Actual source code: vecio.c
petsc-3.5.2 2014-09-08
2: /*
3: This file contains simple binary input routines for vectors. The
4: analogous output routines are within each vector implementation's
5: VecView (with viewer types PETSCVIEWERBINARY)
6: */
8: #include <petscsys.h>
9: #include <petscvec.h> /*I "petscvec.h" I*/
10: #include <petsc-private/vecimpl.h>
11: #include <petscmat.h> /* so that MAT_FILE_CLASSID is defined */
12: #include <petscviewerhdf5.h>
16: static PetscErrorCode PetscViewerBinaryReadVecHeader_Private(PetscViewer viewer,PetscInt *rows)
17: {
19: MPI_Comm comm;
20: PetscInt tr[2],type;
23: PetscObjectGetComm((PetscObject)viewer,&comm);
24: /* Read vector header */
25: PetscViewerBinaryRead(viewer,tr,2,PETSC_INT);
26: type = tr[0];
27: if (type != VEC_FILE_CLASSID) {
28: PetscLogEventEnd(VEC_Load,viewer,0,0,0);
29: if (type == MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Matrix is next in file, not a vector as you requested");
30: else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not a vector next in file");
31: }
32: *rows = tr[1];
33: return(0);
34: }
36: #if defined(PETSC_HAVE_MPIIO)
39: static PetscErrorCode VecLoad_Binary_MPIIO(Vec vec, PetscViewer viewer)
40: {
42: PetscMPIInt lsize;
43: PetscScalar *avec;
44: MPI_File mfdes;
45: MPI_Offset off;
48: VecGetArray(vec,&avec);
49: PetscMPIIntCast(vec->map->n,&lsize);
51: PetscViewerBinaryGetMPIIODescriptor(viewer,&mfdes);
52: PetscViewerBinaryGetMPIIOOffset(viewer,&off);
53: off += vec->map->rstart*sizeof(PetscScalar);
54: MPI_File_set_view(mfdes,off,MPIU_SCALAR,MPIU_SCALAR,(char*)"native",MPI_INFO_NULL);
55: MPIU_File_read_all(mfdes,avec,lsize,MPIU_SCALAR,MPI_STATUS_IGNORE);
56: PetscViewerBinaryAddMPIIOOffset(viewer,vec->map->N*sizeof(PetscScalar));
58: VecRestoreArray(vec,&avec);
59: VecAssemblyBegin(vec);
60: VecAssemblyEnd(vec);
61: return(0);
62: }
63: #endif
67: PetscErrorCode VecLoad_Binary(Vec vec, PetscViewer viewer)
68: {
69: PetscMPIInt size,rank,tag;
70: int fd;
71: PetscInt i,rows = 0,n,*range,N,bs;
73: PetscBool flag;
74: PetscScalar *avec,*avecwork;
75: MPI_Comm comm;
76: MPI_Request request;
77: MPI_Status status;
78: #if defined(PETSC_HAVE_MPIIO)
79: PetscBool useMPIIO;
80: #endif
83: PetscObjectGetComm((PetscObject)viewer,&comm);
84: MPI_Comm_rank(comm,&rank);
85: MPI_Comm_size(comm,&size);
87: PetscViewerBinaryGetDescriptor(viewer,&fd);
88: PetscViewerBinaryReadVecHeader_Private(viewer,&rows);
89: /* Set Vec sizes,blocksize,and type if not already set. Block size first so that local sizes will be compatible. */
90: PetscOptionsGetInt(((PetscObject)vec)->prefix, "-vecload_block_size", &bs, &flag);
91: if (flag) {
92: VecSetBlockSize(vec, bs);
93: }
94: if (vec->map->n < 0 && vec->map->N < 0) {
95: VecSetSizes(vec,PETSC_DECIDE,rows);
96: }
98: /* If sizes and type already set,check if the vector global size is correct */
99: VecGetSize(vec, &N);
100: if (N != rows) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Vector in file different length (%d) then input vector (%d)", rows, N);
102: #if defined(PETSC_HAVE_MPIIO)
103: PetscViewerBinaryGetMPIIO(viewer,&useMPIIO);
104: if (useMPIIO) {
105: VecLoad_Binary_MPIIO(vec, viewer);
106: return(0);
107: }
108: #endif
110: VecGetLocalSize(vec,&n);
111: PetscObjectGetNewTag((PetscObject)viewer,&tag);
112: VecGetArray(vec,&avec);
113: if (!rank) {
114: PetscBinaryRead(fd,avec,n,PETSC_SCALAR);
116: if (size > 1) {
117: /* read in other chuncks and send to other processors */
118: /* determine maximum chunck owned by other */
119: range = vec->map->range;
120: n = 1;
121: for (i=1; i<size; i++) n = PetscMax(n,range[i+1] - range[i]);
123: PetscMalloc1(n,&avecwork);
124: for (i=1; i<size; i++) {
125: n = range[i+1] - range[i];
126: PetscBinaryRead(fd,avecwork,n,PETSC_SCALAR);
127: MPI_Isend(avecwork,n,MPIU_SCALAR,i,tag,comm,&request);
128: MPI_Wait(&request,&status);
129: }
130: PetscFree(avecwork);
131: }
132: } else {
133: MPI_Recv(avec,n,MPIU_SCALAR,0,tag,comm,&status);
134: }
136: VecRestoreArray(vec,&avec);
137: VecAssemblyBegin(vec);
138: VecAssemblyEnd(vec);
139: return(0);
140: }
142: #if defined(PETSC_HAVE_HDF5)
145: PetscErrorCode PetscViewerHDF5OpenGroup(PetscViewer viewer, hid_t *fileId, hid_t *groupId)
146: {
147: hid_t file_id, group;
148: htri_t found;
149: const char *groupName = NULL;
153: PetscViewerHDF5GetFileId(viewer, &file_id);
154: PetscViewerHDF5GetGroup(viewer, &groupName);
155: /* Open group */
156: if (groupName) {
157: PetscBool root;
159: PetscStrcmp(groupName, "/", &root);
160: found = H5Lexists(file_id, groupName, H5P_DEFAULT);
161: if (!root && (found <= 0)) {
162: #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
163: group = H5Gcreate2(file_id, groupName, 0, H5P_DEFAULT, H5P_DEFAULT);
164: #else /* deprecated HDF5 1.6 API */
165: group = H5Gcreate(file_id, groupName, 0);
166: #endif
167: if (group < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not create group %s", groupName);
168: H5Gclose(group);
169: }
170: #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
171: group = H5Gopen2(file_id, groupName, H5P_DEFAULT);
172: #else
173: group = H5Gopen(file_id, groupName);
174: #endif
175: if (group < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not open group %s", groupName);
176: } else group = file_id;
178: *fileId = file_id;
179: *groupId = group;
180: return(0);
181: }
185: PetscErrorCode PetscViewerHDF5ReadSizes(PetscViewer viewer, const char name[], PetscInt *bs, PetscInt *N)
186: {
187: hid_t file_id, group, dset_id, filespace;
188: hsize_t rdim, dim;
189: hsize_t dims[4];
190: herr_t status;
191: PetscInt bsInd, lenInd, timestep;
195: PetscViewerHDF5OpenGroup(viewer, &file_id, &group);
196: PetscViewerHDF5GetTimestep(viewer, ×tep);
197: #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
198: dset_id = H5Dopen2(group, name, H5P_DEFAULT);
199: #else
200: dset_id = H5Dopen(group, name);
201: #endif
202: if (dset_id == -1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not H5Dopen() with PetscObject named %s", name);
203: filespace = H5Dget_space(dset_id);
204: if (filespace == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not H5Dget_space()");
205: dim = 0;
206: if (timestep >= 0) ++dim;
207: ++dim; /* length in blocks */
208: ++dim; /* block size */
209: #if defined(PETSC_USE_COMPLEX)
210: ++dim;
211: #endif
212: rdim = H5Sget_simple_extent_dims(filespace, dims, NULL);
213: #if defined(PETSC_USE_COMPLEX)
214: bsInd = rdim-2;
215: #else
216: bsInd = rdim-1;
217: #endif
218: lenInd = timestep >= 0 ? 1 : 0;
219: if (rdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Dimension of array in file %d not %d as expected", rdim, dim);
220: /* Close/release resources */
221: status = H5Sclose(filespace);CHKERRQ(status);
222: status = H5Dclose(dset_id);CHKERRQ(status);
223: if (group != file_id) {status = H5Gclose(group);CHKERRQ(status);}
224: if (bs) *bs = (PetscInt) dims[bsInd];
225: if (N) *N = (PetscInt) dims[lenInd]*dims[bsInd];
226: return(0);
227: }
231: /*
232: This should handle properly the cases where PetscInt is 32 or 64 and hsize_t is 32 or 64. These means properly casting with
233: checks back and forth between the two types of variables.
234: */
235: PetscErrorCode VecLoad_HDF5(Vec xin, PetscViewer viewer)
236: {
237: hid_t file_id, group, dset_id, filespace, memspace, plist_id;
238: hsize_t rdim, dim;
239: hsize_t dims[4], count[4], offset[4];
240: herr_t status;
241: PetscInt n, N, bs = 1, bsInd, lenInd, low, timestep;
242: PetscScalar *x;
243: const char *vecname;
247: PetscViewerHDF5OpenGroup(viewer, &file_id, &group);
248: PetscViewerHDF5GetTimestep(viewer, ×tep);
249: VecGetBlockSize(xin,&bs);
250: /* Create the dataset with default properties and close filespace */
251: PetscObjectGetName((PetscObject)xin,&vecname);
252: #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
253: dset_id = H5Dopen2(group, vecname, H5P_DEFAULT);
254: #else
255: dset_id = H5Dopen(group, vecname);
256: #endif
257: if (dset_id == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Could not H5Dopen() with Vec named %s",vecname);
258: /* Retrieve the dataspace for the dataset */
259: filespace = H5Dget_space(dset_id);
260: if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Could not H5Dget_space()");
261: dim = 0;
262: if (timestep >= 0) ++dim;
263: ++dim;
264: if (bs >= 1) ++dim;
265: #if defined(PETSC_USE_COMPLEX)
266: ++dim;
267: #endif
268: rdim = H5Sget_simple_extent_dims(filespace, dims, NULL);
269: #if defined(PETSC_USE_COMPLEX)
270: bsInd = rdim-2;
271: #else
272: bsInd = rdim-1;
273: #endif
274: lenInd = timestep >= 0 ? 1 : 0;
275: if (rdim != dim) {
276: if (rdim == dim+1 && bs == -1) bs = dims[bsInd];
277: else SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Dimension of array in file %d not %d as expected",rdim,dim);
278: } else if (bs >= 1 && bs != (PetscInt) dims[bsInd]) {
279: VecSetBlockSize(xin, dims[bsInd]);
280: if (ierr) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Block size %d specified for vector does not match blocksize in file %d",bs,dims[bsInd]);
281: bs = dims[bsInd];
282: }
284: /* Set Vec sizes,blocksize,and type if not already set */
285: if ((xin)->map->n < 0 && (xin)->map->N < 0) {
286: VecSetSizes(xin, PETSC_DECIDE, dims[lenInd]*bs);
287: }
288: /* If sizes and type already set,check if the vector global size is correct */
289: VecGetSize(xin, &N);
290: if (N/bs != (PetscInt) dims[lenInd]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Vector in file different length (%d) then input vector (%d)", (PetscInt) dims[lenInd], N/bs);
292: /* Each process defines a dataset and reads it from the hyperslab in the file */
293: VecGetLocalSize(xin, &n);
294: dim = 0;
295: if (timestep >= 0) {
296: count[dim] = 1;
297: ++dim;
298: }
299: PetscHDF5IntCast(n/bs,count + dim);
300: ++dim;
301: if (bs >= 1) {
302: count[dim] = bs;
303: ++dim;
304: }
305: #if defined(PETSC_USE_COMPLEX)
306: count[dim] = 2;
307: ++dim;
308: #endif
309: memspace = H5Screate_simple(dim, count, NULL);
310: if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Could not H5Screate_simple()");
312: /* Select hyperslab in the file */
313: VecGetOwnershipRange(xin, &low, NULL);
314: dim = 0;
315: if (timestep >= 0) {
316: offset[dim] = timestep;
317: ++dim;
318: }
319: PetscHDF5IntCast(low/bs,offset + dim);
320: ++dim;
321: if (bs >= 1) {
322: offset[dim] = 0;
323: ++dim;
324: }
325: #if defined(PETSC_USE_COMPLEX)
326: offset[dim] = 0;
327: ++dim;
328: #endif
329: status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status);
331: /* Create property list for collective dataset read */
332: plist_id = H5Pcreate(H5P_DATASET_XFER);
333: if (plist_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Could not H5Pcreate()");
334: #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
335: status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status);
336: #endif
337: /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */
339: VecGetArray(xin, &x);
340: status = H5Dread(dset_id, H5T_NATIVE_DOUBLE, memspace, filespace, plist_id, x);CHKERRQ(status);
341: VecRestoreArray(xin, &x);
343: /* Close/release resources */
344: if (group != file_id) {
345: status = H5Gclose(group);CHKERRQ(status);
346: }
347: status = H5Pclose(plist_id);CHKERRQ(status);
348: status = H5Sclose(filespace);CHKERRQ(status);
349: status = H5Sclose(memspace);CHKERRQ(status);
350: status = H5Dclose(dset_id);CHKERRQ(status);
352: VecAssemblyBegin(xin);
353: VecAssemblyEnd(xin);
354: return(0);
355: }
356: #endif
361: PetscErrorCode VecLoad_Default(Vec newvec, PetscViewer viewer)
362: {
364: PetscBool isbinary;
365: #if defined(PETSC_HAVE_HDF5)
366: PetscBool ishdf5;
367: #endif
370: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
371: #if defined(PETSC_HAVE_HDF5)
372: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
373: #endif
375: #if defined(PETSC_HAVE_HDF5)
376: if (ishdf5) {
377: if (!((PetscObject)newvec)->name) {
378: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Since HDF5 format gives ASCII name for each object in file; must use VecLoad() after setting name of Vec with PetscObjectSetName()");
379: PetscLogEventEnd(VEC_Load,viewer,0,0,0);
380: }
381: VecLoad_HDF5(newvec, viewer);
382: } else
383: #endif
384: {
385: VecLoad_Binary(newvec, viewer);
386: }
387: return(0);
388: }
392: /*@
393: VecChop - Set all values in the vector less than the tolerance to zero
395: Input Parameters:
396: + v - The vector
397: - tol - The zero tolerance
399: Output Parameters:
400: . v - The chopped vector
402: Level: intermediate
404: .seealso: VecCreate(), VecSet()
405: @*/
406: PetscErrorCode VecChop(Vec v, PetscReal tol)
407: {
408: PetscScalar *a;
409: PetscInt n, i;
413: VecGetLocalSize(v, &n);
414: VecGetArray(v, &a);
415: for (i = 0; i < n; ++i) {
416: if (PetscAbsScalar(a[i]) < tol) a[i] = 0.0;
417: }
418: VecRestoreArray(v, &a);
419: return(0);
420: }