10#ifndef EIGEN_SPARSEDENSEPRODUCT_H
11#define EIGEN_SPARSEDENSEPRODUCT_H
20template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType,
23 bool ColPerCol = ((DenseRhsType::Flags&
RowMajorBit)==0) || DenseRhsType::ColsAtCompileTime==1>
26template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType>
29 typedef typename internal::remove_all<SparseLhsType>::type Lhs;
30 typedef typename internal::remove_all<DenseRhsType>::type Rhs;
31 typedef typename internal::remove_all<DenseResType>::type Res;
34 static void run(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const typename Res::Scalar& alpha)
38 Index n = lhs.outerSize();
39#ifdef EIGEN_HAS_OPENMP
44 for(
Index c=0; c<rhs.cols(); ++c)
46#ifdef EIGEN_HAS_OPENMP
49 if(threads>1 && lhsEval.nonZerosEstimate() > 20000)
51 #pragma omp parallel for schedule(dynamic,(n+threads*4-1)/(threads*4)) num_threads(threads)
52 for(
Index i=0; i<n; ++i)
53 processRow(lhsEval,rhs,res,alpha,i,c);
58 for(
Index i=0; i<n; ++i)
59 processRow(lhsEval,rhs,res,alpha,i,c);
64 static void processRow(
const LhsEval& lhsEval,
const DenseRhsType& rhs, DenseResType& res,
const typename Res::Scalar& alpha,
Index i,
Index col)
66 typename Res::Scalar tmp(0);
67 for(LhsInnerIterator it(lhsEval,i); it ;++it)
68 tmp += it.value() * rhs.coeff(it.index(),col);
69 res.coeffRef(i,col) += alpha * tmp;
85template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType,
typename AlphaType>
88 typedef typename internal::remove_all<SparseLhsType>::type Lhs;
89 typedef typename internal::remove_all<DenseRhsType>::type Rhs;
90 typedef typename internal::remove_all<DenseResType>::type Res;
92 typedef typename LhsEval::InnerIterator LhsInnerIterator;
93 static void run(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const AlphaType& alpha)
96 for(
Index c=0; c<rhs.cols(); ++c)
98 for(
Index j=0; j<lhs.outerSize(); ++j)
102 for(LhsInnerIterator it(lhsEval,j); it ;++it)
103 res.coeffRef(it.index(),c) += it.value() * rhs_j;
109template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType>
112 typedef typename internal::remove_all<SparseLhsType>::type Lhs;
113 typedef typename internal::remove_all<DenseRhsType>::type Rhs;
114 typedef typename internal::remove_all<DenseResType>::type Res;
116 typedef typename LhsEval::InnerIterator LhsInnerIterator;
117 static void run(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const typename Res::Scalar& alpha)
119 Index n = lhs.rows();
122#ifdef EIGEN_HAS_OPENMP
127 if(threads>1 && lhsEval.nonZerosEstimate()*rhs.cols() > 20000)
129 #pragma omp parallel for schedule(dynamic,(n+threads*4-1)/(threads*4)) num_threads(threads)
130 for(
Index i=0; i<n; ++i)
131 processRow(lhsEval,rhs,res,alpha,i);
136 for(
Index i=0; i<n; ++i)
137 processRow(lhsEval, rhs, res, alpha, i);
141 static void processRow(
const LhsEval& lhsEval,
const DenseRhsType& rhs, Res& res,
const typename Res::Scalar& alpha,
Index i)
143 typename Res::RowXpr res_i(res.row(i));
144 for(LhsInnerIterator it(lhsEval,i); it ;++it)
145 res_i += (alpha*it.value()) * rhs.row(it.index());
149template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType>
152 typedef typename internal::remove_all<SparseLhsType>::type Lhs;
153 typedef typename internal::remove_all<DenseRhsType>::type Rhs;
154 typedef typename internal::remove_all<DenseResType>::type Res;
156 static void run(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const typename Res::Scalar& alpha)
159 for(
Index j=0; j<lhs.outerSize(); ++j)
161 typename Rhs::ConstRowXpr rhs_j(rhs.row(j));
162 for(LhsInnerIterator it(lhsEval,j); it ;++it)
163 res.row(it.index()) += (alpha*it.value()) * rhs_j;
168template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType,
typename AlphaType>
169inline void sparse_time_dense_product(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const AlphaType& alpha)
178template<
typename Lhs,
typename Rhs,
int ProductType>
184 template<
typename Dest>
185 static void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha)
187 typedef typename nested_eval<Lhs,((Rhs::Flags&
RowMajorBit)==0) ? 1 : Rhs::ColsAtCompileTime>::type LhsNested;
189 LhsNested lhsNested(lhs);
190 RhsNested rhsNested(rhs);
191 internal::sparse_time_dense_product(lhsNested, rhsNested, dst, alpha);
195template<
typename Lhs,
typename Rhs,
int ProductType>
200template<
typename Lhs,
typename Rhs,
int ProductType>
206 template<
typename Dst>
207 static void scaleAndAddTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha)
211 LhsNested lhsNested(lhs);
212 RhsNested rhsNested(rhs);
216 internal::sparse_time_dense_product(rhsNested.transpose(), lhsNested.transpose(), dstT, alpha);
220template<
typename Lhs,
typename Rhs,
int ProductType>
225template<
typename LhsT,
typename RhsT,
bool NeedToTranspose>
243 typedef typename ProdXprType::Scalar Scalar;
255 : LhsIterator(xprEval.m_lhsXprImpl, 0),
261 EIGEN_STRONG_INLINE
Index outer()
const {
return m_outer; }
262 EIGEN_STRONG_INLINE
Index row()
const {
return NeedToTranspose ? m_outer : LhsIterator::index(); }
263 EIGEN_STRONG_INLINE
Index col()
const {
return NeedToTranspose ? LhsIterator::index() : m_outer; }
265 EIGEN_STRONG_INLINE Scalar value()
const {
return LhsIterator::value() * m_factor; }
266 EIGEN_STRONG_INLINE
operator bool()
const {
return LhsIterator::operator bool() && (!m_empty); }
271 return rhs.coeff(outer);
276 typename RhsEval::InnerIterator it(rhs, outer);
277 if (it && it.index()==0 && it.value()!=Scalar(0))
289 : m_lhs(lhs), m_lhsXprImpl(m_lhs), m_rhsXprImpl(rhs)
291 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
296 : m_lhs(lhs), m_lhsXprImpl(m_lhs), m_rhsXprImpl(rhs)
298 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
303 evaluator<ActualLhs> m_lhsXprImpl;
304 evaluator<ActualRhs> m_rhsXprImpl;
308template<
typename Lhs,
typename Rhs>
315 typedef typename XprType::PlainObject PlainObject;
318 :
Base(xpr.lhs(), xpr.rhs())
323template<
typename Lhs,
typename Rhs>
330 typedef typename XprType::PlainObject PlainObject;
333 :
Base(xpr.lhs(), xpr.rhs())
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:75
Expression of a dense or sparse matrix with zero or too small values removed.
Definition SparseView.h:46
Expression of the transpose of a matrix.
Definition Transpose.h:54
Definition SparseDenseProduct.h:252
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
const unsigned int RowMajorBit
Definition Constants.h:66
Namespace containing all symbols from the Eigen library.
Definition common.h:81
const int HugeCost
Definition Constants.h:44
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:74
void initParallel()
Definition Parallelizer.h:53
int nbThreads()
Definition Parallelizer.h:63
const int Dynamic
Definition Constants.h:22
Definition Constants.h:528
Definition Constants.h:507
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition XprHelper.h:806
Definition Constants.h:537
Definition Constants.h:510
Definition SparseUtil.h:137
Definition CoreEvaluators.h:91
Definition ProductEvaluators.h:344
Definition ProductEvaluators.h:86
Definition XprHelper.h:458
Definition ForwardDeclarations.h:164
Definition SparseDenseProduct.h:227
Definition SparseDenseProduct.h:24
Definition ForwardDeclarations.h:17