File: hdf5SubFile.cpp

package info (click to toggle)
adios2 2.10.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, 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 (285 lines) | stat: -rw-r--r-- 9,080 bytes parent folder | download
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
/*
 * Distributed under the OSI-approved Apache License, Version 2.0.  See
 * accompanying file Copyright.txt for details.
 *
 * hdf5SubFile.cpp: Simple self-descriptive example of how to write a
 * variable to a parallel HDF5 File using MPI processes.
 *
 *  Created on: March 6, 2023
 *      Author: Junmin
 */

#include <adios2.h>
#include <ios>      //std::ios_base::failure
#include <iostream> //std::cout
#include <mpi.h>
#include <stdexcept> //std::invalid_argument std::exception
#include <vector>

void writeMe(adios2::IO &hdf5IO, int rank, int size, const char *testFileName)
{
    /** Application variable */
    int scale = 1;

    const char *temp = std::getenv("TEST_SCALE");
    if (NULL != temp)
    {
        int itemp = -1;
        sscanf(temp, "%d", &itemp);
        if (itemp > 1)
            scale = itemp;
    }

    const std::size_t Nx = 1024;
    const std::size_t Ny = 1024 * scale;

    std::vector<float> myFloats(Nx * Ny, 0.1f * rank);
    std::vector<int> myInts(Nx * Ny, (int)(1 + rank));

    hdf5IO.SetParameter("IdleH5Writer",
                        "true"); // set this if not all ranks are writting

    adios2::Variable<float> h5Floats = hdf5IO.DefineVariable<float>(
        "h5Floats", {size * Nx, Ny}, {rank * Nx, 0}, {Nx, Ny}, adios2::ConstantDims);

    adios2::Variable<int> h5Ints = hdf5IO.DefineVariable<int>(
        "h5Ints", {size * Nx, Ny}, {rank * Nx, 0}, {Nx, Ny}, adios2::ConstantDims);

    /** Engine derived class, spawned to start IO operations */
    adios2::Engine hdf5Writer = hdf5IO.Open(testFileName, adios2::Mode::Write);

    int nsteps = 5;

    if (size % 2 == 0)
    {
        // all Ranks must call Put
        /** Write variable for buffering */
        for (int i = 0; i < nsteps; i++)
        {
            hdf5Writer.BeginStep();
            hdf5Writer.Put<float>(h5Floats, myFloats.data());
            hdf5Writer.Put(h5Ints, myInts.data());
            hdf5Writer.EndStep();
        }
    }
    else
    {
        // using collective Begin/EndStep() to run the
        // collective HDF5 calls. Now Ranks can skip writting if no data
        // presented
        for (int i = 0; i < nsteps; i++)
        {
            hdf5Writer.BeginStep();
            if (rank == 0)
            {
                hdf5Writer.Put<float>(h5Floats, myFloats.data());
                hdf5Writer.Put(h5Ints, myInts.data());
            }
            hdf5Writer.EndStep();
        }
    }
    std::vector<int64_t> m_globalDims = {10, 20, 30, 40};
    hdf5IO.DefineAttribute<std::string>("adios2_schema/version_major",
                                        std::to_string(ADIOS2_VERSION_MAJOR));
    hdf5IO.DefineAttribute<std::string>("adios2_schema/version_minor",
                                        std::to_string(ADIOS2_VERSION_MINOR));
    hdf5IO.DefineAttribute<std::string>("/adios2_schema/mesh/type", "explicit");
    hdf5IO.DefineAttribute<std::int64_t>("adios2_schema/mesh/dimension0", m_globalDims[0]);
    hdf5IO.DefineAttribute<std::int64_t>("adios2_schema/mesh/dimension1", m_globalDims[1]);
    hdf5IO.DefineAttribute<std::int64_t>("adios2_schema/mesh/dimension2", m_globalDims[2]);
    hdf5IO.DefineAttribute<std::int64_t>("adios2_schema/mesh/dimension3", m_globalDims[3]);
    hdf5IO.DefineAttribute<std::int64_t>("adios2_schema/mesh/dimension-num", m_globalDims.size());

    hdf5Writer.Close();
}

template <class T>
void ReadVarData(adios2::IO h5IO, adios2::Engine &h5Reader, const std::string &name)
{
    adios2::Variable<T> var = h5IO.InquireVariable<T>(name);

    if (var)
    {
        int nDims = (int)var.Shape().size();
        size_t totalSize = 1;
        for (int i = 0; i < nDims; i++)
        {
            totalSize *= var.Shape()[i];
        }
        std::vector<T> myValues(totalSize);
        // myFloats.data is pre-allocated
        h5Reader.Get<T>(var, myValues.data(), adios2::Mode::Sync);

        // std::cout << "\tValues of "<<name<<": ";
        std::cout << "\tPeek Values: ";

        if (totalSize < 20)
        { // print all
            for (const auto number : myValues)
            {
                std::cout << number << " ";
            }
        }
        else
        {
            size_t counter = 0;
            for (const auto number : myValues)
            {
                if ((counter < 5) || (counter > totalSize - 5))
                {
                    std::cout << number << " ";
                }
                else if (counter == 5)
                {
                    std::cout << " ......  ";
                }
                counter++;
            }
        }
        std::cout << "\n";
    }
}

