File: hdf5Stream.cpp

package info (click to toggle)
adios2 2.10.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 33,764 kB
  • sloc: cpp: 175,964; ansic: 160,510; f90: 14,630; yacc: 12,668; python: 7,275; perl: 7,126; sh: 2,825; lisp: 1,106; xml: 1,049; makefile: 579; lex: 557
file content (385 lines) | stat: -rw-r--r-- 11,892 bytes parent folder | download | duplicates (2)
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
273
274
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * hdf5Stream.cpp
 *
 *  Created on: Nov 2018
 *      Author: Norbert Podhorszki
 */

#include "hdf5Stream.h"

#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <stdexcept>
#include <string>
#include <vector>

#include "adios2/helper/adiosLog.h"

hdf5Stream::hdf5Stream(const std::string &streamName, const adios2::Mode mode, MPI_Comm comm)
: Stream(streamName, mode), comm(comm)
{
    hid_t acc_tpl = H5Pcreate(H5P_FILE_ACCESS);
    MPI_Info info = MPI_INFO_NULL;
    herr_t ret = H5Pset_fapl_mpio(acc_tpl, comm, info);

    if (ret < 0)
    {
        adios2::helper::Throw<std::runtime_error>("Utils::adios_iotest", "hdf5Stream", "hdf5Stream",
                                                  "Unable to call set_fapl_mpio");
    }
    // int myRank;
    // MPI_Comm_rank(comm, &myRank);
    // double timeStart, timeEnd;
    // double openTime;
    // double maxOpenTime, minOpenTime;

    if (mode == adios2::Mode::Write)
    {
        // timeStart = MPI_Wtime();
        h5file = H5Fcreate(streamName.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, acc_tpl);
        // timeEnd = MPI_Wtime();
    }
    else
    {
        // timeStart = MPI_Wtime();
        h5file = H5Fopen(streamName.c_str(), H5F_ACC_RDONLY, acc_tpl);
        // timeEnd = MPI_Wtime();
    }
    // openTime = timeEnd - timeStart;
    // MPI_Allreduce(&openTime, &maxOpenTime, 1, MPI_DOUBLE, MPI_MAX, comm);
    // MPI_Allreduce(&openTime, &minOpenTime, 1, MPI_DOUBLE, MPI_MIN, comm);
    // if (myRank == 0)
    // {
    //     std::cout << "        Max open time = " << maxOpenTime << std::endl;
    //     std::cout << "        Min open time = " << minOpenTime << std::endl;
    //     std::ofstream open_perf_log;
    //     open_perf_log.open("open_perf.txt", std::ios::app);
    //     open_perf_log << std::to_string(maxOpenTime) + ", " +
    //                          std::to_string(minOpenTime) + "\n";
    //     open_perf_log.close();
    // }
    ret = H5Pclose(acc_tpl);
    if (ret < 0)
    {
        adios2::helper::Throw<std::runtime_error>("Utils::adios_iotest", "hdf5Stream", "hdf5Stream",
                                                  "Unable to call set_fapl_mpio");
    }
}

hdf5Stream::~hdf5Stream() {}

hid_t hdf5Stream::hdf5Type(std::string &type)
{
    hid_t htype = H5T_NATIVE_CHAR;
    if (type == "double")
    {
        htype = H5T_NATIVE_DOUBLE;
    }
    else if (type == "float")
    {
        htype = H5T_NATIVE_FLOAT;
    }
    else if (type == "int")
    {
        htype = H5T_NATIVE_INT32;
    }
    return htype;
}

void hdf5Stream::defineHDF5Array(const std::shared_ptr<VariableInfo> ov)
{
    const int ndim = ov->ndim + 1;
    std::vector<hsize_t> maxdims(ndim);
    std::vector<hsize_t> dims(ndim);
    std::vector<hsize_t> count(ndim);

    maxdims[0] = H5S_UNLIMITED;
    dims[0] = 1;
    count[0] = 1;
    for (int d = 1; d < ndim; ++d)
    {
        maxdims[d] = ov->shape[d - 1];
        dims[d] = ov->shape[d - 1];
        count[d] = ov->count[d - 1];
    }

    hid_t dataspace = H5Screate_simple(ndim, dims.data(), maxdims.data());

    hid_t cparms;
    /*
     * Set chunking, the only way to have extendible arrays
     */
    cparms = H5Pcreate(H5P_DATASET_CREATE);
    H5Pset_chunk(cparms, ndim, count.data());

    hid_t dataset;
    dataset = H5Dcreate2(h5file, ov->name.c_str(), hdf5Type(ov->type), dataspace, H5P_DEFAULT,
                         cparms, H5P_DEFAULT);
    varmap.emplace(std::make_pair(ov->name, hdf5VarInfo(dataset, dataspace)));
    H5Pclose(cparms);
}

