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

Feature/capps2/sidre group save #104

Merged
merged 4 commits into from
Sep 20, 2019
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
28 changes: 11 additions & 17 deletions src/axom/sidre/core/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,10 +1249,14 @@ void Group::copyToConduitNode(Node& n) const
*
*************************************************************************
*/
bool Group::isEquivalentTo(const Group* other) const
bool Group::isEquivalentTo(const Group* other, bool checkName) const
{
// Equality of names
bool is_equiv = (m_name == other->m_name);
bool is_equiv = true;
if (checkName)
{
is_equiv = (m_name == other->m_name);
}

// Sizes of collections of child items must be equal
if (is_equiv)
Expand Down Expand Up @@ -1902,34 +1906,24 @@ bool Group::exportTo(conduit::Node& result,
{
result.remove("views");
}

}

bool hasSavedGroups = false;
if (getNumGroups() > 0)
{
bool hasSavedGroups = false;
Node & gnode = result["groups"];
IndexType gidx = getFirstValidGroupIndex();
while ( indexIsValid(gidx) )
{
const Group* group = getGroup(gidx);
Node& n_group = gnode.fetch(group->getName());
if ( group->exportTo(n_group, attr, buffer_indices) )
{
hasSavedGroups = true;
}
else
{
gnode.remove(group->getName());
}
bool hsv = group->exportTo(n_group, attr, buffer_indices);
hasSavedViews = hasSavedViews || hsv;
hasSavedGroups = true;

gidx = getNextValidGroupIndex(gidx);
}
if (hasSavedGroups)
{
hasSavedViews = true;
}
else
if (!hasSavedGroups)
{
result.remove("groups");
}
Expand Down
6 changes: 5 additions & 1 deletion src/axom/sidre/core/Group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,13 @@ class Group
* Group hierarchy structures with the same names for all Views and
* Groups in the hierarchy, and the Views are also equivalent.
*
* \param other The group to compare with
* \param checkName If true (default), groups must have the same name
* to be equivalent. If false, disregard the name.
*
* \sa View::isEquivalentTo()
*/
bool isEquivalentTo(const Group* other) const;
bool isEquivalentTo(const Group* other, bool checkName = true) const;
agcapps marked this conversation as resolved.
Show resolved Hide resolved


//@{
Expand Down
153 changes: 153 additions & 0 deletions src/axom/sidre/tests/sidre_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "axom/config.hpp" // for AXOM_USE_HDF5
#include "axom/sidre/core/sidre.hpp"
#include "axom/core/utilities/FileUtilities.hpp"

#include "gtest/gtest.h"

Expand Down Expand Up @@ -1157,6 +1158,158 @@ TEST(sidre_group,save_load_via_hdf5_ids)
// close hdf5 handle
EXPECT_TRUE(H5Fclose(h5_id) >=0);
}

//------------------------------------------------------------------------------
TEST(sidre_group,save_root_restore_as_child)
{
// We'll save the DataStore's root Group into a file then restore it
// into a child group of another DataStore.

// Create a DataStore; put some groups and views into it
const std::string file_path_base("sidre_save_root_restore_as_child_");
DataStore* ds = new DataStore();
Group* root = ds->getRoot();
Group* child1 = root->createGroup("g_a");
Group* child2 = root->createGroup("g_b");
root->createGroup("g_c"); // We don't put anything into g_c

child1->createViewScalar<int>("i0", 1);
child1->createViewString("s0", "I am a string");

const int num_elems = 4;
int * pa0 =
child2->createViewAndAllocate("a0", INT_ID, num_elems)->getArray();
double * pa1 =
child2->createViewAndAllocate("a1", FLOAT64_ID, num_elems)->getArray();

const double factor = 2.3;
const double offset = -0.23;
for (int i = 0; i < num_elems; ++i)
{
pa0[i] = i;
pa1[i] = offset + i*factor;
}

// Save the DataStore's root
for (int i = 0 ; i < nprotocols ; ++i)
{
const std::string file_path = file_path_base + protocols[i];
root->save(file_path, protocols[i]);
}

// Restore the original DataStore into a child group
for (int i = 0; i < nprotocols; ++i)
{
// Only restore sidre_hdf5 protocol
agcapps marked this conversation as resolved.
Show resolved Hide resolved
if(protocols[i] != "sidre_hdf5")
{
continue;
}

DataStore *dscopy = new DataStore();
Group * dsroot = dscopy->getRoot();
const std::string file_path = file_path_base + protocols[i];

const std::string group_base("group_");
const std::string group_name = group_base + protocols[i];
Group* cg = dsroot->createGroup(group_name);

if (axom::utilities::filesystem::pathExists(file_path))
{
std::cout << "loading " << file_path << std::endl;
cg->load(file_path, protocols[i]);

EXPECT_TRUE(cg->isEquivalentTo(root, false));
EXPECT_TRUE(root->isEquivalentTo(cg, false));
}
else
{
FAIL() << "file not present: " << file_path;
}

delete dscopy;
}

delete ds;
}

//------------------------------------------------------------------------------
TEST(sidre_group,save_child_restore_as_root)
{
// We'll save a child Group into a file then restore it as the root of
// a new DataStore.

// Create a DataStore; put some groups and views into it
const std::string file_path_base("sidre_save_child_restore_as_root_");
DataStore* ds = new DataStore();
Group* root = ds->getRoot();
agcapps marked this conversation as resolved.
Show resolved Hide resolved
// child1 and all its descendents will get saved into files
Group* child1 = root->createGroup("g_a");
Group* child2 = child1->createGroup("g_b");
child1->createViewScalar<int>("i0", 1);
child1->createViewString("s0", "I am a string");
// everything else won't be put into the files
Group* child1a = root->createGroup("g_notSaved");
child1a->createViewScalar<int>("i1", 42);
root->createViewString("s1", "string view off the root, not saved");

const int num_elems = 4;
// included in files
int * pa0 =
child2->createViewAndAllocate("a0", INT_ID, num_elems)->getArray();
double * pa1 =
child2->createViewAndAllocate("a1", FLOAT64_ID, num_elems)->getArray();
// not included
int *pa2 =
child1a->createViewAndAllocate("a2", INT_ID, num_elems)->getArray();
int *pa3 =
root->createViewAndAllocate("a3", INT_ID, num_elems)->getArray();

const double factor = 2.3;
const double offset = -0.23;
for (int i = 0; i < num_elems; ++i)
{
pa0[i] = i;
pa1[i] = offset + i*factor;
pa2[i] = i + 2;
pa3[i] = 4 - i;
}

// Save the Group in question (child1) into an archive
for (int i = 0; i < nprotocols; ++i)
{
const std::string file_path = file_path_base + protocols[i];
child1->save(file_path, protocols[i]);
}

// Restore the saved child1 into a root group
for (int i = 0; i < nprotocols; ++i)
{
// Only restore sidre_hdf5 protocol
agcapps marked this conversation as resolved.
Show resolved Hide resolved
if(protocols[i] != "sidre_hdf5")
{
continue;
}

DataStore * dscopy = new DataStore();
const std::string file_path = file_path_base + protocols[i];
if (axom::utilities::filesystem::pathExists(file_path))
{
dscopy->getRoot()->load(file_path, protocols[i]);

EXPECT_TRUE(dscopy->getRoot()->isEquivalentTo(child1, false));
agcapps marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_TRUE(child1->isEquivalentTo(dscopy->getRoot(), false));
}
else
{
FAIL() << "file not present: " << file_path;
}

delete dscopy;
}

delete ds;
}
#endif // AXOM_USE_HDF5

//------------------------------------------------------------------------------
Expand Down