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

WIP. Read stride #4034

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
13 changes: 13 additions & 0 deletions bindings/CXX11/adios2/cxx11/Variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ namespace adios2
} \
\
template <> \
void Variable<T>::SetStride(const Dims &stride, const DoubleMatrix &stencil) \
{ \
helper::CheckForNullptr(m_Variable, "in call to Variable<T>::SetStride"); \
return m_Variable->SetStride(stride, stencil); \
} \
\
template <> \
size_t Variable<T>::SelectionSize() const \
{ \
helper::CheckForNullptr(m_Variable, "in call to Variable<T>::SelectionSize"); \
Expand Down Expand Up @@ -154,6 +161,12 @@ namespace adios2
} \
\
template <> \
Box<Dims> Variable<T>::Selection() const \
{ \
helper::CheckForNullptr(m_Variable, "in call to Variable<T>::Selection"); \
return m_Variable->Selection(); \
} \
template <> \
size_t Variable<T>::Steps() const \
{ \
helper::CheckForNullptr(m_Variable, "in call to Variable<T>::Steps"); \
Expand Down
15 changes: 14 additions & 1 deletion bindings/CXX11/adios2/cxx11/Variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,16 @@ class Variable
*/
void SetAccuracy(const adios2::Accuracy &a);

/**
* Set striding before reading
* @param stride = vector of stride in each dimension
* @param stencil = n-dim matrix for computing each data point
*/
void SetStride(const Dims &stride, const DoubleMatrix &stencil = {});

/**
* Returns the number of elements required for pre-allocation based on
* current count and stepsCount
* current count, stepsCount, and stride
* @return elements of type T required for pre-allocation
*/
size_t SelectionSize() const;
Expand Down Expand Up @@ -286,6 +293,12 @@ class Variable
*/
adios2::Dims Count() const;

/**
* Inspects selection with striding but without steps
* @return pair of start, count vector, that is the actual selection
* in global space after applying striding
*/
Box<Dims> Selection() const;
/**
* For readRandomAccess mode, inspect the number of available steps
* @return available steps
Expand Down
26 changes: 26 additions & 0 deletions bindings/Python/py11Variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,32 @@ size_t Variable::SelectionSize() const
return size;
}

Box<Dims> Variable::Selection() const
{
helper::CheckForNullptr(m_VariableBase, "in call to Variable::Selection");
const adios2::DataType typeCpp = m_VariableBase->m_Type;

if (typeCpp == adios2::DataType::Struct)
{
// not supported
}
#define declare_template_instantiation(T) \
else if (typeCpp == adios2::helper::GetDataType<T>()) \
{ \
const adios2::core::Variable<T> *variable = \
dynamic_cast<const adios2::core::Variable<T> *>(m_VariableBase); \
return variable->Selection(); \
}
ADIOS2_FOREACH_STDTYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation
}

void Variable::SetStride(const Dims &stride)
{
helper::CheckForNullptr(m_VariableBase, "in call to Variable::SetStride");
return m_VariableBase->SetStride(stride);
}

size_t Variable::AddOperation(const Operator op, const Params &parameters)
{
helper::CheckForNullptr(m_VariableBase, "in call to Variable::AddOperation");
Expand Down
4 changes: 4 additions & 0 deletions bindings/Python/py11Variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class Variable

size_t SelectionSize() const;

Box<Dims> Selection() const;

void SetStride(const Dims &stride);

std::string Name() const;

std::string Type() const;
Expand Down
2 changes: 2 additions & 0 deletions bindings/Python/py11glue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ PYBIND11_MODULE(ADIOS2_PYTHON_MODULE_NAME, m)
.def("SetSelection", &adios2::py11::Variable::SetSelection)
.def("SetStepSelection", &adios2::py11::Variable::SetStepSelection)
.def("SelectionSize", &adios2::py11::Variable::SelectionSize)
.def("SetStride", &adios2::py11::Variable::SetStride)
.def("Selection", &adios2::py11::Variable::Selection)
.def("Name", &adios2::py11::Variable::Name)
.def("Type", &adios2::py11::Variable::Type)
.def("Sizeof", &adios2::py11::Variable::Sizeof)
Expand Down
3 changes: 3 additions & 0 deletions examples/basics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ add_subdirectory(localArray)
add_subdirectory(queryWorker)
add_subdirectory(values)
add_subdirectory(variablesShapes)
add_subdirectory(stridedRead)
add_subdirectory(readByAccuracy)

17 changes: 17 additions & 0 deletions examples/basics/readByAccuracy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#------------------------------------------------------------------------------#
# Distributed under the OSI-approved Apache License, Version 2.0. See
# accompanying file Copyright.txt for details.
#------------------------------------------------------------------------------#

cmake_minimum_required(VERSION 3.12)
project(ADIOS2BasicsReadByAccuracyExample)

if(NOT TARGET adios2_core)
set(_components CXX)
find_package(ADIOS2 REQUIRED COMPONENTS ${_components})
endif()

add_executable(adios2_basics_readByAccuracy2D readByAccuracy2D.cpp)
target_link_libraries(adios2_basics_readByAccuracy2D adios2::cxx11 adios2_core)
install(TARGETS adios2_basics_readByAccuracy2D RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

137 changes: 137 additions & 0 deletions examples/basics/readByAccuracy/readByAccuracy2D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* * readByAccuracy2D.cpp : example to read from 2D array with accuracy set
* If MGARD MDR operator is available, it will refactor the data on write
* and support read with a user defined error. Otherwise, reading "normal"
* data is always fully accurate (error = 0.0)
*
* Created on: Jan 17, 2024
* Author: Norbert Podhorszki <pnorbert@ornl.gov>
*/

#include <cstddef> //std::size_t
#include <iomanip> // std::setprecision
#include <iostream> // std::cout
#include <limits> // std::numeric_limits
#include <math.h>
#include <numeric> //std::iota
#include <stdexcept> //std::exception

#include "adios2/helper/adiosString.h" // AccuracyToString
#include <adios2.h>

constexpr double TWOPI = 2.0 * M_PI;

void writer(adios2::ADIOS &adios, const std::size_t nx, const std::size_t ny)
{
auto lf_computeTrig = [](const std::size_t nx, const std::size_t ny) -> std::vector<double> {
const double sp = TWOPI / nx;
std::vector<double> array(nx * ny);
size_t pos = 0;
for (size_t i = 0; i < nx; ++i)
{
double c = cos(i * sp);
for (size_t j = 0; j < ny; ++j)
{
array[pos] = c + sin(j * sp);
++pos;
}
}
return array;
};

adios2::IO io = adios.DeclareIO("readByAccuracy2D-writer");

const adios2::Dims shape = {nx, ny};
adios2::Variable<double> var =
io.DefineVariable<double>("data/full", shape, {0, 0}, shape, adios2::ConstantDims);

#ifdef ADIOS2_HAVE_MGARD_MDR1
adios2::Operator mgardOp = adios.DefineOperator("mgardCompressor", adios2::ops::MDR);
var.AddOperation(mgardOp, {});
io.DefineAttribute<std::string>("operator", "Refactored by adios2::ops::MDR", var.Name());
#else
io.DefineAttribute<std::string>("operator", "none", var.Name());
#endif

const std::vector<double> array = lf_computeTrig(nx, ny);

adios2::Engine writer = io.Open("readByAccuracy2D.bp", adios2::Mode::Write);
writer.BeginStep();
writer.Put(var, array.data());
writer.EndStep();
writer.Close();
}

const std::vector<double> errors = {1.0, 0.1, 0.001, 0.00001};

void reader(adios2::ADIOS &adios)
{
adios2::IO io = adios.DeclareIO("readByAccuracy2D-reader");
io.SetParameter("Threads", "1");

// read data N times into N vectors so that we can output them later
std::vector<std::vector<double>> arrays(errors.size());
std::vector<adios2::Accuracy> actualAccuracy(errors.size());

adios2::Dims varShape;

{
adios2::Engine reader = io.Open("readByAccuracy2D.bp", adios2::Mode::Read);
reader.BeginStep();

adios2::Variable<double> varFull = io.InquireVariable<double>("data/full");
varShape = varFull.Shape();

for (size_t i = 0; i < errors.size(); ++i)
{
adios2::Accuracy requestedAccuracy = {errors[i], adios2::Linf_norm, false};
varFull.SetAccuracy(requestedAccuracy);
// adios will allocate the vector to fit the data
// force reading now, so that we can retrieve the accuracy from 'v'
reader.Get(varFull, arrays[i], adios2::Mode::Sync);
actualAccuracy[i] = varFull.GetAccuracy();
}
reader.EndStep();
reader.Close();
}

// write out the result
{
adios2::IO io = adios.DeclareIO("readByAccuracy2D-write-again");
adios2::Engine writer = io.Open("readByAccuracy2D.bp", adios2::Mode::Append);
writer.BeginStep();
for (size_t i = 0; i < errors.size(); ++i)
{
std::string outname = "data/" + std::to_string(errors[i]);
adios2::Variable<double> v =
io.DefineVariable<double>(outname, varShape, {0, 0}, varShape);
io.DefineAttribute("accuracy", adios2::helper::AccuracyToString(actualAccuracy[i]),
v.Name());

writer.Put(v, arrays[i].data());
}
writer.EndStep();
writer.Close();
}
}

int main(int argc, char *argv[])
{
try
{
constexpr std::size_t nx = 1000;
constexpr std::size_t ny = 1000;
adios2::ADIOS adios;
writer(adios, nx, ny);
reader(adios);
}
catch (const std::exception &e)
{
std::cout << "ERROR: ADIOS2 exception: " << e.what() << "\n";
}

return 0;
}
25 changes: 25 additions & 0 deletions examples/basics/stridedRead/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#------------------------------------------------------------------------------#
# Distributed under the OSI-approved Apache License, Version 2.0. See
# accompanying file Copyright.txt for details.
#------------------------------------------------------------------------------#

cmake_minimum_required(VERSION 3.12)
project(ADIOS2BasicsStridedReadExample)

if(NOT TARGET adios2_core)
set(_components CXX)
find_package(ADIOS2 REQUIRED COMPONENTS ${_components})
endif()

add_executable(adios2_basics_stridedRead1D stridedRead1D.cpp)
target_link_libraries(adios2_basics_stridedRead1D adios2::cxx11 adios2_core)
install(TARGETS adios2_basics_stridedRead1D RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

add_executable(adios2_basics_stridedRead2D stridedRead2D.cpp)
target_link_libraries(adios2_basics_stridedRead2D adios2::cxx11 adios2_core)
install(TARGETS adios2_basics_stridedRead2D RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

add_executable(adios2_basics_stridedRead3D stridedRead3D.cpp)
target_link_libraries(adios2_basics_stridedRead3D adios2::cxx11 adios2_core)
install(TARGETS adios2_basics_stridedRead3D RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

Loading
Loading