Skip to content

SparseLinOp: keep the matrix's index base; check the sparse sketch - #274

Open
mmelnich wants to merge 1 commit into
mainfrom
fix/sparse-index-base
Open

mmelnich wants to merge 1 commit into
mainfrom
fix/sparse-index-base

Conversation

@mmelnich

Copy link
Copy Markdown
Contributor

Summary

Fixes SparseLinOp silently misreading one-based sparse matrices (IndexBase::One). Its views of the matrix, and of its blocks, were always zero-based, so every product read a one-based matrix's indices one too high. The views now keep the index base, so a one-based matrix either works or throws, and nowhere gives a wrong answer. While here, the paths that read an operator's whole matrix stop writing past their buffers when the operator is a leading window of a larger COO matrix, and when a sparse-sparse product asks for a leading block.

Changes

  • linops/rl_sparse_views.hh, linops/rl_sparse_linop.hh: make_view and the four block views keep index_base; the cross-direction blocks filter by index - base.
  • What a one-based matrix does now:
    • the sparse-dense products throw (RandBLAS takes zero-based matrices only); before, they misread it;
    • the sparse-sparse products reject a one-based operator up front; MKL's spgemm (which assumes zero-based) is used only for zero-based matrices, otherwise the fallback runs, as without MKL;
    • a one-based COO matrix still works wherever it is densified (the sketch with a sparse S, a sparse B_sp, materialize); one-based CSR and CSC throw there.
  • Shape checks, all before allocation:
    • the sketch with a sparse S, the sparse-sparse products, materialize and fro_nrm read the whole matrix, so they now require the operator's dimensions to equal its matrix's (a COO window is still supported by the sparse-dense products);
    • the sparse-sparse products check B_sp's stored dimensions, and a product with a leading block of op(A) takes the fallback instead of spgemm, which multiplied all of A_sp into the caller's smaller C.
  • Temporaries that a throwing call could leak are std::unique_ptr.
  • Docs: the class note, the sketch's doc comment, and the make_view example in devnotes/idioms.md.
  • Tests: three TestSparseLinOpIndexBase tests on genuinely one-based CSR, CSC and COO matrices (views and blocks keep the base; each path works or throws as listed, with the index base named in the message), COO windows, and partial sparse-sparse products on both sides. RandLAPACK_tests 731/731, extras 176/176, benchmark tree builds.

SparseLinOp built its view of the matrix, and row_block, col_block and
submatrix built theirs, with the default zero index base, whatever the
matrix's index_base. A one-based matrix (IndexBase::One) was therefore
silently misread by every product: its row or column indices were read
one too high, so entries moved and the last index fell outside the
matrix.

The views now keep the matrix's index base. RandBLAS's sparse-dense
products take zero-based matrices only, so on a one-based matrix they
now throw. The sparse-sparse products reject a one-based operator up
front, since the sparse-dense product they end with would. MKL's spgemm
reads every matrix as zero-based, so the sparse-sparse products use it
only for a zero-based B_sp, and the sketch with a sparse S only for a
zero-based matrix; otherwise they take their fallback, as builds
without MKL do. The fallbacks densify through sparse_to_dense, which
reads a one-based COO matrix and rejects a one-based CSR or CSC one, so
they now reject a one-based CSR or CSC matrix before allocating
anything: the sketch with a sparse S of a one-based COO operator works,
and so does a product with a one-based COO B_sp. materialize reads a
one-based COO operator and throws on a one-based CSR or CSC one. The
cross-direction blocks (a CSR col_block, a CSC row_block) select their
range in zero-based terms and keep the base.

A COO operator may sit on the leading n_rows x n_cols window of a
larger matrix, which the sparse-dense products (and the sketch with a
dense S, which goes through them) read as that window. Every other path
reads the whole matrix: the sketch with a sparse S (spgemm, or a
densified copy in a buffer of the operator's size), the sparse-sparse
products, materialize (sparse_to_dense) and fro_nrm. They now require
the operator's dimensions to equal its matrix's, and the sparse-sparse
products also require B_sp's stored dimensions to be those the product
infers from (m, n, k) and trans_B, since the fallback densifies it into
a buffer of that size. All of these checks run before anything is
allocated. A sparse-sparse product with a leading block of op(A) now
takes the fallback instead of MKL's spgemm, which multiplied all of
A_sp into the caller's smaller C; it then behaves as the sparse-dense
product (COO multiplies the block, CSR and CSC throw).

The temporaries that a throwing call could leak are now
std::unique_ptr: the dense copies in the sketch and sparse-sparse
fallbacks, materialize's copy for a padded ldb, and the operand copies
of materialize's CompositeOperator overload.

The class note describes the routing and the window rule; the sketch's
doc says it reads the whole matrix with a sparse S; the pointer offset
in csr_row_block and csc_col_block is renamed from base to offset; the
make_view example in devnotes/idioms.md shows the index base and COO.
Em-dashes in the touched files are swept.

Tests: one-based CSR, CSC and COO operators keep the base, and their
sparse-dense products throw; the row, column and submatrix blocks of
one-based CSR and CSC operators keep the base and equal the zero-based
blocks with indices raised by one; the sketch with a sparse S of a
one-based COO operator matches the zero-based one, and a one-based CSR
operator's throws; Side::Left and Side::Right (trans_A == NoTrans)
products with a one-based COO B_sp match the zero-based ones, and a
one-based CSR B_sp or a one-based operator throws; materialize reads a
one-based COO operator and throws on one-based CSR and CSC ones, with
and without a padded ldb. Each rejection is checked to name the index
base. A COO operator on the leading window of a larger matrix applies
as that window, while its sketch with a sparse S (zero- or one-based),
its sparse-sparse product, materialize and fro_nrm throw. Sparse-sparse
products with a leading block of a full-size operator, on both sides,
match a dense reference for COO and throw for CSR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant