diff --git a/examples/hello/bpReader/helloBPReader.cpp b/examples/hello/bpReader/helloBPReader.cpp index a0f89b47d7..e27579494f 100644 --- a/examples/hello/bpReader/helloBPReader.cpp +++ b/examples/hello/bpReader/helloBPReader.cpp @@ -11,7 +11,6 @@ * Created on: Feb 16, 2017 * Author: William F Godoy godoywf@ornl.gov */ - #include //std::ios_base::failure #include //std::cout #include @@ -26,12 +25,7 @@ int main(int argc, char *argv[]) int rank, size; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); - - /** Application variable */ - const std::size_t Nx = 10; - std::vector myFloats(Nx); - std::vector myInts(Nx); - + std::string filename = "myVector_cpp.bp"; try { /** ADIOS class factory of IO class objects, DebugON is recommended */ @@ -42,8 +36,7 @@ int main(int argc, char *argv[]) adios2::IO bpIO = adios.DeclareIO("ReadBP"); /** Engine derived class, spawned to start IO operations */ - adios2::Engine bpReader = - bpIO.Open("myVector_cpp.bp", adios2::Mode::Read); + adios2::Engine bpReader = bpIO.Open(filename, adios2::Mode::Read); const std::map variables = bpIO.AvailableVariables(); @@ -64,34 +57,44 @@ int main(int argc, char *argv[]) bpIO.InquireVariable("bpFloats"); adios2::Variable bpInts = bpIO.InquireVariable("bpInts"); + const std::size_t Nx = 10; if (bpFloats) // means found { + std::vector myFloats; + // read only the chunk corresponding to our rank bpFloats.SetSelection({{Nx * rank}, {Nx}}); // myFloats.data is pre-allocated - bpReader.Get(bpFloats, myFloats.data(), adios2::Mode::Sync); + bpReader.Get(bpFloats, myFloats, adios2::Mode::Sync); - std::cout << "MyFloats: \n"; - for (const auto number : myFloats) + if (rank == 0) { - std::cout << number << " "; + std::cout << "MyFloats: \n"; + for (const auto number : myFloats) + { + std::cout << number << " "; + } + std::cout << "\n"; } - std::cout << "\n"; } if (bpInts) // means not found { + std::vector myInts; // read only the chunk corresponding to our rank bpInts.SetSelection({{Nx * rank}, {Nx}}); - // myInts.data is pre-allocated - bpReader.Get(bpInts, myInts.data(), adios2::Mode::Sync); - std::cout << "MyInts: \n"; - for (const auto number : myInts) + bpReader.Get(bpInts, myInts, adios2::Mode::Sync); + + if (rank == 0) { - std::cout << number << " "; + std::cout << "myInts: \n"; + for (const auto number : myInts) + { + std::cout << number << " "; + } + std::cout << "\n"; } - std::cout << "\n"; } /** Close bp file, engine becomes unreachable after this*/ @@ -99,21 +102,39 @@ int main(int argc, char *argv[]) } catch (std::invalid_argument &e) { - std::cout << "Invalid argument exception, STOPPING PROGRAM from rank " - << rank << "\n"; - std::cout << e.what() << "\n"; + if (rank == 0) + { + std::cerr + << "Invalid argument exception, STOPPING PROGRAM from rank " + << rank << "\n"; + std::cerr << e.what() << "\n"; + } + MPI_Abort(MPI_COMM_WORLD, 1); } catch (std::ios_base::failure &e) { - std::cout << "IO System base failure exception, STOPPING PROGRAM " - "from rank " - << rank << "\n"; - std::cout << e.what() << "\n"; + if (rank == 0) + { + std::cerr << "IO System base failure exception, STOPPING PROGRAM " + "from rank " + << rank << "\n"; + std::cerr << e.what() << "\n"; + std::cerr << "The file " << filename << " does not exist." + << " Presumably this is because hello_bpWriter has not " + "been run." + << " Run ./hello_bpWriter before running this program.\n"; + } + MPI_Abort(MPI_COMM_WORLD, 1); } catch (std::exception &e) { - std::cout << "Exception, STOPPING PROGRAM from rank " << rank << "\n"; - std::cout << e.what() << "\n"; + if (rank == 0) + { + std::cerr << "Exception, STOPPING PROGRAM from rank " << rank + << "\n"; + std::cerr << e.what() << "\n"; + } + MPI_Abort(MPI_COMM_WORLD, 1); } MPI_Finalize(); diff --git a/examples/hello/bpReader/helloBPReader_nompi.cpp b/examples/hello/bpReader/helloBPReader_nompi.cpp index 774a849a46..c2969d87cf 100644 --- a/examples/hello/bpReader/helloBPReader_nompi.cpp +++ b/examples/hello/bpReader/helloBPReader_nompi.cpp @@ -18,10 +18,7 @@ int main(int argc, char *argv[]) { - /** Application variable */ - const std::size_t Nx = 10; - std::vector myFloats(Nx); - std::vector myInts(Nx); + std::string filename = "myVector_cpp.bp"; try { @@ -33,8 +30,7 @@ int main(int argc, char *argv[]) adios2::IO bpIO = adios.DeclareIO("ReadBP"); /** Engine derived class, spawned to start IO operations */ - adios2::Engine bpReader = - bpIO.Open("myVector_cpp.bp", adios2::Mode::Read); + adios2::Engine bpReader = bpIO.Open(filename, adios2::Mode::Read); /** Write variable for buffering */ adios2::Variable bpFloats = @@ -44,12 +40,25 @@ int main(int argc, char *argv[]) if (bpFloats) { - bpReader.Get(bpFloats, myFloats.data(), adios2::Mode::Sync); + std::vector myFloats; + bpReader.Get(bpFloats, myFloats, adios2::Mode::Sync); + std::cout << "Float vector inside " << filename << ": {"; + for (auto &x : myFloats) + { + std::cout << x << ", "; + } + std::cout << "}\n"; } if (bpInts) { - bpReader.Get(bpInts, myInts.data(), adios2::Mode::Sync); + std::vector myInts; + bpReader.Get(bpInts, myInts, adios2::Mode::Sync); + } + else + { + std::cout << "There are no integer datasets in " << filename + << ".\n"; } /** Close bp file, engine becomes unreachable after this*/ @@ -57,18 +66,21 @@ int main(int argc, char *argv[]) } catch (std::invalid_argument &e) { - std::cout << "Invalid argument exception, STOPPING PROGRAM\n"; - std::cout << e.what() << "\n"; + std::cerr << "Invalid argument exception, STOPPING PROGRAM\n"; + std::cerr << e.what() << "\n"; } catch (std::ios_base::failure &e) { - std::cout << "IO System base failure exception, STOPPING PROGRAM\n"; - std::cout << e.what() << "\n"; + std::cerr << "IO System base failure exception, STOPPING PROGRAM\n"; + std::cerr << e.what() << "\n"; + std::cerr << "The file " << filename << " does not exist." + << " Presumably this is because hello_bpWriter has not been " + "run. Run ./hello_bpWriter before running this program.\n"; } catch (std::exception &e) { - std::cout << "Exception, STOPPING PROGRAM\n"; - std::cout << e.what() << "\n"; + std::cerr << "Exception, STOPPING PROGRAM\n"; + std::cerr << e.what() << "\n"; } return 0; diff --git a/examples/hello/bpWriter/helloBPWriter.cpp b/examples/hello/bpWriter/helloBPWriter.cpp index 7feb6ddff4..792a20d1bb 100644 --- a/examples/hello/bpWriter/helloBPWriter.cpp +++ b/examples/hello/bpWriter/helloBPWriter.cpp @@ -53,9 +53,9 @@ int main(int argc, char *argv[]) adios2::Variable bpString = bpIO.DefineVariable("bpString"); + std::string filename = "myVector_cpp.bp"; /** Engine derived class, spawned to start IO operations */ - adios2::Engine bpFileWriter = - bpIO.Open("myVector_cpp.bp", adios2::Mode::Write); + adios2::Engine bpFileWriter = bpIO.Open(filename, adios2::Mode::Write); /** Put variables for buffering, template type is optional */ bpFileWriter.Put(bpFloats, myFloats.data()); @@ -64,24 +64,44 @@ int main(int argc, char *argv[]) /** Create bp file, engine becomes unreachable after this*/ bpFileWriter.Close(); + if (rank == 0) + { + std::cout << "Wrote file " << filename + << " to disk. It can now be read by running " + "./bin/hello_bpReader.\n"; + } } catch (std::invalid_argument &e) { - std::cout << "Invalid argument exception, STOPPING PROGRAM from rank " - << rank << "\n"; - std::cout << e.what() << "\n"; + if (rank == 0) + { + std::cerr + << "Invalid argument exception, STOPPING PROGRAM from rank " + << rank << "\n"; + std::cerr << e.what() << "\n"; + } + MPI_Abort(MPI_COMM_WORLD, 1); } catch (std::ios_base::failure &e) { - std::cout << "IO System base failure exception, STOPPING PROGRAM " - "from rank " - << rank << "\n"; - std::cout << e.what() << "\n"; + if (rank == 0) + { + std::cerr << "IO System base failure exception, STOPPING PROGRAM " + "from rank " + << rank << "\n"; + std::cerr << e.what() << "\n"; + } + MPI_Abort(MPI_COMM_WORLD, 1); } catch (std::exception &e) { - std::cout << "Exception, STOPPING PROGRAM from rank " << rank << "\n"; - std::cout << e.what() << "\n"; + if (rank == 0) + { + std::cerr << "Exception, STOPPING PROGRAM from rank " << rank + << "\n"; + std::cerr << e.what() << "\n"; + } + MPI_Abort(MPI_COMM_WORLD, 1); } MPI_Finalize(); diff --git a/examples/hello/bpWriter/helloBPWriter_nompi.cpp b/examples/hello/bpWriter/helloBPWriter_nompi.cpp index 03ae8fe522..1f181f3c87 100644 --- a/examples/hello/bpWriter/helloBPWriter_nompi.cpp +++ b/examples/hello/bpWriter/helloBPWriter_nompi.cpp @@ -35,30 +35,33 @@ int main(int argc, char *argv[]) adios2::Variable bpFloats = bpIO.DefineVariable( "bpFloats", {}, {}, {Nx}, adios2::ConstantDims); + std::string filename = "myVector_cpp.bp"; /** Engine derived class, spawned to start IO operations */ - adios2::Engine bpWriter = - bpIO.Open("myVector_cpp.bp", adios2::Mode::Write); + adios2::Engine bpWriter = bpIO.Open(filename, adios2::Mode::Write); /** Write variable for buffering */ bpWriter.Put(bpFloats, myFloats.data()); /** Create bp file, engine becomes unreachable after this*/ bpWriter.Close(); + std::cout << "Wrote file " << filename + << " to disk. It can now be read by running " + "./bin/hello_bpReader.\n"; } catch (std::invalid_argument &e) { - std::cout << "Invalid argument exception, STOPPING PROGRAM\n"; - std::cout << e.what() << "\n"; + std::cerr << "Invalid argument exception, STOPPING PROGRAM\n"; + std::cerr << e.what() << "\n"; } catch (std::ios_base::failure &e) { - std::cout << "IO System base failure exception, STOPPING PROGRAM\n"; - std::cout << e.what() << "\n"; + std::cerr << "IO System base failure exception, STOPPING PROGRAM\n"; + std::cerr << e.what() << "\n"; } catch (std::exception &e) { - std::cout << "Exception, STOPPING PROGRAM\n"; - std::cout << e.what() << "\n"; + std::cerr << "Exception, STOPPING PROGRAM\n"; + std::cerr << e.what() << "\n"; } return 0;