1 /*
2  * Distributed under the OSI-approved Apache License, Version 2.0.  See
3  * accompanying file Copyright.txt for details.
4  *
5  * helloBPFlushWriter.cpp: Example that tests buffer overflow forcing a flush to
6  * transports when writing a large variable in independent N-to-N mode. This
7  * will have performance penalties, but it's safer.
8  *
9  *  Created on: Feb 16, 2017
10  *      Author: William F Godoy godoywf@ornl.gov
11  */
12 
13 #include <ios>      //std::ios_base::failure
14 #include <iostream> //std::cout
15 #include <mpi.h>
16 #include <stdexcept> //std::invalid_argument std::exception
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(1000000); //~ 4 MB
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_Flush");
40         bpIO.SetEngine("BPFile");
41 
42         //        bpIO.SetParameters({{"MaxBufferSize", "9Mb"},
43         //                            {"BufferGrowthFactor", "1.5"},
44         //                            {"Threads", "2"}});
45         //        bpIO.AddTransport("File", {{"ProfileUnits", "Microseconds"}});
46 
47         /** global array : name, { shape (total) }, { start (local) }, { count
48          * (local) }, all are constant dimensions */
49         adios2::Variable<float> bpFloats = bpIO.DefineVariable<float>(
50             "bpFloats", {size * Nx}, {rank * Nx}, {Nx}, adios2::ConstantDims);
51 
52         /** Engine derived class, spawned to start IO operations */
53         adios2::Engine bpWriter =
54             bpIO.Open("myVectorFlush.bp", adios2::Mode::Write);
55 
56         for (unsigned int t = 0; t < 100; ++t)
57         {
58             /** values to time step */
59             myFloats.assign(myFloats.size(), static_cast<float>(t));
60             /** Write variable for buffering */
61             bpWriter.Put<float>(bpFloats, myFloats.data());
62         }
63 
64         /** Create bp file, engine becomes unreachable after this*/
65         bpWriter.Close();
66     }
67     catch (std::invalid_argument &e)
68     {
69         std::cout << "Invalid argument exception, STOPPING PROGRAM from rank "
70                   << rank << "\n";
71         std::cout << e.what() << "\n";
72     }
73     catch (std::ios_base::failure &e)
74     {
75         std::cout
76             << "IO System base failure exception, STOPPING PROGRAM from rank "
77             << rank << "\n";
78         std::cout << e.what() << "\n";
79     }
80     catch (std::exception &e)
81     {
82         std::cout << "Exception, STOPPING PROGRAM from rank " << rank << "\n";
83         std::cout << e.what() << "\n";
84     }
85 
86     MPI_Finalize();
87 
88     return 0;
89 }
90