Skip to content

Commit a3a9b17

Browse files
committed
Update parser to store Binsparse JSON metadata in attribute instead of
dataset
1 parent f45c932 commit a3a9b17

File tree

3 files changed

+55
-23
lines changed

3 files changed

+55
-23
lines changed

examples/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ CXX ?= clang++
1111
BINSPARSE_DIR ?= ../include
1212

1313
# HDF5 library location
14-
HDF5_CXXFLAGS ?= -I/opt/homebrew/Cellar/hdf5/1.14.1/include
15-
HDF5_LIBRARY_FLAGS ?= -L/opt/homebrew/Cellar/hdf5/1.14.1/lib -lhdf5_hl_cpp -lhdf5_cpp -lhdf5_hl -lhdf5
14+
HDF5_DIR ?= /opt/homebrew/Cellar/hdf5/1.14.2
15+
HDF5_CXXFLAGS ?= -I$(HDF5_DIR)/include
16+
HDF5_LIBRARY_FLAGS ?= -L$(HDF5_DIR)/lib -lhdf5_hl_cpp -lhdf5_cpp -lhdf5_hl -lhdf5
1617

1718
# = = = = = = = = = = = = = = = = = = = = = = = = = = = #
1819

include/binsparse/binsparse.hpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include <binsparse/c_bindings/allocator_wrapper.hpp>
1212
#include <binsparse/matrix_market/matrix_market.hpp>
1313