void hdf5Stream::putHDF5Array(const std::shared_ptr<VariableInfo> ov, size_t step)
{
    /* note: step starts from 1 */
    const auto it = varmap.find(ov->name);
    hdf5VarInfo &vi = it->second;
    int ndim = ov->ndim + 1;
    std::vector<hsize_t> start(ndim);
    std::vector<hsize_t> count(ndim);
    std::vector<hsize_t> dims(ndim);
    start[0] = step - 1;
    count[0] = 1;
    dims[0] = step;
    for (int d = 1; d < ndim; ++d)
    {
        start[d] = ov->start[d - 1];
        count[d] = ov->count[d - 1];
        dims[d] = ov->shape[d - 1];
    }

    H5Dset_extent(vi.dataset, dims.data());
    hid_t filespace = H5Dget_space(vi.dataset);
    H5Sselect_hyperslab(filespace, H5S_SELECT_SET, start.data(), NULL, count.data(), NULL);
    hid_t dxpl_id = H5Pcreate(H5P_DATASET_XFER);
    H5Pset_dxpl_mpio(dxpl_id, H5FD_MPIO_COLLECTIVE);
    hid_t memspace = H5Screate_simple(ndim, count.data(), NULL);
    H5Dwrite(vi.dataset, hdf5Type(ov->type), memspace, filespace, dxpl_id, ov->data.data());
    H5Pclose(dxpl_id);
    H5Sclose(filespace);
    H5Sclose(memspace);
}
void hdf5Stream::Write(CommandWrite *cmdW, Config &cfg, const Settings &settings, size_t step)
{
    if (!settings.myRank && settings.verbose)
    {
        std::cout << "    Write to HDF5 output " << cmdW->streamName << " the group "
                  << cmdW->groupName;
        if (!cmdW->variables.empty())
        {
            std::cout << " with selected variables:  ";
            for (const auto &v : cmdW->variables)
            {
                std::cout << v->name << " ";
            }
        }
        std::cout << std::endl;
    }

    const double div = pow(10.0, static_cast<double>(settings.ndigits(cfg.nSteps - 1)));
    double myValue = static_cast<double>(settings.myRank) + static_cast<double>(step - 1) / div;

    for (auto ov : cmdW->variables)
    {
        // Allocate memory on first access
        if (!ov->data.size())
        {
            ov->data.resize(ov->datasize);
        }
        if (step == 1)
        {
            if (!settings.myRank && settings.verbose)
            {
                std::cout << "        Define array  " << ov->name << "  for output" << std::endl;
            }
            defineHDF5Array(ov);
        }

        // if we read the variable, use the read values otherwise generate data
        // now
        if (!ov->readFromInput)
        {
            if (!settings.myRank && settings.verbose)
            {
                std::cout << "        Fill array  " << ov->name << "  for output" << std::endl;
            }
            fillArray(ov, myValue);
        }
    }

    if (!settings.myRank && settings.verbose)
    {
        std::cout << "        Write data " << std::endl;
    }
    double timeStart, timeEnd;
    double writeTime;
    double maxWriteTime, minWriteTime;
    MPI_Barrier(comm);
    timeStart = MPI_Wtime();
    for (const auto &ov : cmdW->variables)
    {
        putHDF5Array(ov, step);
    }
    timeEnd = MPI_Wtime();
    if (settings.ioTimer)
    {
        writeTime = timeEnd - timeStart;
        MPI_Allreduce(&writeTime, &maxWriteTime, 1, MPI_DOUBLE, MPI_MAX, comm);
        MPI_Allreduce(&writeTime, &minWriteTime, 1, MPI_DOUBLE, MPI_MIN, comm);
        if (settings.myRank == 0)
        {
            std::cout << "        Max write time = " << maxWriteTime << std::endl;
            std::cout << "        Min write time = " << minWriteTime << std::endl;
            std::ofstream wr_perf_log;
            wr_perf_log.open("write_perf.txt", std::ios::app);
            wr_perf_log << std::to_string(maxWriteTime) + ", " + std::to_string(minWriteTime) +
                               "\n";
            wr_perf_log.close();
        }
    }
}

