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

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. More...

#include "ADS/Contract.h"
#include "ADS/numeric/Matrix.h"
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <memory>
#include <new>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/numeric/impl/DenseMatrix.tpp"

Go to the source code of this file.

Classes

class  ADS::DenseMatrix< T >
 an \( m \times n \) matrix by rows in one contiguous vector More...

Namespaces

namespace  ADS

Macros

#define ADS_GEMM_BLAS   0

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

Detailed description

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.

  • gemm() is \( C \leftarrow \alpha A B + \beta C \) on matrices by rows with leading dimensions. With ADS_WITH_BLAS defined (by the option of CMake of the same name, which defines it whenever a CBLAS is found, by the makefile, which does the same, or by hand, with cblas.h and the library of the CBLAS, or with ADS_BLAS_ACCELERATE the framework Accelerate of macOS), gemm() of double and of float goes to cblas_dgemm() and cblas_sgemm() at every size that fits the int of the CBLAS, which chooses its own kernels and blocks for the processor and runs on the threads it is built or set for (e.g., those of OPENBLAS_NUM_THREADS for OpenBLAS); the product with \( k = 0 \), a mere \( C \gets \beta C \) whose \( A \) may have the leading dimension 0, which a CBLAS may refuse, stays on the loops below. Without the macro (or with ADS_GEMM_BLAS defined to 0), and for the other types, it is the product of the definition, \( c_{ij} = \sum_p a_{ip} b_{pj} \), \( 2 m n k \) operations in \( \Theta( m n k ) \) time, by the loops in the order \( i , p , j \), whose innermost one runs along a row of \( B \) and of \( C \) and which the compiler may vectorize, blocked on \( j \) and on \( p \) by kGemmBlock so that the block of \( B \) stays in the cache while the rows of \( C \) run over it: each entry is the sum of its terms in the order of \( p \), the same on every split of the rows. The bound of the error that both routes satisfy is in The backends of GMP and of a CBLAS.
  • transpose() moves blocks of \( 32 \times 32 \) entries, which stay in the cache of first level both read by columns and written by rows, instead of running through whole columns: \( \Theta( m n ) \) time and \( O( m n / L ) \) misses of lines of \( L \) entries when a block of \( 32 L \) entries fits the cache (the tall cache), the bound of the cache-oblivious transposition.
  • multiply() is the product \( A B \), a new matrix, by gemm().
  • multiplyStrassen() is the product by the variant of Winograd of the algorithm of Strassen [see strassen() of Matrix.h], on the views of the quadrants: with \( S_1 = A_{21} + A_{22} \), \( S_2 = S_1 - A_{11} \), \( S_3 = A_{11} - A_{21} \), \( S_4 = A_{12} - S_2 \), \( T_1 = B_{12} - B_{11} \), \( T_2 = B_{22} - T_1 \), \( T_3 = B_{22} - B_{12} \), \( T_4 = T_2 - B_{21} \) and the products \( P_1 = A_{11} B_{11} \), \( P_2 = A_{12} B_{21} \), \( P_3 = S_4 B_{22} \), \( P_4 = A_{22} T_4 \), \( P_5 = S_1 T_1 \), \( P_6 = S_2 T_2 \), \( P_7 = S_3 T_3 \), the quadrants of \( C \) are \( C_{11} = P_1 + P_2 \), \( C_{12} = U + P_5 + P_3 \), \( C_{21} = U + P_7 - P_4 \), \( C_{22} = U + P_7 + P_5 \) with \( U = P_1 + P_6 \): 7 products and 15 sums of halves, \( T( n ) = 7 T( n / 2 ) + O( n^2 ) \), i.e., \( T( n ) = O( n^{\log_2 7} ) = O( n^{2.81} ) \), until a dimension falls below the cutoff, where gemm() takes over; an odd dimension leaves its last row or column to gemm() (dynamic peeling). The bound of the error grows as \( ( n / n_0 )^{\log_2 18} n_0^2 u \| A \| \| B \| \) for the cutoff \( n_0 \) and the unit roundoff \( u \), against the \( n u | A | | B | \) of the classical product (Higham), which is the price of the fewer products: with the cutoff 3072, the default, one level spares \( 1 / 8 \) of the operations of a product of 4096; the schedule of the sums needs three temporaries of a quadrant.

The contracts [see Contract.h]: the sizes of the factors agree, the entries read are inside the matrices; gemm() needs leading dimensions at least the columns. The functions are defined in DenseMatrix.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 2, 3rd ed., Addison-Wesley, 1998, section 4.6.4, the product of matrices
V. Strassen, Gaussian elimination is not optimal, Numerische Mathematik 13(4), 354-356, 1969, doi:10.1007/BF02165411
S. Winograd, On multiplication of 2 x 2 matrices, Linear Algebra and its Applications 4(4), 381-388, 1971, doi:10.1016/0024-3795(71)90009-7
N. J. Higham, Exploiting fast matrix multiplication within the level 3 BLAS, ACM Transactions on Mathematical Software 16(4), 352-368, 1990, doi:10.1145/98267.98290
J. Huang, T. M. Smith, G. M. Henry, R. A. van de Geijn, Strassen's algorithm reloaded, SC16: International Conference for High Performance Computing, Networking, Storage and Analysis, 690-701, 2016, doi:10.1109/SC.2016.58
L. S. Blackford, J. Demmel, J. Dongarra, I. Duff, S. Hammarling, G. Henry, M. Heroux, L. Kaufman, A. Lumsdaine, A. Petitet, R. Pozo, K. Remington, R. C. Whaley, An updated set of basic linear algebra subprograms (BLAS), ACM Transactions on Mathematical Software 28(2), 135-151, 2002, doi:10.1145/567806.567807, the C interface of the backend
M. Frigo, C. E. Leiserson, H. Prokop, S. Ramachandran, Cache-oblivious algorithms, ACM Transactions on Algorithms 8(1), 4:1-22, 2012, doi:10.1145/2071379.2071383
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa

Macro definition documentation

◆ ADS_GEMM_BLAS

#define ADS_GEMM_BLAS   0