|
ADS
Algorithms and Data Structures
|
The parallel products of matrices, beyond the book. More...
Files | |
| file | ADS/parallel/ParallelNumeric.h |
| 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. | |
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 | |
The parallel products of matrices, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of parallelism.
| 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()
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{gemm}( \mathit{ex} , m , n , k , \alpha , A , B , \beta , C ) \cr 2 & \quad \textbf{if}\ \text{a CBLAS is the backend}\ \textbf{then} \ C \gets \alpha A B + \beta C \text{ by one call};\ \textbf{return} \cr 3 & \quad t \gets \min\{ p , m \} \cr 4 & \quad \textbf{for}\ u \gets 0\ \textbf{to}\ t - 1\ \textbf{do in} \ \textbf{parallel} \cr 5 & \qquad \mathrm{gemm}( \text{the rows } [ m u / t , m ( u + 1 ) / t ) \text{ of } A \text{ and } C ) \end{array}} \]
|
nodiscard |
the product \( A x \) of the sparse matrix \( A \) by rows on the threads of ex, as a new vector
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiply}( \mathit{ex} , A , x ) \cr 2 & \quad y \gets \text{a vector of } m \text{ entries}; \ \mathrm{multiply}( \mathit{ex} , A , x , y );\ \textbf{return}\ y \end{array}} \]
| 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
In pseudocode (the partition of the merge path of Merrill and Garland, beyond the book), \( \mathit{start}[ 0 .. m ] \) the first nonzero of each row, \( \mathit{nnz} = \mathit{start}[ m ] \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{multiply}( \mathit{ex} , A , x , y ) \cr 2 & \quad \textbf{if}\ p = 1\ \textbf{or}\ m + \mathit{nnz} < 2^{15} \ \textbf{then}\ \text{the sequential product};\ \textbf{return} \cr 3 & \quad B \gets \min\{ 4 p , m \} \cr 4 & \quad \textbf{for}\ q \gets 0\ \textbf{to}\ B - 1 \ \textbf{do in parallel} \cr 5 & \qquad r_0 \gets \text{the first } i \text{ with } i + \mathit{start}[ i ] \ge \lfloor q ( m + \mathit{nnz} ) / B \rfloor \quad \triangleright\ \text{by a binary search} \cr 6 & \qquad r_1 \gets \text{the first } i \text{ with } i + \mathit{start}[ i ] \ge \lfloor ( q + 1 ) ( m + \mathit{nnz} ) / B \rfloor \cr 7 & \qquad \textbf{for}\ i \gets r_0\ \textbf{to}\ r_1 - 1\ \textbf{do} \ y[ i ] \gets \sum_{k = \mathit{start}[ i ]}^{\mathit{start}[ i + 1 ] - 1} \mathit{val}[ k ] \, x[ \mathit{col}[ k ] ] \end{array}} \]
|
nodiscard |
the product \( A B \) of two dense matrices on the threads of ex
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiply}( \mathit{ex} , A , B ) \cr 2 & \quad C \gets \text{a matrix of } m \times n;\ \mathrm{gemm}( \mathit{ex} , m , n , k , 1 , A , B , 0 , C );\ \textbf{return}\ C \end{array}} \]
| 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
In pseudocode (beyond the book), the chunks cut as the rows of the product of a CsrMatrix above, \( \mathit{start}[ c ] \) the first place of the chunk \( c \) and \( \mathit{pl} \) the places of the \( \mathit{ch} \) chunks:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{multiply}( \mathit{ex} , A , x , y ) \cr 2 & \quad \textbf{if}\ p = 1\ \textbf{or}\ \mathit{pl} + \mathit{ch} < 2^{15}\ \textbf{then}\ \text{the sequential product}; \ \textbf{return} \cr 3 & \quad B \gets \min\{ 4 p , \mathit{ch} \} \cr 4 & \quad \textbf{for}\ q \gets 0\ \textbf{to}\ B - 1 \ \textbf{do in parallel} \cr 5 & \qquad c_0 \gets \text{the first } c \text{ with } c + \mathit{start}[ c ] \ge \lfloor q ( \mathit{pl} + \mathit{ch} ) / B \rfloor \cr 6 & \qquad c_1 \gets \text{the first } c \text{ with } c + \mathit{start}[ c ] \ge \lfloor ( q + 1 ) ( \mathit{pl} + \mathit{ch} ) / B \rfloor \cr 7 & \qquad \text{the sequential product of the chunks } c_0 , \ldots , c_1 - 1 \end{array}} \]
|
nodiscard |
the product \( A x \) of the sparse matrix \( A \) by chunks on the threads of ex, as a new vector
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiply}( \mathit{ex} , A , x ) \cr 2 & \quad y \gets \text{a vector of } m \text{ entries}; \ \mathrm{multiply}( \mathit{ex} , A , x , y );\ \textbf{return}\ y \end{array}} \]