aboutsummaryrefslogtreecommitdiff
path: root/src/ioHDF5UtilGH.h
blob: de2de80519949308b43b23e64e3ead6c34aeba17 (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
 /*@@
   @header    ioHDF5UtilGH.h
   @date      Fri Oct 6 2000
   @author    Thomas Radke
   @desc 
              The GH extensions structure for IOHDF5Util.
   @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;                                                       \
            void *val = value;                                                \
            hsize_t arrayDim = dim;                                           \
                                                                              \
                                                                              \
            if (H5Tget_class (datatype) == H5T_STRING)                        \
            {                                                                 \
              len = strlen ((char *) 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, 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)


/* Mapping off CCTK datatypes to HDF5 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


/* 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"


/* Geometry information structure for output variable */
/* FIXME: allocate arrays dynamically */
#define IOHDF5_MAXDIM 5
typedef struct
{
  int vdim;
  int sdim;
  int direction[IOHDF5_MAXDIM];
  int origin[IOHDF5_MAXDIM];
  int length[IOHDF5_MAXDIM];
  int downsample[IOHDF5_MAXDIM];
  int actlen[IOHDF5_MAXDIM];       /* 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 */
} 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;

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

} ioHDF5UtilGH;


#ifdef __cplusplus
extern "C"
{
#endif

/* exported functions */
int IOHDF5Util_ParseVarsForOutput (const char *var_list, 
                                   char do_output[], 
                                   ioHDF5Geo_t geo_output[]);
void IOHDF5Util_DefaultGeo (ioHDF5Geo_t *slab);
void IOHDF5Util_DumpParameters (cGH *GH, hid_t group);
void IOHDF5Util_DumpGHExtensions (cGH *GH, hid_t group);
int  IOHDF5Util_DumpGH (cGH *GH, int timers[], hid_t file);
void IOHDF5Util_DumpCommonAttributes (cGH *GH,
                                      int index,
                                      int timelevel,
                                      CCTK_INT global_shape[],
                                      ioHDF5Geo_t *slab,
                                      hid_t dataset);
int IOHDF5Util_DumpVar (cGH *GH,
                        int index,
                        int timelevel,
                        ioHDF5Geo_t *slab,
                        hid_t file,
                        int check_exisiting_objects);
int IOHDF5Util_RecoverParameters (fileinfo_t *filenfo);
int IOHDF5Util_RecoverGHextensions (cGH *GH,
                                    fileinfo_t *filenfo);
int IOHDF5Util_RecoverVariables (cGH *GH,
                                 fileinfo_t *filenfo);

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

#endif  /* _IOUTILHDF5_IOUTILHDF5GH_H_ */