Medial Code Documentation
Loading...
Searching...
No Matches
SparseDenseProduct.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_SPARSEDENSEPRODUCT_H
11#define EIGEN_SPARSEDENSEPRODUCT_H
12
13namespace Eigen {
14
15namespace internal {
16
17template <> struct product_promote_storage_type<Sparse,Dense, OuterProduct> { typedef Sparse ret; };
18template <> struct product_promote_storage_type<Dense,Sparse, OuterProduct> { typedef Sparse ret; };
19
20template<typename SparseLhsType, typename DenseRhsType, typename DenseResType,
21 typename AlphaType,
22 int LhsStorageOrder = ((SparseLhsType::Flags&RowMajorBit)==RowMajorBit) ? RowMajor : ColMajor,
23 bool ColPerCol = ((DenseRhsType::Flags&RowMajorBit)==0) || DenseRhsType::ColsAtCompileTime==1>
25
26template<typename SparseLhsType, typename DenseRhsType, typename DenseResType>
28{
33 typedef evaluator<Lhs> LhsEval;
34 static void run(const SparseLhsType& lhs, const DenseRhsType& rhs, DenseResType& res, const typename Res::Scalar& alpha)
35 {
36 LhsEval lhsEval(lhs);
37
38 Index n = lhs.outerSize();
39#ifdef EIGEN_HAS_OPENMP
40 Eigen::initParallel();
41 Index threads = Eigen::nbThreads();
42#endif
43
44 for(Index c=0; c<rhs.cols(); ++c)
45 {
46#ifdef EIGEN_HAS_OPENMP
47 // This 20000 threshold has been found experimentally on 2D and 3D Poisson problems.
48 // It basically represents the minimal amount of work to be done to be worth it.
49 if(threads>1 && lhsEval.nonZerosEstimate() > 20000)
50 {
51 #pragma omp parallel for schedule(static) num_threads(threads)
52 for(Index i=0; i<n; ++i)
53 processRow(lhsEval,rhs,res,alpha,i,c);
54 }
55 else
56#endif
57 {
58 for(Index i=0; i<n; ++i)
59 processRow(lhsEval,rhs,res,alpha,i,c);
60 }
61 }
62 }
63
64 static void processRow(const LhsEval& lhsEval, const DenseRhsType& rhs, DenseResType& res, const typename Res::Scalar& alpha, Index i, Index col)
65 {
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;
70 }
71
72};
73
74// FIXME: what is the purpose of the following specialization? Is it for the BlockedSparse format?
75template<typename T1, typename T2/*, int _Options, typename _StrideType*/>
76struct scalar_product_traits<T1, Ref<T2/*, _Options, _StrideType*/> >
77{
78 enum {
79 Defined = 1
80 };
81 typedef typename CwiseUnaryOp<scalar_multiple2_op<T1, typename T2::Scalar>, T2>::PlainObject ReturnType;
82};
83template<typename SparseLhsType, typename DenseRhsType, typename DenseResType, typename AlphaType>
85{
90 static void run(const SparseLhsType& lhs, const DenseRhsType& rhs, DenseResType& res, const AlphaType& alpha)
91 {
93 for(Index c=0; c<rhs.cols(); ++c)
94 {
95 for(Index j=0; j<lhs.outerSize(); ++j)
96 {
97// typename Res::Scalar rhs_j = alpha * rhs.coeff(j,c);
99 for(LhsInnerIterator it(lhsEval,j); it ;++it)
100 res.coeffRef(it.index(),c) += it.value() * rhs_j;
101 }
102 }
103 }
104};
105
106template<typename SparseLhsType, typename DenseRhsType, typename DenseResType>
108{
113 static void run(const SparseLhsType& lhs, const DenseRhsType& rhs, DenseResType& res, const typename Res::Scalar& alpha)
114 {
116 for(Index j=0; j<lhs.outerSize(); ++j)
117 {
118 typename Res::RowXpr res_j(res.row(j));
119 for(LhsInnerIterator it(lhsEval,j); it ;++it)
120 res_j += (alpha*it.value()) * rhs.row(it.index());
121 }
122 }
123};
124
125template<typename SparseLhsType, typename DenseRhsType, typename DenseResType>
127{
132 static void run(const SparseLhsType& lhs, const DenseRhsType& rhs, DenseResType& res, const typename Res::Scalar& alpha)
133 {
135 for(Index j=0; j<lhs.outerSize(); ++j)
136 {
137 typename Rhs::ConstRowXpr rhs_j(rhs.row(j));
138 for(LhsInnerIterator it(lhsEval,j); it ;++it)
139 res.row(it.index()) += (alpha*it.value()) * rhs_j;
140 }
141 }
142};
143
144template<typename SparseLhsType, typename DenseRhsType, typename DenseResType,typename AlphaType>
145inline void sparse_time_dense_product(const SparseLhsType& lhs, const DenseRhsType& rhs, DenseResType& res, const AlphaType& alpha)
146{
148}
149
150} // end namespace internal
151
152namespace internal {
153
154template<typename Lhs, typename Rhs, int ProductType>
156 : generic_product_impl_base<Lhs,Rhs,generic_product_impl<Lhs,Rhs,SparseShape,DenseShape,ProductType> >
157{
158 typedef typename Product<Lhs,Rhs>::Scalar Scalar;
159
160 template<typename Dest>
161 static void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha)
162 {
163 typedef typename nested_eval<Lhs,((Rhs::Flags&RowMajorBit)==0) ? 1 : Rhs::ColsAtCompileTime>::type LhsNested;
164 typedef typename nested_eval<Rhs,((Lhs::Flags&RowMajorBit)==0) ? 1 : Dynamic>::type RhsNested;
165 LhsNested lhsNested(lhs);
166 RhsNested rhsNested(rhs);
167 internal::sparse_time_dense_product(lhsNested, rhsNested, dst, alpha);
168 }
169};
170
171template<typename Lhs, typename Rhs, int ProductType>
173 : generic_product_impl<Lhs, Rhs, SparseShape, DenseShape, ProductType>
174{};
175
176template<typename Lhs, typename Rhs, int ProductType>
178 : generic_product_impl_base<Lhs,Rhs,generic_product_impl<Lhs,Rhs,DenseShape,SparseShape,ProductType> >
179{
180 typedef typename Product<Lhs,Rhs>::Scalar Scalar;
181
182 template<typename Dst>
183 static void scaleAndAddTo(Dst& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha)
184 {
185 typedef typename nested_eval<Lhs,((Rhs::Flags&RowMajorBit)==0) ? Dynamic : 1>::type LhsNested;
186 typedef typename nested_eval<Rhs,((Lhs::Flags&RowMajorBit)==RowMajorBit) ? 1 : Lhs::RowsAtCompileTime>::type RhsNested;
187 LhsNested lhsNested(lhs);
188 RhsNested rhsNested(rhs);
189
190 // transpose everything
192 internal::sparse_time_dense_product(rhsNested.transpose(), lhsNested.transpose(), dstT, alpha);
193 }
194};
195
196template<typename Lhs, typename Rhs, int ProductType>
198 : generic_product_impl<Lhs, Rhs, DenseShape, SparseShape, ProductType>
199{};
200
201template<typename LhsT, typename RhsT, bool NeedToTranspose>
203{
204protected:
208
209 // if the actual left-hand side is a dense vector,
210 // then build a sparse-view so that we can seamlessly iterate over it.
214 Lhs1 const&, SparseView<Lhs1> >::type LhsArg;
215
218 typedef typename evaluator<ActualLhs>::InnerIterator LhsIterator;
219 typedef typename ProdXprType::Scalar Scalar;
220
221public:
222 enum {
223 Flags = NeedToTranspose ? RowMajorBit : 0,
224 CoeffReadCost = HugeCost
225 };
226
227 class InnerIterator : public LhsIterator
228 {
229 public:
231 : LhsIterator(xprEval.m_lhsXprImpl, 0),
232 m_outer(outer),
233 m_empty(false),
234 m_factor(get(xprEval.m_rhsXprImpl, outer, typename internal::traits<ActualRhs>::StorageKind() ))
235 {}
236
237 EIGEN_STRONG_INLINE Index outer() const { return m_outer; }
238 EIGEN_STRONG_INLINE Index row() const { return NeedToTranspose ? m_outer : LhsIterator::index(); }
239 EIGEN_STRONG_INLINE Index col() const { return NeedToTranspose ? LhsIterator::index() : m_outer; }
240
241 EIGEN_STRONG_INLINE Scalar value() const { return LhsIterator::value() * m_factor; }
242 EIGEN_STRONG_INLINE operator bool() const { return LhsIterator::operator bool() && (!m_empty); }
243
244 protected:
245 Scalar get(const RhsEval &rhs, Index outer, Dense = Dense()) const
246 {
247 return rhs.coeff(outer);
248 }
249
250 Scalar get(const RhsEval &rhs, Index outer, Sparse = Sparse())
251 {
252 typename RhsEval::InnerIterator it(rhs, outer);
253 if (it && it.index()==0 && it.value()!=Scalar(0))
254 return it.value();
255 m_empty = true;
256 return Scalar(0);
257 }
258
259 Index m_outer;
260 bool m_empty;
261 Scalar m_factor;
262 };
263
265 : m_lhs(lhs), m_lhsXprImpl(m_lhs), m_rhsXprImpl(rhs)
266 {
267 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
268 }
269
270 // transpose case
271 sparse_dense_outer_product_evaluator(const ActualRhs &rhs, const Lhs1 &lhs)
272 : m_lhs(lhs), m_lhsXprImpl(m_lhs), m_rhsXprImpl(rhs)
273 {
274 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
275 }
276
277protected:
278 const LhsArg m_lhs;
279 evaluator<ActualLhs> m_lhsXprImpl;
280 evaluator<ActualRhs> m_rhsXprImpl;
281};
282
283// sparse * dense outer product
284template<typename Lhs, typename Rhs>
285struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, OuterProduct, SparseShape, DenseShape>
286 : sparse_dense_outer_product_evaluator<Lhs,Rhs, Lhs::IsRowMajor>
287{
289
291 typedef typename XprType::PlainObject PlainObject;
292
293 explicit product_evaluator(const XprType& xpr)
294 : Base(xpr.lhs(), xpr.rhs())
295 {}
296
297};
298
299template<typename Lhs, typename Rhs>
300struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, OuterProduct, DenseShape, SparseShape>
301 : sparse_dense_outer_product_evaluator<Lhs,Rhs, Rhs::IsRowMajor>
302{
304
306 typedef typename XprType::PlainObject PlainObject;
307
308 explicit product_evaluator(const XprType& xpr)
309 : Base(xpr.lhs(), xpr.rhs())
310 {}
311
312};
313
314} // end namespace internal
315
316} // end namespace Eigen
317
318#endif // EIGEN_SPARSEDENSEPRODUCT_H
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:111
A matrix or vector expression mapping an existing expression.
Definition Ref.h:188
Pseudo expression representing a solving operation.
Definition Solve.h:63
@ ColMajor
Storage order is column major (see TopicStorageOrders).
Definition Constants.h:320
@ RowMajor
Storage order is row major (see TopicStorageOrders).
Definition Constants.h:322
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
Definition Constants.h:511
The type used to identify a dense storage.
Definition Constants.h:490
Definition Constants.h:520
The type used to identify a general sparse storage.
Definition Constants.h:493
Definition SparseUtil.h:137
Definition CoreEvaluators.h:82
Definition ProductEvaluators.h:320
Definition ProductEvaluators.h:81
Definition XprHelper.h:398
Definition ForwardDeclarations.h:165
Definition SparseDenseProduct.h:24
Definition ForwardDeclarations.h:17
Definition Meta.h:30