hdf5_tools.cpp 9.86 KB
Newer Older
1
#include "hdf5_tools.hpp"
2
#include "scope_timer.hpp"
Cristian Lalescu's avatar
Cristian Lalescu committed
3
4
#include <cfloat>
#include <climits>
5

6
7
int hdf5_tools::require_size_single_dataset(hid_t dset, int tsize)
{
8
    TIMEZONE("hdf5_tools::require_size_single_dataset");
9
10
11
12
13
    int ndims;
    hsize_t space;
    space = H5Dget_space(dset);
    ndims = H5Sget_simple_extent_ndims(space);
    hsize_t *dims = new hsize_t[ndims];
14
15
16
    hsize_t *maxdims = new hsize_t[ndims];
    H5Sget_simple_extent_dims(space, dims, maxdims);
    if (dims[0] < hsize_t(tsize) && maxdims[0] == H5S_UNLIMITED)
17
18
19
20
21
    {
        dims[0] = tsize;
        H5Dset_extent(dset, dims);
    }
    H5Sclose(space);
22
    delete[] maxdims;
23
24
25
26
    delete[] dims;
    return EXIT_SUCCESS;
}

27
28
int hdf5_tools::grow_single_dataset(hid_t dset, int tincrement)
{
29
    TIMEZONE("hdf5_tools::grow_single_dataset");
30
31
32
33
34
    int ndims;
    hsize_t space;
    space = H5Dget_space(dset);
    ndims = H5Sget_simple_extent_ndims(space);
    hsize_t *dims = new hsize_t[ndims];
35
36
37
38
39
40
41
    hsize_t *maxdims = new hsize_t[ndims];
    H5Sget_simple_extent_dims(space, dims, maxdims);
    if (maxdims[0] == H5S_UNLIMITED)
    {
        dims[0] += tincrement;
        H5Dset_extent(dset, dims);
    }
42
    H5Sclose(space);
43
    delete[] maxdims;
44
45
46
47
    delete[] dims;
    return EXIT_SUCCESS;
}

48
49
50
51
52
53
herr_t hdf5_tools::require_size_dataset_visitor(
    hid_t o_id,
    const char *name,
    const H5O_info_t *info,
    void *op_data)
{
54
    TIMEZONE("hdf5_tools::require_size_dataset_visitor");
55
56
57
58
59
60
61
62
63
    if (info->type == H5O_TYPE_DATASET)
    {
        hsize_t dset = H5Dopen(o_id, name, H5P_DEFAULT);
        require_size_single_dataset(dset, *((int*)(op_data)));
        H5Dclose(dset);
    }
    return EXIT_SUCCESS;
}

64
65
66
67
68
69
herr_t hdf5_tools::grow_dataset_visitor(
    hid_t o_id,
    const char *name,
    const H5O_info_t *info,
    void *op_data)
{
70
    TIMEZONE("hdf5_tools::grow_dataset_visitor");
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    if (info->type == H5O_TYPE_DATASET)
    {
        hsize_t dset = H5Dopen(o_id, name, H5P_DEFAULT);
        grow_single_dataset(dset, *((int*)(op_data)));
        H5Dclose(dset);
    }
    return EXIT_SUCCESS;
}


int hdf5_tools::grow_file_datasets(
        const hid_t stat_file,
        const std::string group_name,
        int tincrement)
{
86
    TIMEZONE("hdf5_tools::grow_file_datasets");
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    int file_problems = 0;

    hid_t group;
    group = H5Gopen(stat_file, group_name.c_str(), H5P_DEFAULT);
    H5Ovisit(
            group,
            H5_INDEX_NAME,
            H5_ITER_NATIVE,
            grow_dataset_visitor,
            &tincrement);
    H5Gclose(group);
    return file_problems;
}

101
102
103
104
105
106

int hdf5_tools::require_size_file_datasets(
        const hid_t stat_file,
        const std::string group_name,
        int tsize)
{
107
    TIMEZONE("hdf5_tools::require_size_file_datasets");
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    int file_problems = 0;

    hid_t group;
    group = H5Gopen(stat_file, group_name.c_str(), H5P_DEFAULT);
    H5Ovisit(
            group,
            H5_INDEX_NAME,
            H5_ITER_NATIVE,
            require_size_dataset_visitor,
            &tsize);
    H5Gclose(group);
    return file_problems;
}

