1 #include <petscis.h> /*I "petscis.h" I*/ 2 #include <petsc/private/isimpl.h> 3 #include <petsc/private/viewerimpl.h> 4 #include <petsclayouthdf5.h> 5 6 PetscErrorCode ISView_Binary(IS is,PetscViewer viewer) 7 { 8 PetscErrorCode ierr; 9 PetscBool skipHeader; 10 PetscLayout map; 11 PetscInt tr[2],n,s,N; 12 const PetscInt *iarray; 13 14 PetscFunctionBegin; 15 ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 16 ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr); 17 18 ierr = ISGetLayout(is,&map);CHKERRQ(ierr); 19 ierr = PetscLayoutGetLocalSize(map,&n);CHKERRQ(ierr); 20 ierr = PetscLayoutGetRange(map,&s,NULL);CHKERRQ(ierr); 21 ierr = PetscLayoutGetSize(map,&N);CHKERRQ(ierr); 22 23 /* write IS header */ 24 tr[0] = IS_FILE_CLASSID; tr[1] = N; 25 if (!skipHeader) {ierr = PetscViewerBinaryWrite(viewer,tr,2,PETSC_INT);CHKERRQ(ierr);} 26 27 /* write IS indices */ 28 ierr = ISGetIndices(is,&iarray);CHKERRQ(ierr); 29 ierr = PetscViewerBinaryWriteAll(viewer,iarray,n,s,N,PETSC_INT);CHKERRQ(ierr); 30 ierr = ISRestoreIndices(is,&iarray);CHKERRQ(ierr); 31 PetscFunctionReturn(0); 32 } 33 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; 45 46 PetscFunctionBegin; 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 ierr = PetscObjectGetName((PetscObject)is, &isname);CHKERRQ(ierr); 54 ierr = PetscViewerHDF5Load(viewer, isname, is->map, inttype, (void**)&ind);CHKERRQ(ierr); 55 ierr = ISGeneralSetIndices(is, is->map->n, ind, PETSC_OWN_POINTER);CHKERRQ(ierr); 56 PetscFunctionReturn(0); 57 } 58 #endif 59 60 PetscErrorCode ISLoad_Binary(IS is, PetscViewer viewer) 61 { 62 PetscErrorCode ierr; 63 PetscBool isgeneral,skipHeader; 64 PetscInt tr[2],rows,N,n,s,*idx; 65 PetscLayout map; 66 67 PetscFunctionBegin; 68 ierr = PetscObjectTypeCompare((PetscObject)is,ISGENERAL,&isgeneral);CHKERRQ(ierr); 69 if (!isgeneral) SETERRQ(PetscObjectComm((PetscObject)is),PETSC_ERR_ARG_INCOMP,"IS must be of type ISGENERAL to load into it"); 70 ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 71 ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr); 72 73 ierr = ISGetLayout(is,&map);CHKERRQ(ierr); 74 ierr = PetscLayoutGetSize(map,&N);CHKERRQ(ierr); 75 76 /* read IS header */ 77 if (!skipHeader) { 78 ierr = PetscViewerBinaryRead(viewer,tr,2,NULL,PETSC_INT);CHKERRQ(ierr); 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 } 87 88 /* set IS size if not already set */ 89 if (N < 0) {ierr = PetscLayoutSetSize(map,rows);CHKERRQ(ierr);} 90 ierr = PetscLayoutSetUp(map);CHKERRQ(ierr); 91 92 /* get IS sizes and check global size */ 93 ierr = PetscLayoutGetSize(map,&N);CHKERRQ(ierr); 94 ierr = PetscLayoutGetLocalSize(map,&n);CHKERRQ(ierr); 95 ierr = PetscLayoutGetRange(map,&s,NULL);CHKERRQ(ierr); 96 if (N != rows) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"IS in file different size (%D) than input IS (%D)",rows,N); 97 98 /* read IS indices */ 99 ierr = PetscMalloc1(n,&idx);CHKERRQ(ierr); 100 ierr = PetscViewerBinaryReadAll(viewer,idx,n,s,N,PETSC_INT);CHKERRQ(ierr); 101 ierr = ISGeneralSetIndices(is,n,idx,PETSC_OWN_POINTER);CHKERRQ(ierr); 102 PetscFunctionReturn(0); 103 } 104 105 PetscErrorCode ISLoad_Default(IS is, PetscViewer viewer) 106 { 107 PetscBool isbinary,ishdf5; 108 PetscErrorCode ierr; 109 110 PetscFunctionBegin; 111 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 112 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr); 113 if (isbinary) { 114 ierr = ISLoad_Binary(is, viewer);CHKERRQ(ierr); 115 } else if (ishdf5) { 116 #if defined(PETSC_HAVE_HDF5) 117 ierr = ISLoad_HDF5(is, viewer);CHKERRQ(ierr); 118 #endif 119 } 120 PetscFunctionReturn(0); 121 } 122