Skip to content

Commit

Permalink
Merge pull request #991 from pnorbert/iotest_hdf5
Browse files Browse the repository at this point in the history
Added Native Parallel HDF5 writer/reader to adios_iotest
  • Loading branch information
pnorbert committed Nov 7, 2018
2 parents ae4b1da + e7dfa40 commit 1f43693
Show file tree
Hide file tree
Showing 12 changed files with 918 additions and 283 deletions.
15 changes: 14 additions & 1 deletion source/utils/adios_iotest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,22 @@ file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/iotest-config
DESTINATION ${PROJECT_BINARY_DIR}
)

add_executable(adios_iotest settings.cpp decomp.cpp processConfig.cpp adios_iotest.cpp)
add_executable(adios_iotest settings.cpp decomp.cpp processConfig.cpp ioGroup.cpp stream.cpp adiosStream.cpp adios_iotest.cpp)
target_link_libraries(adios_iotest adios2 MPI::MPI_C)

if(ADIOS2_HAVE_HDF5)
if(HDF5_C_INCLUDE_DIRS)
target_include_directories(adios_iotest PRIVATE ${HDF5_C_INCLUDE_DIRS})
else()
target_include_directories(adios_iotest PRIVATE ${HDF5_INCLUDE_DIRS})
endif()

target_sources(adios_iotest PRIVATE hdf5Stream.cpp)

target_link_libraries(adios_iotest ${HDF5_C_LIBRARIES})
endif()


