1 /*
2  * Distributed under the OSI-approved Apache License, Version 2.0.  See
3  * accompanying file Copyright.txt for details.
4  *
5  * helloSstReader.cpp
6  *
7  *  Created on: Aug 17, 2017
8 v *      Author: Greg Eisenhauer
9  */
10 
11 #include <chrono>
12 #include <iostream>
13 #include <numeric>
14 #include <thread>
15 #include <vector>
16 
17 #include <adios2.h>
18 
19 #if ADIOS2_USE_MPI
20 #include <mpi.h>
21 #endif
22 
main(int argc,char * argv[])23 int main(int argc, char *argv[])
24 {
25 
26     int rank;
27     int size;
28 
29 #if ADIOS2_USE_MPI
30     MPI_Init(&argc, &argv);
31     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
32     MPI_Comm_size(MPI_COMM_WORLD, &size);
33 #else
34 
35     rank = 0;
36     size = 1;
37 #endif
38 
39     std::vector<float> myFloats(10);
40 
41     try
42     {
43 #if ADIOS2_USE_MPI
44         adios2::ADIOS adios(MPI_COMM_WORLD);
45 #else
46         adios2::ADIOS adios;
47 #endif
48 
49         adios2::IO sstIO = adios.DeclareIO("myIO");
50         sstIO.SetEngine("Sst");
51 
52         adios2::Engine sstReader = sstIO.Open("helloSst", adios2::Mode::Read);
53         sstReader.BeginStep();
54         adios2::Variable<float> bpFloats =
55             sstIO.InquireVariable<float>("bpFloats");
56         std::cout << "Incoming variable is of size " << bpFloats.Shape()[0]
57                   << "\n";
58         const std::size_t total_size = bpFloats.Shape()[0];
59         const std::size_t my_start = (total_size / size) * rank;
60         const std::size_t my_count = (total_size / size);
61         std::cout << "Reader rank " << rank << " reading " << my_count
62                   << " floats starting at element " << my_start << "\n";
63 
64         const adios2::Dims start{my_start};
65         const adios2::Dims count{my_count};
66 
67         const adios2::Box<adios2::Dims> sel(start, count);
68 
69         std::vector<float> myFloats;
70         myFloats.resize(my_count);
71 
72         bpFloats.SetSelection(sel);
73         sstReader.Get(bpFloats, myFloats.data());
74         sstReader.EndStep();
75 
76         sstReader.Close();
77     }
78     catch (std::invalid_argument &e)
79     {
80         std::cout << "Invalid argument exception, STOPPING PROGRAM from rank "
81                   << rank << "\n";
82         std::cout << e.what() << "\n";
83     }
84     catch (std::ios_base::failure &e)
85     {
86         std::cout << "IO System base failure exception, STOPPING PROGRAM "
87                      "from rank "
88                   << rank << "\n";
89         std::cout << e.what() << "\n";
90     }
91     catch (std::exception &e)
92     {
93         std::cout << "Exception, STOPPING PROGRAM from rank " << rank << "\n";
94         std::cout << e.what() << "\n";
95     }
96 
97 #if ADIOS2_USE_MPI
98     MPI_Finalize();
99 #endif
100 
101     return 0;
102 }
103