15#include <dr/detail/csr_matrix_base.hpp>
16#include <dr/views/csr_matrix_view.hpp>
26template <
typename Tuples,
typename Allocator>
27auto convert_to_csr(Tuples &&tuples,
dr::index<> shape, std::size_t nnz,
28 Allocator &&allocator) {
29 auto &&[index, v] = *tuples.begin();
30 auto &&[i, j] = index;
32 using T = std::remove_reference_t<
decltype(v)>;
33 using I = std::remove_reference_t<
decltype(i)>;
35 typename std::allocator_traits<Allocator>::template rebind_alloc<I>
36 i_allocator(allocator);
38 T *values = allocator.allocate(nnz);
39 I *rowptr = i_allocator.allocate(shape[0] + 1);
40 I *colind = i_allocator.allocate(nnz);
46 for (
auto iter = tuples.begin(); iter != tuples.end(); ++iter) {
47 auto &&[index, value] = *iter;
49 auto &&[i, j] = index;
55 assert(r + 1 <= shape[0]);
66 for (; r < shape[0]; r++) {
74template <
typename Tuples,
typename Allocator>
75auto convert_csr_base_to_csr(Tuples &&csr_matrix,
dr::index<> shape,
76 std::size_t nnz, Allocator &&allocator) {
77 auto &&[v, j] = *csr_matrix.begin()->begin();
79 using T = std::remove_reference_t<
decltype(v)>;
80 using I = std::remove_reference_t<
decltype(j)>;
82 typename std::allocator_traits<Allocator>::template rebind_alloc<I>
83 i_allocator(allocator);
85 T *values = allocator.allocate(nnz);
86 I *rowptr = i_allocator.allocate(shape[0] + 1);
87 I *colind = i_allocator.allocate(nnz);
93 for (
auto iter = csr_matrix.begin(); iter != csr_matrix.end(); ++iter) {
94 for (
auto iter2 = iter->begin(); iter2 != iter->end(); ++iter2) {
95 auto &&[value, j] = *iter2;
101 assert(r + 1 <= shape[0]);
109 for (; r < shape[0]; r++) {
119template <
typename T,
typename I = std::
size_t>
120inline csr_matrix_base<T, I> read_csr_matrix_base(std::string file_path,
121 bool one_indexed =
true) {
122 using size_type = std::size_t;
126 f.open(file_path.c_str());
130 throw std::runtime_error(
"mmread: cannot open " + file_path);
139 std::getline(f, buf);
140 std::istringstream ss(buf);
143 if (item !=
"%%MatrixMarket") {
144 throw std::runtime_error(file_path +
145 " could not be parsed as a Matrix Market file.");
148 if (item !=
"matrix") {
149 throw std::runtime_error(file_path +
150 " could not be parsed as a Matrix Market file.");
153 if (item !=
"coordinate") {
154 throw std::runtime_error(file_path +
155 " could not be parsed as a Matrix Market file.");
159 if (item ==
"pattern") {
167 if (item ==
"general") {
169 }
else if (item ==
"symmetric") {
172 throw std::runtime_error(file_path +
" has an unsupported matrix type");
175 bool outOfComments =
false;
176 while (!outOfComments) {
177 std::getline(f, buf);
180 outOfComments =
true;
193 csr_matrix_base<T, I> matrix({m, n}, nnz);
196 while (std::getline(f, buf)) {
199 std::istringstream ss(buf);
211 if (i >= m || j >= n) {
212 throw std::runtime_error(
213 "read_MatrixMarket: file has nonzero out of bounds.");
216 matrix.push_back(i, {v, j});
218 if (symmetric && i != j) {
219 matrix.push_back(j, {v, i});
224 throw std::runtime_error(
"read_MatrixMarket: error reading Matrix Market "
225 "file, file has more nonzeros than reported.");
235template <
typename T,
typename I,
typename Allocator,
typename... Args>
238 alloc.deallocate(view.values_data(), view.size());
239 typename std::allocator_traits<Allocator>::template rebind_alloc<I> i_alloc(
241 i_alloc.deallocate(view.colind_data(), view.size());
242 i_alloc.deallocate(view.rowptr_data(), view.shape()[0] + 1);
247template <
typename T,
typename I = std::
size_t>
248auto read_csr(std::string file_path,
bool one_indexed =
true) {
249 auto m = __detail::read_csr_matrix_base<T, I>(file_path, one_indexed);
250 auto shape = m.shape();
253 __detail::convert_csr_base_to_csr(m, shape, nnz, std::allocator<T>{});
Definition: csr_matrix_view.hpp:126