ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CsrMatrix.h File Reference

A sparse matrix in compressed sparse rows (CSR), beyond the book: the nonzeros of each row, by column, in one vector of values and one of columns, and the place where each row begins in a vector of \( m + 1 \) offsets, \( 12 \, \mathit{nnz} + 8 m \) bytes for doubles, read in order by the product with a vector instead of the walk of the orthogonal lists of SparseMatrix [see SparseMatrix.h], whose nodes are scattered. More...

#include "ADS/Contract.h"
#include "ADS/external/MappedFile.h"
#include "ADS/numeric/DenseMatrix.h"
#include "ADS/numeric/SparseMatrix.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <numeric>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/numeric/impl/CsrMatrix.tpp"

Go to the source code of this file.

Classes

class  ADS::BasicCsrMatrix< T, I, P >
 a sparse \( m \times n \) matrix of values T in compressed sparse rows, the columns of the type I and the offsets of the rows of the type P More...

Namespaces

namespace  ADS

Typedefs

using ADS::CsrMatrix = BasicCsrMatrix<>
 the matrix of doubles of 32-bit columns and std::size_t offsets

Detailed description

A sparse matrix in compressed sparse rows (CSR), beyond the book: the nonzeros of each row, by column, in one vector of values and one of columns, and the place where each row begins in a vector of \( m + 1 \) offsets, \( 12 \, \mathit{nnz} + 8 m \) bytes for doubles, read in order by the product with a vector instead of the walk of the orthogonal lists of SparseMatrix [see SparseMatrix.h], whose nodes are scattered.

BasicCsrMatrix< T , I , P > takes the type T of the values, the type I of the columns and the type P of the offsets, and CsrMatrix is BasicCsrMatrix<>: doubles, columns of 32 bits without sign and offsets std::size_t, so that the nonzeros may exceed \( 2^{32} \) while a column takes 4 bytes. Any other I gives offsets of the same type, e.g., BasicCsrMatrix< double , int >, whose three arrays are those of a row-major Eigen::SparseMatrix< double , Eigen::RowMajor , int >, of a compressed matrix of METIS or of the container of GraphBLAS with indices of 32 bits, which the libraries map without a copy; with std::int64_t they are those of the 64-bit indices of GraphBLAS and of MKL. The bytes are \( ( s + i ) \, \mathit{nnz} + p ( m + 1 ) \) for s , i , p the sizes of T , I , P.

  • The matrix is built from triplets \( ( i , j , v ) \) in any order by a counting sort on the rows, a sort of the columns of each row and the sum of the equal places, \( O( \mathit{nnz} + m + \mathrm{sort} ) \), or from a SparseMatrix.
  • multiply() is the product \( y = A x \), by rows: the nonzeros stream from memory in one pass, the entries of \( x \) are read by column: \( 2 \, \mathit{nnz} \) operations on \( 12 \, \mathit{nnz} + 8 ( m + 1 ) \) bytes of the matrix, \( 8 m \) written and up to \( 8 \, \mathit{nnz} \) read of \( x \), so that the product is bound by the memory, and on the benchmark neither more accumulators per row nor the gathers of AVX-512 made it faster.
  • transpose() is the CSR of the transpose, i.e., the matrix by columns, by a counting sort on the columns that keeps the rows sorted, \( O( \mathit{nnz} + m + n ) \); a large matrix whose rows span columns far apart (a sample of blocks of 64 rows decides) goes first into 256 buckets of consecutive columns and then each bucket into its slice of the transpose, two passes whose writes stay in few streams and in the cache instead of one pass of writes scattered over all the transpose (the partitioned radix sort of the databases): \( 2 \, \mathit{nnz} \) moves instead of \( \mathit{nnz} \), but \( O( \mathit{nnz} / L ) \) misses instead of up to \( \mathit{nnz} \), for lines of \( L \) items, when a slice of \( \mathit{nnz} / 256 \) items fits the cache.

The contracts [see Contract.h]: the places of the triplets are in the matrix, the columns fit I and the nonzeros P (the rows fit I too, for transpose()); \( x \) has \( n \) entries; the invariant is that the offsets do not decrease and the columns of a row increase, an audit. The functions are defined in CsrMatrix.tpp.

See also
Y. Saad, Iterative Methods for Sparse Linear Systems, 2nd ed., SIAM, 2003, section 3.4
S. Williams, L. Oliker, R. Vuduc, J. Shalf, K. Yelick, J. Demmel, Optimization of sparse matrix-vector multiplication on emerging multicore platforms, Parallel Computing 35(3), 178-194, 2009, doi:10.1016/j.parco.2008.12.006
O. Polychroniou, K. A. Ross, A comprehensive study of main-memory partitioning and its application to large-scale comparison- and radix-sort, Proceedings of the 2014 ACM SIGMOD International Conference on Management of Data, 755-766, 2014, doi:10.1145/2588555.2610522
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa