ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
DenseMatrix.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File DenseMatrix.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
99/*--------------------------------------------------------------------------*/
100/*----------------------------- DEFINITIONS --------------------------------*/
101/*--------------------------------------------------------------------------*/
102
103#ifndef __ADS_DenseMatrix
104 #define __ADS_DenseMatrix
105/* self-identification: #endif constant */
106
107/*--------------------------------------------------------------------------*/
108/*------------------------------ INCLUDES ----------------------------------*/
109/*--------------------------------------------------------------------------*/
110
111#include "ADS/Contract.h"
112#include "ADS/numeric/Matrix.h"
113
114#include <algorithm>
115#include <cstddef>
116#include <cstring>
117#include <memory>
118#include <new>
119#include <type_traits>
120#include <utility>
121#include <vector>
122
123// the backend of a CBLAS, with ADS_WITH_BLAS: the products of double and
124// float go to its cblas_dgemm() and cblas_sgemm(),
125// with ADS_BLAS_ACCELERATE from the header of the CBLAS of vecLib alone,
126// since that of the whole Accelerate brings the names of the old types of
127// macOS (e.g., Point) into the global namespace
128#if defined( ADS_WITH_BLAS ) && ! defined( ADS_GEMM_BLAS )
129 #define ADS_GEMM_BLAS 1
130#endif
131#ifndef ADS_GEMM_BLAS
132 #define ADS_GEMM_BLAS 0
133#endif
134#if ADS_GEMM_BLAS
135 #include <climits>
136 #if defined( ADS_BLAS_ACCELERATE )
137 #include <vecLib/cblas.h>
138 #else
139 #include <cblas.h>
140 #endif
141#endif
142
143/*--------------------------------------------------------------------------*/
144/*--------------------------- NAMESPACE ------------------------------------*/
145/*--------------------------------------------------------------------------*/
146
147namespace ADS
148{
149
150namespace detail
151{
152
155template< class U >
156struct DefaultInitAllocator : std::allocator< U >
157{
158 using value_type = U;
159 template< class V >
160 struct rebind
161 {
162 using other = DefaultInitAllocator< V >;
163 };
164 DefaultInitAllocator( void ) = default;
165 template< class V >
166 DefaultInitAllocator( const DefaultInitAllocator< V > & ) noexcept { }
167 template< class V >
168 void construct( V * p ) noexcept(
169 std::is_nothrow_default_constructible_v< V > ) {
170 ::new( static_cast< void * >( p ) ) V;
171 }
172 template< class V , class... Args >
173 void construct( V * p , Args &&... args ) {
174 ::new( static_cast< void * >( p ) ) V( std::forward< Args >( args )... );
175 }
176 };
177
178} // end( namespace detail )
179
180/*--------------------------------------------------------------------------*/
181/*--------------------------- CLASS DenseMatrix ----------------------------*/
182/*--------------------------------------------------------------------------*/
183
185template< class T >
187{
188 public:
189 using value_type = T;
190
192 DenseMatrix( void ) = default;
193
195 DenseMatrix( std::size_t m , std::size_t n , const T & v = T( 0 ) )
196 : f_m( m ) , f_n( n ) , f_A( m * n ) {
197 std::fill( f_A.begin() , f_A.end() , v );
198 }
199
202 static DenseMatrix uninitialized( std::size_t m , std::size_t n ) {
203 DenseMatrix M;
204 M.f_m = m;
205 M.f_n = n;
206 M.f_A.resize( m * n );
207 return( M );
208 }
209
212 explicit DenseMatrix( const Matrix< T > & A );
213
215 [[nodiscard]] std::size_t rows( void ) const { return( f_m ); }
217 [[nodiscard]] std::size_t cols( void ) const { return( f_n ); }
218
221 [[nodiscard]] T & operator()( std::size_t i , std::size_t j ) {
222 ADS_PRE( ( i < f_m ) && ( j < f_n ) );
223 return( f_A[ i * f_n + j ] );
224 }
225
227 [[nodiscard]] const T & operator()( std::size_t i , std::size_t j ) const {
228 ADS_PRE( ( i < f_m ) && ( j < f_n ) );
229 return( f_A[ i * f_n + j ] );
230 }
231
233 [[nodiscard]] T * data( void ) { return( f_A.data() ); }
235 [[nodiscard]] const T * data( void ) const { return( f_A.data() ); }
236
238 [[nodiscard]] Matrix< T > toMatrix( void ) const;
239
241 bool operator==( const DenseMatrix & ) const = default;
242
243 private:
244 std::size_t f_m = 0 ,
245 f_n = 0;
246 std::vector< T , detail::DefaultInitAllocator< T > > f_A;
247 }; // end( class DenseMatrix )
248
249/*--------------------------------------------------------------------------*/
250/*------------------------------ FUNCTIONS ---------------------------------*/
251/*--------------------------------------------------------------------------*/
252
255inline constexpr std::size_t kGemmBlock = 256;
256
286template< class T >
287void gemm( std::size_t m , std::size_t n , std::size_t k , T alpha ,
288 const T * A ,
289 std::size_t lda , const T * B , std::size_t ldb , T beta , T * C ,
290 std::size_t ldc );
291
295template< class T >
297 const DenseMatrix< T > & B );
298
313template< class T >
315
341template< class T >
343 const DenseMatrix< T > & B ,
344 std::size_t cutoff = 3072 );
345
346/*--------------------------------------------------------------------------*/
347
348} // end( namespace ADS )
349
350/*--------------------------------------------------------------------------*/
351/*-------------------------- IMPLEMENTATION --------------------------------*/
352/*--------------------------------------------------------------------------*/
353
354#include "ADS/numeric/impl/DenseMatrix.tpp"
355
356/*--------------------------------------------------------------------------*/
357
358#endif /* DenseMatrix.h included */
359
360/*--------------------------------------------------------------------------*/
361/*------------------------- End File DenseMatrix.h -------------------------*/
362/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The product of two square matrices (section 12.4):
an matrix by rows in one contiguous vector
Definition DenseMatrix.h:187
const T * data(void) const
the entries of a constant matrix, by rows
Definition DenseMatrix.h:235
Matrix< T > toMatrix(void) const
the Matrix of the book with the same entries
bool operator==(const DenseMatrix &) const =default
true if the two matrices have the same sizes and entries
DenseMatrix(void)=default
the empty matrix
T value_type
the type of the entries
Definition DenseMatrix.h:189
static DenseMatrix uninitialized(std::size_t m, std::size_t n)
the matrix whose entries are not set, which a function then writes, if T is a number (they are T() o...
Definition DenseMatrix.h:202
DenseMatrix(const Matrix< T > &A)
the matrix of the rows of the Matrix of the book
const T & operator()(std::size_t i, std::size_t j) const
the entry of a constant matrix
Definition DenseMatrix.h:227
DenseMatrix(std::size_t m, std::size_t n, const T &v=T(0))
the matrix of entries
Definition DenseMatrix.h:195
std::size_t rows(void) const
the number of rows
Definition DenseMatrix.h:215
T * data(void)
the entries, by rows
Definition DenseMatrix.h:233
T & operator()(std::size_t i, std::size_t j)
the entry , at the place
Definition DenseMatrix.h:221
std::size_t cols(void) const
the number of columns
Definition DenseMatrix.h:217
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
ListGraph transpose(const G &g)
the transposed graph, with the edges reversed
DenseMatrix< T > multiplyStrassen(const DenseMatrix< T > &A, const DenseMatrix< T > &B, std::size_t cutoff=3072)
the product by the variant of Winograd of the algorithm of Strassen, 7 products of halves and 15 sum...
DenseMatrix< T > multiply(const DenseMatrix< T > &A, const DenseMatrix< T > &B)
the product , a new matrix, by gemm() with and
constexpr std::size_t kGemmBlock
the columns and the rows of of a block of the product of the definition, without the CBLAS: a round ...
Definition DenseMatrix.h:255
void 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 , , and by rows, with the leading dimensions lda, ldb, ldc (the distance of two rows...
Definition ADT.h:96
std::vector< std::vector< T > > Matrix
by rows
Definition Matrix.h:74