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 <vector>
17 
18 #include <adios2.h>
19 
20 #define str_helper(X) #X
21 #define str(X) str_helper(X)
22 
23 #ifndef DEFAULT_CONFIG
24 #define DEFAULT_CONFIG helloBPWriter.xml
25 #endif
26 #define DEFAULT_CONFIG_STR str(DEFAULT_CONFIG)
27 
main(int argc,char * argv[])28 int main(int argc, char *argv[])
29 {
30     MPI_Init(&argc, &argv);
31     int rank, size;
32     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
33     MPI_Comm_size(MPI_COMM_WORLD, &size);
34 
35     std::string configFile;
36     if (argc == 1)
37     {
38         configFile = DEFAULT_CONFIG_STR;
39     }
40     else if (argc == 2)
41     {
42         configFile = argv[1];
43     }
44     else
45     {
46         if (rank == 0)
47         {
48             std::cerr << "Usage: " << argv[0] << " [/path/to/config.xml]"
49                       << std::endl;
50         }
51         return 1;
52     }
53     if (rank == 0)
54     {
55         std::cout << "Using config file: " << configFile << std::endl;
56     }
57 
58     /** Application variable */
59     std::vector<float> myFloats = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
60     const std::size_t Nx = myFloats.size();
61 
62     try
63     {
64         /** ADIOS class factory of IO class objects */
65         adios2::ADIOS adios(configFile, MPI_COMM_WORLD);
66 
67         /*** IO class object: settings and factory of Settings: Variables,
68          * Parameters, Transports, and Execution: Engines */
69         adios2::IO bpIO = adios.DeclareIO("BPFile_N2N");
70 
71         /** global array : name, { shape (total) }, { start (local) }, { count
72          * (local) }, all are constant dimensions */
73         adios2::Variable<float> &bpFloats = bpIO.DefineVariable<float>(
74             "bpFloats", {size * Nx}, {rank * Nx}, {Nx}, adios2::ConstantDims);
75 
76         /** Engine derived class, spawned to start IO operations */
77         auto bpWriter = bpIO.Open("myVector.bp", adios2::Mode::Write);
78 
79         if (!bpWriter)
80         {
81             throw std::ios_base::failure(
82                 "ERROR: bpWriter not created at Open\n");
83         }
84 
85         /** Write variable for buffering */
86         bpWriter->Write<float>(bpFloats, myFloats.data());
87 
88         /** Create bp file, engine becomes unreachable after this*/
89         bpWriter->Close();
90     }
91     catch (std::invalid_argument &e)
92     {
93         std::cout << "Invalid argument exception, STOPPING PROGRAM from rank "
94                   << rank << "\n";
95         std::cout << e.what() << "\n";
96     }
97     catch (std::ios_base::failure &e)
98     {
99         std::cout
100             << "IO System base failure exception, STOPPING PROGRAM from rank "
101             << rank << "\n";
102         std::cout << e.what() << "\n";
103     }
104     catch (std::exception &e)
105     {
106         std::cout << "Exception, STOPPING PROGRAM from rank " << rank << "\n";
107         std::cout << e.what() << "\n";
108     }
109 
110     MPI_Finalize();
111 
112     return 0;
113 }
114