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

Group api revision #2614

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions bindings/CXX11/adios2/cxx11/IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ Engine IO::Open(const std::string &name, const Mode mode)
"for engine " + name + ", in call to IO::Open");
return Engine(&m_IO->Open(name, mode));
}
Group IO::InquireGroup(const std::string &path, char delimiter)
Group IO::InquireGroup(char delimiter)
{
return Group(&m_IO->CreateGroup(path, delimiter));
return Group(&m_IO->CreateGroup(delimiter));
};
void IO::FlushAll()
{
Expand Down
2 changes: 1 addition & 1 deletion bindings/CXX11/adios2/cxx11/IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class IO
* @param a delimiter to separate groups in a string representation
* @return Group object
*/
Group InquireGroup(const std::string &path, char delimiter = '/');
Group InquireGroup(char delimiter = '/');

#if ADIOS2_USE_MPI
/**
Expand Down
46 changes: 29 additions & 17 deletions source/adios2/core/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ std::vector<std::string> split(const std::string &s, char delimiter)
}
return tokens;
}
void Group::setPath(std::string path) { currentPath = path; }
void Group::setPath(std::string path) { currentPath = ADIOS_root + "/" + path; }
void Group::setDelimiter(char delimiter) { groupDelimiter = delimiter; }

Group::Group(std::string path, char delimiter, IO &io)
Expand All @@ -51,10 +51,13 @@ Group::Group(const Group &G)
}
Group *Group::InquireGroup(std::string groupName)
{
Group *g_out = new Group(currentPath + groupDelimiter + groupName,
this->groupDelimiter, this->m_IO);
g_out->mapPtr = this->mapPtr;
return g_out;
if (currentPath.compare("") != 0)
{
groupName = currentPath + groupDelimiter + groupName;
}
m_Gr = std::make_shared<Group>(groupName, this->groupDelimiter, this->m_IO);
m_Gr->mapPtr = this->mapPtr;
return m_Gr.get();
}
void Group::PrintTree()
{
Expand All @@ -74,6 +77,12 @@ void Group::BuildTree()
{
std::vector<std::string> tokens =
split(variablePair.first, groupDelimiter);
// Adding artificial root element
if (tokens[0] == "")
tokens[0] = ADIOS_root;
else
tokens.insert(tokens.begin(), ADIOS_root);
currentPath = ADIOS_root;

if (tokens.size() == 0)
{
Expand All @@ -83,7 +92,7 @@ void Group::BuildTree()
{
// case record = "/group1" or "group/"
}
if (tokens.size() > 1)
else
{
std::string key = tokens[0];
for (int level = 1; level < tokens.size(); level++)
Expand Down Expand Up @@ -140,9 +149,10 @@ std::vector<std::string> Group::AvailableVariables()
mapPtr->treeMap.end())
{
const core::VarMap &variables = m_IO.GetVariables();

if (variables.find(currentPath + groupDelimiter + v) !=
variables.end())
std::string variablePath = currentPath + groupDelimiter + v;
variablePath = variablePath.substr(
ADIOS_root.size() + 1, variablePath.size() - ADIOS_root.size());
if (variables.find(variablePath) != variables.end())
{
available_variables.push_back(v);
}
Expand All @@ -164,8 +174,10 @@ std::vector<std::string> Group::AvailableAttributes()
mapPtr->treeMap.end())
{
const core::AttrMap &attributes = m_IO.GetAttributes();
if (attributes.find(currentPath + groupDelimiter + v) !=
attributes.end())
std::string variablePath = currentPath + groupDelimiter + v;
variablePath = variablePath.substr(
ADIOS_root.size() + 1, variablePath.size() - ADIOS_root.size());
if (attributes.find(variablePath) != attributes.end())
{
available_attributes.push_back(v);
}
Expand All @@ -180,14 +192,14 @@ std::vector<std::string> Group::AvailableGroups()

std::vector<std::string> available_groups;
std::set<std::string> val = mapPtr->treeMap[currentPath];

for (auto v : val)
{
if (mapPtr->treeMap.find(currentPath + groupDelimiter + v) !=
mapPtr->treeMap.end())
available_groups.push_back(v);
for (auto v : val)
{
if (mapPtr->treeMap.find(currentPath + groupDelimiter + v) !=
mapPtr->treeMap.end())
available_groups.push_back(v);
}
}

return available_groups;
}

Expand Down
4 changes: 4 additions & 0 deletions source/adios2/core/Group.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Group
char groupDelimiter;
/** shared pointer to a map representing the tree structure */
std::shared_ptr<TreeMap> mapPtr = nullptr;
/** root of the tree */
const std::string ADIOS_root = "_ADIOS_ROOT_";

public:
/**
Expand All @@ -50,6 +52,8 @@ class Group
Group(const Group &G);
/** destructor */
~Group();
/** a pointer to a Group Object */
std::shared_ptr<Group> m_Gr;
/**
* @brief Builds map that represents tree structure from m_Variable and
* m_Attributes from IO class
Expand Down
13 changes: 9 additions & 4 deletions source/adios2/core/Group.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ namespace core
template <class T>
Variable<T> *Group::InquireVariable(const std::string &name) noexcept
{
Variable<T> &variable =
*m_IO.InquireVariable<T>(currentPath + groupDelimiter + name);
std::string variablePath = currentPath + groupDelimiter + name;
variablePath = variablePath.substr(ADIOS_root.size() + 1,
variablePath.size() - ADIOS_root.size());
Variable<T> &variable = *m_IO.InquireVariable<T>(variablePath);
return &variable;
}

Expand All @@ -30,8 +32,11 @@ Attribute<T> *Group::InquireAttribute(const std::string &name,
const std::string &variableName,
const std::string separator) noexcept
{
Attribute<T> &attribute = m_IO.InquireAttribute<T>(
currentPath + groupDelimiter + name, variableName, separator);
std::string variablePath = currentPath + groupDelimiter + name;
variablePath = variablePath.substr(ADIOS_root.size() + 1,
variablePath.size() - ADIOS_root.size());
Attribute<T> &attribute =
m_IO.InquireAttribute<T>(variablePath, variableName, separator);
return &attribute;
}
} // end namespace core
Expand Down
5 changes: 2 additions & 3 deletions source/adios2/core/IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,10 +655,9 @@ Engine &IO::Open(const std::string &name, const Mode mode)
{
return Open(name, mode, m_ADIOS.GetComm().Duplicate());
}
Group &IO::CreateGroup(const std::string &path, char delimiter)
Group &IO::CreateGroup(char delimiter)
{

m_Gr = std::make_shared<Group>(path, delimiter, *this);
m_Gr = std::make_shared<Group>("", delimiter, *this);
m_Gr->BuildTree();
return *m_Gr;
}
Expand Down
2 changes: 1 addition & 1 deletion source/adios2/core/IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ class IO
*/
void FlushAll();

Group &CreateGroup(const std::string &path, char delimiter);
Group &CreateGroup(char delimiter);

// READ FUNCTIONS, not yet implemented:
/**
Expand Down
68 changes: 60 additions & 8 deletions testing/adios2/hierarchy/TestHierarchicalReading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ TEST_F(ADIOSHierarchicalReadVariableTest, Read)
std::string filename = "ADIOSHierarchicalReadVariable.bp";

// Number of steps
const std::size_t NSteps = 1;
const std::size_t NSteps = 2;

long unsigned int rank, size;

#if ADIOS2_USE_MPI
MPI_Comm_rank(MPI_COMM_WORLD, &mpiRank);
MPI_Comm_size(MPI_COMM_WORLD, &mpiSize);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
#else
rank = 0;
size = 1;
#endif

// Write test data using BP
Expand All @@ -43,9 +48,10 @@ TEST_F(ADIOSHierarchicalReadVariableTest, Read)

io.AddTransport("file");
adios2::Engine engine = io.Open(filename, adios2::Mode::Write);
const adios2::Dims shape = {10};
const adios2::Dims start = {0};
const adios2::Dims count = {10};
const std::size_t Nx = 10;
const adios2::Dims shape = {size * Nx};
const adios2::Dims start = {rank * Nx};
const adios2::Dims count = {Nx};

auto var1 = io.DefineVariable<int32_t>(
"group1/group2/group3/group4/variable1", shape, start, count);
Expand All @@ -57,7 +63,10 @@ TEST_F(ADIOSHierarchicalReadVariableTest, Read)
"group1/group2/group3/group4/variable4", shape, start, count);
auto var5 = io.DefineVariable<int32_t>(
"group1/group2/group3/group4/variable5", shape, start, count);
auto var6 =
io.DefineVariable<int32_t>("variable6", shape, start, count);
std::vector<int32_t> Ints = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

for (size_t step = 0; step < NSteps; ++step)
{
engine.BeginStep();
Expand All @@ -67,16 +76,21 @@ TEST_F(ADIOSHierarchicalReadVariableTest, Read)
engine.Put(var3, Ints.data());
engine.Put(var4, Ints.data());
engine.Put(var5, Ints.data());
engine.Put(var6, Ints.data());

engine.EndStep();
}
engine.Close();

engine = io.Open(filename, adios2::Mode::Read);
for (int step = 0; step < NSteps; step++)
{
engine.BeginStep();
auto g = io.InquireGroup("group1", '/');
auto g = io.InquireGroup('/');
auto res = g.AvailableGroups();
EXPECT_EQ(res[0], "group2");
EXPECT_EQ(res[0], "group1");
res = g.AvailableVariables();
EXPECT_EQ(res[0], "variable6");
g.setPath("group1/group2");
res = g.AvailableGroups();
EXPECT_EQ(res[0], "group3");
Expand All @@ -92,6 +106,44 @@ TEST_F(ADIOSHierarchicalReadVariableTest, Read)
EXPECT_EQ(res.size(), 0);
engine.EndStep();
}
for (int step = 0; step < NSteps; step++)
{
engine.BeginStep();
auto g = io.InquireGroup('/');
auto res = g.AvailableGroups();
EXPECT_EQ(res[0], "group1");
res = g.AvailableVariables();
EXPECT_EQ(res[0], "variable6");
engine.EndStep();
}
for (int step = 0; step < NSteps; step++)
{
auto g = io.InquireGroup('/');
auto var = g.InquireVariable<int32_t>("variable6");
EXPECT_TRUE(var);
if (var)
{
std::vector<int32_t> myInts;
var.SetSelection({{Nx * rank}, {Nx}});
engine.Get<int32_t>(var, myInts, adios2::Mode::Sync);
EXPECT_EQ(Ints, myInts);
}
}
for (int step = 0; step < NSteps; step++)
{
auto g = io.InquireGroup('/');
g.setPath("group1/group2/group3/group4");
auto var = g.InquireVariable<int32_t>("variable1");
EXPECT_TRUE(var);
if (var)
{
std::vector<int32_t> myInts;
var.SetSelection({{Nx * rank}, {Nx}});
engine.Get<int32_t>(var, myInts, adios2::Mode::Sync);

EXPECT_EQ(Ints, myInts);
}
}
engine.Close();
}
}
Expand Down