122
123
124
125
126
template <typename number>
std::vector<number> hdf5_tools::read_vector(
        const hid_t group,
        const std::string dset_name)
{
127
    TIMEZONE("hdf5_tools::read_vector");
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    std::vector<number> result;
    hsize_t vector_length;
    // first, read size of array
    hid_t dset, dspace;
    hid_t mem_dtype;
    if (typeid(number) == typeid(int))
        mem_dtype = H5Tcopy(H5T_NATIVE_INT);
    else if (typeid(number) == typeid(double))
        mem_dtype = H5Tcopy(H5T_NATIVE_DOUBLE);
    dset = H5Dopen(group, dset_name.c_str(), H5P_DEFAULT);
    dspace = H5Dget_space(dset);
    assert(H5Sget_simple_extent_ndims(dspace) == 1);
    H5Sget_simple_extent_dims(dspace, &vector_length, NULL);
    result.resize(vector_length);
    H5Dread(dset, mem_dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, &result.front());
    H5Sclose(dspace);
    H5Dclose(dset);
    H5Tclose(mem_dtype);
    return result;
}

Cristian Lalescu's avatar
Cristian Lalescu committed
149
template <typename number>
150
number hdf5_tools::read_value(
Cristian Lalescu's avatar
Cristian Lalescu committed
151
152
153
        const hid_t group,
        const std::string dset_name)
{
154
    TIMEZONE("hdf5_tools::read_value");
Cristian Lalescu's avatar
Cristian Lalescu committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    number result;
    hid_t dset;
    hid_t mem_dtype;
    if (typeid(number) == typeid(int))
        mem_dtype = H5Tcopy(H5T_NATIVE_INT);
    else if (typeid(number) == typeid(double))
        mem_dtype = H5Tcopy(H5T_NATIVE_DOUBLE);
    if (H5Lexists(group, dset_name.c_str(), H5P_DEFAULT))
    {
        dset = H5Dopen(group, dset_name.c_str(), H5P_DEFAULT);
        H5Dread(dset, mem_dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, &result);
        H5Dclose(dset);
    }
    else
    {
        DEBUG_MSG("attempted to read dataset %s which does not exist.\n",
                dset_name.c_str());
        if (typeid(number) == typeid(int))
            result = INT_MAX;
        else if (typeid(number) == typeid(double))
175
            result = number(DBL_MAX);
Cristian Lalescu's avatar
Cristian Lalescu committed
176
177
178
179
180
    }
    H5Tclose(mem_dtype);
    return result;
}

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
template <typename number>
int hdf5_tools::write_value_with_single_rank(
        const hid_t group,
        const std::string dset_name,
        const number value)
{
    hid_t dset, mem_dtype;
    if (typeid(number) == typeid(int))
        mem_dtype = H5Tcopy(H5T_NATIVE_INT);
    else if (typeid(number) == typeid(double))
        mem_dtype = H5Tcopy(H5T_NATIVE_DOUBLE);
    if (H5Lexists(group, dset_name.c_str(), H5P_DEFAULT))
    {
        dset = H5Dopen(group, dset_name.c_str(), H5P_DEFAULT);
    }
    else
    {
        hid_t fspace;
        hsize_t count[1];
        count[0] = 1;
        fspace = H5Screate_simple(1, count, NULL);
        dset = H5Dcreate(group, dset_name.c_str(), mem_dtype, fspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
        H5Sclose(fspace);
    }
    H5Dwrite(dset, mem_dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, &value);
    H5Dclose(dset);
    H5Tclose(mem_dtype);
    return EXIT_SUCCESS;
}

211
212
213
214
215
216
217
218
template <typename dtype>
std::vector<dtype> hdf5_tools::read_vector_with_single_rank(
        const int myrank,
        const int rank_to_use,
        const MPI_Comm COMM,
        const hid_t file_id,
        const std::string dset_name)
{
219
    TIMEZONE("hdf5_tools::read_vector_with_single_rank");
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
    std::vector<dtype> data;
    int vector_size;
    if (myrank == rank_to_use)
    {
        data = hdf5_tools::read_vector<dtype>(
                file_id,
                dset_name);
        vector_size = data.size();
    }
    MPI_Bcast(
            &vector_size,
            1,
            MPI_INT,
            rank_to_use,
            COMM);

    if (myrank != rank_to_use)
        data.resize(vector_size);
    MPI_Bcast(
            &data.front(),
            vector_size,
            (typeid(dtype) == typeid(int)) ? MPI_INT : MPI_DOUBLE,
            rank_to_use,
            COMM);
    return data;
}

Cristian Lalescu's avatar
Cristian Lalescu committed
247
248
249
250
std::string hdf5_tools::read_string(
        const hid_t group,
        const std::string dset_name)
{
251
    TIMEZONE("hdf5_tools::read_string");
252
253
254
255
256
    if (H5Lexists(group, dset_name.c_str(), H5P_DEFAULT))
    {
        hid_t dset = H5Dopen(group, dset_name.c_str(), H5P_DEFAULT);
        hid_t space = H5Dget_space(dset);
        hid_t memtype = H5Dget_type(dset);
Cristian Lalescu's avatar
Cristian Lalescu committed
257
258
259
        // fsanitize complains unless I have a static array here
        // but that doesn't actually work (data is read incorrectly).
        // this is caught by bfps.test_NSVEparticles
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
        char *string_data = (char*)malloc(256);
        H5Dread(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, &string_data);
        std::string std_string_data = std::string(string_data);
        free(string_data);
        H5Sclose(space);
        H5Tclose(memtype);
        H5Dclose(dset);
        return std_string_data;
    }
    else
    {
        DEBUG_MSG("attempted to read dataset %s which does not exist.\n",
                dset_name.c_str());
        return std::string("parameter does not exist");
    }
Cristian Lalescu's avatar
Cristian Lalescu committed
275
276
}

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
template <class partsize_t>
int hdf5_tools::write_particle_ID_pairs_with_single_rank(
        const std::vector<partsize_t> v,
        const hid_t group,
        const std::string dset_name)
{
    TIMEZONE("hdf5_tools::write_particle_ID_pairs_with_single_rank");
    // the vector contains pair information, so its size must be a multiple of 2
    assert((v.size() % 2) == 0);
    // file space creation
    hid_t fspace;
    hsize_t dims[2];
    dims[0] = v.size()/2;
    dims[1] = 2;
    fspace = H5Screate_simple(2, dims, NULL);
    // create dataset
    hsize_t dset_id = H5Dcreate(
            group,
            dset_name.c_str(),
            hdf5_tools::hdf5_type_id<partsize_t>(),
            fspace,
            H5P_DEFAULT,
            H5P_DEFAULT,
            H5P_DEFAULT);
    // write data
    H5Dwrite(
            dset_id,
            hdf5_tools::hdf5_type_id<partsize_t>(),
            H5S_ALL,
            H5S_ALL,
            H5P_DEFAULT,
            &v.front());
    // clean up
    H5Dclose(dset_id);
    H5Sclose(fspace);
    return EXIT_SUCCESS;
}

315
316
template
std::vector<int> hdf5_tools::read_vector<int>(
317
318
319
        const hid_t,
        const std::string);

320
321
template
std::vector<double> hdf5_tools::read_vector<double>(
322
323
324
        const hid_t,
        const std::string);

325
326
template
std::vector<int> hdf5_tools::read_vector_with_single_rank<int>(
327
328
329
330
331
332
        const int myrank,
        const int rank_to_use,
        const MPI_Comm COMM,
        const hid_t file_id,
        const std::string dset_name);

333
334
template
std::vector<double> hdf5_tools::read_vector_with_single_rank<double>(
335
336
337
338
339
340
        const int myrank,
        const int rank_to_use,
        const MPI_Comm COMM,
        const hid_t file_id,
        const std::string dset_name);

341
342
343
344
345
346
347
348
349
350
template
int hdf5_tools::read_value<int>(
        const hid_t,
        const std::string);

template
double hdf5_tools::read_value<double>(
        const hid_t,
        const std::string);

351
352
353
354
355
356
357
358
359
360
361
362
template
int hdf5_tools::write_value_with_single_rank<int>(
        const hid_t group,
        const std::string dset_name,
        const int value);

template
int hdf5_tools::write_value_with_single_rank<double>(
        const hid_t group,
        const std::string dset_name,
        const double value);