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