Actual source code: isio.c
petsc-3.11.0 2019-03-29
1: #include <petscis.h>
2: #include <petsc/private/isimpl.h>
3: #include <petsc/private/viewerimpl.h>
5: #if defined(PETSC_HAVE_HDF5)
6: /*
7: This should handle properly the cases where PetscInt is 32 or 64 and hsize_t is 32 or 64. These means properly casting with
8: checks back and forth between the two types of variables.
9: */
10: PetscErrorCode ISLoad_HDF5(IS is, PetscViewer viewer)
11: {
12: hid_t inttype; /* int type (H5T_NATIVE_INT or H5T_NATIVE_LLONG) */
13: PetscInt *ind;
14: const char *isname;
15: PetscErrorCode ierr;
18: if (!((PetscObject)is)->name) SETERRQ(PetscObjectComm((PetscObject)is), PETSC_ERR_SUP, "Since HDF5 format gives ASCII name for each object in file; must use ISLoad() after setting name of Vec with PetscObjectSetName()");
19: #if defined(PETSC_USE_64BIT_INDICES)
20: inttype = H5T_NATIVE_LLONG;
21: #else
22: inttype = H5T_NATIVE_INT;
23: #endif
24: PetscObjectGetName((PetscObject)is, &isname);
25: PetscViewerHDF5Load(viewer, isname, is->map, inttype, (void**)&ind);
26: ISGeneralSetIndices(is, is->map->n, ind, PETSC_OWN_POINTER);
27: return(0);
28: }
29: #endif
31: PetscErrorCode ISLoad_Binary(IS is, PetscViewer viewer)
32: {
34: PetscBool isgeneral,skipHeader,useMPIIO;
35: int fd;
36: PetscInt tr[2],N,ln,*idx;
37: MPI_Request request;
38: MPI_Status status;
39: MPI_Comm comm;
40: PetscMPIInt rank,size,tag;
43: PetscObjectGetComm((PetscObject)is,&comm);
44: PetscObjectTypeCompare((PetscObject)is,ISGENERAL,&isgeneral);
45: if (!isgeneral) SETERRQ(comm,PETSC_ERR_ARG_INCOMP,"IS must be of type ISGENERAL to load into it");
46: PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);
47: if (skipHeader) SETERRQ(comm,PETSC_ERR_USER, "Currently no support for binary files without headers");
48: /* force binary viewer to load .info file if it has not yet done so */
49: PetscViewerSetUp(viewer);
51: PetscViewerBinaryRead(viewer,tr,2,NULL,PETSC_INT);
52: if (tr[0] != IS_FILE_CLASSID) SETERRQ(comm,PETSC_ERR_ARG_WRONG,"Not an IS next in file");
54: /* Has IS already had its layout defined */
55: /* ISGetLayout(is,&map); */
56: PetscLayoutGetSize(is->map,&N);
57: if (N > -1 && N != tr[1]) SETERRQ2(comm,PETSC_ERR_ARG_SIZ,"Size of IS in file %D does not match size of IS provided",tr[1],N);
58: if (N == -1) {
59: N = tr[1];
60: PetscLayoutSetSize(is->map,N);
61: PetscLayoutSetUp(is->map);
62: }
63: PetscLayoutGetLocalSize(is->map,&ln);
64: PetscMalloc1(ln,&idx);
66: PetscViewerBinaryGetUseMPIIO(viewer,&useMPIIO);
67: #if defined(PETSC_HAVE_MPIIO)
68: if (useMPIIO) {
69: MPI_File mfdes;
70: MPI_Offset off;
71: PetscMPIInt lsize;
72: PetscInt rstart;
74: PetscMPIIntCast(ln,&lsize);
75: PetscViewerBinaryGetMPIIODescriptor(viewer,&mfdes);
76: PetscViewerBinaryGetMPIIOOffset(viewer,&off);
77: PetscLayoutGetRange(is->map,&rstart,NULL);
78: off += rstart*(MPI_Offset)sizeof(PetscInt);
79: MPI_File_set_view(mfdes,off,MPIU_INT,MPIU_INT,(char*)"native",MPI_INFO_NULL);
80: MPIU_File_read_all(mfdes,idx,lsize,MPIU_INT,MPI_STATUS_IGNORE);
81: PetscViewerBinaryAddMPIIOOffset(viewer,N*(MPI_Offset)sizeof(PetscInt));
82: ISGeneralSetIndices(is,ln,idx,PETSC_OWN_POINTER);
83: return(0);
84: }
85: #endif
87: MPI_Comm_rank(comm,&rank);
88: MPI_Comm_size(comm,&size);
89: PetscObjectGetNewTag((PetscObject)viewer,&tag);
90: PetscViewerBinaryGetDescriptor(viewer,&fd);
92: if (!rank) {
93: PetscBinaryRead(fd,idx,ln,PETSC_INT);
95: if (size > 1) {
96: PetscInt *range,n,i,*idxwork;
98: /* read in other chuncks and send to other processors */
99: /* determine maximum chunck owned by other */
100: range = is->map->range;
101: n = 1;
102: for (i=1; i<size; i++) n = PetscMax(n,range[i+1] - range[i]);
104: PetscMalloc1(n,&idxwork);
105: for (i=1; i<size; i++) {
106: n = range[i+1] - range[i];
107: PetscBinaryRead(fd,idxwork,n,PETSC_INT);
108: MPI_Isend(idxwork,n,MPIU_INT,i,tag,comm,&request);
109: MPI_Wait(&request,&status);
110: }
111: PetscFree(idxwork);
112: }
113: } else {
114: MPI_Recv(idx,ln,MPIU_INT,0,tag,comm,&status);
115: }
116: ISGeneralSetIndices(is,ln,idx,PETSC_OWN_POINTER);
117: return(0);
118: }
120: PetscErrorCode ISLoad_Default(IS is, PetscViewer viewer)
121: {
122: PetscBool isbinary;
123: #if defined(PETSC_HAVE_HDF5)
124: PetscBool ishdf5;
125: #endif
129: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
130: #if defined(PETSC_HAVE_HDF5)
131: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
132: #endif
133: if (isbinary) {
134: ISLoad_Binary(is, viewer);
135: #if defined(PETSC_HAVE_HDF5)
136: } else if (ishdf5) {
137: ISLoad_HDF5(is, viewer);
138: #endif
139: }
140: return(0);
141: }