aboutsummaryrefslogtreecommitdiff
path: root/src/ioHDF5UtilGH.h
blob: dfdb31e1e16a6a11b0d9c999e3acc471b11c4617 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
 /*@@
   @header    ioHDF5UtilGH.h
   @date      Fri Oct 6 2000
   @author    Thomas Radke
   @desc
              The GH extensions structure for IOHDF5Util.
   @enddesc
   @version   $Id$
 @@*/

#ifndef _IOUTILHDF5_IOUTILHDF5GH_H_
#define _IOUTILHDF5_IOUTILHDF5GH_H_

/* include the HDF5 header file */
#include <hdf5.h>


/*****************************************************************************/
/* some useful macros                                                        */
/*****************************************************************************/
/* Check error flags from HDF5 calls */
#define IOHDF5_ERROR(fn_call)                                                 \
    do                                                                        \
    {                                                                         \
      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                                                                  \
          {                                                                   \
            int _len;                                                         \
            hid_t _attr;                                                      \
            /* this union is only there to fix a bug in the HDF5 API */       \
            union                                                             \
            {                                                                 \
              const void *const_val;                                          \
              void *non_const_val;                                            \
            } _v;                                                             \
            hsize_t _arrayDim = dim;                                          \
                                                                              \
                                                                              \
            _v.const_val = value;                                             \
            if (H5Tget_class (datatype) == H5T_STRING)                        \
            {                                                                 \
              _len = strlen ((const char *) _v.const_val);                    \
              if (_len == 0)      /* HDF5 doesn't like zero-len strings */    \
              {                                                               \
                _len++;                                                       \
              }                                                               \
              IOHDF5_ERROR (H5Tset_size (datatype, _len));                    \
            }                                                                 \
            if (_arrayDim > 0)                                                \
            {                                                                 \
              IOHDF5_ERROR (H5Sset_extent_simple (dataspace, 1,               \
                                                  &_arrayDim, NULL));         \
            }                                                                 \
            IOHDF5_ERROR (_attr = H5Acreate (dataset, name, datatype,         \
                                             dataspace, H5P_DEFAULT));        \
            IOHDF5_ERROR (H5Awrite (_attr, datatype, _v.non_const_val));      \
            IOHDF5_ERROR (H5Aclose (_attr));                                  \
          } while (0);

/* macro for reading an attribute */
#define READ_ATTRIBUTE(dataset, attrname, requested_type, buffer)             \
          do                                                                  \
          {                                                                   \
            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)                                   \
            {                                                                 \
              IOHDF5_ERROR (_attrtype = H5Aget_type (_attr));                 \
              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;                                  \
              IOHDF5_ERROR (H5Tclose (_attrtype));                            \
            }                                                                 \
            IOHDF5_ERROR (H5Aclose (_attr));                                  \
          } while (0)


/***
 Define the different datatypes used for HDF5 I/O
 NOTE: the complex datatype is defined dynamically at runtime in Startup.c
 ***/
/* char type is easy */
#define IOHDF5_CHAR    H5T_NATIVE_CHAR

/* floating point types are architecture-independent,
   ie. a float has always 4 bytes, and a double has 8 bytes

   IOHDF5_REAL  is used for storing reals of the generic CCTK_REAL type
   IOHDF5_REALn is used to explicitely store n-byte reals */
#ifdef  CCTK_REAL4
#define IOHDF5_REAL4  H5T_NATIVE_FLOAT
#endif
#ifdef  CCTK_REAL8
#define IOHDF5_REAL8  H5T_NATIVE_DOUBLE
#endif
#ifdef  CCTK_REAL16
#define IOHDF5_REAL16 (sizeof (CCTK_REAL16) == sizeof (long double) ?         \
                       H5T_NATIVE_LDOUBLE : -1)
#endif


#ifdef  CCTK_REAL_PRECISION_16
#define IOHDF5_REAL   IOHDF5_REAL16
#elif   CCTK_REAL_PRECISION_8
#define IOHDF5_REAL   IOHDF5_REAL8
#elif   CCTK_REAL_PRECISION_4
#define IOHDF5_REAL   IOHDF5_REAL4
#endif


/* integer types are architecture-dependent:
   IOHDF5_INT  is used for communicating integers of the generic CCTK_INT type
   IOHDF5_INTn is used to explicitely communicate n-byte integers */
#ifdef  CCTK_INT8
#define IOHDF5_INT8   (sizeof (CCTK_INT8) == sizeof (int) ? H5T_NATIVE_INT :  \
                       sizeof (CCTK_INT8) == sizeof (long) ? H5T_NATIVE_LONG :\
                       sizeof (CCTK_INT8) == sizeof (long long) ?             \
                       H5T_NATIVE_LLONG : -1)
