Skip to content

Commit 7898a21

Browse files
committed
Support writing to groups
1 parent 8e9ef66 commit 7898a21

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

examples/convert_binsparse.cpp

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,32 @@
55

66
template <typename T, typename I>
77
void convert_to_binsparse(std::string input_file, std::string output_file,
8-
std::string format, std::string comment) {
8+
std::string format, std::string comment,
9+
std::optional<std::string> group = {}) {
10+
H5::H5File file;
11+
std::unique_ptr<H5::Group> f_p;
12+
13+
if (!group.has_value()) {
14+
f_p = std::unique_ptr<H5::Group>(
15+
new H5::H5File(output_file.c_str(), H5F_ACC_TRUNC));
16+
} else {
17+
file = H5::H5File(output_file.c_str(), H5F_ACC_RDWR);
18+
H5::Group g = file.createGroup(group.value().c_str());
19+
f_p = std::unique_ptr<H5::Group>(new H5::Group(g));
20+
}
21+
22+
H5::Group& f = *f_p;
23+
924
nlohmann::json user_keys;
1025
user_keys["comment"] = comment;
1126
if (format == "CSR") {
12-
std::cout << "Reading in " << input_file << "...\n";
1327
auto x = binsparse::__detail::mmread<
1428
T, I, binsparse::__detail::csr_matrix_owning<T, I>>(input_file);
1529
binsparse::csr_matrix<T, I> matrix{
1630
x.values().data(), x.colind().data(), x.rowptr().data(),
1731
std::get<0>(x.shape()), std::get<1>(x.shape()), I(x.size()),
1832
x.structure()};
19-
binsparse::write_csr_matrix(output_file, matrix, user_keys);
33+
binsparse::write_csr_matrix(f, matrix, user_keys);
2034
std::cout << "Writing to binsparse file " << output_file << " using "
2135
<< format << " format...\n";
2236
} else {
@@ -26,7 +40,7 @@ void convert_to_binsparse(std::string input_file, std::string output_file,
2640
x.values().data(), x.rowind().data(), x.colind().data(),
2741
std::get<0>(x.shape()), std::get<1>(x.shape()), I(x.size()),
2842
x.structure()};
29-
binsparse::write_coo_matrix(output_file, matrix, user_keys);
43+
binsparse::write_coo_matrix(f, matrix, user_keys);
3044
std::cout << "Writing to binsparse file " << output_file << " using "
3145
<< format << " format...\n";
3246
}
@@ -35,32 +49,38 @@ void convert_to_binsparse(std::string input_file, std::string output_file,
3549
template <typename I>
3650
void convert_to_binsparse(std::string input_file, std::string output_file,
3751
std::string type, std::string format,
38-
std::string comment) {
52+
std::string comment,
53+
std::optional<std::string> group = {}) {
3954
if (type == "real") {
40-
convert_to_binsparse<float, I>(input_file, output_file, format, comment);
55+
convert_to_binsparse<float, I>(input_file, output_file, format, comment,
56+
group);
4157
} else if (type == "complex") {
4258
assert(false);
4359
// convert_to_binsparse<std::complex<float>, I>(input_file, output_file,
4460
// format, comment);
4561
} else if (type == "integer") {
46-
convert_to_binsparse<int64_t, I>(input_file, output_file, format, comment);
62+
convert_to_binsparse<int64_t, I>(input_file, output_file, format, comment,
63+
group);
4764
} else if (type == "pattern") {
48-
convert_to_binsparse<uint8_t, I>(input_file, output_file, format, comment);
65+
convert_to_binsparse<uint8_t, I>(input_file, output_file, format, comment,
66+
group);
4967
}
5068
}
5169

5270
int main(int argc, char** argv) {
5371

5472
if (argc < 3) {
5573
std::cout << "usage: ./convert_binsparse [input_file.mtx] "
56-
"[output_file.hdf5] [optional: format {CSR, COO}]\n";
74+
"[output_file.hdf5] [optional: format {CSR, COO}] [optional: "
75+
"HDF5 group name]\n";
5776
return 1;
5877
}
5978

6079
std::string input_file(argv[1]);
6180
std::string output_file(argv[2]);
6281

6382
std::string format;
83+
std::optional<std::string> group;
6484

6585
if (argc >= 4) {
6686
format = argv[3];
@@ -72,6 +92,10 @@ int main(int argc, char** argv) {
7292
format = "COO";
7393
}
7494

95+
if (argc >= 5) {
96+
group = argv[4];
97+
}
98+
7599
auto [m, n, nnz, type, structure, comment] =
76100
binsparse::mmread_metadata(input_file);
77101

@@ -88,16 +112,16 @@ int main(int argc, char** argv) {
88112

89113
if (max_size + 1 <= std::numeric_limits<uint8_t>::max()) {
90114
convert_to_binsparse<uint8_t>(input_file, output_file, type, format,
91-
comment);
115+
comment, group);
92116
} else if (max_size + 1 <= std::numeric_limits<uint16_t>::max()) {
93117
convert_to_binsparse<uint16_t>(input_file, output_file, type, format,
94-
comment);
118+
comment, group);
95119
} else if (max_size + 1 <= std::numeric_limits<uint32_t>::max()) {
96120
convert_to_binsparse<uint32_t>(input_file, output_file, type, format,
97-
comment);
121+
comment, group);
98122
} else if (max_size + 1 <= std::numeric_limits<uint64_t>::max()) {
99123
convert_to_binsparse<uint64_t>(input_file, output_file, type, format,
100-
comment);
124+
comment, group);
101125
} else {
102126
throw std::runtime_error(
103127
"Error! Matrix dimensions or NNZ too large to handle.");

include/binsparse/binsparse.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ auto read_dense_matrix(std::string fname, Allocator&& alloc = Allocator{}) {
6262
using json = nlohmann::json;
6363
auto data = json::parse(metadata);
6464

65-
std::cout << "Reading values...\n";
6665
auto binsparse_metadata = data["binsparse"];
6766

6867
auto format = __detail::unalias_format(binsparse_metadata["format"]);

0 commit comments

Comments
 (0)