|
ADS
Algorithms and Data Structures
|
The dense matrices by rows, beyond the book. More...
Files | |
| file | ADS/numeric/DenseMatrix.h |
| The dense matrices by rows, beyond the book: DenseMatrix keeps the \( m \times n \) entries by rows in one contiguous vector, instead of the vector of rows of Matrix.h [see Matrix.h], and the product, on a CBLAS when there is one, and the transposition run on it. | |
Classes | |
| class | ADS::DenseMatrix< T > |
| an \( m \times n \) matrix by rows in one contiguous vector More... | |
Functions | |
| template<class T> | |
| void | ADS::gemm (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) |
| the product \( C \gets \alpha A B + \beta C \), \( A \) \( m \times k \), \( B \) \( k \times n \) and \( C \) \( m \times n \) by rows, with the leading dimensions lda, ldb, ldc (the distance of two rows); \( C \) is not read when \( \beta = 0 \) | |
| template<class T> | |
| DenseMatrix< T > | ADS::multiply (const DenseMatrix< T > &A, const DenseMatrix< T > &B) |
| the product \( A B \), a new matrix, by gemm() with \( \alpha = 1 \) and \( \beta = 0 \) | |
| template<class T> | |
| DenseMatrix< T > | ADS::transpose (const DenseMatrix< T > &A) |
| the transpose \( A^T \), by blocks of \( 32 \times 32 \) | |
| template<class T> | |
| DenseMatrix< T > | ADS::multiplyStrassen (const DenseMatrix< T > &A, const DenseMatrix< T > &B, std::size_t cutoff=3072) |
| the product \( A B \) by the variant of Winograd of the algorithm of Strassen, 7 products of halves and 15 sums a level, down to blocks of fewer than \( \mathit{cutoff} \) rows, columns or inner terms, multiplied by gemm() | |
Variables | |
| constexpr std::size_t | ADS::kGemmBlock = 256 |
| the columns and the rows of \( B \) of a block of the product of the definition, without the CBLAS: a round number, not measured | |
The dense matrices by rows, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of numerical algorithms.
| void ADS::gemm | ( | 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 ) |
the product \( C \gets \alpha A B + \beta C \), \( A \) \( m \times k \), \( B \) \( k \times n \) and \( C \) \( m \times n \) by rows, with the leading dimensions lda, ldb, ldc (the distance of two rows); \( C \) is not read when \( \beta = 0 \)
In pseudocode (the product of the definition, section 4.6.4 of Knuth), by blocks of \( L \) = kGemmBlock columns and rows of \( B \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{gemm}( 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 it};\ \textbf{return} \cr 3 & \quad C \gets \beta C \cr 4 & \quad \textbf{for}\ j_c \gets 0\ \textbf{to}\ n - 1\ \textbf{step} \ L\ \textbf{do} \cr 5 & \qquad \textbf{for}\ p_c \gets 0\ \textbf{to}\ k - 1 \ \textbf{step}\ L\ \textbf{do} \cr 6 & \qquad\quad \textbf{for}\ i \gets 0\ \textbf{to}\ m - 1 \ \textbf{do} \cr 7 & \qquad\qquad \textbf{foreach}\ p \in [ p_c , p_c + L ) , j \in [ j_c , j_c + L )\ \textbf{do}\ c_{ij} \gets c_{ij} + \alpha a_{ip} b_{pj} \end{array}} \]
| DenseMatrix< T > ADS::multiply | ( | const DenseMatrix< T > & | A, |
| const DenseMatrix< T > & | B ) |
the product \( A B \), a new matrix, by gemm() with \( \alpha = 1 \) and \( \beta = 0 \)
| DenseMatrix< T > ADS::multiplyStrassen | ( | const DenseMatrix< T > & | A, |
| const DenseMatrix< T > & | B, | ||
| std::size_t | cutoff = 3072 ) |
the product \( A B \) by the variant of Winograd of the algorithm of Strassen, 7 products of halves and 15 sums a level, down to blocks of fewer than \( \mathit{cutoff} \) rows, columns or inner terms, multiplied by gemm()
In pseudocode (Strassen and Winograd, beyond the book), \( S_i \), \( T_i \), \( P_i \) and \( U \) those of the file:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{strassenRec}( A , B ) \cr 2 & \quad \textbf{if}\ \min( m , n , k ) < \mathit{cutoff} \ \textbf{then return}\ \mathrm{gemm}( A , B ) \cr 3 & \quad \text{split the even parts of } A \text{ and } B \text{ in } \text{their quadrants} \cr 4 & \quad P_1 , \ldots , P_7 \gets \text{the products of the sums } S_i \text{ and } T_i \text{, by } \mathrm{strassenRec} \cr 5 & \quad U \gets P_1 + P_6;\ C_{11} \gets P_1 + P_2;\ C_{12} \gets U + P_5 + P_3 \cr 6 & \quad C_{21} \gets U + P_7 - P_4;\ C_{22} \gets U + P_7 + P_5 \cr 7 & \quad \text{add the odd inner term, the odd column and row by } \mathrm{gemm} \cr 8 & \quad \textbf{return}\ C \end{array}} \]
| DenseMatrix< T > ADS::transpose | ( | const DenseMatrix< T > & | A | ) |
the transpose \( A^T \), by blocks of \( 32 \times 32 \)
In pseudocode (beyond the book), each block read and written in the first level of the cache:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{transpose}( A ) \cr 2 & \quad \textbf{for}\ j_b \gets 0 , 32 , \ldots < n\ \textbf{do} \cr 3 & \qquad \textbf{for}\ i_b \gets 0 , 32 , \ldots < m\ \textbf{do} \cr 4 & \qquad\quad \textbf{foreach}\ j \in [ j_b , j_b + 32 ) , i \in [ i_b , i_b + 32 )\ \textbf{do}\ b_{ji} \gets a_{ij} \cr 5 & \quad \textbf{return}\ B \end{array}} \]
|
inlineconstexpr |
the columns and the rows of \( B \) of a block of the product of the definition, without the CBLAS: a round number, not measured