All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SMatDVecMultExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SMATDVECMULTEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SMATDVECMULTEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <stdexcept>
52 #include <blaze/math/shims/Reset.h>
64 #include <blaze/util/Assert.h>
66 #include <blaze/util/DisableIf.h>
67 #include <blaze/util/EnableIf.h>
69 #include <blaze/util/SelectType.h>
70 #include <blaze/util/Types.h>
72 
73 
74 namespace blaze {
75 
76 //=================================================================================================
77 //
78 // CLASS SMATDVECMULTEXPR
79 //
80 //=================================================================================================
81 
82 //*************************************************************************************************
89 template< typename MT // Type of the left-hand side sparse matrix
90  , typename VT > // Type of the right-hand side dense vector
91 class SMatDVecMultExpr : public DenseVector< SMatDVecMultExpr<MT,VT>, false >
92  , private MatVecMultExpr
93  , private Computation
94 {
95  private:
96  //**Type definitions****************************************************************************
97  typedef typename MT::ResultType MRT;
98  typedef typename VT::ResultType VRT;
99  typedef typename MT::CompositeType MCT;
100  typedef typename VT::CompositeType VCT;
101  //**********************************************************************************************
102 
103  //**********************************************************************************************
105 
111  enum { useAssign = ( RequiresEvaluation<MT>::value || IsComputation<VT>::value ) };
112  //**********************************************************************************************
113 
114  //**********************************************************************************************
116  template< typename VT2 >
118  struct UseAssign {
119  enum { value = useAssign };
120  };
122  //**********************************************************************************************
123 
124  public:
125  //**Type definitions****************************************************************************
130  typedef const ElementType ReturnType;
131 
134 
136  typedef typename SelectType< IsExpression<MT>::value, const MT, const MT& >::Type LeftOperand;
137 
139  typedef typename SelectType< IsExpression<VT>::value, const VT, const VT& >::Type RightOperand;
140 
142  typedef MCT LT;
143 
145  typedef typename SelectType< IsComputation<VT>::value, const VRT, VCT >::Type RT;
146  //**********************************************************************************************
147 
148  //**Compilation flags***************************************************************************
150  enum { vectorizable = 0 };
151 
153  enum { smpAssignable = !useAssign };
154  //**********************************************************************************************
155 
156  //**Constructor*********************************************************************************
162  explicit inline SMatDVecMultExpr( const MT& mat, const VT& vec )
163  : mat_( mat ) // Left-hand side sparse matrix of the multiplication expression
164  , vec_( vec ) // Right-hand side dense vector of the multiplication expression
165  {
166  BLAZE_INTERNAL_ASSERT( mat_.columns() == vec_.size(), "Invalid matrix and vector sizes" );
167  }
168  //**********************************************************************************************
169 
170  //**Subscript operator**************************************************************************
176  inline ReturnType operator[]( size_t index ) const {
177  BLAZE_INTERNAL_ASSERT( index < mat_.rows(), "Invalid vector access index" );
178 
180 
181  ElementType tmp = ElementType();
182 
183  // Early exit
184  if( mat_.columns() == 0UL )
185  return tmp;
186 
187  // Fast computation in case the left-hand side sparse matrix directly provides iterators
189  {
190  MCT A( mat_ ); // Evaluation of the left-hand side sparse matrix operand
191 
192  const ConstIterator end( A.end(index) );
193  ConstIterator element( A.begin(index) );
194 
195  // Early exit in case row 'index' is empty
196  if( element == end )
197  return tmp;
198 
199  // Calculating element 'index' for numeric data types
201  {
202  const size_t last( A.nonZeros(index) & size_t(-2) );
203  ElementType tmp2 = ElementType();
204 
205  for( size_t i=0UL; i<last; i+=2UL )
206  {
207  const ElementType value1( element->value() );
208  const size_t index1( element->index() );
209  ++element;
210  const ElementType value2( element->value() );
211  const size_t index2( element->index() );
212  ++element;
213 
214  tmp += value1 * vec_[index1];
215  tmp2 += value2 * vec_[index2];
216  }
217  if( element!=end ) {
218  tmp += element->value() * vec_[element->index()];
219  }
220 
221  tmp += tmp2;
222  }
223 
224  // Calculating element 'index' for non-numeric data types
225  else {
226  tmp = element->value() * vec_[element->index()];
227  ++element;
228  for( ; element!=end; ++element )
229  tmp += element->value() * vec_[element->index()];
230  }
231  }
232 
233  // Default computation in case the left-hand side sparse matrix doesn't provide iterators
234  else {
235  tmp = mat_(index,0UL) * vec_[0UL];
236  for( size_t k=1UL; k<mat_.columns(); ++k ) {
237  tmp += mat_(index,k) * vec_[k];
238  }
239  }
240 
241  return tmp;
242  }
243  //**********************************************************************************************
244 
245  //**Size function*******************************************************************************
250  inline size_t size() const {
251  return mat_.rows();
252  }
253  //**********************************************************************************************
254 
255  //**Left operand access*************************************************************************
260  inline LeftOperand leftOperand() const {
261  return mat_;
262  }
263  //**********************************************************************************************
264 
265  //**Right operand access************************************************************************
270  inline RightOperand rightOperand() const {
271  return vec_;
272  }
273  //**********************************************************************************************
274 
275  //**********************************************************************************************
281  template< typename T >
282  inline bool canAlias( const T* alias ) const {
283  return ( mat_.isAliased( alias ) || vec_.isAliased( alias ) );
284  }
285  //**********************************************************************************************
286 
287  //**********************************************************************************************
293  template< typename T >
294  inline bool isAliased( const T* alias ) const {
295  return ( mat_.isAliased( alias ) || vec_.isAliased( alias ) );
296  }
297  //**********************************************************************************************
298 
299  //**********************************************************************************************
304  inline bool isAligned() const {
305  return vec_.isAligned();
306  }
307  //**********************************************************************************************
308 
309  //**********************************************************************************************
314  inline bool canSMPAssign() const {
315  return ( size() > SMP_SMATDVECMULT_THRESHOLD );
316  }
317  //**********************************************************************************************
318 
319  private:
320  //**Member variables****************************************************************************
321  LeftOperand mat_;
322  RightOperand vec_;
323  //**********************************************************************************************
324 
325  //**Assignment to dense vectors*****************************************************************
341  template< typename VT1 > // Type of the target dense vector
342  friend inline typename EnableIf< UseAssign<VT1> >::Type
344  {
346 
347  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
348 
350 
351  if( rhs.mat_.columns() == 0UL ) {
352  reset( ~lhs );
353  return;
354  }
355 
356  LT A( rhs.mat_ ); // Evaluation of the left-hand side sparse matrix operand
357  RT x( rhs.vec_ ); // Evaluation of the right-hand side dense vector operand
358 
359  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.mat_.rows() , "Invalid number of rows" );
360  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.mat_.columns(), "Invalid number of columns" );
361  BLAZE_INTERNAL_ASSERT( x.size() == rhs.vec_.size() , "Invalid vector size" );
362  BLAZE_INTERNAL_ASSERT( A.rows() == (~lhs).size() , "Invalid vector size" );
363 
364  smpAssign( ~lhs, A * x );
365  }
367  //**********************************************************************************************
368 
369  //**Assignment to sparse vectors****************************************************************
385  template< typename VT1 > // Type of the target sparse vector
386  friend inline typename EnableIf< UseAssign<VT1> >::Type
388  {
390 
394 
395  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
396 
397  const ResultType tmp( rhs );
398  smpAssign( ~lhs, tmp );
399  }
401  //**********************************************************************************************
402 
403  //**Addition assignment to dense vectors********************************************************
419  template< typename VT1 > // Type of the target dense vector
420  friend inline typename EnableIf< UseAssign<VT1> >::Type
421  addAssign( DenseVector<VT1,false>& lhs, const SMatDVecMultExpr& rhs )
422  {
424 
425  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
426 
428 
429  if( rhs.mat_.columns() == 0UL ) {
430  return;
431  }
432 
433  LT A( rhs.mat_ ); // Evaluation of the left-hand side sparse matrix operand
434  RT x( rhs.vec_ ); // Evaluation of the right-hand side dense vector operand
435 
436  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.mat_.rows() , "Invalid number of rows" );
437  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.mat_.columns(), "Invalid number of columns" );
438  BLAZE_INTERNAL_ASSERT( x.size() == rhs.vec_.size() , "Invalid vector size" );
439  BLAZE_INTERNAL_ASSERT( A.rows() == (~lhs).size() , "Invalid vector size" );
440 
441  smpAddAssign( ~lhs, A * x );
442  }
444  //**********************************************************************************************
445 
446  //**Addition assignment to sparse vectors*******************************************************
447  // No special implementation for the addition assignment to sparse vectors.
448  //**********************************************************************************************
449 
450  //**Subtraction assignment to dense vectors*****************************************************
466  template< typename VT1 > // Type of the target dense vector
467  friend inline typename EnableIf< UseAssign<VT1> >::Type
468  subAssign( DenseVector<VT1,false>& lhs, const SMatDVecMultExpr& rhs )
469  {
471 
472  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
473 
475 
476  if( rhs.mat_.columns() == 0UL ) {
477  return;
478  }
479 
480  LT A( rhs.mat_ ); // Evaluation of the left-hand side sparse matrix operand
481  RT x( rhs.vec_ ); // Evaluation of the right-hand side dense vector operand
482 
483  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.mat_.rows() , "Invalid number of rows" );
484  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.mat_.columns(), "Invalid number of columns" );
485  BLAZE_INTERNAL_ASSERT( x.size() == rhs.vec_.size() , "Invalid vector size" );
486  BLAZE_INTERNAL_ASSERT( A.rows() == (~lhs).size() , "Invalid vector size" );
487 
488  smpSubAssign( ~lhs, A * x );
489  }
491  //**********************************************************************************************
492 
493  //**Subtraction assignment to sparse vectors****************************************************
494  // No special implementation for the subtraction assignment to sparse vectors.
495  //**********************************************************************************************
496 
497  //**Multiplication assignment to dense vectors**************************************************
513  template< typename VT1 > // Type of the target dense vector
514  friend inline typename EnableIf< UseAssign<VT1> >::Type
515  multAssign( DenseVector<VT1,false>& lhs, const SMatDVecMultExpr& rhs )
516  {
518 
522 
523  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
524 
525  const ResultType tmp( rhs );
526  smpMultAssign( ~lhs, tmp );
527  }
529  //**********************************************************************************************
530 
531  //**Multiplication assignment to sparse vectors*************************************************
532  // No special implementation for the multiplication assignment to sparse vectors.
533  //**********************************************************************************************
534 
535  //**Compile time checks*************************************************************************
542  //**********************************************************************************************
543 };
544 //*************************************************************************************************
545 
546 
547 
548 
549 //=================================================================================================
550 //
551 // GLOBAL BINARY ARITHMETIC OPERATORS
552 //
553 //=================================================================================================
554 
555 //*************************************************************************************************
586 template< typename T1 // Type of the left-hand side sparse matrix
587  , typename T2 > // Type of the right-hand side dense vector
588 inline const typename DisableIf< IsMatMatMultExpr<T1>, SMatDVecMultExpr<T1,T2> >::Type
590 {
592 
593  if( (~mat).columns() != (~vec).size() )
594  throw std::invalid_argument( "Matrix and vector sizes do not match" );
595 
596  return SMatDVecMultExpr<T1,T2>( ~mat, ~vec );
597 }
598 //*************************************************************************************************
599 
600 
601 
602 
603 //=================================================================================================
604 //
605 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
606 //
607 //=================================================================================================
608 
609 //*************************************************************************************************
622 template< typename T1 // Type of the left-hand side sparse matrix
623  , bool SO // Storage order of the left-hand side sparse matrix
624  , typename T2 > // Type of the right-hand side dense vector
625 inline const typename EnableIf< IsMatMatMultExpr<T1>, MultExprTrait<T1,T2> >::Type::Type
627 {
629 
630  return (~mat).leftOperand() * ( (~mat).rightOperand() * vec );
631 }
632 //*************************************************************************************************
633 
634 
635 
636 
637 //=================================================================================================
638 //
639 // EXPRESSION TRAIT SPECIALIZATIONS
640 //
641 //=================================================================================================
642 
643 //*************************************************************************************************
645 template< typename MT, typename VT, bool AF >
646 struct SubvectorExprTrait< SMatDVecMultExpr<MT,VT>, AF >
647 {
648  public:
649  //**********************************************************************************************
650  typedef typename MultExprTrait< typename SubmatrixExprTrait<const MT,AF>::Type, VT >::Type Type;
651  //**********************************************************************************************
652 };
654 //*************************************************************************************************
655 
656 } // namespace blaze
657 
658 #endif
Compile time check whether the given type is a computational expression template.This type trait clas...
Definition: IsComputation.h:89
Compile time check for numeric types.This type trait tests whether or not the given template paramete...
Definition: IsNumeric.h:98
MT::ResultType MRT
Result type of the left-hand side sparse matrix expression.
Definition: SMatDVecMultExpr.h:97
void reset(DynamicMatrix< Type, SO > &m)
Resetting the given dense matrix.
Definition: DynamicMatrix.h:4579
const DMatDMatMultExpr< T1, T2 > operator*(const DenseMatrix< T1, false > &lhs, const DenseMatrix< T2, false > &rhs)
Multiplication operator for the multiplication of two row-major dense matrices ( ).
Definition: DMatDMatMultExpr.h:4075
const size_t SMP_SMATDVECMULT_THRESHOLD
SMP row-major sparse matrix/dense vector multiplication threshold.This threshold represents the syste...
Definition: Thresholds.h:243
ResultType::TransposeType TransposeType
Transpose type for expression template evaluations.
Definition: SMatDVecMultExpr.h:128
VT::CompositeType VCT
Composite type of the right-hand side dense vector expression.
Definition: SMatDVecMultExpr.h:100
void smpSubAssign(DenseMatrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the SMP subtraction assignment of a matrix to dense matrix.
Definition: DenseMatrix.h:151
#define BLAZE_CONSTRAINT_MUST_BE_COLUMN_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a column dense or sparse vector type...
Definition: TransposeFlag.h:159
Efficient implementation of a compressed matrix.The CompressedMatrix class template is the represent...
Definition: CompressedMatrix.h:197
bool isAliased(const T *alias) const
Returns whether the expression is aliased with the given address alias.
Definition: SMatDVecMultExpr.h:294
void smpMultAssign(DenseVector< VT1, TF1 > &lhs, const Vector< VT2, TF2 > &rhs)
Default implementation of the SMP multiplication assignment of a vector to a dense vector...
Definition: DenseVector.h:178
Expression object for sparse matrix-dense vector multiplications.The SMatDVecMultExpr class represent...
Definition: Forward.h:89
RightOperand rightOperand() const
Returns the right-hand side dense vector operand.
Definition: SMatDVecMultExpr.h:270
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2384
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:249
Header file for the DenseVector base class.
SelectType< useAssign, const ResultType, const SMatDVecMultExpr & >::Type CompositeType
Data type for composite expression templates.
Definition: SMatDVecMultExpr.h:133
LeftOperand leftOperand() const
Returns the left-hand side sparse matrix operand.
Definition: SMatDVecMultExpr.h:260
Header file for the Computation base class.
MultTrait< MRT, VRT >::Type ResultType
Result type for expression template evaluations.
Definition: SMatDVecMultExpr.h:127
Header file for the RequiresEvaluation type trait.
Base class for sparse matrices.The SparseMatrix class is a base class for all sparse matrix classes...
Definition: Forward.h:104
SelectType< IsComputation< VT >::value, const VRT, VCT >::Type RT
Type for the assignment of the right-hand side dense matrix operand.
Definition: SMatDVecMultExpr.h:145
Constraint on the data type.
RightOperand vec_
Right-hand side dense vector of the multiplication expression.
Definition: SMatDVecMultExpr.h:322
VT::ResultType VRT
Result type of the right-hand side dense vector expression.
Definition: SMatDVecMultExpr.h:98
Constraint on the data type.
Header file for the MultExprTrait class template.
void smpAddAssign(DenseMatrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the SMP addition assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:121
Compile time check to query the requirement to evaluate an expression.Via this type trait it is possi...
Definition: RequiresEvaluation.h:90
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:251
LeftOperand mat_
Left-hand side sparse matrix of the multiplication expression.
Definition: SMatDVecMultExpr.h:321
Compile time type selection.The SelectType class template selects one of the two given types T1 and T...
Definition: SelectType.h:59
Header file for the DisableIf class template.
Header file for the multiplication trait.
Header file for the dense vector SMP implementation.
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2388
MCT LT
Type for the assignment of the left-hand side sparse matrix operand.
Definition: SMatDVecMultExpr.h:142
Header file for the IsMatMatMultExpr type trait class.
SelectType< IsExpression< MT >::value, const MT, const MT & >::Type LeftOperand
Composite type of the left-hand side sparse matrix expression.
Definition: SMatDVecMultExpr.h:136
void assign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the assignment of a matrix to a matrix.
Definition: Matrix.h:179
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
Constraint on the data type.
#define BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE(T)
Constraint on the data type.In case the given data type T is not a reference type, a compilation error is created.
Definition: Reference.h:78
Constraints on the storage order of matrix types.
Constraint on the data type.
void multAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the multiplication assignment of a matrix to a matrix.
Definition: Matrix.h:269
Header file for the SelectType class template.
Header file for all forward declarations for expression class templates.
Header file for the EnableIf class template.
SMatDVecMultExpr< MT, VT > This
Type of this SMatDVecMultExpr instance.
Definition: SMatDVecMultExpr.h:126
SelectType< IsExpression< VT >::value, const VT, const VT & >::Type RightOperand
Composite type of the right-hand side dense vector expression.
Definition: SMatDVecMultExpr.h:139
void smpAssign(DenseMatrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the SMP assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:91
Header file for the SubmatrixExprTrait class template.
#define BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a row-major dense or sparse matrix t...
Definition: StorageOrder.h:81
Removal of reference modifiers.The RemoveCV type trait removes any reference modifiers from the given...
Definition: RemoveReference.h:69
const ElementType ReturnType
Return type for expression template evaluations.
Definition: SMatDVecMultExpr.h:130
SMatDVecMultExpr(const MT &mat, const VT &vec)
Constructor for the SMatDVecMultExpr class.
Definition: SMatDVecMultExpr.h:162
bool canAlias(const T *alias) const
Returns whether the expression can alias with the given address alias.
Definition: SMatDVecMultExpr.h:282
bool isAligned() const
Returns whether the operands of the expression are properly aligned in memory.
Definition: SMatDVecMultExpr.h:304
Header file for run time assertion macros.
Base template for the MultTrait class.
Definition: MultTrait.h:141
void addAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the addition assignment of a matrix to a matrix.
Definition: Matrix.h:209
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
Header file for the reset shim.
void subAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the subtraction assignment of a matrix to matrix.
Definition: Matrix.h:239
size_t size() const
Returns the current size/dimension of the vector.
Definition: SMatDVecMultExpr.h:250
Header file for the RemoveReference type trait.
MT::CompositeType MCT
Composite type of the left-hand side sparse matrix expression.
Definition: SMatDVecMultExpr.h:99
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional vector type...
Definition: DenseVector.h:79
Header file for the IsComputation type trait class.
Header file for the sparse vector SMP implementation.
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:105
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:157
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2379
size_t columns(const Matrix< MT, SO > &m)
Returns the current number of columns of the matrix.
Definition: Matrix.h:154
Header file for basic type definitions.
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SMatDVecMultExpr.h:176
Header file for the SubvectorExprTrait class template.
Header file for the MatVecMultExpr base class.
Size type of the Blaze library.
Header file for the thresholds for matrix/vector and matrix/matrix multiplications.
bool canSMPAssign() const
Returns whether the expression can be used in SMP assignments.
Definition: SMatDVecMultExpr.h:314
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional matrix type...
Definition: SparseMatrix.h:79
ResultType::ElementType ElementType
Resulting element type.
Definition: SMatDVecMultExpr.h:129
Header file for the IsExpression type trait class.
Header file for the FunctionTrace class.