#endif

#ifdef  CCTK_INT4
#define IOHDF5_INT4   (sizeof (CCTK_INT4) == sizeof (int) ? H5T_NATIVE_INT :  \
                       sizeof (CCTK_INT4) == sizeof (short) ?                 \
                       H5T_NATIVE_SHORT : -1)
#endif

#ifdef  CCTK_INT2
#define IOHDF5_INT2   (sizeof (CCTK_INT2) == sizeof (short) ?                 \
                       H5T_NATIVE_SHORT : -1)
#endif

#ifdef  CCTK_INTEGER_PRECISION_8
#define IOHDF5_INT    IOHDF5_INT8
#elif   CCTK_INTEGER_PRECISION_4
#define IOHDF5_INT    IOHDF5_INT4
#elif   CCTK_INTEGER_PRECISION_2
#define IOHDF5_INT    IOHDF5_INT2
#endif


/* Constants to index timers within the timers array */
#define CP_PARAMETERS_TIMER   0
#define CP_VARIABLES_TIMER    1
#define CP_TOTAL_TIMER        2
#define RECOVERY_TIMER        3
#define IOHDF5_NUM_TIMERS     4

/* names of the groups that hold global attributes and parameters */
#define GLOBAL_ATTRIBUTES_GROUP "Global Attributes"
#define CACTUS_PARAMETERS_GROUP "Cactus Parameters"
#define ALL_PARAMETERS          "All Parameters"


#ifdef __cplusplus
extern "C"
{
#endif

/* Geometry information structure for output variable */
typedef struct
{
  int vdim;
  int sdim;
  int *direction;
  int *origin;
  int *length;
  int *downsample;
  int *actlen;       /* actual index slab length (by PUGHSlab)*/
} ioHDF5Geo_t;


/* Structure describing a given recovery file */
typedef struct
{
  int is_HDF5_file;         /* flag indicating valid file info */
  hid_t file;               /* HDF5 file handle */
  char *filename;           /* complete file name for recovery */
  int nprocs;               /* number of total processors */
  int ioproc;               /* the associated IO processor */
  int ioproc_every;         /* how many IO processors there are */
  int unchunked;            /* whether data was written chunked or unchunked */
  int has_version;          /* whether file contains a Cactus version ID
                               (this is used to distinguish checkpoint files
                                with old/new timelevel naming scheme) */
} fileinfo_t;


/* IOHDF5Util GH extension structure */
typedef struct
{
  /* while HDF5 error output is disabled
     we keep the original error printing routine and its argument in here */
  H5E_auto_t print_error_fn;
  void      *print_error_fn_arg;

  /* predefined dataspaces for writing scalar and array attributes */
  hid_t scalar_dataspace, array_dataspace;

  /* predefined datatype for writing CCTK_COMPLEX types */
  hid_t IOHDF5_COMPLEX,
        IOHDF5_COMPLEX8,
        IOHDF5_COMPLEX16,
        IOHDF5_COMPLEX32;

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

} ioHDF5UtilGH;


/* exported functions */
hid_t IOHDF5Util_DataType (const ioHDF5UtilGH *myGH, int cctk_type);
void IOHDF5Util_ParseVarsForOutput (const char *output_varstring,
                                    ioHDF5Geo_t *output_request_list[]);
void IOHDF5Util_DumpParameters (const cGH *GH, int all, hid_t group);
void IOHDF5Util_DumpGHExtensions (const cGH *GH, hid_t group);
int  IOHDF5Util_DumpGH (const cGH *GH, const int *timers, hid_t file);
void IOHDF5Util_DumpCommonAttributes (const cGH *GH,
                                      int vindex,
                                      int timelevel,
                                      const CCTK_INT *global_shape,
                                      const ioHDF5Geo_t *slab,
                                      hid_t dataset);
int IOHDF5Util_DumpVar (const cGH *GH,
                        int vindex,
                        int timelevel,
                        const ioHDF5Geo_t *slab,
                        hid_t file,
                        int check_exisiting_objects);
int IOHDF5Util_RecoverParameters (const fileinfo_t *filenfo);
int IOHDF5Util_RecoverGHextensions (cGH *GH,
                                    const fileinfo_t *filenfo);
int IOHDF5Util_RecoverVariables (cGH *GH,
                                 const fileinfo_t *filenfo);

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

#endif  /* _IOUTILHDF5_IOUTILHDF5GH_H_ */