xref: /petsc/src/vec/is/utils/isio.c (revision f92d9ac335f2807b63bc18d4d5e71a180ea5371c)
1235f7792SMatthew G. Knepley #include <petscis.h>         /*I  "petscis.h"  I*/
2af0996ceSBarry Smith #include <petsc/private/isimpl.h>
390e28553SVaclav Hapla #include <petsc/private/viewerimpl.h>
420e823e8SBarry Smith #include <petsclayouthdf5.h>
5235f7792SMatthew G. Knepley 
6*f92d9ac3SLisandro Dalcin PetscErrorCode ISView_Binary(IS is,PetscViewer viewer)
7*f92d9ac3SLisandro Dalcin {
8*f92d9ac3SLisandro Dalcin   PetscErrorCode ierr;
9*f92d9ac3SLisandro Dalcin   PetscBool      skipHeader;
10*f92d9ac3SLisandro Dalcin   PetscLayout    map;
11*f92d9ac3SLisandro Dalcin   PetscInt       tr[2],n,s,N;
12*f92d9ac3SLisandro Dalcin   const PetscInt *iarray;
13*f92d9ac3SLisandro Dalcin 
14*f92d9ac3SLisandro Dalcin   PetscFunctionBegin;
15*f92d9ac3SLisandro Dalcin   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
16*f92d9ac3SLisandro Dalcin   ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
17*f92d9ac3SLisandro Dalcin 
18*f92d9ac3SLisandro Dalcin   ierr = ISGetLayout(is,&map);CHKERRQ(ierr);
19*f92d9ac3SLisandro Dalcin   ierr = PetscLayoutGetLocalSize(map,&n);CHKERRQ(ierr);
20*f92d9ac3SLisandro Dalcin   ierr = PetscLayoutGetRange(map,&s,NULL);CHKERRQ(ierr);
21*f92d9ac3SLisandro Dalcin   ierr = PetscLayoutGetSize(map,&N);CHKERRQ(ierr);
22*f92d9ac3SLisandro Dalcin 
23*f92d9ac3SLisandro Dalcin   /* write IS header */
24*f92d9ac3SLisandro Dalcin   tr[0] = IS_FILE_CLASSID; tr[1] = N;
25*f92d9ac3SLisandro Dalcin   if (!skipHeader) {ierr = PetscViewerBinaryWrite(viewer,tr,2,PETSC_INT);CHKERRQ(ierr);}
26*f92d9ac3SLisandro Dalcin 
27*f92d9ac3SLisandro Dalcin   /* write IS indices */
28*f92d9ac3SLisandro Dalcin   ierr = ISGetIndices(is,&iarray);CHKERRQ(ierr);
29*f92d9ac3SLisandro Dalcin   ierr = PetscViewerBinaryWriteAll(viewer,iarray,n,s,N,PETSC_INT);CHKERRQ(ierr);
30*f92d9ac3SLisandro Dalcin   ierr = ISRestoreIndices(is,&iarray);CHKERRQ(ierr);
31*f92d9ac3SLisandro Dalcin   PetscFunctionReturn(0);
32*f92d9ac3SLisandro Dalcin }
33*f92d9ac3SLisandro Dalcin 
34235f7792SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
35235f7792SMatthew G. Knepley /*
36235f7792SMatthew G. Knepley      This should handle properly the cases where PetscInt is 32 or 64 and hsize_t is 32 or 64. These means properly casting with
37235f7792SMatthew G. Knepley    checks back and forth between the two types of variables.
38235f7792SMatthew G. Knepley */
39235f7792SMatthew G. Knepley PetscErrorCode ISLoad_HDF5(IS is, PetscViewer viewer)
40235f7792SMatthew G. Knepley {
41235f7792SMatthew G. Knepley   hid_t           inttype;    /* int type (H5T_NATIVE_INT or H5T_NATIVE_LLONG) */
4290e28553SVaclav Hapla   PetscInt       *ind;
43235f7792SMatthew G. Knepley   const char     *isname;
44235f7792SMatthew G. Knepley   PetscErrorCode  ierr;
45235f7792SMatthew G. Knepley 
46235f7792SMatthew G. Knepley   PetscFunctionBegin;
47ede126feSBarry Smith   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()");
48235f7792SMatthew G. Knepley #if defined(PETSC_USE_64BIT_INDICES)
49235f7792SMatthew G. Knepley   inttype = H5T_NATIVE_LLONG;
50235f7792SMatthew G. Knepley #else
51235f7792SMatthew G. Knepley   inttype = H5T_NATIVE_INT;
52235f7792SMatthew G. Knepley #endif
5390e28553SVaclav Hapla   ierr = PetscObjectGetName((PetscObject)is, &isname);CHKERRQ(ierr);
541c92607dSVaclav Hapla   ierr = PetscViewerHDF5Load(viewer, isname, is->map, inttype, (void**)&ind);CHKERRQ(ierr);
5590e28553SVaclav Hapla   ierr = ISGeneralSetIndices(is, is->map->n, ind, PETSC_OWN_POINTER);CHKERRQ(ierr);
56235f7792SMatthew G. Knepley   PetscFunctionReturn(0);
57235f7792SMatthew G. Knepley }
58235f7792SMatthew G. Knepley #endif
59235f7792SMatthew G. Knepley 
60ede126feSBarry Smith PetscErrorCode ISLoad_Binary(IS is, PetscViewer viewer)
61ede126feSBarry Smith {
62ede126feSBarry Smith   PetscErrorCode ierr;
63*f92d9ac3SLisandro Dalcin   PetscBool      isgeneral,skipHeader;
64*f92d9ac3SLisandro Dalcin   PetscInt       tr[2],rows,N,n,s,*idx;
65*f92d9ac3SLisandro Dalcin   PetscLayout    map;
66ede126feSBarry Smith 
67ede126feSBarry Smith   PetscFunctionBegin;
68ede126feSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)is,ISGENERAL,&isgeneral);CHKERRQ(ierr);
69*f92d9ac3SLisandro Dalcin   if (!isgeneral) SETERRQ(PetscObjectComm((PetscObject)is),PETSC_ERR_ARG_INCOMP,"IS must be of type ISGENERAL to load into it");
70de8a3bf8SLisandro Dalcin   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
71*f92d9ac3SLisandro Dalcin   ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
72ede126feSBarry Smith 
73*f92d9ac3SLisandro Dalcin   ierr = ISGetLayout(is,&map);CHKERRQ(ierr);
74*f92d9ac3SLisandro Dalcin   ierr = PetscLayoutGetSize(map,&N);CHKERRQ(ierr);
75*f92d9ac3SLisandro Dalcin 
76*f92d9ac3SLisandro Dalcin   /* read IS header */
77*f92d9ac3SLisandro Dalcin   if (!skipHeader) {
78ede126feSBarry Smith     ierr = PetscViewerBinaryRead(viewer,tr,2,NULL,PETSC_INT);CHKERRQ(ierr);
79*f92d9ac3SLisandro Dalcin     if (tr[0] != IS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Not an IS next in file");
80*f92d9ac3SLisandro Dalcin     if (tr[1] < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"IS size (%D) in file is negative",tr[1]);
81*f92d9ac3SLisandro Dalcin     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*f92d9ac3SLisandro Dalcin     rows = tr[1];
83ede126feSBarry Smith   } else {
84*f92d9ac3SLisandro Dalcin     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*f92d9ac3SLisandro Dalcin     rows = N;
86ede126feSBarry Smith   }
87*f92d9ac3SLisandro Dalcin 
88*f92d9ac3SLisandro Dalcin   /* set IS size if not already set */
89*f92d9ac3SLisandro Dalcin   if (N < 0) {ierr = PetscLayoutSetSize(map,rows);CHKERRQ(ierr);}
90*f92d9ac3SLisandro Dalcin   ierr = PetscLayoutSetUp(map);CHKERRQ(ierr);
91*f92d9ac3SLisandro Dalcin 
92*f92d9ac3SLisandro Dalcin   /* get IS sizes and check global size */
93*f92d9ac3SLisandro Dalcin   ierr = PetscLayoutGetSize(map,&N);CHKERRQ(ierr);
94*f92d9ac3SLisandro Dalcin   ierr = PetscLayoutGetLocalSize(map,&n);CHKERRQ(ierr);
95*f92d9ac3SLisandro Dalcin   ierr = PetscLayoutGetRange(map,&s,NULL);CHKERRQ(ierr);
96*f92d9ac3SLisandro Dalcin   if (N != rows) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"IS in file different size (%D) than input IS (%D)",rows,N);
97*f92d9ac3SLisandro Dalcin 
98*f92d9ac3SLisandro Dalcin   /* read IS indices */
99*f92d9ac3SLisandro Dalcin   ierr = PetscMalloc1(n,&idx);CHKERRQ(ierr);
100*f92d9ac3SLisandro Dalcin   ierr = PetscViewerBinaryReadAll(viewer,idx,n,s,N,PETSC_INT);CHKERRQ(ierr);
101*f92d9ac3SLisandro Dalcin   ierr = ISGeneralSetIndices(is,n,idx,PETSC_OWN_POINTER);CHKERRQ(ierr);
102ede126feSBarry Smith   PetscFunctionReturn(0);
103ede126feSBarry Smith }
104ede126feSBarry Smith 
105235f7792SMatthew G. Knepley PetscErrorCode ISLoad_Default(IS is, PetscViewer viewer)
106235f7792SMatthew G. Knepley {
107*f92d9ac3SLisandro Dalcin   PetscBool      isbinary,ishdf5;
108235f7792SMatthew G. Knepley   PetscErrorCode ierr;
109235f7792SMatthew G. Knepley 
110235f7792SMatthew G. Knepley   PetscFunctionBegin;
111235f7792SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
112235f7792SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
113235f7792SMatthew G. Knepley   if (isbinary) {
114ede126feSBarry Smith     ierr = ISLoad_Binary(is, viewer);CHKERRQ(ierr);
115235f7792SMatthew G. Knepley   } else if (ishdf5) {
116*f92d9ac3SLisandro Dalcin #if defined(PETSC_HAVE_HDF5)
117235f7792SMatthew G. Knepley     ierr = ISLoad_HDF5(is, viewer);CHKERRQ(ierr);
118235f7792SMatthew G. Knepley #endif
119235f7792SMatthew G. Knepley   }
120235f7792SMatthew G. Knepley   PetscFunctionReturn(0);
121235f7792SMatthew G. Knepley }
122