aboutsummaryrefslogtreecommitdiff
path: root/src/StreamedHDF5GH.h
blob: 19cdac74c380adfe8d70c102d8d5cccac6c60b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
 /*@@
   @header    StreamedHDF5GH.h
   @date      Fri May 21 1999
   @author    Thomas Radke
   @desc 
   The extensions to the GH structure from StreamedHDF5.
   @history
   @hauthor Thomas Radke @hdate May 21 1999
   @hdesc Just copied from thorn FlexIO.
   @endhistory
 @@*/

#ifdef __cplusplus
extern "C" {
#endif

#include <string.h>
#include <hdf5.h>

#include "StoreNamedData.h"


/* names of the (empty) groups that hold GH extensions or global parameters */
#define GHEXTENSIONS_GROUP      "GHextensions_group"
#define GLOBAL_PARAMETERS_GROUP "global_parameters_group"
#define GLOBAL_PARAMETERS       "global_parameters"

/*****************************************************************************/
/* some useful macros                                                        */
/*****************************************************************************/
/* Check error flags from HDF5 calls */
#define CACTUS_IOHDF5_ERROR(fn_call)                                          \
    do {                                                                      \
      size_t strlen(const char *str);                                         \
                                                                              \
      int error_code = fn_call;                                               \
                                                                              \
      if (error_code < 0)                                                     \
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,                  \
                    "HDF5 call '%s' returned error code %d\n",                \
                    #fn_call, error_code);                                    \
    } while (0)

/* macro for writing an attribute */
#define WRITE_ATTRIBUTE(name, value, dataset, dataspace, dim, datatype)       \
          do {                                                                \
            hid_t attr;                                                       \
            void *val = value;                                                \
            hsize_t arrayDim = dim;                                           \
                                                                              \
            if (H5Tget_class (datatype) == H5T_STRING) {                      \
              int len = strlen ((char *) val);                                \
                                                                              \
              if (len == 0)      /* HDF5 doesn't like zero-len strings */     \
                len++;                                                        \
              CACTUS_IOHDF5_ERROR (H5Tset_size (datatype, len));              \
            }                                                                 \
            if (arrayDim > 0)                                                 \
              CACTUS_IOHDF5_ERROR (H5Sset_extent_simple (dataspace, 1,        \
                                   &arrayDim, NULL));                         \
            CACTUS_IOHDF5_ERROR (attr = H5Acreate (dataset, name, datatype,   \
                                 dataspace, H5P_DEFAULT));                    \
            CACTUS_IOHDF5_ERROR (H5Awrite (attr, datatype, val));             \
            CACTUS_IOHDF5_ERROR (H5Aclose (attr));                            \
          } while (0);

/* macro for reading an attribute */
#define READ_ATTRIBUTE(dataset, attrname, requested_type, buffer)             \
          { hid_t attr, attrtype;                                             \
            hsize_t asize = 0;                                                \
                                                                              \
            if ((attr = H5Aopen_name (dataset, attrname)) < 0)                \
              CCTK_WARN (1, "Can't find " attrname " attribute");             \
            if (requested_type == H5T_C_S1) {                                 \
              CACTUS_IOHDF5_ERROR (attrtype = H5Aget_type (attr));            \
              CACTUS_IOHDF5_ERROR (asize = H5Tget_size (attrtype));           \
              if (asize + 1 >= sizeof (buffer))                               \
                CCTK_WARN (1, "Can't read " attrname " attribute (too long)");\
            } else                                                            \
              attrtype = requested_type;                                      \
            if (H5Aread (attr, attrtype, buffer) < 0)                         \
              CCTK_WARN (1, "Can't read " attrname " attribute");             \
            if (requested_type == H5T_C_S1) {                                 \
              ((char *) buffer) [asize] = 0;                                  \
              CACTUS_IOHDF5_ERROR (H5Tclose (attrtype));                      \
            }                                                                 \
            CACTUS_IOHDF5_ERROR (H5Aclose (attr));                            \
          }


/* define the HDF5 datatypes according to CCTK_??? datatypes */
#define IOHDF5_REAL4   H5T_NATIVE_FLOAT

#ifdef  CCTK_REAL_PRECISION_16
#define IOHDF5_REAL    H5T_NATIVE_LDOUBLE
#elif   CCTK_REAL_PRECISION_8
#define IOHDF5_REAL    H5T_NATIVE_DOUBLE
#elif   CCTK_REAL_PRECISION_4
#define IOHDF5_REAL    H5T_NATIVE_FLOAT
#endif

#define IOHDF5_INT     (sizeof (CCTK_INT) == sizeof (int) ?                   \
                        H5T_NATIVE_INT : H5T_NATIVE_SHORT)
#define IOHDF5_INT4    (sizeof (int) == 4 ?                                   \
                        H5T_NATIVE_INT : H5T_NATIVE_SHORT)
#define IOHDF5_CHAR    H5T_NATIVE_CHAR

#define STREAM_MAXDIM 5

/* Geometry information structure for output variable */
typedef struct 
{
  int vdim;
  int sdim;
  int direction[STREAM_MAXDIM];
  int origin[STREAM_MAXDIM];
  int length[STREAM_MAXDIM];
  int downs[STREAM_MAXDIM];
} StreamGeo_t;


/* StreamedHDF5 GH extension structure */
typedef struct StreamedHDF5GH
{
  /* how often to output */
  int  out_every;

  /* flags indicating output for var [i] */
  char        *do_output;
  StreamGeo_t *geo_output;

  /* the last iteration output */
  int   *out_last;

  /* variables holding the original error printing routine and its argument */
  H5E_auto_t printErrorFn;
  void      *printErrorFnArg;

  /* predefined dataspaces for writing scalar and array attributes */
  hid_t scalarDataspace, arrayDataspace;

  /* predefined datatype for writing CCTK_COMPLEX types */
  hid_t IOHDF5_COMPLEX;

  /* predefined datatype for writing C string string attributes */
  hid_t IOHDF5_STRING;

  /* the socket to pass to the Stream VFD */
  int socket;

} StreamedHDF5GH;

/* function prototypes */
void StreamedHDF5_Write (cGH *GH, int index, const char *alias);
void StreamedHDF5_DumpParams (cGH *GH, hid_t group);
void StreamedHDF5_DumpGHExtensions (cGH *GH, hid_t group);
int StreamedHDF5_DumpVar (cGH *GH, int index, int timelevel, hid_t iof);
int ParseVarsForOutput (StreamedHDF5GH *h5GH, const char *var_list);
void SetDefaultGeo(StreamGeo_t *geo);
int GeometryParser(const char *before, char **outname, StreamGeo_t *geo);

#ifdef __cplusplus
} // extern "C" {
#endif