1 #include <petsc/private/viewerhdf5impl.h> 2 #include <petsclayouthdf5.h> /*I "petsclayoutdf5.h" I*/ 3 #include <petscis.h> /*I "petscis.h" I*/ 4 5 #if defined(PETSC_HAVE_HDF5) 6 7 struct _n_HDF5ReadCtx { 8 hid_t file, group, dataset, dataspace; 9 int lenInd, bsInd, complexInd, rdim; 10 hsize_t *dims; 11 PetscBool complexVal, dim2; 12 }; 13 typedef struct _n_HDF5ReadCtx *HDF5ReadCtx; 14 15 PetscErrorCode PetscViewerHDF5CheckTimestepping_Internal(PetscViewer viewer, const char name[]) 16 { 17 PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data; 18 PetscBool timestepping = PETSC_FALSE; 19 20 PetscFunctionBegin; 21 PetscCall(PetscViewerHDF5ReadAttribute(viewer, name, "timestepping", PETSC_BOOL, &hdf5->defTimestepping, ×tepping)); 22 if (timestepping != hdf5->timestepping) { 23 char *group; 24 25 PetscCall(PetscViewerHDF5GetGroup(viewer, NULL, &group)); 26 SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Dataset %s/%s stored with timesteps? %s Timestepping pushed? %s", group, name, PetscBools[timestepping], PetscBools[hdf5->timestepping]); 27 } 28 PetscFunctionReturn(PETSC_SUCCESS); 29 } 30 31 static PetscErrorCode PetscViewerHDF5ReadInitialize_Private(PetscViewer viewer, const char name[], HDF5ReadCtx *ctx) 32 { 33 PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data; 34 HDF5ReadCtx h = NULL; 35 36 PetscFunctionBegin; 37 PetscCall(PetscViewerHDF5CheckTimestepping_Internal(viewer, name)); 38 PetscCall(PetscNew(&h)); 39 PetscCall(PetscViewerHDF5OpenGroup(viewer, NULL, &h->file, &h->group)); 40 PetscCallHDF5Return(h->dataset, H5Dopen2, (h->group, name, H5P_DEFAULT)); 41 PetscCallHDF5Return(h->dataspace, H5Dget_space, (h->dataset)); 42 PetscCall(PetscViewerHDF5ReadAttribute(viewer, name, "complex", PETSC_BOOL, &h->complexVal, &h->complexVal)); 43 if (!hdf5->horizontal) { 44 /* MATLAB stores column vectors horizontally */ 45 PetscCall(PetscViewerHDF5HasAttribute(viewer, name, "MATLAB_class", &hdf5->horizontal)); 46 } 47 *ctx = h; 48 PetscFunctionReturn(PETSC_SUCCESS); 49 } 50 51 static PetscErrorCode PetscViewerHDF5ReadFinalize_Private(PetscViewer viewer, HDF5ReadCtx *ctx) 52 { 53 HDF5ReadCtx h; 54 55 PetscFunctionBegin; 56 h = *ctx; 57 PetscCallHDF5(H5Gclose, (h->group)); 58 PetscCallHDF5(H5Sclose, (h->dataspace)); 59 PetscCallHDF5(H5Dclose, (h->dataset)); 60 PetscCall(PetscFree((*ctx)->dims)); 61 PetscCall(PetscFree(*ctx)); 62 PetscFunctionReturn(PETSC_SUCCESS); 63 } 64 65 static PetscErrorCode PetscViewerHDF5ReadSizes_Private(PetscViewer viewer, HDF5ReadCtx ctx, PetscBool setup, PetscLayout *map_) 66 { 67 PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data; 68 PetscInt bs, N; 69 PetscLayout map; 70 71 PetscFunctionBegin; 72 if (!*map_) PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)viewer), map_)); 73 map = *map_; 74 75 /* Get actual number of dimensions in dataset */ 76 PetscCallHDF5Return(ctx->rdim, H5Sget_simple_extent_dims, (ctx->dataspace, NULL, NULL)); 77 PetscCall(PetscMalloc1(ctx->rdim, &ctx->dims)); 78 PetscCallHDF5Return(ctx->rdim, H5Sget_simple_extent_dims, (ctx->dataspace, ctx->dims, NULL)); 79 80 /* 81 Dimensions are in this order: 82 [0] timesteps (optional) 83 [lenInd] entries (numbers or blocks) 84 ... 85 [bsInd] entries of blocks (optional) 86 [bsInd+1] real & imaginary part (optional) 87 = rdim-1 88 */ 89 90 /* Get entries dimension index */ 91 ctx->lenInd = 0; 92 if (hdf5->timestepping) ++ctx->lenInd; 93 94 /* Get block dimension index */ 95 if (ctx->complexVal) { 96 ctx->bsInd = ctx->rdim - 2; 97 ctx->complexInd = ctx->rdim - 1; 98 } else { 99 ctx->bsInd = ctx->rdim - 1; 100 ctx->complexInd = -1; 101 } 102 PetscCheck(ctx->lenInd <= ctx->bsInd, PetscObjectComm((PetscObject)viewer), PETSC_ERR_PLIB, "Calculated block dimension index = %d < %d = length dimension index.", ctx->bsInd, ctx->lenInd); 103 PetscCheck(ctx->bsInd <= ctx->rdim - 1, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Calculated block dimension index = %d > %d = total number of dimensions - 1.", ctx->bsInd, ctx->rdim - 1); 104 PetscCheck(!ctx->complexVal || ctx->dims[ctx->complexInd] == 2, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Complex numbers must have exactly 2 parts (%" PRIuHSIZE ")", ctx->dims[ctx->complexInd]); 105 106 if (hdf5->horizontal) { 107 PetscInt t; 108 /* support horizontal 1D arrays (MATLAB vectors) - swap meaning of blocks and entries */ 109 t = ctx->lenInd; 110 ctx->lenInd = ctx->bsInd; 111 ctx->bsInd = t; 112 } 113 114 /* Get block size */ 115 ctx->dim2 = PETSC_FALSE; 116 if (ctx->lenInd == ctx->bsInd) { 117 bs = 1; /* support vectors stored as 1D array */ 118 } else { 119 bs = (PetscInt)ctx->dims[ctx->bsInd]; 120 if (bs == 1) ctx->dim2 = PETSC_TRUE; /* vector with blocksize of 1, still stored as 2D array */ 121 } 122 123 /* Get global size */ 124 PetscCall(PetscIntCast(bs * ctx->dims[ctx->lenInd], &N)); 125 126 /* Set global size, blocksize and type if not yet set */ 127 if (map->bs < 0) { 128 PetscCall(PetscLayoutSetBlockSize(map, bs)); 129 } else PetscCheck(map->bs == bs, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Block size of array in file is %" PetscInt_FMT ", not %" PetscInt_FMT " as expected", bs, map->bs); 130 if (map->N < 0) { 131 PetscCall(PetscLayoutSetSize(map, N)); 132 } else PetscCheck(map->N == N, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Global size of array in file is %" PetscInt_FMT ", not %" PetscInt_FMT " as expected", N, map->N); 133 if (setup) PetscCall(PetscLayoutSetUp(map)); 134 PetscFunctionReturn(PETSC_SUCCESS); 135 } 136 137 static PetscErrorCode PetscViewerHDF5ReadSelectHyperslab_Private(PetscViewer viewer, HDF5ReadCtx ctx, PetscLayout map, hid_t *memspace) 138 { 139 PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data; 140 hsize_t *count, *offset; 141 PetscInt bs, n, low; 142 int i; 143 144 PetscFunctionBegin; 145 /* Compute local size and ownership range */ 146 PetscCall(PetscLayoutSetUp(map)); 147 PetscCall(PetscLayoutGetBlockSize(map, &bs)); 148 PetscCall(PetscLayoutGetLocalSize(map, &n)); 149 PetscCall(PetscLayoutGetRange(map, &low, NULL)); 150 151 /* Each process defines a dataset and reads it from the hyperslab in the file */ 152 PetscCall(PetscMalloc2(ctx->rdim, &count, ctx->rdim, &offset)); 153 for (i = 0; i < ctx->rdim; i++) { 154 /* By default, select all entries with no offset */ 155 offset[i] = 0; 156 count[i] = ctx->dims[i]; 157 } 158 if (hdf5->timestepping) { 159 count[0] = 1; 160 offset[0] = hdf5->timestep; 161 } 162 { 163 PetscCall(PetscHDF5IntCast(n / bs, &count[ctx->lenInd])); 164 PetscCall(PetscHDF5IntCast(low / bs, &offset[ctx->lenInd])); 165 } 166 PetscCallHDF5Return(*memspace, H5Screate_simple, (ctx->rdim, count, NULL)); 167 PetscCallHDF5(H5Sselect_hyperslab, (ctx->dataspace, H5S_SELECT_SET, offset, NULL, count, NULL)); 168 PetscCall(PetscFree2(count, offset)); 169 PetscFunctionReturn(PETSC_SUCCESS); 170 } 171 172 static PetscErrorCode PetscViewerHDF5ReadArray_Private(PetscViewer viewer, HDF5ReadCtx h, hid_t datatype, hid_t memspace, void *arr) 173 { 174 PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data; 175 176 PetscFunctionBegin; 177 PetscCallHDF5(H5Dread, (h->dataset, datatype, memspace, h->dataspace, hdf5->dxpl_id, arr)); 178 PetscFunctionReturn(PETSC_SUCCESS); 179 } 180 181 /*@C 182 PetscViewerHDF5Load - Read a raw array from the `PETSCVIEWERHDF5` dataset in parallel 183 184 Collective; No Fortran Support 185 186 Input Parameters: 187 + viewer - The `PETSCVIEWERHDF5` viewer 188 . name - The dataset name 189 - datatype - The HDF5 datatype of the items in the dataset 190 191 Input/Output Parameter: 192 . map - The layout which specifies array partitioning, on output the 193 set up layout (with global size and blocksize according to dataset) 194 195 Output Parameter: 196 . newarr - The partitioned array, a memory image of the given dataset 197 198 Level: developer 199 200 Notes: 201 This is intended mainly for internal use; users should use higher level routines such as `ISLoad()`, `VecLoad()`, `DMLoad()`. 202 203 The array is partitioned according to the given `PetscLayout` which is converted to an HDF5 hyperslab. 204 205 This name is relative to the current group returned by `PetscViewerHDF5OpenGroup()`. 206 207 .seealso: `PetscViewer`, `PETSCVIEWERHDF5`, `PetscViewerHDF5Open()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5OpenGroup()`, `PetscViewerHDF5ReadSizes()`, 208 `VecLoad()`, `ISLoad()`, `PetscLayout` 209 @*/ 210 PetscErrorCode PetscViewerHDF5Load(PetscViewer viewer, const char name[], PetscLayout map, hid_t datatype, void **newarr) 211 { 212 PetscBool has; 213 char *group; 214 HDF5ReadCtx h = NULL; 215 hid_t memspace = 0; 216 size_t unitsize; 217 void *arr; 218 219 PetscFunctionBegin; 220 PetscCall(PetscViewerHDF5GetGroup(viewer, NULL, &group)); 221 PetscCall(PetscViewerHDF5HasDataset(viewer, name, &has)); 222 PetscCheck(has, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Object (dataset) \"%s\" not stored in group %s", name, group); 223 PetscCall(PetscViewerHDF5ReadInitialize_Private(viewer, name, &h)); 224 #if defined(PETSC_USE_COMPLEX) 225 if (!h->complexVal) { 226 H5T_class_t clazz = H5Tget_class(datatype); 227 PetscCheck(clazz != H5T_FLOAT, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Dataset %s/%s is marked as real but PETSc is configured for complex scalars. The conversion is not yet implemented. Configure with --with-scalar-type=real to read this dataset", group ? group : "", name); 228 } 229 #else 230 PetscCheck(!h->complexVal, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Dataset %s/%s is marked as complex but PETSc is configured for real scalars. Configure with --with-scalar-type=complex to read this dataset", group, name); 231 #endif 232 233 PetscCall(PetscViewerHDF5ReadSizes_Private(viewer, h, PETSC_TRUE, &map)); 234 PetscCall(PetscViewerHDF5ReadSelectHyperslab_Private(viewer, h, map, &memspace)); 235 236 unitsize = H5Tget_size(datatype); 237 if (h->complexVal) unitsize *= 2; 238 /* unitsize is size_t i.e. always unsigned, so the negative check is pointless? */ 239 PetscCheck(unitsize > 0 && unitsize <= PetscMax(sizeof(PetscInt), sizeof(PetscScalar)), PETSC_COMM_SELF, PETSC_ERR_LIB, "Sanity check failed: HDF5 function H5Tget_size(datatype) returned suspicious value %zu", unitsize); 240 PetscCall(PetscMalloc(map->n * unitsize, &arr)); 241 242 PetscCall(PetscViewerHDF5ReadArray_Private(viewer, h, datatype, memspace, arr)); 243 PetscCallHDF5(H5Sclose, (memspace)); 244 PetscCall(PetscViewerHDF5ReadFinalize_Private(viewer, &h)); 245 PetscCall(PetscFree(group)); 246 *newarr = arr; 247 PetscFunctionReturn(PETSC_SUCCESS); 248 } 249 250 /*@C 251 PetscViewerHDF5ReadSizes - Read block size and global size of a `Vec` or `IS` stored in an HDF5 file. 252 253 Input Parameters: 254 + viewer - The `PETSCVIEWERHDF5` viewer 255 - name - The dataset name 256 257 Output Parameters: 258 + bs - block size 259 - N - global size 260 261 Level: advanced 262 263 Notes: 264 The dataset is stored as an HDF5 dataspace with 1-4 dimensions in the order 265 1) # timesteps (optional), 2) # blocks, 3) # elements per block (optional), 4) real and imaginary part (only for complex). 266 267 The dataset can be stored as a 2D dataspace even if its blocksize is 1; see `PetscViewerHDF5SetBaseDimension2()`. 268 269 .seealso: `PetscViewer`, `PETSCVIEWERHDF5`, `PetscViewerHDF5Open()`, `VecLoad()`, `ISLoad()`, `VecGetSize()`, `ISGetSize()`, `PetscViewerHDF5SetBaseDimension2()` 270 @*/ 271 PetscErrorCode PetscViewerHDF5ReadSizes(PetscViewer viewer, const char name[], PetscInt *bs, PetscInt *N) 272 { 273 HDF5ReadCtx h = NULL; 274 PetscLayout map = NULL; 275 276 PetscFunctionBegin; 277 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 278 PetscCall(PetscViewerHDF5ReadInitialize_Private(viewer, name, &h)); 279 PetscCall(PetscViewerHDF5ReadSizes_Private(viewer, h, PETSC_FALSE, &map)); 280 PetscCall(PetscViewerHDF5ReadFinalize_Private(viewer, &h)); 281 if (bs) *bs = map->bs; 282 if (N) *N = map->N; 283 PetscCall(PetscLayoutDestroy(&map)); 284 PetscFunctionReturn(PETSC_SUCCESS); 285 } 286 287 #endif /* defined(PETSC_HAVE_HDF5) */ 288