Skip to content

Commit

Permalink
Fix bugs in undocumented sparse matrix conversion functions (#124)
Browse files Browse the repository at this point in the history
There were bugs in undocumented functions for converting from CSC or CSR formats to
COO format. These bugs did not affect our examples that read sparse matrices from disk
and ran low-rank approximation algorithms; those files have only ever used conversions
from COO to CSC or CSR.
  • Loading branch information
rileyjmurray authored Jan 5, 2025
1 parent 7bf0552 commit 2d33270
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
4 changes: 2 additions & 2 deletions RandBLAS/sparse_data/conversions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void csc_to_coo(CSCMatrix<T, sint_t1> &csc, COOMatrix<T, sint_t2> &coo) {
for (int64_t j = 0; j < csc.n_cols; ++j) {
for (int64_t i = csc.colptr[j]; i < csc.colptr[j+1]; ++i) {
coo.vals[ell] = csc.vals[ell];
coo.rows[ell] = (sint_t2) i;
coo.rows[ell] = (sint_t2) csc.rowidxs[i];
coo.cols[ell] = (sint_t2) j;
++ell;
}
Expand Down Expand Up @@ -114,7 +114,7 @@ void csr_to_coo(CSRMatrix<T, sint_t1> &csr, COOMatrix<T, sint_t2> &coo) {
for (int64_t j = csr.rowptr[i]; j < csr.rowptr[i+1]; ++j) {
coo.vals[ell] = csr.vals[ell];
coo.rows[ell] = (sint_t2) i;
coo.cols[ell] = (sint_t2) j;
coo.cols[ell] = (sint_t2) csr.colidxs[j];
++ell;
}
}
Expand Down
2 changes: 0 additions & 2 deletions rtd/source/updates/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ RandBLAS follows `Semantic Versioning <https://semver.org>`_. Any function docum
on this website is part of the public API. There are many functions which are not
part of our public API, but could be added to it if there is user interest.

RandBLAS is in the 1.0.x release series. The latest version is :ref:`1.0.1 <v10x_patches>`.
See below for a general overview of each release series.

RandBLAS 1.0
------------
Expand Down
26 changes: 26 additions & 0 deletions test/test_datastructures/test_spmats/test_csc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <vector>

using namespace RandBLAS::sparse_data;
using namespace RandBLAS::sparse_data::coo;
using namespace RandBLAS::sparse_data::csc;
using namespace test::test_datastructures::test_spmats;
using namespace RandBLAS::sparse_data::conversions;
Expand Down Expand Up @@ -106,7 +107,32 @@ class TestCSC_Conversions : public ::testing::Test {
delete [] mat_actual;
return;
}
template <typename T = double>
static void test_csc_to_coo_band_diag() {
int64_t n = 8;
int64_t nnz = 32;
std::vector<T> vals{6, -1, -2, -3, -1, 6, -1, -1, -1, 6, -1, -1, -1, -1, 6, -1, -1, 6, -1, -1, -1, -1, 6, -1, -1, -1, 6, -1, -1, -1, -1, 6};
std::vector<int64_t> colptr{0, 4, 8, 12, 16, 20, 24, 28, 32};
std::vector<int64_t> rowidxs{0, 1, 2, 4, 0, 1, 3, 5, 0, 2, 3, 6, 1, 2, 3, 7, 0, 4, 5, 6, 1, 4, 5, 7, 2, 4, 6, 7, 3, 5, 6, 7};
CSCMatrix<T> A_csc(n,n,nnz,vals.data(),rowidxs.data(),colptr.data());
COOMatrix<T> A_coo(n,n);
csc_to_coo(A_csc, A_coo);
std::vector<T> A_dense_coo(n*n);
std::vector<T> A_dense_csc(n*n);
coo_to_dense(A_coo, Layout::ColMajor, A_dense_coo.data());
csc_to_dense(A_csc, Layout::ColMajor, A_dense_csc.data());
test::comparison::matrices_approx_equal(
Layout::ColMajor, Layout::ColMajor, blas::Op::NoTrans,
n, n, A_dense_csc.data(), n, A_dense_coo.data(), n,
__PRETTY_FUNCTION__, __FILE__, __LINE__
);
}

};

TEST_F(TestCSC_Conversions, band) {
test_csc_to_coo_band_diag();
}

TEST_F(TestCSC_Conversions, dense_random_rowmajor) {
test_csc_from_random_sparsified(Layout::RowMajor, 10, 5, 0.7);
Expand Down
27 changes: 27 additions & 0 deletions test/test_datastructures/test_spmats/test_csr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <vector>

using namespace RandBLAS::sparse_data;
using namespace RandBLAS::sparse_data::coo;
using namespace RandBLAS::sparse_data::csr;
using namespace test::test_datastructures::test_spmats;
using namespace RandBLAS::sparse_data::conversions;
Expand Down Expand Up @@ -130,8 +131,34 @@ class TestCSR_Conversions : public ::testing::Test
delete [] mat_actual;
return;
}

template <typename T = double>
static void test_csr_to_coo_band_diagonal() {
int64_t n = 8;
int64_t nnz = 32;
std::vector<T> vals{6, -1, -1, -1, -1, 6, -1, -1, -1, 6, -1, -1, -1, -1, 6, -1, -1, 6, -1, -100, 99, -1, 6, -1, -1, -1, 6, -1, -1, -1, -1, 6};
std::vector<int64_t> rowptr{0, 4, 8, 12, 16, 20, 24, 28, 32};
std::vector<int64_t> colidxs{0, 1, 2, 4, 0, 1, 3, 5, 0, 2, 3, 6, 1, 2, 3, 7, 0, 4, 5, 6, 1, 4, 5, 7, 2, 4, 6, 7, 3, 5, 6, 7};
CSRMatrix<T> A_csr(n,n,nnz,vals.data(),rowptr.data(),colidxs.data());
COOMatrix<T> A_coo(n,n);
csr_to_coo(A_csr, A_coo);
std::vector<T> A_dense_coo(n*n);
std::vector<T> A_dense_csr(n*n);
coo_to_dense(A_coo, Layout::ColMajor, A_dense_coo.data());
csr_to_dense(A_csr, Layout::ColMajor, A_dense_csr.data());
test::comparison::matrices_approx_equal(
Layout::ColMajor, Layout::ColMajor, blas::Op::NoTrans,
n, n, A_dense_csr.data(), n, A_dense_coo.data(), n,
__PRETTY_FUNCTION__, __FILE__, __LINE__
);
}

};

TEST_F(TestCSR_Conversions, band) {
test_csr_to_coo_band_diagonal();
}

TEST_F(TestCSR_Conversions, dense_square_diagonal) {
test_csr_to_dense_diagonal(3);
}
Expand Down

0 comments on commit 2d33270

Please sign in to comment.