10 #ifndef EIGEN_SPARSEDENSEPRODUCT_H 11 #define EIGEN_SPARSEDENSEPRODUCT_H 17 template <>
struct product_promote_storage_type<Sparse,Dense, OuterProduct> {
typedef Sparse ret; };
18 template <>
struct product_promote_storage_type<Dense,Sparse, OuterProduct> {
typedef Sparse ret; };
20 template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType,
23 bool ColPerCol = ((DenseRhsType::Flags&
RowMajorBit)==0) || DenseRhsType::ColsAtCompileTime==1>
24 struct sparse_time_dense_product_impl;
26 template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType>
27 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType, typename DenseResType::Scalar,
RowMajor, true>
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;
32 typedef typename evaluator<Lhs>::InnerIterator LhsInnerIterator;
33 typedef evaluator<Lhs> LhsEval;
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 40 Eigen::initParallel();
41 Index threads = Eigen::nbThreads();
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;
85 template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType,
typename AlphaType>
86 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType, AlphaType,
ColMajor, true>
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;
91 typedef typename evaluator<Lhs>::InnerIterator LhsInnerIterator;
92 static void run(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const AlphaType& alpha)
94 evaluator<Lhs> lhsEval(lhs);
95 for(
Index c=0; c<rhs.cols(); ++c)
97 for(
Index j=0; j<lhs.outerSize(); ++j)
100 typename ScalarBinaryOpTraits<AlphaType, typename Rhs::Scalar>::ReturnType rhs_j(alpha * rhs.coeff(j,c));
101 for(LhsInnerIterator it(lhsEval,j); it ;++it)
102 res.coeffRef(it.index(),c) += it.value() * rhs_j;
108 template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType>
109 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType, typename DenseResType::Scalar,
RowMajor, false>
111 typedef typename internal::remove_all<SparseLhsType>::type Lhs;
112 typedef typename internal::remove_all<DenseRhsType>::type Rhs;
113 typedef typename internal::remove_all<DenseResType>::type Res;
114 typedef typename evaluator<Lhs>::InnerIterator LhsInnerIterator;
115 static void run(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const typename Res::Scalar& alpha)
117 evaluator<Lhs> lhsEval(lhs);
118 for(
Index j=0; j<lhs.outerSize(); ++j)
120 typename Res::RowXpr res_j(res.row(j));
121 for(LhsInnerIterator it(lhsEval,j); it ;++it)
122 res_j += (alpha*it.value()) * rhs.row(it.index());
127 template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType>
128 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType, typename DenseResType::Scalar,
ColMajor, false>
130 typedef typename internal::remove_all<SparseLhsType>::type Lhs;
131 typedef typename internal::remove_all<DenseRhsType>::type Rhs;
132 typedef typename internal::remove_all<DenseResType>::type Res;
133 typedef typename evaluator<Lhs>::InnerIterator LhsInnerIterator;
134 static void run(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const typename Res::Scalar& alpha)
136 evaluator<Lhs> lhsEval(lhs);
137 for(
Index j=0; j<lhs.outerSize(); ++j)
139 typename Rhs::ConstRowXpr rhs_j(rhs.row(j));
140 for(LhsInnerIterator it(lhsEval,j); it ;++it)
141 res.row(it.index()) += (alpha*it.value()) * rhs_j;
146 template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType,
typename AlphaType>
147 inline void sparse_time_dense_product(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const AlphaType& alpha)
149 sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType, AlphaType>::run(lhs, rhs, res, alpha);
156 template<
typename Lhs,
typename Rhs,
int ProductType>
157 struct generic_product_impl<Lhs, Rhs, SparseShape, DenseShape, ProductType>
158 : generic_product_impl_base<Lhs,Rhs,generic_product_impl<Lhs,Rhs,SparseShape,DenseShape,ProductType> >
160 typedef typename Product<Lhs,Rhs>::Scalar Scalar;
162 template<
typename Dest>
163 static void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha)
165 typedef typename nested_eval<Lhs,((Rhs::Flags&RowMajorBit)==0) ? 1 : Rhs::ColsAtCompileTime>::type LhsNested;
166 typedef typename nested_eval<Rhs,((Lhs::Flags&RowMajorBit)==0) ? 1 : Dynamic>::type RhsNested;
167 LhsNested lhsNested(lhs);
168 RhsNested rhsNested(rhs);
169 internal::sparse_time_dense_product(lhsNested, rhsNested, dst, alpha);
173 template<
typename Lhs,
typename Rhs,
int ProductType>
174 struct generic_product_impl<Lhs, Rhs, SparseTriangularShape, DenseShape, ProductType>
175 : generic_product_impl<Lhs, Rhs, SparseShape, DenseShape, ProductType>
178 template<
typename Lhs,
typename Rhs,
int ProductType>
179 struct generic_product_impl<Lhs, Rhs, DenseShape, SparseShape, ProductType>
180 : generic_product_impl_base<Lhs,Rhs,generic_product_impl<Lhs,Rhs,DenseShape,SparseShape,ProductType> >
182 typedef typename Product<Lhs,Rhs>::Scalar Scalar;
184 template<
typename Dst>
185 static void scaleAndAddTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha)
187 typedef typename nested_eval<Lhs,((Rhs::Flags&RowMajorBit)==0) ? Dynamic : 1>::type LhsNested;
188 typedef typename nested_eval<Rhs,((Lhs::Flags&RowMajorBit)==RowMajorBit) ? 1 : Lhs::RowsAtCompileTime>::type RhsNested;
189 LhsNested lhsNested(lhs);
190 RhsNested rhsNested(rhs);
193 Transpose<Dst> dstT(dst);
194 internal::sparse_time_dense_product(rhsNested.transpose(), lhsNested.transpose(), dstT, alpha);
198 template<
typename Lhs,
typename Rhs,
int ProductType>
199 struct generic_product_impl<Lhs, Rhs, DenseShape, SparseTriangularShape, ProductType>
200 : generic_product_impl<Lhs, Rhs, DenseShape, SparseShape, ProductType>
203 template<
typename LhsT,
typename RhsT,
bool NeedToTranspose>
204 struct sparse_dense_outer_product_evaluator
207 typedef typename conditional<NeedToTranspose,RhsT,LhsT>::type Lhs1;
208 typedef typename conditional<NeedToTranspose,LhsT,RhsT>::type ActualRhs;
209 typedef Product<LhsT,RhsT,DefaultProduct> ProdXprType;
213 typedef typename conditional<is_same<typename internal::traits<Lhs1>::StorageKind,Sparse>::value,
214 Lhs1, SparseView<Lhs1> >::type ActualLhs;
215 typedef typename conditional<is_same<typename internal::traits<Lhs1>::StorageKind,Sparse>::value,
216 Lhs1
const&, SparseView<Lhs1> >::type LhsArg;
218 typedef evaluator<ActualLhs> LhsEval;
219 typedef evaluator<ActualRhs> RhsEval;
220 typedef typename evaluator<ActualLhs>::InnerIterator LhsIterator;
221 typedef typename ProdXprType::Scalar Scalar;
229 class InnerIterator :
public LhsIterator
232 InnerIterator(
const sparse_dense_outer_product_evaluator &xprEval,
Index outer)
233 : LhsIterator(xprEval.m_lhsXprImpl, 0),
236 m_factor(get(xprEval.m_rhsXprImpl, outer, typename
internal::traits<ActualRhs>::StorageKind() ))
239 EIGEN_STRONG_INLINE
Index outer()
const {
return m_outer; }
240 EIGEN_STRONG_INLINE
Index row()
const {
return NeedToTranspose ? m_outer : LhsIterator::index(); }
241 EIGEN_STRONG_INLINE
Index col()
const {
return NeedToTranspose ? LhsIterator::index() : m_outer; }
243 EIGEN_STRONG_INLINE Scalar value()
const {
return LhsIterator::value() * m_factor; }
244 EIGEN_STRONG_INLINE
operator bool()
const {
return LhsIterator::operator bool() && (!m_empty); }
247 Scalar
get(
const RhsEval &rhs,
Index outer, Dense = Dense())
const 249 return rhs.coeff(outer);
252 Scalar
get(
const RhsEval &rhs,
Index outer, Sparse = Sparse())
254 typename RhsEval::InnerIterator it(rhs, outer);
255 if (it && it.index()==0 && it.value()!=Scalar(0))
266 sparse_dense_outer_product_evaluator(
const Lhs1 &lhs,
const ActualRhs &rhs)
267 : m_lhs(lhs), m_lhsXprImpl(m_lhs), m_rhsXprImpl(rhs)
269 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
273 sparse_dense_outer_product_evaluator(
const ActualRhs &rhs,
const Lhs1 &lhs)
274 : m_lhs(lhs), m_lhsXprImpl(m_lhs), m_rhsXprImpl(rhs)
276 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
281 evaluator<ActualLhs> m_lhsXprImpl;
282 evaluator<ActualRhs> m_rhsXprImpl;
286 template<
typename Lhs,
typename Rhs>
287 struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, OuterProduct, SparseShape, DenseShape>
288 : sparse_dense_outer_product_evaluator<Lhs,Rhs, Lhs::IsRowMajor>
290 typedef sparse_dense_outer_product_evaluator<Lhs,Rhs, Lhs::IsRowMajor> Base;
292 typedef Product<Lhs, Rhs> XprType;
293 typedef typename XprType::PlainObject PlainObject;
295 explicit product_evaluator(
const XprType& xpr)
296 : Base(xpr.lhs(), xpr.rhs())
301 template<
typename Lhs,
typename Rhs>
302 struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, OuterProduct, DenseShape, SparseShape>
303 : sparse_dense_outer_product_evaluator<Lhs,Rhs, Rhs::IsRowMajor>
305 typedef sparse_dense_outer_product_evaluator<Lhs,Rhs, Rhs::IsRowMajor> Base;
307 typedef Product<Lhs, Rhs> XprType;
308 typedef typename XprType::PlainObject PlainObject;
310 explicit product_evaluator(
const XprType& xpr)
311 : Base(xpr.lhs(), xpr.rhs())
320 #endif // EIGEN_SPARSEDENSEPRODUCT_H Definition: Constants.h:320
const int HugeCost
Definition: Constants.h:39
Namespace containing all symbols from the Eigen library.
Definition: Core:287
const unsigned int RowMajorBit
Definition: Constants.h:61
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Definition: Eigen_Colamd.h:50
Definition: Constants.h:322