@@ -67,6 +67,49 @@ void convert_to_binsparse(std::string input_file, std::string output_file,
6767 }
6868}
6969
70+ template <typename T>
71+ void convert_to_binsparse_vector (std::string input_file,
72+ std::string output_file, std::string type,
73+ std::string comment,
74+ std::optional<std::string> group) {
75+ H5::H5File file;
76+ std::unique_ptr<H5::Group> f_p;
77+
78+ if (!group.has_value ()) {
79+ f_p = std::unique_ptr<H5::Group>(
80+ new H5::H5File (output_file.c_str (), H5F_ACC_TRUNC));
81+ } else {
82+ file = H5::H5File (output_file.c_str (), H5F_ACC_RDWR);
83+ H5::Group g = file.createGroup (group.value ().c_str ());
84+ f_p = std::unique_ptr<H5::Group>(new H5::Group (g));
85+ }
86+
87+ H5::Group& f = *f_p;
88+
89+ nlohmann::json user_keys;
90+ user_keys[" comment" ] = comment;
91+
92+ auto x = binsparse::__detail::mmread_array<float >(input_file);
93+ binsparse::write_dense_vector (f, std::span (x), user_keys);
94+ std::cout << " Writing to binsparse file " << output_file << " as vector"
95+ << std::endl;
96+ }
97+
98+ inline void convert_to_binsparse_vector (std::string input_file,
99+ std::string output_file,
100+ std::string type, std::string comment,
101+ std::optional<std::string> group = {}) {
102+ if (type == " real" ) {
103+ convert_to_binsparse_vector<float >(input_file, output_file, type, comment,
104+ group);
105+ } else if (type == " integer" ) {
106+ convert_to_binsparse_vector<int64_t >(input_file, output_file, type, comment,
107+ group);
108+ } else {
109+ throw std::runtime_error (" convert_to_binsparse_vector: unsupported type" );
110+ }
111+ }
112+
70113int main (int argc, char ** argv) {
71114
72115 if (argc < 3 ) {
@@ -96,35 +139,41 @@ int main(int argc, char** argv) {
96139 group = argv[4 ];
97140 }
98141
99- auto [m, n, nnz, type, structure, comment] =
142+ auto [m, n, nnz, mm_format, type, structure, comment] =
100143 binsparse::mmread_metadata (input_file);
101144
102- std::cout << " Matrix is " << m << " x " << n << " with " << nnz
103- << " values.\n " ;
104- std::cout << " Type: " << type << std::endl;
105- std::cout << " Structure: " << structure << std::endl;
106- std::cout << " Comment:\n " ;
107- std::cout << comment;
108-
109- assert (format == " COO" || format == " CSR" );
110-
111- auto max_size = std::max ({m, n, nnz});
112-
113- if (max_size + 1 <= std::numeric_limits<uint8_t >::max ()) {
114- convert_to_binsparse<uint8_t >(input_file, output_file, type, format,
115- comment, group);
116- } else if (max_size + 1 <= std::numeric_limits<uint16_t >::max ()) {
117- convert_to_binsparse<uint16_t >(input_file, output_file, type, format,
118- comment, group);
119- } else if (max_size + 1 <= std::numeric_limits<uint32_t >::max ()) {
120- convert_to_binsparse<uint32_t >(input_file, output_file, type, format,
121- comment, group);
122- } else if (max_size + 1 <= std::numeric_limits<uint64_t >::max ()) {
123- convert_to_binsparse<uint64_t >(input_file, output_file, type, format,
124- comment, group);
145+ if (mm_format == " coordinate" ) {
146+ std::cout << " Matrix is " << m << " x " << n << " with " << nnz
147+ << " values.\n " ;
148+ std::cout << " Type: " << type << std::endl;
149+ std::cout << " Structure: " << structure << std::endl;
150+ std::cout << " Comment:\n " ;
151+ std::cout << comment;
152+
153+ assert (format == " COO" || format == " CSR" );
154+
155+ auto max_size = std::max ({m, n, nnz});
156+
157+ if (max_size + 1 <= std::numeric_limits<uint8_t >::max ()) {
158+ convert_to_binsparse<uint8_t >(input_file, output_file, type, format,
159+ comment, group);
160+ } else if (max_size + 1 <= std::numeric_limits<uint16_t >::max ()) {
161+ convert_to_binsparse<uint16_t >(input_file, output_file, type, format,
162+ comment, group);
163+ } else if (max_size + 1 <= std::numeric_limits<uint32_t >::max ()) {
164+ convert_to_binsparse<uint32_t >(input_file, output_file, type, format,
165+ comment, group);
166+ } else if (max_size + 1 <= std::numeric_limits<uint64_t >::max ()) {
167+ convert_to_binsparse<uint64_t >(input_file, output_file, type, format,
168+ comment, group);
169+ } else {
170+ throw std::runtime_error (
171+ " Error! Matrix dimensions or NNZ too large to handle." );
172+ }
173+ } else if (mm_format == " array" && n == 1 ) {
174+ convert_to_binsparse_vector (input_file, output_file, type, comment, group);
125175 } else {
126- throw std::runtime_error (
127- " Error! Matrix dimensions or NNZ too large to handle." );
176+ throw std::runtime_error (" Encountered unsupported MatrixMarket format" );
128177 }
129178
130179 return 0 ;
0 commit comments