void readMe(adios2::IO &h5IO, int rank, int size, const char *fileName)
{
    /** Engine derived class, spawned to start IO operations */
    adios2::Engine h5Reader = h5IO.Open(fileName, adios2::Mode::Read);

    const std::map<std::string, adios2::Params> variables = h5IO.AvailableVariables();

    if (0 == rank)
        std::cout << " Num Vars: " << variables.size() << std::endl;

    for (const auto &variablePair : variables)
    {
        std::cout << "Name: " << variablePair.first;
        std::cout << std::endl;

        for (const auto &parameter : variablePair.second)
        {
            std::cout << "\t" << parameter.first << ": " << parameter.second << "\n";
            if (parameter.second == "double")
            {
                ReadVarData<double>(h5IO, h5Reader, variablePair.first);
            }
            else if (parameter.second == "float")
            {
                ReadVarData<float>(h5IO, h5Reader, variablePair.first);
            }
            else if (parameter.second == "unsigned int")
            {
                ReadVarData<unsigned int>(h5IO, h5Reader, variablePair.first);
            }
            else if (parameter.second == "int")
            {
                ReadVarData<int>(h5IO, h5Reader, variablePair.first);
            }
        }
    } // variables

    const std::map<std::string, adios2::Params> attributes = h5IO.AvailableAttributes();

    if (0 == rank)
        std::cout << "Num Attrs:" << attributes.size() << std::endl;

    for (const auto &attrPair : attributes)
    {
        std::cout << "AttrName: " << attrPair.first;
        std::cout << std::endl;

        for (const auto &parameter : attrPair.second)
        {
            std::cout << "\t" << parameter.first << ": " << parameter.second << "\n";

            if (parameter.second == "double")
            {
                // ReadVarData<double>(h5IO, h5Reader, variablePair.first);
            }
            else if (parameter.second == "float")
            {
                // ReadVarData<float>(h5IO, h5Reader, variablePair.first);
            }
            else if (parameter.second == "unsigned int")
            {
                // ReadVarData<unsigned int>(h5IO, h5Reader,
                // variablePair.first);
            }
            else if (parameter.second == "int")
            {
                // ReadVarData<int>(h5IO, h5Reader, variablePair.first);
            }
            //... add more types if needed
        }
    }

    h5Reader.Close();
}

int main(int argc, char *argv[])
{
    int provided;

    // MPI_THREAD_MULTIPLE is only required if you enable the SST MPI_DP
    MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);

    if (provided < MPI_THREAD_MULTIPLE)
    {
        std::cout << "MPI_THREAD_MULTIPLE is not supported, not able to use "
                     "the subfile feature in HDF5. Aborting. \n"
                  << std::endl;
        MPI_Abort(MPI_COMM_WORLD, -1);
    }

    int rank, size;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    try
    {
        /** ADIOS class factory of IO class objects */
        adios2::ADIOS adios(MPI_COMM_WORLD);
        adios2::IO writerIO = adios.DeclareIO("HDFFileIOWriter");

        std::string testName = "test.h5";
        if (argc > 1)
            testName = argv[1];

        if (1 == testName.size())
        {
            writerIO.SetEngine("NullCore");
            writeMe(writerIO, rank, size, "null.bp");
        }
        else
        {
            writeMe(writerIO, rank, size, testName.c_str());
        }

        // read back  if required
        if (argc > 2)
        {
            MPI_Barrier(MPI_COMM_WORLD);
            adios2::IO readerIO = adios.DeclareIO("HDFFileIOReader");
            readMe(readerIO, rank, size, testName.c_str());
        }
    }
    catch (std::invalid_argument &e)
    {
        std::cout << "Invalid argument exception, STOPPING PROGRAM from rank " << rank << "\n";
        std::cout << e.what() << "\n";
    }
    catch (std::ios_base::failure &e)
    {
        std::cout << "IO System base failure exception, STOPPING PROGRAM from rank " << rank
                  << "\n";
        std::cout << e.what() << "\n";
    }
    catch (std::exception &e)
    {
        std::cout << "Exception, STOPPING PROGRAM from rank " << rank << "\n";
        std::cout << e.what() << "\n";
    }

    MPI_Finalize();

    return 0;
}