Skip to content

Commit 8d66cfe

Browse files
authored
Updates for the most recent Binsparse specification (#3)
* Update `nnz` -> `number_of_stored_values` * Updates to conform with current version of Binsparse * Update for Mac OS.
1 parent 83dabc6 commit 8d66cfe

File tree

4 files changed

+77
-25
lines changed

4 files changed

+77
-25
lines changed

examples/convert_matrixmarket.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
#include <iostream>
55

66
int main(int argc, char** argv) {
7-
87
if (argc < 2) {
9-
std::cout << "usage: ./inspect_binsparse [matrix.bsp.h5]\n";
8+
std::cout << "usage: ./convert_binsparse [matrix.bsp.h5]\n";
109
return 1;
1110
}
1211

@@ -18,8 +17,8 @@ int main(int argc, char** argv) {
1817

1918
std::cout << "Inspecting Binsparse v" << metadata["version"] << " file...\n";
2019
std::cout << metadata["format"] << " format matrix of dimension "
21-
<< metadata["shape"] << " with " << metadata["nnz"]
22-
<< " nonzeros\n";
20+
<< metadata["shape"] << " with "
21+
<< metadata["number_of_stored_values"] << " nonzeros\n";
2322

2423
if (metadata["format"] == "COO") {
2524
auto i0 = metadata["data_types"]["indices_0"];

examples/inspect_binsparse.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ int main(int argc, char** argv) {
2121

2222
std::cout << "Inspecting Binsparse v" << metadata["version"] << " file...\n";
2323
std::cout << metadata["format"] << " format matrix of dimension "
24-
<< metadata["shape"] << " with " << metadata["nnz"]
25-
<< " nonzeros\n";
24+
<< metadata["shape"] << " with "
25+
<< metadata["number_of_stored_values"] << " nonzeros\n";
2626

2727
if (metadata["format"] == "COO") {
2828
auto i0 = metadata["data_types"]["indices_0"];

include/binsparse/binsparse.hpp

+68-19
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace binsparse {
1515

16-
inline constexpr double version = 0.1;
16+
inline const std::string version = "0.1";
1717

1818
template <typename T>
1919
void write_dense_vector(H5::Group& f, std::span<T> v,
@@ -25,7 +25,7 @@ void write_dense_vector(H5::Group& f, std::span<T> v,
2525
j["binsparse"]["version"] = version;
2626
j["binsparse"]["format"] = "DVEC";
2727
j["binsparse"]["shape"] = {v.size()};
28-
j["binsparse"]["nnz"] = v.size();
28+
j["binsparse"]["number_of_stored_values"] = v.size();
2929
j["binsparse"]["data_types"]["values"] = type_info<T>::label();
3030

3131
for (auto&& v : user_keys.items()) {
@@ -51,7 +51,7 @@ auto read_dense_vector(std::string fname, Allocator&& alloc = Allocator{}) {
5151
assert(format == "DVEC");
5252

5353
auto nvalues = binsparse_metadata["shape"][0];
54-
auto nnz = binsparse_metadata["nnz"];
54+
auto nnz = binsparse_metadata["number_of_stored_values"];
5555

5656
assert(nvalues == nnz);
5757

@@ -76,8 +76,14 @@ void write_dense_matrix(H5::Group& f, dense_matrix<T, I, Order> m,
7676
j["binsparse"]["version"] = version;
7777
j["binsparse"]["format"] = __detail::get_matrix_format_string(m);
7878
j["binsparse"]["shape"] = {m.m, m.n};
79-
j["binsparse"]["nnz"] = m.m * m.n;
80-
j["binsparse"]["data_types"]["values"] = type_info<T>::label();
79+
j["binsparse"]["number_of_stored_values"] = m.m * m.n;
80+
81+
if (!m.is_iso) {
82+
j["binsparse"]["data_types"]["values"] =
83+
std::string("iso[") + type_info<T>::label() + "]";
84+
} else {
85+
j["binsparse"]["data_types"]["values"] = type_info<T>::label();
86+
}
8187

8288
if (m.structure != general) {
8389
j["binsparse"]["structure"] =
@@ -118,7 +124,13 @@ auto read_dense_matrix(std::string fname, Allocator&& alloc = Allocator{}) {
118124

119125
auto nrows = binsparse_metadata["shape"][0];
120126
auto ncols = binsparse_metadata["shape"][1];
121-
auto nnz = binsparse_metadata["nnz"];
127+
auto nnz = binsparse_metadata["number_of_stored_values"];
128+
129+
bool is_iso = false;
130+
if (std::string(binsparse_metadata["data_types"]["values"])
131+
.starts_with("iso")) {
132+
is_iso = true;
133+
}
122134

123135
auto values = hdf5_tools::read_dataset<T>(f, "values", alloc);
124136

@@ -128,7 +140,8 @@ auto read_dense_matrix(std::string fname, Allocator&& alloc = Allocator{}) {
128140
structure = __detail::parse_structure(binsparse_metadata["structure"]);
129141
}
130142

131-
return dense_matrix<T, I, Order>{values.data(), nrows, ncols, structure};
143+
return dense_matrix<T, I, Order>{values.data(), nrows, ncols, structure,
144+
is_iso};
132145
}
133146

134147
// CSR Format
@@ -149,10 +162,16 @@ void write_csr_matrix(H5::Group& f, csr_matrix<T, I> m,
149162
j["binsparse"]["version"] = version;
150163
j["binsparse"]["format"] = "CSR";
151164
j["binsparse"]["shape"] = {m.m, m.n};
152-
j["binsparse"]["nnz"] = m.nnz;
165+
j["binsparse"]["number_of_stored_values"] = m.nnz;
153166
j["binsparse"]["data_types"]["pointers_to_1"] = type_info<I>::label();
154167
j["binsparse"]["data_types"]["indices_1"] = type_info<I>::label();
155-
j["binsparse"]["data_types"]["values"] = type_info<T>::label();
168+
169+
if (!m.is_iso) {
170+
j["binsparse"]["data_types"]["values"] =
171+
std::string("iso[") + type_info<T>::label() + "]";
172+
} else {
173+
j["binsparse"]["data_types"]["values"] = type_info<T>::label();
174+
}
156175

157176
if (m.structure != general) {
158177
j["binsparse"]["structure"] =
@@ -189,7 +208,13 @@ csr_matrix<T, I> read_csr_matrix(std::string fname, Allocator&& alloc) {
189208

190209
auto nrows = binsparse_metadata["shape"][0];
191210
auto ncols = binsparse_metadata["shape"][1];
192-
auto nnz = binsparse_metadata["nnz"];
211+
auto nnz = binsparse_metadata["number_of_stored_values"];
212+
213+
bool is_iso = false;
214+
if (std::string(binsparse_metadata["data_types"]["values"])
215+
.starts_with("iso")) {
216+
is_iso = true;
217+
}
193218

194219
typename std::allocator_traits<
195220
std::remove_cvref_t<Allocator>>::template rebind_alloc<I>
@@ -206,7 +231,7 @@ csr_matrix<T, I> read_csr_matrix(std::string fname, Allocator&& alloc) {
206231
}
207232

208233
return csr_matrix<T, I>{values.data(), colind.data(), row_ptr.data(), nrows,
209-
ncols, nnz, structure};
234+
ncols, nnz, structure, is_iso};
210235
}
211236

212237
template <typename T, typename I>
@@ -232,10 +257,16 @@ void write_csc_matrix(H5::Group& f, csc_matrix<T, I> m,
232257
j["binsparse"]["version"] = version;
233258
j["binsparse"]["format"] = "CSR";
234259
j["binsparse"]["shape"] = {m.m, m.n};
235-
j["binsparse"]["nnz"] = m.nnz;
260+
j["binsparse"]["number_of_stored_values"] = m.nnz;
236261
j["binsparse"]["data_types"]["pointers_to_1"] = type_info<I>::label();
237262
j["binsparse"]["data_types"]["indices_1"] = type_info<I>::label();
238-
j["binsparse"]["data_types"]["values"] = type_info<T>::label();
263+
264+
if (!m.is_iso) {
265+
j["binsparse"]["data_types"]["values"] =
266+
std::string("iso[") + type_info<T>::label() + "]";
267+
} else {
268+
j["binsparse"]["data_types"]["values"] = type_info<T>::label();
269+
}
239270

240271
if (m.structure != general) {
241272
j["binsparse"]["structure"] =
@@ -272,7 +303,13 @@ csc_matrix<T, I> read_csc_matrix(std::string fname, Allocator&& alloc) {
272303

273304
auto nrows = binsparse_metadata["shape"][0];
274305
auto ncols = binsparse_metadata["shape"][1];
275-
auto nnz = binsparse_metadata["nnz"];
306+
auto nnz = binsparse_metadata["number_of_stored_values"];
307+
308+
bool is_iso = false;
309+
if (std::string(binsparse_metadata["data_types"]["values"])
310+
.starts_with("iso")) {
311+
is_iso = true;
312+
}
276313

277314
typename std::allocator_traits<
278315
std::remove_cvref_t<Allocator>>::template rebind_alloc<I>
@@ -289,7 +326,7 @@ csc_matrix<T, I> read_csc_matrix(std::string fname, Allocator&& alloc) {
289326
}
290327

291328
return csc_matrix<T, I>{values.data(), rowind.data(), col_ptr.data(), nrows,
292-
ncols, nnz, structure};
329+
ncols, nnz, structure, is_iso};
293330
}
294331

295332
template <typename T, typename I>
@@ -315,10 +352,16 @@ void write_coo_matrix(H5::Group& f, coo_matrix<T, I> m,
315352
j["binsparse"]["version"] = version;
316353
j["binsparse"]["format"] = "COO";
317354
j["binsparse"]["shape"] = {m.m, m.n};
318-
j["binsparse"]["nnz"] = m.nnz;
355+
j["binsparse"]["number_of_stored_values"] = m.nnz;
319356
j["binsparse"]["data_types"]["indices_0"] = type_info<I>::label();
320357
j["binsparse"]["data_types"]["indices_1"] = type_info<I>::label();
321-
j["binsparse"]["data_types"]["values"] = type_info<T>::label();
358+
359+
if (!m.is_iso) {
360+
j["binsparse"]["data_types"]["values"] =
361+
std::string("iso[") + type_info<T>::label() + "]";
362+
} else {
363+
j["binsparse"]["data_types"]["values"] = type_info<T>::label();
364+
}
322365

323366
if (m.structure != general) {
324367
j["binsparse"]["structure"] =
@@ -357,7 +400,13 @@ coo_matrix<T, I> read_coo_matrix(std::string fname, Allocator&& alloc) {
357400

358401
auto nrows = binsparse_metadata["shape"][0];
359402
auto ncols = binsparse_metadata["shape"][1];
360-
auto nnz = binsparse_metadata["nnz"];
403+
auto nnz = binsparse_metadata["number_of_stored_values"];
404+
405+
bool is_iso = false;
406+
if (std::string(binsparse_metadata["data_types"]["values"])
407+
.starts_with("iso")) {
408+
is_iso = true;
409+
}
361410

362411
typename std::allocator_traits<
363412
std::remove_cvref_t<Allocator>>::template rebind_alloc<I>
@@ -374,7 +423,7 @@ coo_matrix<T, I> read_coo_matrix(std::string fname, Allocator&& alloc) {
374423
}
375424

376425
return coo_matrix<T, I>{values.data(), rows.data(), cols.data(), nrows,
377-
ncols, nnz, structure};
426+
ncols, nnz, structure, is_iso};
378427
}
379428

380429
template <typename T, typename I>

include/binsparse/containers/matrices.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct csr_matrix {
3434

3535
I m, n, nnz;
3636
structure_t structure = general;
37+
bool is_iso = false;
3738
};
3839

3940
template <typename T, typename I>
@@ -44,6 +45,7 @@ struct csc_matrix {
4445

4546
I m, n, nnz;
4647
structure_t structure = general;
48+
bool is_iso = false;
4749
};
4850

4951
template <typename T, typename I>
@@ -54,6 +56,7 @@ struct coo_matrix {
5456

5557
I m, n, nnz;
5658
structure_t structure = general;
59+
bool is_iso = false;
5760
};
5861

5962
template <typename T, typename I = std::size_t, typename Order = row_major>
@@ -64,6 +67,7 @@ struct dense_matrix {
6467
structure_t structure = general;
6568

6669
using order = Order;
70+
bool is_iso = false;
6771
};
6872

6973
} // namespace binsparse

0 commit comments

Comments
 (0)