Skip to content
Snippets Groups Projects
Commit 32179c99 authored by William F Godoy's avatar William F Godoy
Browse files

WriteStep example and implementation

examples/hello/bpTimeWriter/helloBPWriteStep.cpp
parent 6a1ae939
No related branches found
No related tags found
1 merge request!294Bp1read : Initial Reading Capabilities and latest API
......@@ -5,9 +5,12 @@
if(ADIOS2_HAVE_MPI)
add_executable(hello_bpTimeWriter helloBPTimeWriter.cpp)
add_executable(hello_bpWriteStep helloBPWriteStep.cpp)
target_include_directories(hello_bpTimeWriter PRIVATE ${MPI_C_INCLUDE_PATH})
target_link_libraries(hello_bpTimeWriter ${MPI_C_LIBRARIES})
else()
add_executable(hello_bpTimeWriter helloBPTimeWriter_nompi.cpp)
endif()
target_link_libraries(hello_bpTimeWriter adios2)
target_link_libraries(hello_bpWriteStep adios2)
\ No newline at end of file
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* helloBPWriteStep.cpp example for writing a variable using the Advance
* function for time aggregation. Time step is saved as an additional (global)
* single value variable, just for tracking purposes.
*
* Created on: Feb 16, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#include <algorithm> //std::for_each
#include <ios> //std::ios_base::failure
#include <iostream> //std::cout
#include <mpi.h>
#include <stdexcept> //std::invalid_argument std::exception
#include <vector>
#include <adios2.h>
int main(int argc, char *argv[])
{
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Application variable
std::vector<float> myFloats = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> myInts = {0, -1, -2, -3, -4, -5, -6, -7, -8, -9};
const std::size_t Nx = myFloats.size();
try
{
/** ADIOS class factory of IO class objects, DebugON is recommended */
adios2::ADIOS adios(MPI_COMM_WORLD, adios2::DebugON);
/*** IO class object: settings and factory of Settings: Variables,
* Parameters, Transports, and Execution: Engines */
adios2::IO &bpIO = adios.DeclareIO("BPFile_N2N");
bpIO.SetParameters({{"Threads", "2"}});
/** global array: name, { shape (total dimensions) }, { start
* (local) },
* { count (local) }, all are constant dimensions */
adios2::Variable<float> &bpFloats = bpIO.DefineVariable<float>(
"bpFloats", {size * Nx}, {rank * Nx}, {Nx}, adios2::ConstantDims,
myFloats.data());
adios2::Variable<int> &bpInts =
bpIO.DefineVariable<int>("bpInts", {size * Nx}, {rank * Nx}, {Nx},
adios2::ConstantDims, myInts.data());
/** Engine derived class, spawned to start IO operations */
adios2::Engine &bpWriter =
bpIO.Open("myWriteStep.bp", adios2::Mode::Write);
for (unsigned int timeStep = 0; timeStep < 10; ++timeStep)
{
myFloats[0] = timeStep;
myInts[0] = timeStep;
bpWriter.WriteStep();
}
bpWriter.Close();
}
catch (std::invalid_argument &e)
{
std::cout << "Invalid argument exception, STOPPING PROGRAM from rank "
<< rank << "\n";
std::cout << e.what() << "\n";
}
catch (std::ios_base::failure &e)
{
std::cout << "IO System base failure exception, STOPPING PROGRAM "
"from rank "
<< rank << "\n";
std::cout << e.what() << "\n";
}
catch (std::exception &e)
{
std::cout << "Exception, STOPPING PROGRAM from rank " << rank << "\n";
std::cout << e.what() << "\n";
}
MPI_Finalize();
return 0;
}
......@@ -25,8 +25,8 @@ Engine::Engine(const std::string engineType, IO &io, const std::string &name,
IO &Engine::GetIO() noexcept { return m_IO; }
void Engine::BeginStep() { ThrowUp("AcquireStep"); }
void Engine::EndStep() { ThrowUp("ReleaseStep"); }
void Engine::BeginStep() { ThrowUp("BeginStep"); }
void Engine::EndStep() { ThrowUp("EndStep"); }
void Engine::PutSync(const std::string &variableName)
{
......@@ -79,7 +79,36 @@ void Engine::PutDeferred(const std::string &variableName)
void Engine::PerformPuts() { ThrowUp("PerformPuts"); }
void Engine::PerformGets() { ThrowUp("PerformGets"); }
void Engine::WriteStep() { ThrowUp("WriteStep"); }
void Engine::WriteStep()
{
BeginStep();
const auto &variablesDataMap = m_IO.GetVariablesDataMap();
for (const auto &variablePair : variablesDataMap)
{
const std::string variableName(variablePair.first);
const std::string type(variablePair.second.first);
if (type == "compound")
{
// not supported
}
#define declare_template_instantiation(T) \
else if (type == adios2::GetType<T>()) \
{ \
Variable<T> *variable = m_IO.InquireVariable<T>(variableName); \
if (variable->GetData() != nullptr) \
{ \
PutDeferred<T>(*variable, variable->GetData()); \
} \
}
ADIOS2_FOREACH_TYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation
}
PerformPuts();
EndStep();
}
void Engine::ReadStep() { ThrowUp("ReadStep"); }
// PROTECTED
......
......@@ -180,9 +180,9 @@ public:
virtual void PerformGets();
/** Convenience function to write all variables in IO */
virtual void WriteStep();
void WriteStep();
/** Convenience function to read all variables in IO */
virtual void ReadStep();
void ReadStep();
/**
* Closes a particular transport, or all if -1
......
......@@ -51,8 +51,6 @@ void BPFileWriter::PerformPuts()
}
}
void BPFileWriter::WriteStep() {}
void BPFileWriter::EndStep()
{
m_BP3Serializer.SerializeData(m_IO, true); // true: advances step
......
......@@ -38,8 +38,6 @@ public:
void PerformPuts() final;
void EndStep() final;
void WriteStep() final;
void Close(const int transportIndex = -1) final;
private:
......
......@@ -55,7 +55,7 @@ void BPFileWriter::PutSyncCommon(Variable<T> &variable, const T *values)
m_BP3Serializer.PutVariableMetadata(variable);
m_BP3Serializer.PutVariablePayload(variable);
variable.SetData(nullptr); // not needed after PutSync
// variable.SetData(nullptr); // not needed after PutSync
}
template <class T>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment