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

The parallel products of matrices, beyond the book: the products by a vector of the sparse matrices CsrMatrix and SellMatrix [see CsrMatrix.h and SellMatrix.h], and the product of dense matrices of gemm() [see DenseMatrix.h], on the threads of an executor [see Executor.h], with the same results as the sequential ones, bit for bit: each entry of the output is computed by the same sequential code, with the same order of the sums, by one thread. More...

#include "ADS/Contract.h"
#include "ADS/numeric/CsrMatrix.h"
#include "ADS/numeric/DenseMatrix.h"
#include "ADS/numeric/SellMatrix.h"
#include "ADS/parallel/Executor.h"
#include <algorithm>
#include <cstddef>
#include <vector>
#include "ADS/parallel/impl/ParallelNumeric.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

template<Executor E, class T, std::integral I, std::integral P>
void ADS::multiply (E &ex, const BasicCsrMatrix< T, I, P > &A, const T *x, T *y)
 the product \( y = A x \) of the sparse matrix \( A \) by rows on the threads of ex
template<Executor E, class T, std::integral I, std::integral P>
std::vector< T > ADS::multiply (E &ex, const BasicCsrMatrix< T, I, P > &A, const std::vector< T > &x)
 the product \( A x \) of the sparse matrix \( A \) by rows on the threads of ex, as a new vector
template<Executor E, std::size_t C>
void ADS::multiply (E &ex, const SellMatrix< C > &A, const double *x, double *y)
 the product \( y = A x \) of the sparse matrix \( A \) by chunks of \( C \) rows on the threads of ex
template<Executor E, std::size_t C>
std::vector< double > ADS::multiply (E &ex, const SellMatrix< C > &A, const std::vector< double > &x)
 the product \( A x \) of the sparse matrix \( A \) by chunks on the threads of ex, as a new vector
template<Executor E, class T>
void ADS::gemm (E &ex, std::size_t m, std::size_t n, std::size_t k, T alpha, const T *A, std::size_t lda, const T *B, std::size_t ldb, T beta, T *C, std::size_t ldc)
 \( C \gets \alpha A B + \beta C \) on the threads of ex, for \( A \) of \( m \times k \), \( B \) of \( k \times n \) and \( C \) of \( m \times n \) by rows, as gemm()
template<Executor E, class T>
DenseMatrix< T > ADS::multiply (E &ex, const DenseMatrix< T > &A, const DenseMatrix< T > &B)
 the product \( A B \) of two dense matrices on the threads of ex

Detailed description

The parallel products of matrices, beyond the book: the products by a vector of the sparse matrices CsrMatrix and SellMatrix [see CsrMatrix.h and SellMatrix.h], and the product of dense matrices of gemm() [see DenseMatrix.h], on the threads of an executor [see Executor.h], with the same results as the sequential ones, bit for bit: each entry of the output is computed by the same sequential code, with the same order of the sums, by one thread.

  • multiply( ex , A , x , y ) for a CsrMatrix (or any BasicCsrMatrix, whatever its types of values and indices) of \( m \) rows and \( \mathit{nnz} \) nonzeros cuts the rows into \( B = 4 p \) parts of equal \( m + \mathit{nnz} \), the cost of a row being its nonzeros plus the store of its sum (the diagonal of the merge path of Merrill and Garland: the part \( q \) ends at the first row \( i \) with \( i + \mathit{start}( i ) \ge ( q + 1 ) ( m + \mathit{nnz} ) / B \), found by a binary search, since \( i + \mathit{start}( i ) \) grows with \( i \)), and runs the product of each part on one thread. A row is never split among threads, which would change the order of its sums; a row longer than a part is therefore a part of its own, and only a matrix with a few rows holding most of the nonzeros is unbalanced.
  • multiply( ex , A , x , y ) for a SellMatrix cuts its chunks of \( C \) rows in the same way by their places, padding included.
  • gemm( ex , ... ) with the backend of a CBLAS [see DenseMatrix.h] is one call of the CBLAS from the calling thread, as the sequential gemm(): the CBLAS runs on its own threads (e.g., those of OPENBLAS_NUM_THREADS for OpenBLAS), and the executor runs nothing, since the threads of both would multiply each other; the result is that of the sequential gemm(), bit for bit, as it is the same call. Without it, each of \( t = \min\{ p , m \} \) threads takes a range of the rows of \( C \), as evenly as they go, and computes them by the loops of the sequential gemm(), whose sums of each entry run in the order of the common index on every split of the rows.

The products by a vector move \( 12 \mathit{nnz} + 8 ( m + n ) \) bytes and are bound by the bandwidth of the memory, so that \( p \) threads gain at most the ratio of the bandwidth of the socket to that of one core.

The contracts [see Contract.h]: those of the sequential products. The functions are defined in ParallelNumeric.tpp.

See also
D. Merrill, M. Garland, Merge-based parallel sparse matrix-vector multiplication, International Conference for High Performance Computing, Networking, Storage and Analysis (SC), 678-689, 2016, doi:10.1109/SC.2016.57
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa