From 2d332701d88f9a327fc0fbf050d1618a83418b1a Mon Sep 17 00:00:00 2001 From: Riley Murray Date: Sat, 4 Jan 2025 22:09:26 -0500 Subject: [PATCH] Fix bugs in undocumented sparse matrix conversion functions (#124) 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. --- RandBLAS/sparse_data/conversions.hh | 4 +-- rtd/source/updates/index.rst | 2 -- .../test_spmats/test_csc.cc | 26 ++++++++++++++++++ .../test_spmats/test_csr.cc | 27 +++++++++++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/RandBLAS/sparse_data/conversions.hh b/RandBLAS/sparse_data/conversions.hh index cb4958be..3830ea1b 100644 --- a/RandBLAS/sparse_data/conversions.hh +++ b/RandBLAS/sparse_data/conversions.hh @@ -72,7 +72,7 @@ void csc_to_coo(CSCMatrix &csc, COOMatrix &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; } @@ -114,7 +114,7 @@ void csr_to_coo(CSRMatrix &csr, COOMatrix &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; } } diff --git a/rtd/source/updates/index.rst b/rtd/source/updates/index.rst index 3bad6ccd..eb984cdb 100644 --- a/rtd/source/updates/index.rst +++ b/rtd/source/updates/index.rst @@ -11,8 +11,6 @@ RandBLAS follows `Semantic Versioning `_. 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 `. -See below for a general overview of each release series. RandBLAS 1.0 ------------ diff --git a/test/test_datastructures/test_spmats/test_csc.cc b/test/test_datastructures/test_spmats/test_csc.cc index aea06e9a..03700caa 100644 --- a/test/test_datastructures/test_spmats/test_csc.cc +++ b/test/test_datastructures/test_spmats/test_csc.cc @@ -34,6 +34,7 @@ #include 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; @@ -106,7 +107,32 @@ class TestCSC_Conversions : public ::testing::Test { delete [] mat_actual; return; } + template + static void test_csc_to_coo_band_diag() { + int64_t n = 8; + int64_t nnz = 32; + std::vector 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 colptr{0, 4, 8, 12, 16, 20, 24, 28, 32}; + std::vector 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 A_csc(n,n,nnz,vals.data(),rowidxs.data(),colptr.data()); + COOMatrix A_coo(n,n); + csc_to_coo(A_csc, A_coo); + std::vector A_dense_coo(n*n); + std::vector 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); diff --git a/test/test_datastructures/test_spmats/test_csr.cc b/test/test_datastructures/test_spmats/test_csr.cc index 501cde57..547892b0 100644 --- a/test/test_datastructures/test_spmats/test_csr.cc +++ b/test/test_datastructures/test_spmats/test_csr.cc @@ -34,6 +34,7 @@ #include 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; @@ -130,8 +131,34 @@ class TestCSR_Conversions : public ::testing::Test delete [] mat_actual; return; } + + template + static void test_csr_to_coo_band_diagonal() { + int64_t n = 8; + int64_t nnz = 32; + std::vector 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 rowptr{0, 4, 8, 12, 16, 20, 24, 28, 32}; + std::vector 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 A_csr(n,n,nnz,vals.data(),rowptr.data(),colidxs.data()); + COOMatrix A_coo(n,n); + csr_to_coo(A_csr, A_coo); + std::vector A_dense_coo(n*n); + std::vector 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); }