File: bpReaderHeatMap2D.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 (143 lines) | stat: -rw-r--r-- 4,716 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
/*
 * Distributed under the OSI-approved Apache License, Version 2.0.  See
 * accompanying file Copyright.txt for details.
 *
 * bpReaderHeatMap2D.cpp : Writes a heat map in a regular 2D mesh,
 * values grow from 0 in increments of 1
 *
 * temperature[gNx, Ny]
 * where: gNx = MPI_size_x * Nx
 *
 * 0                1       2  ...     Ny-1
 * Ny            Ny+1    Ny+2  ...   2*Ny-1
 * ...
 * ...
 * (gNx-1)*Ny   ...                  gNx*Ny-1
 *
 *
 *  Created on: Nov 1, 2017
 *      Author: William F Godoy godoywf@ornl.gov
 */

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

#include <adios2.h>

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);
    int rank, size;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    /** Application variable dimensions */
    constexpr std::size_t Nx = 10;
    constexpr std::size_t Ny = 10;

    const adios2::Dims count{Nx, Ny};
    const adios2::Dims start{rank * Nx, 0};
    const adios2::Dims shape{size * Nx, Ny};

    // populate local temperature values
    std::vector<unsigned int> temperatures(Nx * Ny);
    for (unsigned int i = 0; i < Nx; ++i)
    {
        const unsigned int iGlobal = static_cast<unsigned int>(start[0] + i);

        for (unsigned int j = 0; j < Ny; ++j)
        {
            const unsigned int value = static_cast<unsigned int>(iGlobal * shape[1] + j);
            temperatures[i * Ny + j] = value;
        }
    }

    try
    {
        /** ADIOS class factory of IO class objects, Debug is ON by default */
        adios2::ADIOS adios(MPI_COMM_WORLD);

        // ************************** WRITE
        /*** IO class object: settings and factory of Settings: Variables,
         * Parameters, Transports, and Execution: Engines */
        adios2::IO putHeatMap = adios.DeclareIO("HeatMapWriter");

        adios2::Variable<unsigned int> outTemperature = putHeatMap.DefineVariable<unsigned int>(
            "temperature", shape, start, count, adios2::ConstantDims);

        /** Will create HeatMap.bp */
        adios2::Engine bpWriter = putHeatMap.Open("HeatMap2D.bp", adios2::Mode::Write);

        bpWriter.BeginStep();
        bpWriter.Put(outTemperature, temperatures.data());
        bpWriter.EndStep();
        bpWriter.Close();

        // ************************** READ
        if (rank == 0)
        {
            adios2::IO getHeatMap = adios.DeclareIO("HeatMapReader");
            adios2::Engine bpReader =
                getHeatMap.Open("HeatMap2D.bp", adios2::Mode::Read, MPI_COMM_SELF);

            bpReader.BeginStep();
            // this just discovers in the metadata file that the variable exists
            adios2::Variable<unsigned int> inTemperature =
                getHeatMap.InquireVariable<unsigned int>("temperature");
            // now read the variable
            if (inTemperature)
            {
                inTemperature.SetSelection({{2, 2}, {6, 1}});
                size_t elementsSize = inTemperature.SelectionSize();
                std::vector<unsigned int> inTemperatures(elementsSize);
                std::cout << "Pre-allocated " << elementsSize << " elements, "
                          << elementsSize * sizeof(unsigned int) << " bytes\n";

                bpReader.Get(inTemperature, inTemperatures.data(), adios2::Mode::Sync);

                std::cout << "Incoming temperature map:\n";

                for (size_t i = 0; i < inTemperatures.size(); ++i)
                {
                    std::cout << inTemperatures[i] << " ";
                    if ((i + 1) % inTemperature.Count().back() == 0)
                    {
                        std::cout << "\n";
                    }
                }
                std::cout << "\n";
            }
            bpReader.EndStep();

            bpReader.Close();
        }
    }
    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;
}