void hdf5Stream::getHDF5Array(std::shared_ptr<VariableInfo> ov, size_t step)
{
    hid_t dataset;
    hid_t filespace;
    if (step == 1)
    {
        dataset = H5Dopen2(h5file, ov->name.c_str(), H5P_DEFAULT);
        if (dataset == -1)
        {
            std::cout << "        Variable " << ov->name << " is not in the file: " << std::endl;
            ov->readFromInput = false;
            return;
        }
        filespace = H5Dget_space(dataset);
        varmap.emplace(std::make_pair(ov->name, hdf5VarInfo(dataset, filespace)));
    }
    else
    {
        const auto it = varmap.find(ov->name);
        hdf5VarInfo &vi = it->second;
        dataset = vi.dataset;
        filespace = vi.dataspace;
    }
    int ndim = ov->ndim + 1;

    std::vector<hsize_t> start(ndim);
    std::vector<hsize_t> count(ndim);
    start[0] = step - 1;
    count[0] = 1;
    for (int d = 1; d < ndim; ++d)
    {
        start[d] = ov->start[d - 1];
        count[d] = ov->count[d - 1];
    }

    // Allocate memory on first access
    if (!ov->data.size())
    {
        ov->data.resize(ov->datasize);
    }

    hid_t memspace = H5Screate_simple(ndim, count.data(), NULL);
    hid_t dxpl_id = H5Pcreate(H5P_DATASET_XFER);
    H5Pset_dxpl_mpio(dxpl_id, H5FD_MPIO_COLLECTIVE);
    H5Sselect_hyperslab(filespace, H5S_SELECT_SET, start.data(), NULL, count.data(), NULL);
    void *buf = reinterpret_cast<void *>(ov->data.data());
    H5Dread(dataset, hdf5Type(ov->type), memspace, filespace, dxpl_id, buf);

    H5Sclose(memspace);
    H5Pclose(dxpl_id);
    ov->readFromInput = true;
}

adios2::StepStatus hdf5Stream::Read(CommandRead *cmdR, Config &cfg, const Settings &settings,
                                    size_t step)
{
    if (!settings.myRank && settings.verbose)
    {
        std::cout << "    Read ";
        if (cmdR->stepMode == adios2::StepMode::Read)
        {
            std::cout << "next available step from ";
        }

        std::cout << cmdR->streamName << " using the group " << cmdR->groupName;
        if (!cmdR->variables.empty())
        {
            std::cout << " with selected variables:  ";
            for (const auto &v : cmdR->variables)
            {
                std::cout << v->name << " ";
            }
        }
        std::cout << std::endl;
    }
    double timeStart, timeEnd;
    double readTime;
    double maxReadTime, minReadTime;
    MPI_Barrier(comm);
    timeStart = MPI_Wtime();
    if (step == 1)
    {
        /* Get the number of available steps in the HDF5 file */
        std::shared_ptr<VariableInfo> var = cmdR->variables.front();
        hid_t dataset = H5Dopen2(h5file, var->name.c_str(), H5P_DEFAULT);
        hid_t filespace = H5Dget_space(dataset);
        std::vector<hsize_t> dims(var->ndim + 1);
        H5Sget_simple_extent_dims(filespace, dims.data(), NULL);
        nSteps = dims[0];
        if (!settings.myRank && settings.verbose)
        {
            std::cout << "        Number of steps in file: " << nSteps << std::endl;
        }
        H5Sclose(filespace);
        H5Dclose(dataset);
    }

    if (step > nSteps)
    {
        return adios2::StepStatus::EndOfStream;
    }

    /*
    if (!settings.myRank && settings.verbose && step == 1)
    {
        const auto varmap = PARSE HERE THE HDF5 FILE FOR VARIABLES
        std::cout << "    Variables in input for reading: " << std::endl;
        for (const auto &v : varmap)
        {
            std::cout << "        " << v.first << std::endl;
        }
    }
    */

    if (!settings.myRank && settings.verbose)
    {
        std::cout << "    Read data " << std::endl;
    }

    for (auto ov : cmdR->variables)
    {
        getHDF5Array(ov, step);
    }
    timeEnd = MPI_Wtime();
    if (settings.ioTimer)
    {
        readTime = timeEnd - timeStart;
        MPI_Allreduce(&readTime, &maxReadTime, 1, MPI_DOUBLE, MPI_MAX, comm);
        MPI_Allreduce(&readTime, &minReadTime, 1, MPI_DOUBLE, MPI_MIN, comm);
        if (settings.myRank == 0)
        {
            std::cout << "        Max read time = " << maxReadTime << std::endl;
            std::cout << "        Min read time = " << minReadTime << std::endl;
            std::ofstream rd_perf_log;
            rd_perf_log.open("read_perf.txt", std::ios::app);
            rd_perf_log << std::to_string(maxReadTime) + ", " + std::to_string(minReadTime) + "\n";
            rd_perf_log.close();
        }
    }

    return adios2::StepStatus::OK;
}
void hdf5Stream::Close()
{
    for (const auto &it : varmap)
    {
        auto &vi = it.second;
        H5Dclose(vi.dataset);
        H5Sclose(vi.dataspace);
    }
    H5Fclose(h5file);
}