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

New property: enable setting attribute phase change thresholds. #785

Merged
merged 2 commits into from
Jul 10, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions include/highfive/H5PropertyList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,41 @@ class LinkCreationOrder {
unsigned _flags;
};


///
/// \brief Set threshold for attribute storage.
///
/// HDF5 can store Attributes in the object header (compact) or in the B-tree
/// (dense). This property sets the threshold when attributes are moved to one
/// or the other storage format.
///
/// Please refer to the upstream documentation of `H5Pset_attr_phase_change` or
/// Section 8 (Attributes) in the User Guide, in particular Subsection 8.5.
///
class AttributePhaseChange {
public:
///
/// \brief Create the property from the threshold values.
///
/// When the number of attributes hits `max_compact` the attributes are
/// moved to dense storage, once the number drops to below `min_dense` the
/// attributes are moved to compact storage.
AttributePhaseChange(unsigned max_compact, unsigned min_dense);

/// \brief Extract threshold values from property list.
explicit AttributePhaseChange(const GroupCreateProps& gcpl);

unsigned max_compact() const;
unsigned min_dense() const;

private:
friend GroupCreateProps;
void apply(hid_t hid) const;

unsigned _max_compact;
unsigned _min_dense;
};

/// @}

} // namespace HighFive
Expand Down
28 changes: 28 additions & 0 deletions include/highfive/bits/H5PropertyList_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,32 @@
"Error getting property for link creation order");
}
}

inline AttributePhaseChange::AttributePhaseChange(unsigned max_compact, unsigned min_dense)
: _max_compact(max_compact)
, _min_dense(min_dense) {}

inline AttributePhaseChange::AttributePhaseChange(const GroupCreateProps& gcpl) {
if (H5Pget_attr_phase_change(gcpl.getId(), &_max_compact, &_min_dense) < 0) {
HDF5ErrMapper::ToException<PropertyException>(

Check warning on line 553 in include/highfive/bits/H5PropertyList_misc.hpp

View check run for this annotation

Codecov / codecov/patch

include/highfive/bits/H5PropertyList_misc.hpp#L553

Added line #L553 was not covered by tests
"Error getting property for attribute phase change");
}
}

inline unsigned AttributePhaseChange::max_compact() const {
return _max_compact;
}

inline unsigned AttributePhaseChange::min_dense() const {
return _min_dense;
}

inline void AttributePhaseChange::apply(hid_t hid) const {
if (H5Pset_attr_phase_change(hid, _max_compact, _min_dense) < 0) {
HDF5ErrMapper::ToException<PropertyException>(
"Error getting property for attribute phase change");
}
}


} // namespace HighFive
20 changes: 20 additions & 0 deletions src/examples/create_large_attribute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <H5Fpublic.h>
#include <highfive/H5File.hpp>
#include <highfive/H5Group.hpp>
#include <numeric>
#include <vector>

int main() {
std::vector<double> large_attr(16000, 0.0);

Check warning on line 8 in src/examples/create_large_attribute.cpp

View check run for this annotation

Codecov / codecov/patch

src/examples/create_large_attribute.cpp#L7-L8

Added lines #L7 - L8 were not covered by tests

auto fapl = HighFive::FileAccessProps::Default();
fapl.add(HighFive::FileVersionBounds(H5F_LIBVER_LATEST, H5F_LIBVER_LATEST));
HighFive::File file("create_large_attribute.h5", HighFive::File::Truncate, fapl);
auto gcpl = HighFive::GroupCreateProps::Default();
gcpl.add(HighFive::AttributePhaseChange(0, 0));

Check warning on line 14 in src/examples/create_large_attribute.cpp

View check run for this annotation

Codecov / codecov/patch

src/examples/create_large_attribute.cpp#L10-L14

Added lines #L10 - L14 were not covered by tests

auto group = file.createGroup("grp", gcpl);
group.createAttribute("attr", large_attr);

Check warning on line 17 in src/examples/create_large_attribute.cpp

View check run for this annotation

Codecov / codecov/patch

src/examples/create_large_attribute.cpp#L16-L17

Added lines #L16 - L17 were not covered by tests

return 0;

Check warning on line 19 in src/examples/create_large_attribute.cpp

View check run for this annotation

Codecov / codecov/patch

src/examples/create_large_attribute.cpp#L19

Added line #L19 was not covered by tests
}
28 changes: 28 additions & 0 deletions tests/unit/tests_high_five_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,34 @@ TEMPLATE_LIST_TEST_CASE("ReadWriteAttributeVector", "[template]", dataset_test_t
readWriteAttributeVectorTest<TestType>();
}

TEST_CASE("WriteLargeAttribute") {
std::vector<double> large_attr(16000, 0.0);

auto fapl = HighFive::FileAccessProps::Default();
fapl.add(HighFive::FileVersionBounds(H5F_LIBVER_LATEST, H5F_LIBVER_LATEST));
HighFive::File file("create_large_attribute.h5", HighFive::File::Truncate, fapl);
auto gcpl = HighFive::GroupCreateProps::Default();
gcpl.add(HighFive::AttributePhaseChange(0, 0));

auto group = file.createGroup("grp", gcpl);
CHECK_NOTHROW(group.createAttribute("attr", large_attr));
}

TEST_CASE("AttributePhaseChange") {
auto fapl = HighFive::FileAccessProps::Default();
fapl.add(HighFive::FileVersionBounds(H5F_LIBVER_LATEST, H5F_LIBVER_LATEST));
HighFive::File file("attribute_phase_change.h5", HighFive::File::Truncate, fapl);

auto gcpl = HighFive::GroupCreateProps::Default();
gcpl.add(HighFive::AttributePhaseChange(42, 24));

auto group = file.createGroup("grp", gcpl);

auto actual = AttributePhaseChange(group.getCreatePropertyList());
CHECK(actual.min_dense() == 24);
CHECK(actual.max_compact() == 42);
}

TEST_CASE("datasetOffset") {
std::string filename = "datasetOffset.h5";
std::string dsetname = "dset";
Expand Down