Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small quality of life improvements to basic examples. #1532

Merged
merged 6 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions examples/hello/bpReader/helloBPReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <ios> //std::ios_base::failure
#include <iostream> //std::cout
#include <fstream> //std::ifstream
NAThompson marked this conversation as resolved.
Show resolved Hide resolved
#include <mpi.h>
#include <stdexcept> //std::invalid_argument std::exception
#include <vector>
Expand All @@ -22,16 +23,25 @@

int main(int argc, char *argv[])
{
// Test if the file has been written by hello_bpWriter:
std::string filename = "myVector_cpp.bp";
std::ifstream f(filename.c_str());
if (!f.good())
{
std::cerr << "The file " << filename << " does not exist."
NAThompson marked this conversation as resolved.
Show resolved Hide resolved
<< " Presumably this is because hello_bpWriter has not been run. Run ./hello_bpWriter before running this program.\n";
return 1;
}
else
{
f.close();
}

MPI_Init(&argc, &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);

try
{
/** ADIOS class factory of IO class objects, DebugON is recommended */
Expand Down Expand Up @@ -64,12 +74,15 @@ int main(int argc, char *argv[])
bpIO.InquireVariable<float>("bpFloats");
adios2::Variable<int> bpInts = bpIO.InquireVariable<int>("bpInts");

unsigned long Nx = 10;
NAThompson marked this conversation as resolved.
Show resolved Hide resolved
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
NAThompson marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand All @@ -81,12 +94,13 @@ int main(int argc, char *argv[])

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);
bpReader.Get<int>(bpInts, myInts, adios2::Mode::Sync);

std::cout << "MyInts: \n";
std::cout << "myInts: \n";
for (const auto number : myInts)
{
std::cout << number << " ";
Expand Down
36 changes: 29 additions & 7 deletions examples/hello/bpReader/helloBPReader_nompi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,28 @@

#include <ios> //std::ios_base::failure
#include <iostream> //std::cout
#include <fstream> //std::fstream
#include <stdexcept> //std::invalid_argument std::exception
#include <vector>

#include <adios2.h>

int main(int argc, char *argv[])
{
/** Application variable */
const std::size_t Nx = 10;
std::vector<float> myFloats(Nx);
std::vector<int> myInts(Nx);
// Test if the file has been written by hello_bpWriter:
std::string filename = "myVector_cpp.bp";
std::ifstream f(filename.c_str());
if (!f.good())
{
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";
return 1;
}
else
{
f.close();
}


try
{
Expand All @@ -34,7 +45,7 @@ int main(int argc, char *argv[])

/** Engine derived class, spawned to start IO operations */
adios2::Engine bpReader =
bpIO.Open("myVector_cpp.bp", adios2::Mode::Read);
bpIO.Open(filename.c_str(), adios2::Mode::Read);
NAThompson marked this conversation as resolved.
Show resolved Hide resolved

/** Write variable for buffering */
adios2::Variable<float> bpFloats =
Expand All @@ -44,12 +55,23 @@ 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*/
Expand Down
4 changes: 3 additions & 1 deletion examples/hello/bpWriter/helloBPWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ 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);
bpIO.Open(filename, adios2::Mode::Write);

/** Put variables for buffering, template type is optional */
bpFileWriter.Put<float>(bpFloats, myFloats.data());
Expand All @@ -64,6 +65,7 @@ int main(int argc, char *argv[])

/** Create bp file, engine becomes unreachable after this*/
bpFileWriter.Close();
std::cout << "Wrote file " << filename << " to disk. It can now be read by running ./bin/hello_bpReader.\n";
}
catch (std::invalid_argument &e)
{
Expand Down
4 changes: 3 additions & 1 deletion examples/hello/bpWriter/helloBPWriter_nompi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ 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);
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)
NAThompson marked this conversation as resolved.
Show resolved Hide resolved
{
Expand Down