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; tr[1] = N;
     23:   if (!skipHeader) PetscViewerBinaryWrite(viewer,tr,2,PETSC_INT);

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

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

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

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

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

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

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

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

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

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

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

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