14-
#include <iostream>
15-
1614
namespace binsparse {
1715

1816
// CSR Format
@@ -46,7 +44,7 @@ void write_csr_matrix(std::string fname,
4644
j[v.key()] = v.value();
4745
}
4846

49-
hdf5_tools::write_dataset(f, "metadata", j.dump(2));
47+
hdf5_tools::set_attribute(f, "binsparse", j.dump(2));
5048

5149
f.close();
5250
}
@@ -60,10 +58,12 @@ csr_matrix<T, I> read_csr_matrix(std::string fname, Allocator&& alloc) {
6058
using json = nlohmann::json;
6159
auto data = json::parse(metadata);
6260

63-
if (data["binsparse"]["format"] == "CSR") {
64-
auto nrows = data["binsparse"]["shape"][0];
65-
auto ncols = data["binsparse"]["shape"][1];
66-
auto nnz = data["binsparse"]["nnz"];
61+
auto binsparse_metadata = data["binsparse"];
62+
63+
if (binsparse_metadata["format"] == "CSR") {
64+
auto nrows = binsparse_metadata["shape"][0];
65+
auto ncols = binsparse_metadata["shape"][1];
66+
auto nnz = binsparse_metadata["nnz"];
6767

6868
typename std::allocator_traits<std::remove_cvref_t<Allocator>>
6969
:: template rebind_alloc<I> i_alloc(alloc);
@@ -114,7 +114,7 @@ void write_coo_matrix(std::string fname,
114114
j[v.key()] = v.value();
115115
}
116116

117-
hdf5_tools::write_dataset(f, "metadata", j.dump(2));
117+
hdf5_tools::set_attribute(f, "binsparse", j.dump(2));
118118

119119
f.close();
120120
}
@@ -128,10 +128,12 @@ coo_matrix<T, I> read_coo_matrix(std::string fname, Allocator&& alloc) {
128128
using json = nlohmann::json;
129129
auto data = json::parse(metadata);
130130

131-
if (data["binsparse"]["format"] == "COO") {
132-
auto nrows = data["binsparse"]["shape"][0];
133-
auto ncols = data["binsparse"]["shape"][1];
134-
auto nnz = data["binsparse"]["nnz"];
131+
auto binsparse_metadata = data["binsparse"];
132+
133+
if (binsparse_metadata["format"] == "COO") {
134+
auto nrows = binsparse_metadata["shape"][0];
135+
auto ncols = binsparse_metadata["shape"][1];
136+
auto nnz = binsparse_metadata["nnz"];
135137

136138
typename std::allocator_traits<std::remove_cvref_t<Allocator>>
137139
:: template rebind_alloc<I> i_alloc(alloc);
@@ -154,12 +156,14 @@ coo_matrix<T, I> read_coo_matrix(std::string fname) {
154156
inline auto inspect(std::string fname) {
155157
H5::H5File f(fname.c_str(), H5F_ACC_RDWR);
156158

157-
auto metadata = hdf5_tools::read_dataset<char>(f, "metadata");
159+
auto metadata = hdf5_tools::get_attribute(f, "binsparse");
158160

159161
using json = nlohmann::json;
160162
auto data = json::parse(metadata);
161163

162-
if (data["binsparse"]["version"] >= 0.1) {
164+
auto binsparse_metadata = data["binsparse"];
165+
166+
if (binsparse_metadata["version"] >= 0.1) {
163167
return data;
164168
} else {
165169
assert(false);

include/binsparse/hdf5_tools.hpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <ranges>
66
#include <H5Cpp.h>
77

8+
#include <iostream>
9+
810
namespace hdf5_tools {
911

1012
template <typename U>
@@ -119,8 +121,8 @@ inline H5::PredType get_type(H5::DataSet& dataset) {
119121
}
120122
}
121123

122-
template <std::ranges::contiguous_range R>
123-
void write_dataset(H5::H5File& f, const std::string& label, R&& r) {
124+
template <typename H5GroupOrFile, std::ranges::contiguous_range R>
125+
void write_dataset(H5GroupOrFile& f, const std::string& label, R&& r) {
124126
using T = std::ranges::range_value_t<R>;
125127
hsize_t size = std::ranges::size(r);
126128
H5::DataSpace dataspace(1, &size);
@@ -131,8 +133,32 @@ void write_dataset(H5::H5File& f, const std::string& label, R&& r) {
131133
dataspace.close();
132134
}
133135

134-
template <typename T, typename Allocator>
135-
std::span<T> read_dataset(H5::H5File& f, const std::string& label, Allocator&& alloc) {
136+
inline std::string get_attribute(H5::H5Object& f, const std::string& key) {
137+
auto attribute = f.openAttribute(key.c_str());
138+
139+
std::size_t size = attribute.getStorageSize();
140+
141+
auto type = attribute.getDataType();
142+
std::string attribute_string(" ", size);
143+
144+
attribute.read(type, attribute_string);
145+
146+
return attribute_string;
147+
}
148+
149+
inline void set_attribute(H5::H5Object& f, const std::string& key, const std::string& value) {
150+
hsize_t size = value.size();
151+
H5::DataSpace dataspace(1, &size);
152+
153+
f.createAttribute(key.c_str(), H5::PredType::NATIVE_CHAR, dataspace);
154+
155+
auto attribute = f.openAttribute(key.c_str());
156+
157+
attribute.write(H5::PredType::NATIVE_CHAR, value.data());
158+
}
159+
160+
template <typename T, typename Allocator, typename H5GroupOrFile>
161+
std::span<T> read_dataset(H5GroupOrFile& f, const std::string& label, Allocator&& alloc) {
136162
H5::DataSet dataset = f.openDataSet(label.c_str());
137163

138164
H5::DataSpace space = dataset.getSpace();
@@ -148,12 +174,13 @@ std::span<T> read_dataset(H5::H5File& f, const std::string& label, Allocator&& a
148174
return std::span<T>(data, dims);
149175
}
150176

151-
template <typename T>
152-
std::span<T> read_dataset(H5::H5File& f, const std::string& label) {
177+
template <typename T, typename H5GroupOrFile>
178+
std::span<T> read_dataset(H5GroupOrFile& f, const std::string& label) {
153179
return read_dataset<T>(f, label, std::allocator<T>{});
154180
}
155181

156-
inline H5::PredType dataset_type(H5::H5File& f, const std::string& label) {
182+
template <typename H5GroupOrFile>
183+
inline H5::PredType dataset_type(H5GroupOrFile& f, const std::string& label) {
157184
H5::DataSet dataset = f.openDataSet(label.c_str());
158185
auto type = get_type(dataset);
159186
dataset.close();

0 commit comments

Comments
 (0)