11#ifndef EIGEN_CONSERVATIVESPARSESPARSEPRODUCT_H
12#define EIGEN_CONSERVATIVESPARSESPARSEPRODUCT_H
15#include "./InternalHeaderCheck.h"
21template <
typename Lhs,
typename Rhs,
typename ResultType,
bool ReverseProduct = false>
22static void conservative_sparse_sparse_product_impl(
const Lhs& lhs,
const Rhs& rhs, ResultType& res,
23 bool sortedInsertion =
false) {
24 using LhsScalar =
typename remove_all_t<Lhs>::Scalar;
25 using RhsScalar =
typename remove_all_t<Rhs>::Scalar;
26 using ResScalar =
typename remove_all_t<ResultType>::Scalar;
27 const sparse_scalar_product_op<LhsScalar, RhsScalar, ReverseProduct> product;
30 Index rows = lhs.innerSize();
31 Index cols = rhs.outerSize();
32 eigen_assert(lhs.outerSize() == rhs.innerSize());
34 ei_declare_aligned_stack_constructed_variable(
bool, mask, rows, 0);
35 ei_declare_aligned_stack_constructed_variable(ResScalar, values, rows, 0);
36 ei_declare_aligned_stack_constructed_variable(Index, indices, rows, 0);
38 std::fill_n(mask, rows,
false);
40 evaluator<Lhs> lhsEval(lhs);
41 evaluator<Rhs> rhsEval(rhs);
49 Index estimated_nnz_prod = lhsEval.nonZerosEstimate() + rhsEval.nonZerosEstimate();
52 res.reserve(Index(estimated_nnz_prod));
54 for (Index j = 0; j < cols; ++j) {
57 for (
typename evaluator<Rhs>::InnerIterator rhsIt(rhsEval, j); rhsIt; ++rhsIt) {
58 RhsScalar y = rhsIt.value();
59 Index k = rhsIt.index();
60 for (
typename evaluator<Lhs>::InnerIterator lhsIt(lhsEval, k); lhsIt; ++lhsIt) {
61 Index i = lhsIt.index();
62 LhsScalar x = lhsIt.value();
65 values[i] = product(x, y);
69 product.accumulate(values[i], x, y);
72 if (!sortedInsertion) {
74 for (Index k = 0; k < nnz; ++k) {
76 res.insertBackByOuterInnerUnordered(j, i) = values[i];
81 const Index t200 = rows / 11;
82 const Index t = (rows * 100) / 139;
88 if ((nnz < 200 && nnz < t200) || nnz * numext::log2(
int(nnz)) < t) {
89 if (nnz > 1) std::sort(indices, indices + nnz);
90 for (Index k = 0; k < nnz; ++k) {
92 res.insertBackByOuterInner(j, i) = values[i];
97 for (Index i = 0; i < rows; ++i) {
100 res.insertBackByOuterInner(j, i) = values[i];
110template <
class Source,
int Order>
111using WithStorageOrder = SparseMatrix<typename Source::Scalar, Order, typename Source::StorageIndex>;
113template <
typename Lhs,
typename Rhs,
typename ResultType,
117struct conservative_sparse_sparse_product_selector;
119template <
typename Lhs,
typename Rhs,
typename ResultType>
120struct conservative_sparse_sparse_product_selector<Lhs, Rhs, ResultType, ColMajor, ColMajor, ColMajor> {
121 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
122 using RowMajorMatrix = WithStorageOrder<ResultType, RowMajor>;
123 using ColMajorMatrixAux = WithStorageOrder<ResultType, ColMajor>;
129 if (lhs.rows() > rhs.cols()) {
130 using ColMajorMatrix =
typename sparse_eval<ColMajorMatrixAux, ResultType::RowsAtCompileTime,
131 ResultType::ColsAtCompileTime, ColMajorMatrixAux::Flags>::type;
132 ColMajorMatrix resCol(lhs.rows(), rhs.cols());
134 internal::conservative_sparse_sparse_product_impl<Lhs, Rhs, ColMajorMatrix>(lhs, rhs, resCol,
true);
135 res = resCol.markAsRValue();
137 ColMajorMatrixAux resCol(lhs.rows(), rhs.cols());
139 internal::conservative_sparse_sparse_product_impl<Lhs, Rhs, ColMajorMatrixAux>(lhs, rhs, resCol,
false);
140 RowMajorMatrix resRow(resCol);
141 res = resRow.markAsRValue();
146template <
typename Lhs,
typename Rhs,
typename ResultType>
148 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
149 using RowMajorRhs = WithStorageOrder<Rhs, RowMajor>;
150 using RowMajorRes = WithStorageOrder<ResultType, RowMajor>;
151 RowMajorRhs rhsRow = rhs;
152 RowMajorRes resRow(lhs.rows(), rhs.cols());
153 internal::conservative_sparse_sparse_product_impl<RowMajorRhs, Lhs, RowMajorRes, true>(rhsRow, lhs, resRow);
158template <
typename Lhs,
typename Rhs,
typename ResultType>
160 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
161 using RowMajorLhs = WithStorageOrder<Lhs, RowMajor>;
162 using RowMajorRes = WithStorageOrder<ResultType, RowMajor>;
163 RowMajorLhs lhsRow = lhs;
164 RowMajorRes resRow(lhs.rows(), rhs.cols());
165 internal::conservative_sparse_sparse_product_impl<Rhs, RowMajorLhs, RowMajorRes, true>(rhs, lhsRow, resRow);
170template <
typename Lhs,
typename Rhs,
typename ResultType>
172 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
173 using RowMajorRes = WithStorageOrder<ResultType, RowMajor>;
174 RowMajorRes resRow(lhs.rows(), rhs.cols());
175 internal::conservative_sparse_sparse_product_impl<Rhs, Lhs, RowMajorRes, true>(rhs, lhs, resRow);
180template <
typename Lhs,
typename Rhs,
typename ResultType>
182 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
183 using ColMajorRes = WithStorageOrder<ResultType, ColMajor>;
184 ColMajorRes resCol(lhs.rows(), rhs.cols());
185 internal::conservative_sparse_sparse_product_impl<Lhs, Rhs, ColMajorRes>(lhs, rhs, resCol);
190template <
typename Lhs,
typename Rhs,
typename ResultType>
192 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
193 using ColMajorLhs = WithStorageOrder<Lhs, ColMajor>;
194 using ColMajorRes = WithStorageOrder<ResultType, ColMajor>;
195 ColMajorLhs lhsCol = lhs;
196 ColMajorRes resCol(lhs.rows(), rhs.cols());
197 internal::conservative_sparse_sparse_product_impl<ColMajorLhs, Rhs, ColMajorRes>(lhsCol, rhs, resCol);
202template <
typename Lhs,
typename Rhs,
typename ResultType>
204 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
205 using ColMajorRhs = WithStorageOrder<Rhs, ColMajor>;
206 using ColMajorRes = WithStorageOrder<ResultType, ColMajor>;
207 ColMajorRhs rhsCol = rhs;
208 ColMajorRes resCol(lhs.rows(), rhs.cols());
209 internal::conservative_sparse_sparse_product_impl<Lhs, ColMajorRhs, ColMajorRes>(lhs, rhsCol, resCol);
214template <
typename Lhs,
typename Rhs,
typename ResultType>
216 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
217 using ColMajorRes = WithStorageOrder<ResultType, ColMajor>;
218 using RowMajorRes = WithStorageOrder<ResultType, RowMajor>;
219 RowMajorRes resRow(lhs.rows(), rhs.cols());
220 internal::conservative_sparse_sparse_product_impl<Rhs, Lhs, RowMajorRes, true>(rhs, lhs, resRow);
222 ColMajorRes resCol(resRow);
227template <
typename Lhs,
typename Rhs,
typename ResultType,
bool ReverseProduct = false>
228static void sparse_sparse_to_dense_product_impl(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
229 using LhsScalar =
typename remove_all_t<Lhs>::Scalar;
230 using RhsScalar =
typename remove_all_t<Rhs>::Scalar;
231 const sparse_scalar_product_op<LhsScalar, RhsScalar, ReverseProduct> product;
232 Index cols = rhs.outerSize();
233 eigen_assert(lhs.outerSize() == rhs.innerSize());
235 evaluator<Lhs> lhsEval(lhs);
236 evaluator<Rhs> rhsEval(rhs);
238 for (Index j = 0; j < cols; ++j) {
239 for (
typename evaluator<Rhs>::InnerIterator rhsIt(rhsEval, j); rhsIt; ++rhsIt) {
240 RhsScalar y = rhsIt.value();
241 Index k = rhsIt.index();
242 for (
typename evaluator<Lhs>::InnerIterator lhsIt(lhsEval, k); lhsIt; ++lhsIt) {
243 Index i = lhsIt.index();
244 LhsScalar x = lhsIt.value();
245 product.accumulate(res.coeffRef(i, j), x, y);
255template <
typename Lhs,
typename Rhs,
typename ResultType,
258struct sparse_sparse_to_dense_product_selector;
260template <
typename Lhs,
typename Rhs,
typename ResultType>
261struct sparse_sparse_to_dense_product_selector<Lhs, Rhs, ResultType, ColMajor, ColMajor> {
262 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
263 internal::sparse_sparse_to_dense_product_impl<Lhs, Rhs, ResultType>(lhs, rhs, res);
267template <
typename Lhs,
typename Rhs,
typename ResultType>
268struct sparse_sparse_to_dense_product_selector<Lhs, Rhs, ResultType, RowMajor, ColMajor> {
269 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
270 using ColMajorLhs = WithStorageOrder<Lhs, ColMajor>;
271 ColMajorLhs lhsCol(lhs);
272 internal::sparse_sparse_to_dense_product_impl<ColMajorLhs, Rhs, ResultType>(lhsCol, rhs, res);
276template <
typename Lhs,
typename Rhs,
typename ResultType>
277struct sparse_sparse_to_dense_product_selector<Lhs, Rhs, ResultType, ColMajor, RowMajor> {
278 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
279 using ColMajorRhs = WithStorageOrder<Rhs, ColMajor>;
280 ColMajorRhs rhsCol(rhs);
281 internal::sparse_sparse_to_dense_product_impl<Lhs, ColMajorRhs, ResultType>(lhs, rhsCol, res);
285template <
typename Lhs,
typename Rhs,
typename ResultType>
286struct sparse_sparse_to_dense_product_selector<Lhs, Rhs, ResultType, RowMajor, RowMajor> {
287 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res) {
288 Transpose<ResultType> trRes(res);
289 internal::sparse_sparse_to_dense_product_impl<Rhs, Lhs, Transpose<ResultType>,
true>(rhs, lhs, trRes);
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
constexpr unsigned int RowMajorBit
Definition Constants.h:71