Actual source code: isio.c

  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: {
  8:   PetscBool       skipHeader;
  9:   PetscLayout     map;
 10:   PetscInt        tr[2], n, s, N;
 11:   const PetscInt *iarray;

 13:   PetscViewerSetUp(viewer);
 14:   PetscViewerBinaryGetSkipHeader(viewer, &skipHeader);

 16:   ISGetLayout(is, &map);
 17:   PetscLayoutGetLocalSize(map, &n);
 18:   PetscLayoutGetRange(map, &s, NULL);
 19:   PetscLayoutGetSize(map, &N);

 21:   /* write IS header */
 22:   tr[0] = IS_FILE_CLASSID;
 23:   tr[1] = N;
 24:   if (!skipHeader) PetscViewerBinaryWrite(viewer, tr, 2, PETSC_INT);

 26:   /* write IS indices */
 27:   ISGetIndices(is, &iarray);
 28:   PetscViewerBinaryWriteAll(viewer, iarray, n, s, N, PETSC_INT);
 29:   ISRestoreIndices(is, &iarray);
 30:   return 0;
 31: }

 33: #if defined(PETSC_HAVE_HDF5)
 34: /*
 35:      This should handle properly the cases where PetscInt is 32 or 64 and hsize_t is 32 or 64. These means properly casting with
 36:    checks back and forth between the two types of variables.
 37: */
 38: PetscErrorCode ISLoad_HDF5(IS is, PetscViewer viewer)
 39: {
 40:   hid_t       inttype; /* int type (H5T_NATIVE_INT or H5T_NATIVE_LLONG) */
 41:   PetscInt   *ind;
 42:   const char *isname;

 45:   #if defined(PETSC_USE_64BIT_INDICES)
 46:   inttype = H5T_NATIVE_LLONG;
 47:   #else
 48:   inttype = H5T_NATIVE_INT;
 49:   #endif
 50:   PetscObjectGetName((PetscObject)is, &isname);
 51:   PetscViewerHDF5Load(viewer, isname, is->map, inttype, (void **)&ind);
 52:   ISGeneralSetIndices(is, is->map->n, ind, PETSC_OWN_POINTER);
 53:   return 0;
 54: }
 55: #endif

 57: PetscErrorCode ISLoad_Binary(IS is, PetscViewer viewer)
 58: {
 59:   PetscBool   isgeneral, skipHeader;
 60:   PetscInt    tr[2], rows, N, n, s, *idx;
 61:   PetscLayout map;

 63:   PetscObjectTypeCompare((PetscObject)is, ISGENERAL, &isgeneral);
 65:   PetscViewerSetUp(viewer);
 66:   PetscViewerBinaryGetSkipHeader(viewer, &skipHeader);

 68:   ISGetLayout(is, &map);
 69:   PetscLayoutGetSize(map, &N);

 71:   /* read IS header */
 72:   if (!skipHeader) {
 73:     PetscViewerBinaryRead(viewer, tr, 2, NULL, PETSC_INT);
 77:     rows = tr[1];
 78:   } else {
 80:     rows = N;
 81:   }

 83:   /* set IS size if not already set */
 84:   if (N < 0) PetscLayoutSetSize(map, rows);
 85:   PetscLayoutSetUp(map);

 87:   /* get IS sizes and check global size */
 88:   PetscLayoutGetSize(map, &N);
 89:   PetscLayoutGetLocalSize(map, &n);
 90:   PetscLayoutGetRange(map, &s, NULL);

 93:   /* read IS indices */
 94:   PetscMalloc1(n, &idx);
 95:   PetscViewerBinaryReadAll(viewer, idx, n, s, N, PETSC_INT);
 96:   ISGeneralSetIndices(is, n, idx, PETSC_OWN_POINTER);
 97:   return 0;
 98: }

100: PetscErrorCode ISLoad_Default(IS is, PetscViewer viewer)
101: {
102:   PetscBool isbinary, ishdf5;

104:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary);
105:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
106:   if (isbinary) {
107:     ISLoad_Binary(is, viewer);
108:   } else if (ishdf5) {
109: #if defined(PETSC_HAVE_HDF5)
110:     ISLoad_HDF5(is, viewer);
111: #endif
112:   }
113:   return 0;
114: }