Actual source code: isio.c
petsc-3.15.0 2021-03-30
1: #include <petscis.h>
2: #include <petsc/private/isimpl.h>
3: #include <petsc/private/viewerimpl.h>
4: #include <petsclayouthdf5.h>
6: PetscErrorCode ISView_Binary(IS is,PetscViewer viewer)
7: {
9: PetscBool skipHeader;
10: PetscLayout map;
11: PetscInt tr[2],n,s,N;
12: const PetscInt *iarray;
15: PetscViewerSetUp(viewer);
16: PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);
18: ISGetLayout(is,&map);
19: PetscLayoutGetLocalSize(map,&n);
20: PetscLayoutGetRange(map,&s,NULL);
21: PetscLayoutGetSize(map,&N);
23: /* write IS header */
24: tr[0] = IS_FILE_CLASSID; tr[1] = N;
25: if (!skipHeader) {PetscViewerBinaryWrite(viewer,tr,2,PETSC_INT);}
27: /* write IS indices */
28: ISGetIndices(is,&iarray);
29: PetscViewerBinaryWriteAll(viewer,iarray,n,s,N,PETSC_INT);
30: ISRestoreIndices(is,&iarray);
31: return(0);
32: }
34: #if defined(PETSC_HAVE_HDF5)
35: /*
36: This should handle properly the cases where PetscInt is 32 or 64 and hsize_t is 32 or 64. These means properly casting with
37: checks back and forth between the two types of variables.
38: */
39: PetscErrorCode ISLoad_HDF5(IS is, PetscViewer viewer)
40: {
41: hid_t inttype; /* int type (H5T_NATIVE_INT or H5T_NATIVE_LLONG) */
42: PetscInt *ind;
43: const char *isname;
44: PetscErrorCode ierr;
47: 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()");
48: #if defined(PETSC_USE_64BIT_INDICES)
49: inttype = H5T_NATIVE_LLONG;
50: #else
51: inttype = H5T_NATIVE_INT;
52: #endif
53: PetscObjectGetName((PetscObject)is, &isname);
54: PetscViewerHDF5Load(viewer, isname, is->map, inttype, (void**)&ind);
55: ISGeneralSetIndices(is, is->map->n, ind, PETSC_OWN_POINTER);
56: return(0);
57: }
58: #endif
60: PetscErrorCode ISLoad_Binary(IS is, PetscViewer viewer)
61: {
63: PetscBool isgeneral,skipHeader;
64: PetscInt tr[2],rows,N,n,s,*idx;
65: PetscLayout map;
68: PetscObjectTypeCompare((PetscObject)is,ISGENERAL,&isgeneral);
69: if (!isgeneral) SETERRQ(PetscObjectComm((PetscObject)is),PETSC_ERR_ARG_INCOMP,"IS must be of type ISGENERAL to load into it");
70: PetscViewerSetUp(viewer);
71: PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);
73: ISGetLayout(is,&map);
74: PetscLayoutGetSize(map,&N);
76: /* read IS header */
77: if (!skipHeader) {
78: PetscViewerBinaryRead(viewer,tr,2,NULL,PETSC_INT);
79: if (tr[0] != IS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Not an IS next in file");
80: if (tr[1] < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"IS size (%D) in file is negative",tr[1]);
81: if (N >= 0 && N != tr[1]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"IS in file different size (%D) than input IS (%D)",tr[1],N);
82: rows = tr[1];
83: } else {
84: if (N < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"IS binary file header was skipped, thus the user must specify the global size of input IS");
85: rows = N;
86: }
88: /* set IS size if not already set */
89: if (N < 0) {PetscLayoutSetSize(map,rows);}
90: PetscLayoutSetUp(map);
92: /* get IS sizes and check global size */
93: PetscLayoutGetSize(map,&N);
94: PetscLayoutGetLocalSize(map,&n);
95: PetscLayoutGetRange(map,&s,NULL);
96: if (N != rows) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"IS in file different size (%D) than input IS (%D)",rows,N);
98: /* read IS indices */
99: PetscMalloc1(n,&idx);
100: PetscViewerBinaryReadAll(viewer,idx,n,s,N,PETSC_INT);
101: ISGeneralSetIndices(is,n,idx,PETSC_OWN_POINTER);
102: return(0);
103: }
105: PetscErrorCode ISLoad_Default(IS is, PetscViewer viewer)
106: {
107: PetscBool isbinary,ishdf5;
111: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
112: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
113: if (isbinary) {
114: ISLoad_Binary(is, viewer);
115: } else if (ishdf5) {
116: #if defined(PETSC_HAVE_HDF5)
117: ISLoad_HDF5(is, viewer);
118: #endif
119: }
120: return(0);
121: }