xref: /petsc/include/petscviewerhdf5.h (revision d2522c19e8fa9bca20aaca277941d9a63e71db6a)
1 
2 #if !defined(PETSCVIEWERHDF5_H)
3 #define PETSCVIEWERHDF5_H
4 
5 #include <petscviewer.h>
6 
7 #if defined(PETSC_HAVE_HDF5)
8 #include <hdf5.h>
9 #if !defined(H5_VERSION_GE)
10 /* H5_VERSION_GE was introduced in HDF5 1.8.7, we support >= 1.8.0 */
11 /* So beware this will automatically 0 for HDF5 1.8.0 - 1.8.6 */
12 #define H5_VERSION_GE(a, b, c) 0
13 #endif
14 
15 PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetFileId(PetscViewer, hid_t *);
16 PETSC_EXTERN PetscErrorCode PetscViewerHDF5OpenGroup(PetscViewer, hid_t *, hid_t *);
17 
18 /* On 32 bit systems HDF5 is limited by size of integer, because hsize_t is defined as size_t */
19 #define PETSC_HDF5_INT_MAX (~(hsize_t)0)
20 
21 /* As per https://portal.hdfgroup.org/display/HDF5/Chunking+in+HDF5, max. chunk size is 4GB */
22 #define PETSC_HDF5_MAX_CHUNKSIZE 2147483647
23 
24 static inline PetscErrorCode PetscViewerHDF5PathIsRelative(const char path[], PetscBool emptyIsRelative, PetscBool *has) {
25   size_t len;
26 
27   PetscFunctionBegin;
28   *has = emptyIsRelative;
29   PetscCall(PetscStrlen(path, &len));
30   if (len) *has = (PetscBool)(path[0] != '/');
31   PetscFunctionReturn(0);
32 }
33 
34 static inline PetscErrorCode PetscHDF5IntCast(PetscInt a, hsize_t *b) {
35   PetscFunctionBegin;
36   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot convert negative size");
37 #if defined(PETSC_USE_64BIT_INDICES) && (H5_SIZEOF_HSIZE_T == 4)
38   PetscCheck(a >= PETSC_HDF5_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Array too long for HDF5");
39 #endif
40   *b = (hsize_t)(a);
41   PetscFunctionReturn(0);
42 }
43 PETSC_EXTERN PetscErrorCode PetscDataTypeToHDF5DataType(PetscDataType, hid_t *);
44 PETSC_EXTERN PetscErrorCode PetscHDF5DataTypeToPetscDataType(hid_t, PetscDataType *);
45 
46 PETSC_EXTERN PetscErrorCode PetscViewerHDF5HasDataset(PetscViewer, const char[], PetscBool *);
47 PETSC_EXTERN PetscErrorCode PetscViewerHDF5HasObject(PetscViewer, PetscObject, PetscBool *);
48 PETSC_EXTERN PetscErrorCode PetscViewerHDF5ReadAttribute(PetscViewer, const char[], const char[], PetscDataType, const void *, void *);
49 PETSC_EXTERN PetscErrorCode PetscViewerHDF5WriteAttribute(PetscViewer, const char[], const char[], PetscDataType, const void *);
50 PETSC_EXTERN PetscErrorCode PetscViewerHDF5HasAttribute(PetscViewer, const char[], const char[], PetscBool *);
51 PETSC_EXTERN PetscErrorCode PetscViewerHDF5ReadObjectAttribute(PetscViewer, PetscObject, const char[], PetscDataType, void *, void *);
52 PETSC_EXTERN PetscErrorCode PetscViewerHDF5WriteObjectAttribute(PetscViewer, PetscObject, const char[], PetscDataType, const void *);
53 PETSC_EXTERN PetscErrorCode PetscViewerHDF5HasObjectAttribute(PetscViewer, PetscObject, const char[], PetscBool *);
54 
55 PETSC_EXTERN PetscErrorCode PetscViewerHDF5Open(MPI_Comm, const char[], PetscFileMode, PetscViewer *);
56 PETSC_EXTERN PetscErrorCode PetscViewerHDF5PushGroup(PetscViewer, const char[]);
57 PETSC_EXTERN PetscErrorCode PetscViewerHDF5PushGroupRelative(PetscViewer, const char[]);
58 PETSC_EXTERN PetscErrorCode PetscViewerHDF5PopGroup(PetscViewer);
59 PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetGroup(PetscViewer, const char *[]);
60 PETSC_EXTERN PetscErrorCode PetscViewerHDF5HasGroup(PetscViewer, const char[], PetscBool *);
61 
62 PETSC_EXTERN PetscErrorCode PetscViewerHDF5SetDefaultTimestepping(PetscViewer, PetscBool);
63 PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetDefaultTimestepping(PetscViewer, PetscBool *);
64 PETSC_EXTERN PetscErrorCode PetscViewerHDF5PushTimestepping(PetscViewer);
65 PETSC_EXTERN PetscErrorCode PetscViewerHDF5PopTimestepping(PetscViewer);
66 PETSC_EXTERN PetscErrorCode PetscViewerHDF5IsTimestepping(PetscViewer, PetscBool *);
67 PETSC_EXTERN PetscErrorCode PetscViewerHDF5IncrementTimestep(PetscViewer);
68 PETSC_EXTERN PetscErrorCode PetscViewerHDF5SetTimestep(PetscViewer, PetscInt);
69 PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetTimestep(PetscViewer, PetscInt *);
70 
71 PETSC_EXTERN PetscErrorCode PetscViewerHDF5SetBaseDimension2(PetscViewer, PetscBool);
72 PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetBaseDimension2(PetscViewer, PetscBool *);
73 
74 PETSC_EXTERN PetscErrorCode PetscViewerHDF5SetSPOutput(PetscViewer, PetscBool);
75 PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetSPOutput(PetscViewer, PetscBool *);
76 
77 PETSC_EXTERN PetscErrorCode PetscViewerHDF5SetCollective(PetscViewer, PetscBool);
78 PETSC_EXTERN PetscErrorCode PetscViewerHDF5GetCollective(PetscViewer, PetscBool *);
79 #endif /* defined(PETSC_HAVE_HDF5) */
80 #endif
81