Actual source code: petscviewerhdf5.h
1: #pragma once
3: #include <petscviewer.h>
5: /* MANSEC = Sys */
6: /* SUBMANSEC = Viewer */
8: #if PetscDefined(HAVE_HDF5)
9: #include <hdf5.h>
10: #if !defined(H5_VERSION_GE)
11: /* H5_VERSION_GE was introduced in HDF5 1.8.7, we support >= 1.8.0 */
12: /* So beware this will automatically 0 for HDF5 1.8.0 - 1.8.6 */
13: #define H5_VERSION_GE(a, b, c) 0
14: #endif
16: // HDF5 1.13.0 switched from hsize_t being typedef'd to unsigned long long to being uint64_t and introduced the
17: // PRIuHSIZE macro for printing. Definition of PRIuHSIZE actually precedes the change of typedef in the Git history,
18: // though there was never a release with the old definitions. Nonetheless, the logic here will work any commit.
19: #if !defined(PRIuHSIZE)
20: #define PRIuHSIZE "llu"
21: #endif
23: PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetFileId(PetscViewer, hid_t *);
25: /* On 32-bit systems HDF5 is limited by size of integer, because hsize_t is defined as size_t */
26: #define PETSC_HDF5_INT_MAX (~(hsize_t)0)
28: /* As per https://portal.hdfgroup.org/display/HDF5/Chunking+in+HDF5, max. chunk size is 4GB */
29: #define PETSC_HDF5_MAX_CHUNKSIZE 2147483647
31: /*@C
32: PetscHDF5IntCast - Safely cast a nonnegative `PetscInt` to an HDF5 `hsize_t`, raising an error on overflow or negative input
34: Not Collective; No Fortran Support
36: Input Parameter:
37: . a - the `PetscInt` value to convert
39: Output Parameter:
40: . b - on output, the value as an `hsize_t`
42: Level: developer
44: Note:
45: Used internally by PETSc's HDF5 viewer routines when computing dataset dimensions and offsets.
47: .seealso: `PetscViewerHDF5`, `PetscIntCast()`, `PetscMPIIntCast()`
48: @*/
49: static inline PetscErrorCode PetscHDF5IntCast(PetscInt a, hsize_t *b)
50: {
51: PetscFunctionBegin;
52: PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot convert negative size");
53: #if PetscDefined(USE_64BIT_INDICES) && (H5_SIZEOF_HSIZE_T == 4)
54: PetscCheck(a >= PETSC_HDF5_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Array too long for HDF5");
55: #endif
56: *b = (hsize_t)(a);
57: PetscFunctionReturn(PETSC_SUCCESS);
58: }
59: PETSC_EXTERN PetscErrorCode PetscDataTypeToHDF5DataType(PetscDataType, hid_t *);
60: PETSC_EXTERN PetscErrorCode PetscHDF5DataTypeToPetscDataType(hid_t, PetscDataType *);
61: PETSC_EXTERN PetscErrorCode PetscViewerHDF5OpenGroup(PetscViewer, const char[], hid_t *, hid_t *);
62: #endif /* PetscDefined(HAVE_HDF5) */
64: /*@
65: PetscViewerHDF5PathIsRelative - Determine whether an HDF5 path string is relative (does not begin with `/`).
67: Not Collective
69: Input Parameters:
70: + path - the HDF5 path
71: - emptyIsRelative - the value to return when `path` is empty
73: Output Parameter:
74: . rel - `PETSC_TRUE` if `path` is relative, `PETSC_FALSE` if it is absolute
76: Level: developer
78: .seealso: `PetscViewer`, `PETSCVIEWERHDF5`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5GetGroup()`
79: @*/
80: static inline PetscErrorCode PetscViewerHDF5PathIsRelative(const char path[], PetscBool emptyIsRelative, PetscBool *rel)
81: {
82: size_t len;
84: PetscFunctionBegin;
85: *rel = emptyIsRelative;
86: PetscCall(PetscStrlen(path, &len));
87: if (len) *rel = (PetscBool)(path[0] != '/');
88: PetscFunctionReturn(PETSC_SUCCESS);
89: }
91: PETSC_EXTERN PetscErrorCode PetscViewerHDF5HasDataset(PetscViewer, const char[], PetscBool *);
92: PETSC_EXTERN PetscErrorCode PetscViewerHDF5HasObject(PetscViewer, PetscObject, PetscBool *);
93: PETSC_EXTERN PetscErrorCode PetscViewerHDF5ReadAttribute(PetscViewer, const char[], const char[], PetscDataType, const void *, void *);
94: PETSC_EXTERN PetscErrorCode PetscViewerHDF5WriteAttribute(PetscViewer, const char[], const char[], PetscDataType, const void *);
95: PETSC_EXTERN PetscErrorCode PetscViewerHDF5HasAttribute(PetscViewer, const char[], const char[], PetscBool *);
96: PETSC_EXTERN PetscErrorCode PetscViewerHDF5ReadObjectAttribute(PetscViewer, PetscObject, const char[], PetscDataType, void *, void *);
97: PETSC_EXTERN PetscErrorCode PetscViewerHDF5WriteObjectAttribute(PetscViewer, PetscObject, const char[], PetscDataType, const void *);
98: PETSC_EXTERN PetscErrorCode PetscViewerHDF5HasObjectAttribute(PetscViewer, PetscObject, const char[], PetscBool *);
100: PETSC_EXTERN PetscErrorCode PetscViewerHDF5Open(MPI_Comm, const char[], PetscFileMode, PetscViewer *);
101: PETSC_EXTERN PetscErrorCode PetscViewerHDF5PushGroup(PetscViewer, const char[]);
102: PETSC_EXTERN PetscErrorCode PetscViewerHDF5PopGroup(PetscViewer);
103: PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetGroup(PetscViewer, const char[], const char *[]);
104: PETSC_EXTERN PetscErrorCode PetscViewerHDF5HasGroup(PetscViewer, const char[], PetscBool *);
105: PETSC_EXTERN PetscErrorCode PetscViewerHDF5WriteGroup(PetscViewer, const char[]);
107: PETSC_EXTERN PetscErrorCode PetscViewerHDF5SetDefaultTimestepping(PetscViewer, PetscBool);
108: PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetDefaultTimestepping(PetscViewer, PetscBool *);
109: PETSC_EXTERN PetscErrorCode PetscViewerHDF5PushTimestepping(PetscViewer);
110: PETSC_EXTERN PetscErrorCode PetscViewerHDF5PopTimestepping(PetscViewer);
111: PETSC_EXTERN PetscErrorCode PetscViewerHDF5IsTimestepping(PetscViewer, PetscBool *);
112: PETSC_EXTERN PetscErrorCode PetscViewerHDF5IncrementTimestep(PetscViewer);
113: PETSC_EXTERN PetscErrorCode PetscViewerHDF5SetTimestep(PetscViewer, PetscInt);
114: PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetTimestep(PetscViewer, PetscInt *);
116: PETSC_EXTERN PetscErrorCode PetscViewerHDF5SetBaseDimension2(PetscViewer, PetscBool);
117: PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetBaseDimension2(PetscViewer, PetscBool *);
119: PETSC_EXTERN PetscErrorCode PetscViewerHDF5SetSPOutput(PetscViewer, PetscBool);
120: PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetSPOutput(PetscViewer, PetscBool *);
122: PETSC_EXTERN PetscErrorCode PetscViewerHDF5SetCollective(PetscViewer, PetscBool);
123: PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetCollective(PetscViewer, PetscBool *);
125: PETSC_EXTERN PetscErrorCode PetscViewerHDF5SetCompress(PetscViewer, PetscBool);
126: PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetCompress(PetscViewer, PetscBool *);