1 /*
2 * Distributed under the OSI-approved Apache License, Version 2.0. See
3 * accompanying file Copyright.txt for details.
4 *
5 * helloDataSpacesWriter.cpp
6 *
7 * Created on: Feb 06, 2019
8 * Author: Pradeep Subedi
9 * pradeep.subedi@rutgers.edu
10 */
11
12 #include <iostream>
13 #include <vector>
14
15 #include <adios2.h>
16
17 #if ADIOS2_USE_MPI
18 #include <mpi.h>
19 #endif
20
main(int argc,char * argv[])21 int main(int argc, char *argv[])
22 {
23
24 int rank;
25 int size;
26
27 #if ADIOS2_USE_MPI
28 MPI_Init(&argc, &argv);
29 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
30 MPI_Comm_size(MPI_COMM_WORLD, &size);
31 #else
32 rank = 0;
33 size = 1;
34 #endif
35
36 std::vector<float> myFloats = {
37 (float)10.0 * rank + 0, (float)10.0 * rank + 1, (float)10.0 * rank + 2,
38 (float)10.0 * rank + 3, (float)10.0 * rank + 4, (float)10.0 * rank + 5,
39 (float)10.0 * rank + 6, (float)10.0 * rank + 7, (float)10.0 * rank + 8,
40 (float)10.0 * rank + 9};
41 const std::size_t Nx = myFloats.size();
42
43 try
44 {
45 #if ADIOS2_USE_MPI
46 adios2::ADIOS adios(MPI_COMM_WORLD);
47 #else
48 adios2::ADIOS adios;
49 #endif
50 adios2::IO dataSpacesIO = adios.DeclareIO("myIO");
51 dataSpacesIO.SetEngine("DATASPACES");
52
53 // Define variable and local size
54 auto bpFloats = dataSpacesIO.DefineVariable<float>(
55 "bpFloats", {size * Nx}, {rank * Nx}, {Nx});
56
57 // Create engine smart pointer to Sst Engine due to polymorphism,
58 // Open returns a smart pointer to Engine containing the Derived class
59 adios2::Engine dataSpacesWriter =
60 dataSpacesIO.Open("helloDataSpaces", adios2::Mode::Write);
61
62 dataSpacesWriter.BeginStep();
63 dataSpacesWriter.Put<float>(bpFloats, myFloats.data());
64 dataSpacesWriter.EndStep();
65 dataSpacesWriter.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 #if ADIOS2_USE_MPI
87 MPI_Finalize();
88 #endif
89
90 return 0;
91 }
92