Skip to content

Commit

Permalink
Merge pull request #1532 from NAThompson/examples_idiomatic
Browse files Browse the repository at this point in the history
Small quality of life improvements to basic examples.
  • Loading branch information
williamfgc authored Jun 25, 2019
2 parents fc89837 + 97abdae commit 1ffc3c3
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 62 deletions.
79 changes: 50 additions & 29 deletions examples/hello/bpReader/helloBPReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* Created on: Feb 16, 2017
* Author: William F Godoy godoywf@ornl.gov
*/

#include <ios> //std::ios_base::failure
#include <iostream> //std::cout
#include <mpi.h>
Expand All @@ -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<float> myFloats(Nx);
std::vector<int> myInts(Nx);

std::string filename = "myVector_cpp.bp";
try
{
/** ADIOS class factory of IO class objects, DebugON is recommended */
Expand All @@ -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<std::string, adios2::Params> variables =
bpIO.AvailableVariables();
Expand All @@ -64,56 +57,84 @@ int main(int argc, char *argv[])
bpIO.InquireVariable<float>("bpFloats");
adios2::Variable<int> bpInts = bpIO.InquireVariable<int>("bpInts");

const std::size_t Nx = 10;
if (bpFloats) // means found
{
std::vector<float> myFloats;

// read only the chunk corresponding to our rank
bpFloats.SetSelection({{Nx * rank}, {Nx}});
// myFloats.data is pre-allocated
bpReader.Get<float>(bpFloats, myFloats.data(), adios2::Mode::Sync);
bpReader.Get<float>(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<int> myInts;
// read only the chunk corresponding to our rank
bpInts.SetSelection({{Nx * rank}, {Nx}});
// myInts.data is pre-allocated
bpReader.Get<int>(bpInts, myInts.data(), adios2::Mode::Sync);

std::cout << "MyInts: \n";
for (const auto number : myInts)
bpReader.Get<int>(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*/
bpReader.Close();
}
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();
Expand Down
40 changes: 26 additions & 14 deletions examples/hello/bpReader/helloBPReader_nompi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

int main(int argc, char *argv[])
{
/** Application variable */
const std::size_t Nx = 10;
std::vector<float> myFloats(Nx);
std::vector<int> myInts(Nx);
std::string filename = "myVector_cpp.bp";

try
{
Expand All @@ -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<float> bpFloats =
Expand All @@ -44,31 +40,47 @@ int main(int argc, char *argv[])

if (bpFloats)
{
bpReader.Get<float>(bpFloats, myFloats.data(), adios2::Mode::Sync);
std::vector<float> myFloats;
bpReader.Get<float>(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<int>(bpInts, myInts.data(), adios2::Mode::Sync);
std::vector<int> myInts;
bpReader.Get<int>(bpInts, myInts, adios2::Mode::Sync);
}
else
{
std::cout << "There are no integer datasets in " << filename
<< ".\n";
}

/** Close bp file, engine becomes unreachable after this*/
bpReader.Close();
}
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;
Expand Down
42 changes: 31 additions & 11 deletions examples/hello/bpWriter/helloBPWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ int main(int argc, char *argv[])
adios2::Variable<std::string> bpString =
bpIO.DefineVariable<std::string>("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<float>(bpFloats, myFloats.data());
Expand All @@ -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();
Expand Down
19 changes: 11 additions & 8 deletions examples/hello/bpWriter/helloBPWriter_nompi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,33 @@ int main(int argc, char *argv[])
adios2::Variable<float> bpFloats = bpIO.DefineVariable<float>(
"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<float>(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;
Expand Down

0 comments on commit 1ffc3c3

Please sign in to comment.