Skip to content

Commit

Permalink
Merge pull request #1293 from germasch/pr/bug
Browse files Browse the repository at this point in the history
testing: expose a bug
  • Loading branch information
williamfgc authored Mar 16, 2019
2 parents d17b219 + adf0be8 commit 2e9f079
Showing 1 changed file with 254 additions and 0 deletions.
254 changes: 254 additions & 0 deletions testing/adios2/interface/TestADIOSInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstdint>

#include <iostream>
#include <numeric>
#include <stdexcept>

#include <adios2.h>
Expand Down Expand Up @@ -77,6 +78,259 @@ TEST_F(ADIOS2_CXX11_API_IO, EngineDefault)
// changes the engine_type string?
}

template <class T>
struct MyData
{
using Block = std::vector<T>;
using Box = adios2::Box<adios2::Dims>;

explicit MyData(const std::vector<Box> &selections)
: m_Blocks(selections.size()), m_Selections(selections)
{
for (int b = 0; b < nBlocks(); ++b)
{
m_Blocks[b].resize(selections[b].second[0]);
}
}

size_t nBlocks() const { return m_Selections.size(); }
size_t start(int b) const { return m_Selections[b].first[0]; }
size_t count(int b) const { return m_Selections[b].second[0]; }
const Box &selection(int b) const { return m_Selections[b]; }
Block &operator[](int b) { return m_Blocks[b]; }

private:
std::vector<Block> m_Blocks;
std::vector<Box> m_Selections;
};

template <class T>
struct MyDataView
{
using Block = T *;
using Box = adios2::Box<adios2::Dims>;

explicit MyDataView(const std::vector<Box> &selections)
: m_Blocks(selections.size()), m_Selections(selections)
{
}

void place(int b, T *arr) { m_Blocks[b] = arr; }

size_t nBlocks() const { return m_Selections.size(); }
size_t start(int b) const { return m_Selections[b].first[0]; }
size_t count(int b) const { return m_Selections[b].second[0]; }
const Box &selection(int b) const { return m_Selections[b]; }
Block operator[](int b) { return m_Blocks[b]; }

private:
std::vector<Block> m_Blocks;
std::vector<Box> m_Selections;
};

class ADIOS2_CXX11_API_Put : public ADIOS2_CXX11_API_IO
{
public:
using T = double;
using Box = adios2::Box<adios2::Dims>;

using ADIOS2_CXX11_API_IO::ADIOS2_CXX11_API_IO;

void SetupDecomposition(size_t Nx)
{
m_Nx = Nx;
m_Shape = {size * Nx};
m_Selections = {{{rank * Nx}, {Nx / 2}},
{{rank * Nx + Nx / 2}, {Nx / 2}}};
}

template <class MyData>
void PopulateBlock(MyData &myData, int b)
{
std::iota(&myData[b][0], &myData[b][myData.count(b)],
T(myData.start(b)));
}

bool checkOutput(std::string filename)
{
if (rank != 0)
{
return true;
}
adios2::IO io = ad.DeclareIO("CXX11_API_CheckIO");
#ifdef ADIOS2_HAVE_MPI
adios2::Engine engine =
io.Open(filename, adios2::Mode::Read, MPI_COMM_SELF);
#else
adios2::Engine engine = io.Open(filename, adios2::Mode::Read);
#endif
adios2::Variable<T> var = io.InquireVariable<T>("var");
adios2::Dims shape = var.Shape();
std::vector<T> data(shape[0]);
engine.Get(var, data, adios2::Mode::Sync);
engine.Close();

std::vector<T> ref(shape[0]);
std::iota(ref.begin(), ref.end(), T());
return data == ref;
}

size_t m_Nx;
adios2::Dims m_Shape;
std::vector<Box> m_Selections;
};

TEST_F(ADIOS2_CXX11_API_Put, MultiBlockPutSync)
{
SetupDecomposition(10);

adios2::Engine engine = io.Open("multi_sync.bp", adios2::Mode::Write);
adios2::Variable<T> var = io.DefineVariable<T>("var", m_Shape);

MyData<T> myData(m_Selections);

for (int b = 0; b < myData.nBlocks(); ++b)
{
PopulateBlock(myData, b);

var.SetSelection(myData.selection(b));
engine.Put(var, &myData[b][0], adios2::Mode::Sync);
}
engine.Close();

EXPECT_TRUE(checkOutput("multi_sync.bp"));
}