install(TARGETS adios_iotest EXPORT adios2
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
Expand Down
251 changes: 251 additions & 0 deletions source/utils/adios_iotest/adiosStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
/*
* adiosStream.cpp
*
* Created on: Nov 2018
* Author: Norbert Podhorszki
*/

#include "adiosStream.h"

#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <stdexcept>
#include <string>

adiosStream::adiosStream(const std::string &streamName, adios2::IO &io,
const adios2::Mode mode, MPI_Comm comm)
: Stream(streamName, mode), io(io)
{

if (mode == adios2::Mode::Write)
{
engine = io.Open(streamName, adios2::Mode::Write, comm);
}
else
{
engine = io.Open(streamName, adios2::Mode::Read, comm);
}
}

adiosStream::~adiosStream(){};

void adiosStream::defineADIOSArray(const std::shared_ptr<VariableInfo> ov)
{
if (ov->type == "double")
{
adios2::Variable<double> v = io.DefineVariable<double>(
ov->name, ov->shape, ov->start, ov->count, true);
// v = io->InquireVariable<double>(ov->name);
}
else if (ov->type == "float")
{
adios2::Variable<float> v = io.DefineVariable<float>(
ov->name, ov->shape, ov->start, ov->count, true);
}
else if (ov->type == "int")
{
adios2::Variable<int> v = io.DefineVariable<int>(
ov->name, ov->shape, ov->start, ov->count, true);
}
}

void adiosStream::putADIOSArray(const std::shared_ptr<VariableInfo> ov)
{
if (ov->type == "double")
{
const double *a = reinterpret_cast<const double *>(ov->data.data());
engine.Put<double>(ov->name, a);
}
else if (ov->type == "float")
{
const float *a = reinterpret_cast<const float *>(ov->data.data());
engine.Put<float>(ov->name, a);
}
else if (ov->type == "int")
{
const int *a = reinterpret_cast<const int *>(ov->data.data());
engine.Put<int>(ov->name, a);
}
}

void adiosStream::getADIOSArray(std::shared_ptr<VariableInfo> ov)
{
if (ov->type == "double")
{
adios2::Variable<double> v = io.InquireVariable<double>(ov->name);
if (!v)
{
ov->readFromInput = false;
return;
}
v.SetSelection({ov->start, ov->count});
double *a = reinterpret_cast<double *>(ov->data.data());
engine.Get<double>(v, a);
ov->readFromInput = true;
}
else if (ov->type == "float")
{
adios2::Variable<float> v = io.InquireVariable<float>(ov->name);
if (!v)
{
ov->readFromInput = false;
return;
}
v.SetSelection({ov->start, ov->count});
float *a = reinterpret_cast<float *>(ov->data.data());
engine.Get<float>(v, a);
ov->readFromInput = true;
}
else if (ov->type == "int")
{
adios2::Variable<int> v = io.InquireVariable<int>(ov->name);
if (!v)
{
ov->readFromInput = false;
return;
}
v.SetSelection({ov->start, ov->count});
int *a = reinterpret_cast<int *>(ov->data.data());
engine.Get<int>(v, a);
ov->readFromInput = true;
}
}

/* return true if read-in completed */
adios2::StepStatus adiosStream::readADIOS(CommandRead *cmdR, Config &cfg,
const Settings &settings, size_t step)
{
if (!settings.myRank && settings.verbose)
{
std::cout << " Read ";
if (cmdR->stepMode == adios2::StepMode::NextAvailable)
{
std::cout << "next available step from ";
}
else
{
std::cout << "latest step from ";
}

std::cout << cmdR->streamName << " using the group " << cmdR->groupName;
if (!cmdR->variables.empty())
{
std::cout << " with selected variables: ";
for (const auto &v : cmdR->variables)
{
std::cout << v->name << " ";
}
}
std::cout << std::endl;
}
adios2::StepStatus status =
engine.BeginStep(cmdR->stepMode, cmdR->timeout_sec);
if (status != adios2::StepStatus::OK)
{
return status;
}

if (!settings.myRank && settings.verbose && step == 1)
{
const auto varmap = io.AvailableVariables();
std::cout << " Variables in input for reading: " << std::endl;
for (const auto &v : varmap)
{
std::cout << " " << v.first << std::endl;
}
}

if (!settings.myRank && settings.verbose)
{
std::cout << " Read data " << std::endl;
}

for (auto ov : cmdR->variables)
{
getADIOSArray(ov);
}
engine.EndStep();
return status;
}

void adiosStream::writeADIOS(CommandWrite *cmdW, Config &cfg,
const Settings &settings, size_t step)
{
if (!settings.myRank && settings.verbose)
{
std::cout << " Write to output " << cmdW->streamName << " the group "
<< cmdW->groupName;
if (!cmdW->variables.empty())
{
std::cout << " with selected variables: ";
for (const auto &v : cmdW->variables)
{
std::cout << v->name << " ";
}
}
std::cout << std::endl;
}

const double div =
pow(10.0, static_cast<const double>(settings.ndigits(cfg.nSteps - 1)));
double myValue = static_cast<double>(settings.myRank) +
static_cast<double>(step - 1) / div;

std::map<std::string, adios2::Params> definedVars = io.AvailableVariables();
for (auto ov : cmdW->variables)
{
// if the variable is not in the IO group it means
// we have not defined it yet (e.g. a write-only variable or a linked
// variable defined in another read group)
const auto it = definedVars.find(ov->name);
if (it == definedVars.end())
{
if (!settings.myRank && settings.verbose)
{
std::cout << " Define array " << ov->name
<< " for output" << std::endl;
}
defineADIOSArray(ov);
}

// if we read the variable, use the read values otherwise generate data
// now
if (!ov->readFromInput)
{
if (!settings.myRank && settings.verbose)
{
std::cout << " Fill array " << ov->name
<< " for output" << std::endl;
}
fillArray(ov, myValue);
}
}

if (!settings.myRank && settings.verbose)
{
std::cout << " Write data " << std::endl;
}
engine.BeginStep();
for (const auto ov : cmdW->variables)
{
putADIOSArray(ov);
}
engine.EndStep();
}

void adiosStream::Write(CommandWrite *cmdW, Config &cfg,
const Settings &settings, size_t step)
{

writeADIOS(cmdW, cfg, settings, step);
}

adios2::StepStatus adiosStream::Read(CommandRead *cmdR, Config &cfg,
const Settings &settings, size_t step)
{
return readADIOS(cmdR, cfg, settings, step);
}

void adiosStream::Close() { engine.Close(); }
38 changes: 38 additions & 0 deletions source/utils/adios_iotest/adiosStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* adiosStream.h
*
* Created on: Nov 2018
* Author: Norbert Podhorszki
*/

#ifndef ADIOSSTREAM_H
#define ADIOSSTREAM_H

#include "adios2.h"
#include "stream.h"

class adiosStream : public Stream
{
public:
adios2::Engine engine;
adios2::IO io;
adiosStream(const std::string &streamName, adios2::IO &io,
const adios2::Mode mode, MPI_Comm comm);
~adiosStream();
void Write(CommandWrite *cmdW, Config &cfg, const Settings &settings,
size_t step);
adios2::StepStatus Read(CommandRead *cmdR, Config &cfg,
const Settings &settings, size_t step);
void Close();

private:
void defineADIOSArray(const std::shared_ptr<VariableInfo> ov);
void putADIOSArray(const std::shared_ptr<VariableInfo> ov);
void getADIOSArray(std::shared_ptr<VariableInfo> ov);
adios2::StepStatus readADIOS(CommandRead *cmdR, Config &cfg,
const Settings &settings, size_t step);
void writeADIOS(CommandWrite *cmdW, Config &cfg, const Settings &settings,
size_t step);
};

#endif /* ADIOSSTREAM_H */

0 comments on commit 1f43693

Please sign in to comment.