1 /*
2 * Distributed under the OSI-approved Apache License, Version 2.0. See
3 * accompanying file Copyright.txt for details.
4 *
5 * helloBPWriter.cpp: Simple self-descriptive example of how to write a variable
6 * to a BP File that lives in several MPI processes.
7 *
8 * Created on: Feb 16, 2017
9 * Author: William F Godoy godoywf@ornl.gov
10 */
11
12 #include <ios> //std::ios_base::failure
13 #include <iostream> //std::cout
14 #include <mpi.h>
15 #include <stdexcept> //std::invalid_argument std::exception
16 #include <string>
17 #include <vector>
18
19 #include <adios2.h>
20
main(int argc,char * argv[])21 int main(int argc, char *argv[])
22 {
23 MPI_Init(&argc, &argv);
24 int rank, size;
25 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
26 MPI_Comm_size(MPI_COMM_WORLD, &size);
27
28 /** Application variable */
29 std::vector<float> myFloats = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
30 const std::size_t Nx = myFloats.size();
31
32 try
33 {
34 /** ADIOS class factory of IO class objects */
35 adios2::ADIOS adios(MPI_COMM_WORLD);
36
37 /*** IO class object: settings and factory of Settings: Variables,
38 * Parameters, Transports, and Execution: Engines */
39 adios2::IO bpIO = adios.DeclareIO("BPFile_N2N");
40
41 /** global array : name, { shape (total) }, { start (local) }, { count
42 * (local) }, all are constant dimensions */
43 adios2::Variable<float> bpFloats = bpIO.DefineVariable<float>(
44 "bpFloats", {size * Nx}, {rank * Nx}, {Nx}, adios2::ConstantDims);
45
46 bpIO.DefineAttribute<std::string>("Single_String",
47 "File generated with ADIOS2");
48
49 std::vector<std::string> myStrings = {"one", "two", "three"};
50 bpIO.DefineAttribute<std::string>("Array_of_Strings", myStrings.data(),
51 myStrings.size());
52
53 bpIO.DefineAttribute<double>("Attr_Double", 0.f);
54 std::vector<double> myDoubles = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
55 bpIO.DefineAttribute<double>("Array_of_Doubles", myDoubles.data(),
56 myDoubles.size());
57
58 /** Engine derived class, spawned to start IO operations */
59 adios2::Engine bpWriter =
60 bpIO.Open("fileAttributes.bp", adios2::Mode::Write);
61
62 /** Write variable for buffering */
63 bpWriter.Put<float>(bpFloats, myFloats.data());
64
65 /** Create bp file, engine becomes unreachable after this*/
66 bpWriter.Close();
67
68 adios2::IO bpReader = adios.DeclareIO("BPReader");
69
70 adios2::Engine bpReaderEngine =
71 bpReader.Open("fileAttributes.bp", adios2::Mode::Read);
72
73 const auto attributesInfo = bpReader.AvailableAttributes();
74
75 for (const auto &attributeInfoPair : attributesInfo)
76 {
77 std::cout << "Attribute: " << attributeInfoPair.first;
78 for (const auto &attributePair : attributeInfoPair.second)
79 {
80 std::cout << "\tKey: " << attributePair.first
81 << "\tValue: " << attributePair.second << "\n";
82 }
83 std::cout << "\n";
84 }
85
86 bpReaderEngine.Close();
87 }
88 catch (std::invalid_argument &e)
89 {
90 std::cout << "Invalid argument exception, STOPPING PROGRAM from rank "
91 << rank << "\n";
92 std::cout << e.what() << "\n";
93 }
94 catch (std::ios_base::failure &e)
95 {
96 std::cout
97 << "IO System base failure exception, STOPPING PROGRAM from rank "
98 << rank << "\n";
99 std::cout << e.what() << "\n";
100 }
101 catch (std::exception &e)
102 {
103 std::cout << "Exception, STOPPING PROGRAM from rank " << rank << "\n";
104 std::cout << e.what() << "\n";
105 }
106
107 MPI_Finalize();
108
109 return 0;
110 }
111