TEST_F(ADIOS2_CXX11_API_Put, MultiBlockPutDeferred)
{
SetupDecomposition(10);

adios2::Engine engine = io.Open("multi_deferred.bp", adios2::Mode::Write);
adios2::Variable<T> var = io.DefineVariable<T>("var", m_Shape);

MyData<T> myData(m_Selections);

for (int b = 0; b < myData.nBlocks(); ++b)
{
PopulateBlock(myData, b);

var.SetSelection(myData.selection(b));
engine.Put(var, &myData[b][0], adios2::Mode::Deferred);
}
engine.Close();

EXPECT_TRUE(checkOutput("multi_deferred.bp"));
}

TEST_F(ADIOS2_CXX11_API_Put, MultiBlockPutDS)
{
SetupDecomposition(10);

adios2::Engine engine = io.Open("multi_ds.bp", adios2::Mode::Write);
adios2::Variable<T> var = io.DefineVariable<T>("var", m_Shape);

MyData<T> myData(m_Selections);

for (int b = 0; b < myData.nBlocks(); ++b)
{
PopulateBlock(myData, b);

var.SetSelection(myData.selection(b));
if (b == 0)
{
engine.Put(var, &myData[b][0], adios2::Mode::Deferred);
}
else
{
engine.Put(var, &myData[b][0], adios2::Mode::Sync);
}
}
engine.Close();

EXPECT_TRUE(checkOutput("multi_ds.bp"));
}

TEST_F(ADIOS2_CXX11_API_Put, MultiBlockPutZeroCopySync)
{
SetupDecomposition(10);

adios2::Engine engine = io.Open("multi0_sync.bp", adios2::Mode::Write);
adios2::Variable<T> var = io.DefineVariable<T>("var", m_Shape);

MyDataView<T> myData(m_Selections);
for (int b = 0; b < myData.nBlocks(); ++b)
{
var.SetSelection(myData.selection(b));
auto span = engine.Put(var);
myData.place(b, span.data());
}

for (int b = 0; b < myData.nBlocks(); ++b)
{
PopulateBlock(myData, b);
}
engine.Close();

EXPECT_TRUE(checkOutput("multi0_sync.bp"));
}

TEST_F(ADIOS2_CXX11_API_Put, MultiBlockPutZeroCopySync2)
{
SetupDecomposition(10);

adios2::Engine engine = io.Open("multi0_sync2.bp", adios2::Mode::Write);
adios2::Variable<T> var = io.DefineVariable<T>("var", m_Shape);

MyDataView<T> myData(m_Selections);
for (int b = 0; b < 1; ++b)
{
var.SetSelection(myData.selection(b));
auto span = engine.Put(var);
myData.place(b, span.data());
}

for (int b = 0; b < 1; ++b)
{
PopulateBlock(myData, b);
}
std::vector<T> lastBlock(m_Nx / 2);
std::iota(lastBlock.begin(), lastBlock.end(), T(rank * m_Nx + m_Nx / 2));
var.SetSelection(myData.selection(1));
engine.Put(var, lastBlock.data(), adios2::Mode::Deferred);
engine.Close();

EXPECT_TRUE(checkOutput("multi0_sync2.bp"));
}

#if 0
TEST_F(ADIOS2_CXX11_API_Put, MultiBlockPutZeroCopySync3)
{
SetupDecomposition(10);

adios2::Engine engine = io.Open("multi0_sync3.bp", adios2::Mode::Write);
adios2::Variable<T> var = io.DefineVariable<T>("var", m_Shape);

MyDataView<T> myData(m_Selections);
for (int b = 0; b < 1; ++b)
{
var.SetSelection(myData.selection(b));
auto span = engine.Put(var);
myData.place(b, span.data());
}

for (int b = 0; b < 1; ++b)
{
PopulateBlock(myData, b);
}

engine.Put(var, std::vector<T>{5., 6., 7., 8., 9.}.data(),
adios2::Mode::Sync);
engine.Close();

EXPECT_TRUE(checkOutput("multi0_sync3.bp"));
}
#endif

int main(int argc, char **argv)
{
#ifdef ADIOS2_HAVE_MPI
Expand Down

0 comments on commit 2e9f079

Please sign in to comment.