SparseSparseProductWithPruning.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2014 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_SPARSESPARSEPRODUCTWITHPRUNING_H
11 #define EIGEN_SPARSESPARSEPRODUCTWITHPRUNING_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 // perform a pseudo in-place sparse * sparse product assuming all matrices are col major
21 template <typename Lhs, typename Rhs, typename ResultType>
22 static void sparse_sparse_product_with_pruning_impl(const Lhs& lhs, const Rhs& rhs, ResultType& res,
23  const typename ResultType::RealScalar& tolerance) {
24  // return sparse_sparse_product_with_pruning_impl2(lhs,rhs,res);
25 
26  typedef typename remove_all_t<Rhs>::Scalar RhsScalar;
27  typedef typename remove_all_t<ResultType>::Scalar ResScalar;
28  typedef typename remove_all_t<Lhs>::StorageIndex StorageIndex;
29 
30  // make sure to call innerSize/outerSize since we fake the storage order.
31  Index rows = lhs.innerSize();
32  Index cols = rhs.outerSize();
33  // Index size = lhs.outerSize();
34  eigen_assert(lhs.outerSize() == rhs.innerSize());
35 
36  // allocate a temporary buffer
38 
39  // mimics a resizeByInnerOuter:
40  if (ResultType::IsRowMajor)
41  res.resize(cols, rows);
42  else
43  res.resize(rows, cols);
44 
45  evaluator<Lhs> lhsEval(lhs);
46  evaluator<Rhs> rhsEval(rhs);
47 
48  // estimate the number of non zero entries
49  // given a rhs column containing Y non zeros, we assume that the respective Y columns
50  // of the lhs differs in average of one non zeros, thus the number of non zeros for
51  // the product of a rhs column with the lhs is X+Y where X is the average number of non zero
52  // per column of the lhs.
53  // Therefore, we have nnz(lhs*rhs) = nnz(lhs) + nnz(rhs)
54  Index estimated_nnz_prod = lhsEval.nonZerosEstimate() + rhsEval.nonZerosEstimate();
55 
56  res.reserve(estimated_nnz_prod);
57  double ratioColRes = double(estimated_nnz_prod) / (double(lhs.rows()) * double(rhs.cols()));
58  for (Index j = 0; j < cols; ++j) {
59  // FIXME:
60  // double ratioColRes = (double(rhs.innerVector(j).nonZeros()) +
61  // double(lhs.nonZeros())/double(lhs.cols()))/double(lhs.rows());
62  // let's do a more accurate determination of the nnz ratio for the current column j of res
63  tempVector.init(ratioColRes);
64  tempVector.setZero();
65  for (typename evaluator<Rhs>::InnerIterator rhsIt(rhsEval, j); rhsIt; ++rhsIt) {
66  // FIXME should be written like this: tmp += rhsIt.value() * lhs.col(rhsIt.index())
67  tempVector.restart();
68  RhsScalar x = rhsIt.value();
69  for (typename evaluator<Lhs>::InnerIterator lhsIt(lhsEval, rhsIt.index()); lhsIt; ++lhsIt) {
70  tempVector.coeffRef(lhsIt.index()) += lhsIt.value() * x;
71  }
72  }
73  res.startVec(j);
74  for (typename AmbiVector<ResScalar, StorageIndex>::Iterator it(tempVector, tolerance); it; ++it)
75  res.insertBackByOuterInner(j, it.index()) = it.value();
76  }
77  res.finalize();
78 }
79 
80 template <typename Lhs, typename Rhs, typename ResultType, int LhsStorageOrder = traits<Lhs>::Flags & RowMajorBit,
81  int RhsStorageOrder = traits<Rhs>::Flags & RowMajorBit,
82  int ResStorageOrder = traits<ResultType>::Flags & RowMajorBit>
84 
85 template <typename Lhs, typename Rhs, typename ResultType>
88 
89  static void run(const Lhs& lhs, const Rhs& rhs, ResultType& res, const RealScalar& tolerance) {
90  remove_all_t<ResultType> res_(res.rows(), res.cols());
91  internal::sparse_sparse_product_with_pruning_impl<Lhs, Rhs, ResultType>(lhs, rhs, res_, tolerance);
92  res.swap(res_);
93  }
94 };
95 
96 template <typename Lhs, typename Rhs, typename ResultType>
99  static void run(const Lhs& lhs, const Rhs& rhs, ResultType& res, const RealScalar& tolerance) {
100  // we need a col-major matrix to hold the result
102  SparseTemporaryType res_(res.rows(), res.cols());
103  internal::sparse_sparse_product_with_pruning_impl<Lhs, Rhs, SparseTemporaryType>(lhs, rhs, res_, tolerance);
104  res = res_;
105  }
106 };
107 
108 template <typename Lhs, typename Rhs, typename ResultType>
111  static void run(const Lhs& lhs, const Rhs& rhs, ResultType& res, const RealScalar& tolerance) {
112  // let's transpose the product to get a column x column product
113  remove_all_t<ResultType> res_(res.rows(), res.cols());
114  internal::sparse_sparse_product_with_pruning_impl<Rhs, Lhs, ResultType>(rhs, lhs, res_, tolerance);
115  res.swap(res_);
116  }
117 };
118 
119 template <typename Lhs, typename Rhs, typename ResultType>
122  static void run(const Lhs& lhs, const Rhs& rhs, ResultType& res, const RealScalar& tolerance) {
125  ColMajorMatrixLhs colLhs(lhs);
126  ColMajorMatrixRhs colRhs(rhs);
127  internal::sparse_sparse_product_with_pruning_impl<ColMajorMatrixLhs, ColMajorMatrixRhs, ResultType>(colLhs, colRhs,
128  res, tolerance);
129 
130  // let's transpose the product to get a column x column product
131  // typedef SparseMatrix<typename ResultType::Scalar> SparseTemporaryType;
132  // SparseTemporaryType res_(res.cols(), res.rows());
133  // sparse_sparse_product_with_pruning_impl<Rhs,Lhs,SparseTemporaryType>(rhs, lhs, res_);
134  // res = res_.transpose();
135  }
136 };
137 
138 template <typename Lhs, typename Rhs, typename ResultType>
141  static void run(const Lhs& lhs, const Rhs& rhs, ResultType& res, const RealScalar& tolerance) {
143  RowMajorMatrixLhs rowLhs(lhs);
145  res, tolerance);
146  }
147 };
148 
149 template <typename Lhs, typename Rhs, typename ResultType>
152  static void run(const Lhs& lhs, const Rhs& rhs, ResultType& res, const RealScalar& tolerance) {
154  RowMajorMatrixRhs rowRhs(rhs);
156  lhs, rowRhs, res, tolerance);
157  }
158 };
159 
160 template <typename Lhs, typename Rhs, typename ResultType>
163  static void run(const Lhs& lhs, const Rhs& rhs, ResultType& res, const RealScalar& tolerance) {
165  ColMajorMatrixRhs colRhs(rhs);
166  internal::sparse_sparse_product_with_pruning_impl<Lhs, ColMajorMatrixRhs, ResultType>(lhs, colRhs, res, tolerance);
167  }
168 };
169 
170 template <typename Lhs, typename Rhs, typename ResultType>
173  static void run(const Lhs& lhs, const Rhs& rhs, ResultType& res, const RealScalar& tolerance) {
175  ColMajorMatrixLhs colLhs(lhs);
176  internal::sparse_sparse_product_with_pruning_impl<ColMajorMatrixLhs, Rhs, ResultType>(colLhs, rhs, res, tolerance);
177  }
178 };
179 
180 } // end namespace internal
181 
182 } // end namespace Eigen
183 
184 #endif // EIGEN_SPARSESPARSEPRODUCTWITHPRUNING_H
#define eigen_assert(x)
Definition: Macros.h:910
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
A versatible sparse matrix representation.
Definition: SparseMatrix.h:121
Definition: AmbiVector.h:251
Definition: AmbiVector.h:26
Scalar & coeffRef(Index i)
Definition: AmbiVector.h:171
void setZero()
Definition: AmbiVector.h:160
void init(double estimatedDensity)
Definition: AmbiVector.h:130
void restart()
Definition: AmbiVector.h:154
@ ColMajor
Definition: Constants.h:318
@ RowMajor
Definition: Constants.h:320
const unsigned int RowMajorBit
Definition: Constants.h:70
static void sparse_sparse_product_with_pruning_impl(const Lhs &lhs, const Rhs &rhs, ResultType &res, const typename ResultType::RealScalar &tolerance)
Definition: SparseSparseProductWithPruning.h:22
@ Lhs
Definition: TensorContractionMapper.h:20
@ Rhs
Definition: TensorContractionMapper.h:20
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Definition: Eigen_Colamd.h:49
list x
Definition: plotDoE.py:28
static void run(const Lhs &lhs, const Rhs &rhs, ResultType &res, const RealScalar &tolerance)
Definition: SparseSparseProductWithPruning.h:99
static void run(const Lhs &lhs, const Rhs &rhs, ResultType &res, const RealScalar &tolerance)
Definition: SparseSparseProductWithPruning.h:111
static void run(const Lhs &lhs, const Rhs &rhs, ResultType &res, const RealScalar &tolerance)
Definition: SparseSparseProductWithPruning.h:122
static void run(const Lhs &lhs, const Rhs &rhs, ResultType &res, const RealScalar &tolerance)
Definition: SparseSparseProductWithPruning.h:163
static void run(const Lhs &lhs, const Rhs &rhs, ResultType &res, const RealScalar &tolerance)
Definition: SparseSparseProductWithPruning.h:141
static void run(const Lhs &lhs, const Rhs &rhs, ResultType &res, const RealScalar &tolerance)
Definition: SparseSparseProductWithPruning.h:89
static void run(const Lhs &lhs, const Rhs &rhs, ResultType &res, const RealScalar &tolerance)
Definition: SparseSparseProductWithPruning.h:173
static void run(const Lhs &lhs, const Rhs &rhs, ResultType &res, const RealScalar &tolerance)
Definition: SparseSparseProductWithPruning.h:152
Definition: SparseSparseProductWithPruning.h:83
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2