Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
ProductEvaluators.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
6// Copyright (C) 2011 Jitse Niesen <jitse@maths.leeds.ac.uk>
7//
8// This Source Code Form is subject to the terms of the Mozilla
9// Public License v. 2.0. If a copy of the MPL was not distributed
10// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11// SPDX-License-Identifier: MPL-2.0
12
13#ifndef EIGEN_PRODUCTEVALUATORS_H
14#define EIGEN_PRODUCTEVALUATORS_H
15
16// IWYU pragma: private
17#include "./InternalHeaderCheck.h"
18
19// C4804: unsafe use of type 'bool' in operation. Unavoidable in generic code
20// instantiated with bool scalars (e.g. += and * on bool).
21#if EIGEN_COMP_MSVC
22#pragma warning(push)
23#pragma warning(disable : 4804)
24#endif
25
26namespace Eigen {
27
28namespace internal {
29
38template <typename Lhs, typename Rhs, int Options>
39struct evaluator<Product<Lhs, Rhs, Options>> : public product_evaluator<Product<Lhs, Rhs, Options>> {
40 using XprType = Product<Lhs, Rhs, Options>;
41 using Base = product_evaluator<XprType>;
42
43 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit evaluator(const XprType& xpr) : Base(xpr) {}
44};
45
46// A scalar factor cannot be folded into a unit diagonal or a permutation.
47template <typename Lhs, typename Shape = typename evaluator_traits<Lhs>::Shape>
48struct product_can_fold_scalar
49 : bool_constant<std::is_same<Shape, DenseShape>::value || std::is_same<Shape, SparseShape>::value ||
50 std::is_same<Shape, DiagonalShape>::value || std::is_same<Shape, SelfAdjointShape>::value> {};
51
52template <typename Lhs>
53struct product_can_fold_scalar<Lhs, TriangularShape> : bool_constant<(int(Lhs::Mode) & int(UnitDiag)) == 0> {};
54
55template <typename Lhs, typename Rhs, typename LhsShape = typename evaluator_traits<Lhs>::Shape,
56 typename RhsShape = typename evaluator_traits<Rhs>::Shape>
57struct product_can_scale_unit_triangle : std::false_type {};
58
59// The kernel must accept a commuting outer scale without combining it with extracted operand factors.
60template <typename Lhs, typename Rhs>
61struct product_can_scale_unit_triangle<Lhs, Rhs, TriangularShape, DenseShape>
62 : bool_constant<
63 (int(Lhs::Mode) & int(UnitDiag)) != 0 && std::is_same<typename Lhs::Scalar, typename Rhs::Scalar>::value &&
64 (std::is_floating_point<typename Lhs::Scalar>::value ||
65 (std::is_same<typename Lhs::Scalar, std::complex<typename NumTraits<typename Lhs::Scalar>::Real>>::value &&
66 std::is_floating_point<typename NumTraits<typename Lhs::Scalar>::Real>::value)) &&
67 !blas_traits<typename Lhs::MatrixType>::HasScalarFactor && !blas_traits<Rhs>::HasScalarFactor> {};
68
69template <typename Lhs, typename Rhs>
70struct product_can_scale_unit_triangle<Lhs, Rhs, TriangularShape, DiagonalShape>
71 : product_can_scale_unit_triangle<Lhs, Rhs, TriangularShape, DenseShape> {};
72
73// Folding s into a structured lhs forms s * lhs, which those views define only for their own Scalar.
74template <typename Factor, typename Lhs, typename Shape = typename evaluator_traits<Lhs>::Shape>
75struct product_can_fold_scalar_factor : product_can_fold_scalar<Lhs, Shape> {};
76
77template <typename Factor, typename Lhs>
78struct product_can_fold_scalar_factor<Factor, Lhs, TriangularShape>
79 : bool_constant<product_can_fold_scalar<Lhs>::value && std::is_convertible<Factor, typename Lhs::Scalar>::value> {};
80
81template <typename Factor, typename Lhs>
82struct product_can_fold_scalar_factor<Factor, Lhs, SelfAdjointShape>
83 : bool_constant<product_can_fold_scalar<Lhs>::value && std::is_convertible<Factor, typename Lhs::Scalar>::value> {};
84
85// The lazy selfadjoint/diagonal evaluator would conjugate a folded complex factor.
86template <typename Factor, typename Lhs, typename Rhs>
87struct product_evaluator_can_fold_scalar
88 : bool_constant<product_can_fold_scalar_factor<Factor, Lhs>::value &&
89 !(std::is_same<typename evaluator_traits<Lhs>::Shape, SelfAdjointShape>::value &&
90 std::is_same<typename evaluator_traits<Rhs>::Shape, DiagonalShape>::value)> {};
91
92template <typename Xpr, bool Fold = product_evaluator_can_fold_scalar<typename Xpr::Lhs::Scalar, typename Xpr::Rhs::Lhs,
93 typename Xpr::Rhs::Rhs>::value>
94struct scaled_product_evaluator_type {
95 // The assignment kernel extracts selfadjoint factors; materializing also protects nested aliases.
96 using type =
97 std::conditional_t<product_can_fold_scalar_factor<typename Xpr::Lhs::Scalar, typename Xpr::Rhs::Lhs>::value,
98 evaluator<EvalToTemp<Xpr>>, binary_evaluator<Xpr>>;
99};
100
101template <typename Xpr>
102struct scaled_product_evaluator_type<Xpr, true> {
103 using type =
104 evaluator<remove_all_t<decltype((std::declval<Xpr>().lhs().functor().m_other * std::declval<Xpr>().rhs().lhs()) *
105 std::declval<Xpr>().rhs().rhs())>>;
106};
107
108// Catch "scalar * ( A * B )" and transform it to "(scalar*A) * B"
109// TODO: we should apply that rule only if that's really helpful
110template <typename Lhs, typename Rhs, typename Scalar1, typename Scalar2, typename Plain1>
111struct evaluator_assume_aliasing<CwiseBinaryOp<internal::scalar_product_op<Scalar1, Scalar2>,
112 const CwiseNullaryOp<internal::scalar_constant_op<Scalar1>, Plain1>,
113 const Product<Lhs, Rhs, DefaultProduct>>> : std::true_type {};
114template <typename Lhs, typename Rhs, typename Scalar1, typename Scalar2, typename Plain1>
115struct evaluator<CwiseBinaryOp<internal::scalar_product_op<Scalar1, Scalar2>,
116 const CwiseNullaryOp<internal::scalar_constant_op<Scalar1>, Plain1>,
117 const Product<Lhs, Rhs, DefaultProduct>>>
118 : scaled_product_evaluator_type<CwiseBinaryOp<internal::scalar_product_op<Scalar1, Scalar2>,
119 const CwiseNullaryOp<internal::scalar_constant_op<Scalar1>, Plain1>,
120 const Product<Lhs, Rhs, DefaultProduct>>>::type {
121 using XprType = CwiseBinaryOp<internal::scalar_product_op<Scalar1, Scalar2>,
122 const CwiseNullaryOp<internal::scalar_constant_op<Scalar1>, Plain1>,
123 const Product<Lhs, Rhs, DefaultProduct>>;
124 using Base = typename scaled_product_evaluator_type<XprType>::type;
125
126 template <bool Fold = product_evaluator_can_fold_scalar<Scalar1, Lhs, Rhs>::value, std::enable_if_t<Fold, int> = 0>
127 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit evaluator(const XprType& xpr)
128 : Base(xpr.lhs().functor().m_other * xpr.rhs().lhs() * xpr.rhs().rhs()) {}
129
130 template <bool Fold = product_evaluator_can_fold_scalar<Scalar1, Lhs, Rhs>::value, std::enable_if_t<!Fold, int> = 0>
131 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit evaluator(const XprType& xpr) : Base(xpr) {}
132};
133
134template <typename Lhs, typename Rhs, int DiagIndex>
135struct evaluator<Diagonal<const Product<Lhs, Rhs, DefaultProduct>, DiagIndex>>
136 : public evaluator<Diagonal<const Product<Lhs, Rhs, LazyProduct>, DiagIndex>> {
137 using XprType = Diagonal<const Product<Lhs, Rhs, DefaultProduct>, DiagIndex>;
138 using Base = evaluator<Diagonal<const Product<Lhs, Rhs, LazyProduct>, DiagIndex>>;
139
140 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit evaluator(const XprType& xpr)
141 : Base(Diagonal<const Product<Lhs, Rhs, LazyProduct>, DiagIndex>(
142 Product<Lhs, Rhs, LazyProduct>(xpr.nestedExpression().lhs(), xpr.nestedExpression().rhs()), xpr.index())) {}
143};
144
145// Helper class to perform a matrix product with the destination at hand.
146// Depending on the sizes of the factors, there are different evaluation strategies
147// as controlled by internal::product_type.
148template <typename Lhs, typename Rhs, typename LhsShape = typename evaluator_traits<Lhs>::Shape,
149 typename RhsShape = typename evaluator_traits<Rhs>::Shape,
150 int ProductType = internal::product_type<Lhs, Rhs>::value>
151struct generic_product_impl;
152
153template <typename Lhs, typename Rhs>
154struct evaluator_assume_aliasing<Product<Lhs, Rhs, DefaultProduct>> : std::true_type {};
155
156// This is the default evaluator implementation for products:
157// It creates a temporary and call generic_product_impl
158template <typename Lhs, typename Rhs, int Options, int ProductTag, typename LhsShape, typename RhsShape>
159struct product_evaluator<Product<Lhs, Rhs, Options>, ProductTag, LhsShape, RhsShape>
160 : public evaluator<typename Product<Lhs, Rhs, Options>::PlainObject> {
161 using XprType = Product<Lhs, Rhs, Options>;
162 using PlainObject = typename XprType::PlainObject;
163 using Base = evaluator<PlainObject>;
164 enum { Flags = Base::Flags | EvalBeforeNestingBit };
165
166 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit product_evaluator(const XprType& xpr)
167 : m_result(xpr.rows(), xpr.cols()) {
168 internal::construct_at<Base>(this, m_result);
169
170 // FIXME shall we handle nested_eval here?,
171 // if so, then we must take care at removing the call to nested_eval in the specializations (e.g., in
172 // permutation_matrix_product, transposition_matrix_product, etc.)
173 // typedef typename internal::nested_eval<Lhs,Rhs::ColsAtCompileTime>::type LhsNested;
174 // typedef typename internal::nested_eval<Rhs,Lhs::RowsAtCompileTime>::type RhsNested;
175 // typedef internal::remove_all_t<LhsNested> LhsNestedCleaned;
176 // typedef internal::remove_all_t<RhsNested> RhsNestedCleaned;
177 //
178 // const LhsNested lhs(xpr.lhs());
179 // const RhsNested rhs(xpr.rhs());
180 //
181 // generic_product_impl<LhsNestedCleaned, RhsNestedCleaned>::evalTo(m_result, lhs, rhs);
182
183 generic_product_impl<Lhs, Rhs, LhsShape, RhsShape, ProductTag>::evalTo(m_result, xpr.lhs(), xpr.rhs());
184 }
185
186 protected:
187 PlainObject m_result;
188};
189
190// The following three shortcuts are enabled only if the scalar types match exactly.
191// TODO: we could enable them for different scalar types when the product is not vectorized.
192
193// Dense = Product
194template <typename DstXprType, typename Lhs, typename Rhs, int Options, typename Scalar>
195struct Assignment<DstXprType, Product<Lhs, Rhs, Options>, internal::assign_op<Scalar, Scalar>, Dense2Dense,
196 std::enable_if_t<(Options == DefaultProduct || Options == AliasFreeProduct)>> {
197 using SrcXprType = Product<Lhs, Rhs, Options>;
198 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(DstXprType& dst, const SrcXprType& src,
199 const internal::assign_op<Scalar, Scalar>& func) {
200 resize_if_allowed(dst, src, func);
201 // FIXME shall we handle nested_eval here?
202 generic_product_impl<Lhs, Rhs>::evalTo(dst, src.lhs(), src.rhs());
203 }
204};
205
206// Dense += Product
207template <typename DstXprType, typename Lhs, typename Rhs, int Options, typename Scalar>
208struct Assignment<DstXprType, Product<Lhs, Rhs, Options>, internal::add_assign_op<Scalar, Scalar>, Dense2Dense,
209 std::enable_if_t<(Options == DefaultProduct || Options == AliasFreeProduct)>> {
210 using SrcXprType = Product<Lhs, Rhs, Options>;
211 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(DstXprType& dst, const SrcXprType& src,
212 const internal::add_assign_op<Scalar, Scalar>&) {
213 EIGEN_INTERNAL_ASSERT_SAME_SHAPE(dst, src);
214 // FIXME shall we handle nested_eval here?
215 generic_product_impl<Lhs, Rhs>::addTo(dst, src.lhs(), src.rhs());
216 }
217};
218
219// Dense -= Product
220template <typename DstXprType, typename Lhs, typename Rhs, int Options, typename Scalar>
221struct Assignment<DstXprType, Product<Lhs, Rhs, Options>, internal::sub_assign_op<Scalar, Scalar>, Dense2Dense,
222 std::enable_if_t<(Options == DefaultProduct || Options == AliasFreeProduct)>> {
223 using SrcXprType = Product<Lhs, Rhs, Options>;
224 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(DstXprType& dst, const SrcXprType& src,
225 const internal::sub_assign_op<Scalar, Scalar>&) {
226 EIGEN_INTERNAL_ASSERT_SAME_SHAPE(dst, src);
227 // FIXME shall we handle nested_eval here?
228 generic_product_impl<Lhs, Rhs>::subTo(dst, src.lhs(), src.rhs());
229 }
230};
231
232// Dense ?= scalar * Product
233// TODO: we should apply that rule if that's really helpful
234// for instance, this is not good for inner products
235template <typename DstXprType, typename Lhs, typename Rhs, typename AssignFunc, typename Scalar, typename ScalarBis,
236 typename Plain>
237struct Assignment<DstXprType,
238 CwiseBinaryOp<internal::scalar_product_op<ScalarBis, Scalar>,
239 const CwiseNullaryOp<internal::scalar_constant_op<ScalarBis>, Plain>,
240 const Product<Lhs, Rhs, DefaultProduct>>,
241 AssignFunc, Dense2Dense, std::enable_if_t<product_can_fold_scalar_factor<ScalarBis, Lhs>::value>> {
242 using SrcXprType = CwiseBinaryOp<internal::scalar_product_op<ScalarBis, Scalar>,
243 const CwiseNullaryOp<internal::scalar_constant_op<ScalarBis>, Plain>,
244 const Product<Lhs, Rhs, DefaultProduct>>;
245 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(DstXprType& dst, const SrcXprType& src,
246 const AssignFunc& func) {
247 call_assignment_no_alias(dst, (src.lhs().functor().m_other * src.rhs().lhs()) * src.rhs().rhs(), func);
248 }
249};
250
251// Follow BLAS zero-scale semantics: clear overwrites, but leave compound updates unchanged without reading operands.
252template <typename Dst, typename Lhs, typename Rhs, typename Func, typename Scalar, typename Scale, typename Plain>
253struct Assignment<
254 Dst,
255 CwiseBinaryOp<scalar_product_op<Scale, Scalar>, const CwiseNullaryOp<scalar_constant_op<Scale>, Plain>,
256 const Product<Lhs, Rhs, DefaultProduct>>,
257 Func, Dense2Dense,
258 std::enable_if_t<
259 product_can_scale_unit_triangle<Lhs, Rhs>::value && std::is_same<Scale, Scalar>::value &&
260 std::is_same<typename Lhs::Scalar, Scalar>::value && std::is_same<typename Dst::Scalar, Scalar>::value &&
261 std::is_same<typename traits<Dst>::XprKind, MatrixXpr>::value && ((int(Dst::Flags) & DirectAccessBit) != 0) &&
262 (Rhs::ColsAtCompileTime != 1 || Dst::IsVectorAtCompileTime) &&
263 (std::is_same<Func, assign_op<Scalar, Scalar>>::value ||
264 std::is_same<Func, add_assign_op<Scalar, Scalar>>::value ||
265 std::is_same<Func, sub_assign_op<Scalar, Scalar>>::value)>> {
266 using Src = CwiseBinaryOp<scalar_product_op<Scale, Scalar>, const CwiseNullaryOp<scalar_constant_op<Scale>, Plain>,
267 const Product<Lhs, Rhs, DefaultProduct>>;
268 static EIGEN_STRONG_INLINE void run(Dst& dst, const Src& src, const Func& func) {
269 Scalar alpha = src.lhs().functor().m_other;
270 resize_if_allowed(dst, src, func);
271 EIGEN_IF_CONSTEXPR ((std::is_same<Func, assign_op<Scalar, Scalar>>::value)) dst.setZero();
272 if (numext::is_exactly_zero(alpha)) return;
273 EIGEN_IF_CONSTEXPR ((std::is_same<Func, sub_assign_op<Scalar, Scalar>>::value)) alpha = -alpha;
274 generic_product_impl<Lhs, Rhs>::scaleAndAddTo(dst, src.rhs().lhs(), src.rhs().rhs(), alpha);
275 }
276};
277
278//----------------------------------------
279// Catch "Dense ?= xpr + Product<>" expression to save one temporary
280// FIXME: consider enabling these rules for all product types, not only Dense and DefaultProduct.
281
282template <typename OtherXpr, typename Lhs, typename Rhs>
283struct evaluator_assume_aliasing<
284 CwiseBinaryOp<
285 internal::scalar_sum_op<typename OtherXpr::Scalar, typename Product<Lhs, Rhs, DefaultProduct>::Scalar>,
286 const OtherXpr, const Product<Lhs, Rhs, DefaultProduct>>,
287 DenseShape> : std::true_type {};
288
289template <typename OtherXpr, typename Lhs, typename Rhs>
290struct evaluator_assume_aliasing<
291 CwiseBinaryOp<
292 internal::scalar_difference_op<typename OtherXpr::Scalar, typename Product<Lhs, Rhs, DefaultProduct>::Scalar>,
293 const OtherXpr, const Product<Lhs, Rhs, DefaultProduct>>,
294 DenseShape> : std::true_type {};
295
296template <typename DstXprType, typename OtherXpr, typename ProductType, typename Func1, typename Func2>
297struct assignment_from_xpr_op_product {
298 template <typename SrcXprType, typename InitialFunc>
299 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(DstXprType& dst, const SrcXprType& src,
300 const InitialFunc& /*func*/) {
301 call_assignment_no_alias(dst, src.lhs(), Func1());
302 call_assignment_no_alias(dst, src.rhs(), Func2());
303 }
304};
305
306#define EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(ASSIGN_OP, BINOP, ASSIGN_OP2) \
307 template <typename DstXprType, typename OtherXpr, typename Lhs, typename Rhs, typename DstScalar, \
308 typename SrcScalar, typename OtherScalar, typename ProdScalar> \
309 struct Assignment<DstXprType, \
310 CwiseBinaryOp<internal::BINOP<OtherScalar, ProdScalar>, const OtherXpr, \
311 const Product<Lhs, Rhs, DefaultProduct>>, \
312 internal::ASSIGN_OP<DstScalar, SrcScalar>, Dense2Dense> \
313 : assignment_from_xpr_op_product<DstXprType, OtherXpr, Product<Lhs, Rhs, DefaultProduct>, \
314 internal::ASSIGN_OP<DstScalar, OtherScalar>, \
315 internal::ASSIGN_OP2<DstScalar, ProdScalar>> {}
316
317EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(assign_op, scalar_sum_op, add_assign_op);
318EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(add_assign_op, scalar_sum_op, add_assign_op);
319EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(sub_assign_op, scalar_sum_op, sub_assign_op);
320
321EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(assign_op, scalar_difference_op, sub_assign_op);
322EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(add_assign_op, scalar_difference_op, sub_assign_op);
323EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(sub_assign_op, scalar_difference_op, add_assign_op);
324
325//----------------------------------------
326
327template <typename Lhs, typename Rhs>
328struct generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, InnerProduct> {
329 using impl = inner_product_dispatch<Lhs, Rhs, false>;
330 template <typename Dst>
331 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE void evalTo(Dst& dst, const Lhs& lhs, const Rhs& rhs) {
332 dst.coeffRef(0, 0) = impl::run(lhs, rhs);
333 }
334
335 template <typename Dst>
336 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE void addTo(Dst& dst, const Lhs& lhs, const Rhs& rhs) {
337 dst.coeffRef(0, 0) += impl::run(lhs, rhs);
338 }
339
340 template <typename Dst>
341 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE void subTo(Dst& dst, const Lhs& lhs, const Rhs& rhs) {
342 dst.coeffRef(0, 0) -= impl::run(lhs, rhs);
343 }
344};
345
346/***********************************************************************
347 * Implementation of outer dense * dense vector product
348 ***********************************************************************/
349
350// Column major result
351template <typename Dst, typename Lhs, typename Rhs, typename Func>
352void EIGEN_DEVICE_FUNC outer_product_selector_run(Dst& dst, const Lhs& lhs, const Rhs& rhs, const Func& func,
353 const std::false_type&) {
354 evaluator<Rhs> rhsEval(rhs);
355 ei_declare_local_nested_eval(Lhs, lhs, Rhs::SizeAtCompileTime, actual_lhs);
356 // FIXME if cols is large enough, then it might be useful to make sure that lhs is sequentially stored
357 // FIXME not very good if rhs is real and lhs complex while alpha is real too
358 const Index cols = dst.cols();
359 for (Index j = 0; j < cols; ++j) func(dst.col(j), actual_lhs * rhsEval.coeff(Index(0), j));
360}
361
362// Row major result
363template <typename Dst, typename Lhs, typename Rhs, typename Func>
364void EIGEN_DEVICE_FUNC outer_product_selector_run(Dst& dst, const Lhs& lhs, const Rhs& rhs, const Func& func,
365 const std::true_type&) {
366 evaluator<Lhs> lhsEval(lhs);
367 ei_declare_local_nested_eval(Rhs, rhs, Lhs::SizeAtCompileTime, actual_rhs);
368 // FIXME if rows is large enough, then it might be useful to make sure that rhs is sequentially stored
369 // FIXME not very good if lhs is real and rhs complex while alpha is real too
370 const Index rows = dst.rows();
371 for (Index i = 0; i < rows; ++i) func(dst.row(i), lhsEval.coeff(i, Index(0)) * actual_rhs);
372}
373
374template <typename Dst>
375EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool outer_product_use_small_assignment(const Dst& dst) {
376 return dst.rows() <= 16 && dst.cols() <= 16;
377}
378
379template <bool ApplyScale = true, typename Dst, typename Lhs, typename Rhs, typename Func, typename Scalar>
380void EIGEN_DEVICE_FUNC outer_product_selector_run_small(Dst& dst, const Lhs& lhs, const Rhs& rhs, const Func& func,
381 const Scalar& alpha, const std::false_type&) {
382 evaluator<Rhs> rhsEval(rhs);
383 ei_declare_local_nested_eval(Lhs, lhs, Rhs::SizeAtCompileTime, actual_lhs);
384 const Index rows = dst.rows();
385 const Index cols = dst.cols();
386 for (Index j = 0; j < cols; ++j) {
387 const typename Rhs::Scalar rhs_j = rhsEval.coeff(Index(0), j);
388 for (Index i = 0; i < rows; ++i) {
389 EIGEN_IF_CONSTEXPR (ApplyScale) {
390 func.assignCoeff(dst.coeffRef(i, j), internal::mul(alpha, internal::mul(actual_lhs.coeff(i, Index(0)), rhs_j)));
391 } else {
392 func.assignCoeff(dst.coeffRef(i, j), internal::mul(actual_lhs.coeff(i, Index(0)), rhs_j));
393 }
394 }
395 }
396}
397
398template <bool ApplyScale = true, typename Dst, typename Lhs, typename Rhs, typename Func, typename Scalar>
399void EIGEN_DEVICE_FUNC outer_product_selector_run_small(Dst& dst, const Lhs& lhs, const Rhs& rhs, const Func& func,
400 const Scalar& alpha, const std::true_type&) {
401 evaluator<Lhs> lhsEval(lhs);
402 ei_declare_local_nested_eval(Rhs, rhs, Lhs::SizeAtCompileTime, actual_rhs);
403 const Index rows = dst.rows();
404 const Index cols = dst.cols();
405 for (Index i = 0; i < rows; ++i) {
406 const typename Lhs::Scalar lhs_i = lhsEval.coeff(i, Index(0));
407 for (Index j = 0; j < cols; ++j) {
408 EIGEN_IF_CONSTEXPR (ApplyScale) {
409 func.assignCoeff(dst.coeffRef(i, j), internal::mul(alpha, internal::mul(lhs_i, actual_rhs.coeff(Index(0), j))));
410 } else {
411 func.assignCoeff(dst.coeffRef(i, j), internal::mul(lhs_i, actual_rhs.coeff(Index(0), j)));
412 }
413 }
414 }
415}
416
417template <typename Lhs, typename Rhs>
418struct generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, OuterProduct> {
419 template <typename T>
420 struct is_row_major : bool_constant<(int(T::Flags) & RowMajorBit)> {};
421 using Scalar = typename Product<Lhs, Rhs>::Scalar;
422
423 // TODO: it would be nice to be able to exploit our *_assign_op functors for that purpose
424 struct set {
425 template <typename Dst, typename Src>
426 EIGEN_DEVICE_FUNC void operator()(const Dst& dst, const Src& src) const {
427 dst.const_cast_derived() = src;
428 }
429 };
430 struct add {
432 template <typename Dst, typename Src>
433 EIGEN_DEVICE_FUNC void operator()(const Dst& dst, const Src& src) const {
434 dst.const_cast_derived() += src;
435 }
436 };
437 struct sub {
438 template <typename Dst, typename Src>
439 EIGEN_DEVICE_FUNC void operator()(const Dst& dst, const Src& src) const {
440 dst.const_cast_derived() -= src;
441 }
442 };
444 struct adds {
445 Scalar m_scale;
447 EIGEN_DEVICE_FUNC explicit adds(const Scalar& s) : m_scale(s) {}
449 template <typename Dst, typename Src>
450 void EIGEN_DEVICE_FUNC operator()(const Dst& dst, const Src& src) const {
451 dst.const_cast_derived() += m_scale * src;
452 }
453 };
454
455 template <typename Dst>
456 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dst& dst, const Lhs& lhs, const Rhs& rhs) {
457 if (internal::outer_product_use_small_assignment(dst)) {
458 internal::outer_product_selector_run_small<false>(
459 dst, lhs, rhs, internal::assign_op<typename Dst::Scalar, Scalar>(), Scalar(1), is_row_major<Dst>());
460 } else {
461 internal::outer_product_selector_run(dst, lhs, rhs, set(), is_row_major<Dst>());
462 }
463 }
464
465 template <typename Dst>
466 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void addTo(Dst& dst, const Lhs& lhs, const Rhs& rhs) {
467 if (internal::outer_product_use_small_assignment(dst)) {
468 internal::outer_product_selector_run_small<false>(
469 dst, lhs, rhs, internal::add_assign_op<typename Dst::Scalar, Scalar>(), Scalar(1), is_row_major<Dst>());
470 } else {
471 internal::outer_product_selector_run(dst, lhs, rhs, add(), is_row_major<Dst>());
472 }
473 }
474
475 template <typename Dst>
476 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void subTo(Dst& dst, const Lhs& lhs, const Rhs& rhs) {
477 if (internal::outer_product_use_small_assignment(dst)) {
478 internal::outer_product_selector_run_small<false>(
479 dst, lhs, rhs, internal::sub_assign_op<typename Dst::Scalar, Scalar>(), Scalar(1), is_row_major<Dst>());
480 } else {
481 internal::outer_product_selector_run(dst, lhs, rhs, sub(), is_row_major<Dst>());
482 }
483 }
484
485 template <typename Dst>
486 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void scaleAndAddTo(Dst& dst, const Lhs& lhs, const Rhs& rhs,
487 const Scalar& alpha) {
488 if (internal::outer_product_use_small_assignment(dst)) {
489 internal::outer_product_selector_run_small(dst, lhs, rhs, internal::add_assign_op<typename Dst::Scalar, Scalar>(),
490 alpha, is_row_major<Dst>());
491 } else {
492 internal::outer_product_selector_run(dst, lhs, rhs, adds(alpha), is_row_major<Dst>());
493 }
494 }
495};
496
497// This base class provides default implementations for evalTo, addTo, subTo, in terms of scaleAndAddTo
498template <typename Lhs, typename Rhs, typename Derived>
499struct generic_product_impl_base {
500 using Scalar = typename Product<Lhs, Rhs>::Scalar;
501
502 template <typename Dst>
503 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dst& dst, const Lhs& lhs, const Rhs& rhs) {
504 dst.setZero();
505 scaleAndAddTo(dst, lhs, rhs, Scalar(1));
506 }
507
508 template <typename Dst>
509 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void addTo(Dst& dst, const Lhs& lhs, const Rhs& rhs) {
510 scaleAndAddTo(dst, lhs, rhs, Scalar(1));
511 }
512
513 template <typename Dst>
514 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void subTo(Dst& dst, const Lhs& lhs, const Rhs& rhs) {
515 scaleAndAddTo(dst, lhs, rhs, Scalar(-1));
516 }
517
518 template <typename Dst>
519 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void scaleAndAddTo(Dst& dst, const Lhs& lhs, const Rhs& rhs,
520 const Scalar& alpha) {
521 Derived::scaleAndAddTo(dst, lhs, rhs, alpha);
522 }
523};
524
525template <typename Lhs, typename Rhs, typename Scalar>
526struct gemv_leading_constant : std::false_type {};
527
528template <typename Plain, typename Nested, typename Rhs, typename Scalar>
529struct gemv_leading_constant<
530 CwiseBinaryOp<scalar_product_op<Scalar>, const CwiseNullaryOp<scalar_constant_op<Scalar>, Plain>, Nested>, Rhs,
531 Scalar> : std::is_same<typename Rhs::Scalar, Scalar> {
532 using Inner = remove_all_t<Nested>;
533};
534
535// Seed all sums from their first term without requiring a default-constructible scalar.
536// Inline initialization so the compiler can keep the accumulator array in registers.
537template <typename Scalar, int Count>
538struct gemv_ordered_accumulators {
539 Scalar values[Count];
540 template <typename Term, int... Indices>
541 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit gemv_ordered_accumulators(const Term& term, Index offset,
542 std::integer_sequence<int, Indices...>)
543 : values{term(offset + Indices, 0)...} {}
544};
545
546// dst += alpha * P, where accumulate(P) adds the unscaled product into a zeroed P. Never inlined: a stack buffer lives
547// until the function that declared it returns, so a caller that inlines it into a loop grows the stack each iteration.
548template <typename Plain, typename Dest, typename Scalar, typename Accumulate>
549EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE void gemv_add_scaled_product(Dest& dst, const Scalar& alpha,
550 const Accumulate& accumulate) {
551 ei_declare_aligned_stack_constructed_variable(Scalar, buffer, dst.size(), nullptr);
552 Map<Plain> product(buffer, dst.rows(), dst.cols());
553 product.setZero();
554 accumulate(product);
555 for (Index j = 0; j < dst.cols(); ++j)
556 for (Index i = 0; i < dst.rows(); ++i) dst.coeffRef(i, j) += alpha * product.coeff(i, j);
557}
558
559template <typename Lhs, typename Rhs>
560struct generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, GemvProduct>
561 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, GemvProduct>> {
562 using LhsNested = typename nested_eval<Lhs, 1>::type;
563 using RhsNested = typename nested_eval<Rhs, 1>::type;
564 using Scalar = typename Product<Lhs, Rhs>::Scalar;
565 enum { Side = Lhs::IsVectorAtCompileTime ? OnTheLeft : OnTheRight };
566 // A class constant, not a local of run_ordered(): MSVC in C++17 mode does not accept a constexpr local that a
567 // lambda captures by reference as the condition of an if constexpr inside that lambda (C2131).
568 static constexpr bool OnRight = int(Side) == int(OnTheRight);
569 using MatrixType = internal::remove_all_t<std::conditional_t<int(Side) == OnTheRight, LhsNested, RhsNested>>;
570
571 template <typename Dest>
572 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs,
573 const Scalar& alpha) {
574 run(dst, lhs, rhs, alpha, blas_product_is_commutative<typename Lhs::Scalar, typename Rhs::Scalar>());
575 }
576
577 private:
578 template <typename Dest>
579 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha,
580 std::false_type) {
581 if (lhs.cols() == 0 || numext::is_exactly_zero(alpha) || blas_traits<Lhs>::hasZeroScalarFactor(lhs) ||
582 blas_traits<Rhs>::hasZeroScalarFactor(rhs))
583 return;
584 run_ordered(dst, lhs, rhs, alpha, gemv_leading_constant<Lhs, Rhs, Scalar>());
585 }
586
587 template <typename Dest>
588 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run_ordered(Dest& dst, const Lhs& lhs, const Rhs& rhs,
589 const Scalar& alpha, std::true_type) {
590 const Scalar scale = lhs.lhs().functor().m_other;
591 // A zero that is part of a constant expression must still multiply its coefficients and propagate NaNs.
592 if (numext::is_exactly_zero(scale)) return run_ordered(dst, lhs, rhs, alpha, std::false_type());
593 using Inner = typename gemv_leading_constant<Lhs, Rhs, Scalar>::Inner;
594 using Impl = generic_product_impl<Inner, Rhs, DenseShape, DenseShape, GemvProduct>;
595 if (numext::is_exactly_one(alpha)) return Impl::scaleAndAddTo(dst, lhs.rhs(), rhs, scale);
596 // Keep alpha * (scale * sum) separate: forming alpha * scale can underflow or overflow.
597 gemv_add_scaled_product<typename Product<Lhs, Rhs>::PlainObject>(
598 dst, alpha, [&](auto& product) { Impl::scaleAndAddTo(product, lhs.rhs(), rhs, scale); });
599 }
600
601 template <typename Dest>
602 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run_ordered(Dest& dst, const Lhs& lhs, const Rhs& rhs,
603 const Scalar& alpha, std::false_type) {
604 // Factor extraction and alpha folding need not preserve the written scalar order.
605 LhsNested actual_lhs(lhs);
606 RhsNested actual_rhs(rhs);
607 const bool unit_alpha = numext::is_exactly_one(alpha);
608 const Index size = OnRight ? lhs.rows() : rhs.cols();
609 const Index depth = lhs.cols();
610 const auto out = [&](Index i) -> decltype(auto) { return OnRight ? dst.coeffRef(i, 0) : dst.coeffRef(0, i); };
611 EIGEN_IF_CONSTEXPR (OnRight == !(MatrixType::Flags & RowMajorBit)) {
612 const auto accumulate = [&](const auto& at) {
613 for (Index k = 0; k < depth; ++k) {
614 EIGEN_IF_CONSTEXPR (OnRight) {
615 const typename Rhs::Scalar value = actual_rhs.coeff(k, 0);
616 for (Index i = 0; i < size; ++i) at(i) += actual_lhs.coeff(i, k) * value;
617 } else {
618 const typename Lhs::Scalar value = actual_lhs.coeff(0, k);
619 for (Index i = 0; i < size; ++i) at(i) += value * actual_rhs.coeff(k, i);
620 }
621 }
622 };
623 if (unit_alpha) {
624 accumulate(out);
625 } else {
626 gemv_add_scaled_product<typename Product<Lhs, Rhs>::PlainObject>(
627 dst, alpha, [&](auto& product) { accumulate([&](Index i) -> Scalar& { return product.coeffRef(i); }); });
628 }
629 return;
630 }
631 const auto term = [&](Index i, Index k) -> Scalar {
632 return OnRight ? Scalar(actual_lhs.coeff(i, k) * actual_rhs.coeff(k, 0))
633 : Scalar(actual_lhs.coeff(0, k) * actual_rhs.coeff(k, i));
634 };
635 // Keep independent ordered sums in flight, sharing each vector coefficient across the block.
636 constexpr int Block = sizeof(Scalar) <= 16 ? 8 : 4;
637 Index i = 0;
638 for (; size - i >= Block; i += Block) {
639 gemv_ordered_accumulators<Scalar, Block> sums(term, i, std::make_integer_sequence<int, Block>{});
640 for (Index k = 1; k < depth; ++k) {
641 EIGEN_IF_CONSTEXPR (OnRight) {
642 const typename Rhs::Scalar value = actual_rhs.coeff(k, 0);
643 EIGEN_UNROLL_LOOP
644 for (int r = 0; r < Block; ++r) sums.values[r] += actual_lhs.coeff(i + r, k) * value;
645 } else {
646 const typename Lhs::Scalar value = actual_lhs.coeff(0, k);
647 EIGEN_UNROLL_LOOP
648 for (int r = 0; r < Block; ++r) sums.values[r] += value * actual_rhs.coeff(k, i + r);
649 }
650 }
651 EIGEN_UNROLL_LOOP
652 for (int r = 0; r < Block; ++r) out(i + r) += unit_alpha ? sums.values[r] : alpha * sums.values[r];
653 }
654 constexpr int OutputSize = OnRight ? int(traits<Lhs>::RowsAtCompileTime) : int(traits<Rhs>::ColsAtCompileTime);
655 EIGEN_IF_CONSTEXPR (OutputSize == Dynamic || OutputSize % Block != 0) {
656 for (; i < size; ++i) {
657 Scalar sum = term(i, 0);
658 for (Index k = 1; k < depth; ++k) sum += term(i, k);
659 out(i) += unit_alpha ? sum : alpha * sum;
660 }
661 }
662 }
663
664 template <typename Dest>
665 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha,
666 std::true_type) {
667 // Fallback to inner product if both the lhs and rhs is a runtime vector.
668 if (lhs.rows() == 1 && rhs.cols() == 1) {
669 // Same zero-factor contract as the kernels below: a zero must not multiply the operands.
670 if (numext::is_exactly_zero(alpha) || blas_traits<Lhs>::hasZeroScalarFactor(lhs) ||
671 blas_traits<Rhs>::hasZeroScalarFactor(rhs))
672 return;
673 dst.coeffRef(0, 0) += alpha * lhs.row(0).conjugate().dot(rhs.col(0));
674 return;
675 }
676 LhsNested actual_lhs(lhs);
677 RhsNested actual_rhs(rhs);
678 internal::gemv_dense_selector<Side, (int(MatrixType::Flags) & RowMajorBit) ? RowMajor : ColMajor,
679 bool(internal::blas_traits<MatrixType>::HasUsableDirectAccess)>::run(actual_lhs,
680 actual_rhs, dst,
681 alpha);
682 }
683};
684
685// Packet-cascade assignment for small fixed-size coefficient-based products
686// (dst = / += / -= lhs * rhs). The generic assignment path types the product's
687// packet via find_best_packet -- the widest packet that *exactly divides* the
688// vectorized extent, leaving no tail. For odd/non-power-of-two extents that
689// overshoots: with no exact divisor it falls back to a narrow packet, or all
690// the way to scalars (e.g. 3x3 double becomes scalar, ~3x slower than it needs
691// to be). find_largest_packet instead picks the widest packet that *fits*
692// (size <= extent) and we emit an explicit cascade -- full packet(s), then the
693// half packet, ... then a scalar tail -- so e.g. 3x3 double does one Packet2d
694// plus one scalar per column, and 6x6 double does one Packet4d plus one Packet2d
695// instead of three Packet2d. See find_largest_packet in XprHelper.h.
696
697// Emit the inner segment [begin, end) for a single outer index, cascading from
698// `Packet` down through its half packets to a scalar tail. `ColMajor` selects
699// whether the contiguous inner index is the row (true) or the column (false).
700// The bool `Terminal` partial specialization stops the recursion at the
701// narrowest packet without requiring C++17 `if constexpr`.
702template <typename Packet, bool ColMajor,
703 bool Terminal = std::is_same<typename unpacket_traits<Packet>::half, Packet>::value>
704struct product_packet_cascade {
705 template <typename Func, typename Dst, typename ProdEval>
706 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(const Func& func, Dst& dst, const ProdEval& prod, Index outer,
707 Index begin, Index end) {
708 constexpr int PacketSize = unpacket_traits<Packet>::size;
709 Index pktEnd = begin + numext::round_down(end - begin, PacketSize);
710 Index i = begin;
711 EIGEN_IF_CONSTEXPR (ProdEval::MixedRealComplex) {
712 if (pktEnd - begin == PacketSize && pktEnd < end) {
713 // Read the tail before storing the full packet so their real-factor broadcasts can be shared.
714 Index r = ColMajor ? begin : outer, c = ColMajor ? outer : begin;
715 const Packet result = prod.template packet<Unaligned, Packet>(r, c);
716 product_packet_cascade<typename unpacket_traits<Packet>::half, ColMajor>::run(func, dst, prod, outer, pktEnd,
717 end);
718 func.template assignPacket<Unaligned, Packet>(&dst.coeffRef(r, c), result);
719 return;
720 }
721 }
722 for (; i < pktEnd; i += PacketSize) {
723 Index r = ColMajor ? i : outer, c = ColMajor ? outer : i;
724 func.template assignPacket<Unaligned, Packet>(&dst.coeffRef(r, c), prod.template packet<Unaligned, Packet>(r, c));
725 }
726 // Fall back to the half packet for the remainder [i, end).
727 product_packet_cascade<typename unpacket_traits<Packet>::half, ColMajor>::run(func, dst, prod, outer, i, end);
728 }
729};
730
731// Narrowest packet in the chain: lay down its full packets, then a scalar tail.
732template <typename Packet, bool ColMajor>
733struct product_packet_cascade<Packet, ColMajor, /*Terminal=*/true> {
734 template <typename Func, typename Dst, typename ProdEval>
735 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(const Func& func, Dst& dst, const ProdEval& prod, Index outer,
736 Index begin, Index end) {
737 constexpr int PacketSize = unpacket_traits<Packet>::size;
738 Index pktEnd = begin + numext::round_down(end - begin, PacketSize);
739 Index i = begin;
740 for (; i < pktEnd; i += PacketSize) {
741 Index r = ColMajor ? i : outer, c = ColMajor ? outer : i;
742 func.template assignPacket<Unaligned, Packet>(&dst.coeffRef(r, c), prod.template packet<Unaligned, Packet>(r, c));
743 }
744 EIGEN_IF_CONSTEXPR (ProdEval::MixedRealComplex &&
745 (ColMajor ? bool(NumTraits<typename ProdEval::LhsScalar>::IsComplex)
746 : bool(NumTraits<typename ProdEval::RhsScalar>::IsComplex))) {
747 if (i > begin && i < end) {
748 // Overlap complex loads to avoid scalar tail reductions; duplicating real lanes again is more expensive.
749 // Store only new lanes so compound assignments visit each coefficient once.
750 const Index start = end - PacketSize;
751 const Index r = ColMajor ? start : outer, c = ColMajor ? outer : start;
752 const Packet result = prod.template packet<Unaligned, Packet>(r, c);
753 func.template assignPacketSegment<Unaligned, Packet>(&dst.coeffRef(r, c), result, i - start, end - i);
754 return;
755 }
756 }
757 for (; i < end; ++i) {
758 Index r = ColMajor ? i : outer, c = ColMajor ? outer : i;
759 func.assignCoeff(dst.coeffRef(r, c), prod.coeff(r, c));
760 }
761 }
762};
763
764// Largest packet size in the (halving) chain that *exactly divides* Size, or 1
765// if none does. This is what the generic InnerVectorized assignment path can
766// actually use -- unlike find_best_packet, whose stop condition returns the
767// terminal (non-dividing) packet when the chain bottoms out above size 1 (on
768// x86 the chain ends at Packet2d / Packet4f, not a size-1 packet), so e.g.
769// find_best_packet<double,3> reports Packet2d even though 3 % 2 != 0.
770template <typename Packet, int Size,
771 bool Terminal = std::is_same<typename unpacket_traits<Packet>::half, Packet>::value>
772struct largest_exact_divisor_size { // recurse into the narrower half packet
773 static constexpr int s = unpacket_traits<Packet>::size;
774 static constexpr int value =
775 (Size % s == 0) ? s : largest_exact_divisor_size<typename unpacket_traits<Packet>::half, Size>::value;
776};
777template <typename Packet, int Size>
778struct largest_exact_divisor_size<Packet, Size, /*Terminal=*/true> { // narrowest packet in the chain
779 static constexpr int s = unpacket_traits<Packet>::size;
780 static constexpr int value = (Size % s == 0) ? s : 1;
781};
782
783// When is the cascade worth diverging from the generic assignment path, and
784// along which axis? Only for fixed-size products where the widest packet that
785// fits the vectorized extent (find_largest_packet) is strictly wider than the
786// widest that exactly divides it -- i.e. the extent is not a clean packet
787// multiple, so the generic path is forced down to a narrower packet or scalars.
788// Exact-fit sizes (2, 4, 8, ...) keep the proven generic path, as do dynamic
789// sizes (GEMM / generic kernels) and extents too small to hold even one packet
790// (e.g. 3-row float). Complex is included: the generic path scalarizes odd
791// extents (find_best<complex<double>,3> == Packet1cd), so the cascade's
792// Packet2cd/Packet4cf tail is a sizeable win there too.
793//
794// The destination must also store the vectorized axis contiguously
795// (DirectAccessBit and InnerStrideAtCompileTime == 1): the cascade writes whole
796// packets with a single unaligned store, so a strided destination such as
797// `m.row(i)` of a column-major matrix would clobber neighbouring coefficients.
798// The generic assignment path makes this check along with MightVectorize and
799// EIGEN_UNALIGNED_VECTORIZE; here we must replicate them since we bypass that path.
800template <typename ProdEval, typename Dst, typename Func>
801struct product_packet_cascade_traits {
802 using Scalar = typename ProdEval::Scalar;
803 using DstEval = evaluator<Dst>;
804 static constexpr int Rows = ProdEval::RowsAtCompileTime;
805 static constexpr int Cols = ProdEval::ColsAtCompileTime;
806 static constexpr bool DstRowMajor = bool(Dst::IsRowMajor);
807 static constexpr bool DstContiguous =
808 (int(DstEval::Flags) & DirectAccessBit) != 0 && int(Dst::InnerStrideAtCompileTime) == 1;
809 // The product's packets run along its storage order. The cascade axis must match
810 // it -- and so must the destination's storage order, so the contiguous packet
811 // store lands on contiguous coefficients.
812 static constexpr bool ProdRowMajor = (int(ProdEval::Flags) & RowMajorBit) != 0;
813 static constexpr bool VecLhs = // column-major product & dst: vectorize down each column
814 bool(ProdEval::Vectorize) && !ProdRowMajor && !DstRowMajor && Rows != Dynamic;
815 static constexpr bool VecRhs = // row-major product & dst: vectorize along each row
816 bool(ProdEval::Vectorize) && ProdRowMajor && DstRowMajor && Cols != Dynamic;
817 static constexpr int Extent = VecLhs ? Rows : VecRhs ? Cols : 1;
818 using Packet = typename find_largest_packet<Scalar, Extent>::type;
819 static constexpr int LargestSize = unpacket_traits<Packet>::size;
820 static constexpr int GenericSize = largest_exact_divisor_size<typename packet_traits<Scalar>::type, Extent>::value;
821 static constexpr bool MightVectorize =
822 bool(EIGEN_UNALIGNED_VECTORIZE) &&
823 (int(DstEval::Flags) & int(ProdEval::Flags) & int(ActualPacketAccessBit)) != 0 &&
824 bool(functor_traits<Func>::PacketAccess);
825 static constexpr bool Enable = (ProdEval::SameType || ProdEval::MixedRealComplex) && (VecLhs || VecRhs) &&
826 DstContiguous && MightVectorize && (LargestSize <= Extent) &&
827 (LargestSize > GenericSize);
828};
829
830// Run the cascade over every outer index of dst. Traits::VecLhs is a compile-time
831// constant, so it folds the bounds away and selects the cascade's ColMajor axis,
832// instantiating only the active orientation (no dead branch on either axis).
833template <typename Traits, typename Func, typename Dst, typename ProdEval>
834EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void product_run_packet_cascade(const Func& func, Dst& dst,
835 const ProdEval& prod) {
836 using Packet = typename Traits::Packet;
837 Index rows = dst.rows(), cols = dst.cols();
838 Index outerCount = Traits::VecLhs ? cols : rows;
839 Index innerEnd = Traits::VecLhs ? rows : cols;
840 for (Index outer = 0; outer < outerCount; ++outer)
841 product_packet_cascade<Packet, Traits::VecLhs>::run(func, dst, prod, outer, 0, innerEnd);
842}
843
844// Product assignment: take the packet cascade when the traits enable
845// it, otherwise the generic assignment path. Dispatch on a bool tag (rather than
846// a plain `if`) so the cascade is only *instantiated* for the vectorizable
847// fixed-size case -- a runtime branch would instantiate it for every product,
848// including scalar types with no packet support.
849template <typename Lhs, typename Rhs, bool EvalToRowMajor>
850struct coeff_based_product_evaluator;
851
852// The coefficient-based evaluator of lhs * rhs with packets along dst's storage order.
853template <typename Lhs, typename Rhs, typename Dst>
854using dst_ordered_product_evaluator = coeff_based_product_evaluator<Lhs, Rhs, bool(Dst::IsRowMajor)>;
855
856template <typename Func, typename Dst, typename Lhs, typename Rhs>
857EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void product_packet_assign(std::true_type, const Func& func, Dst& dst,
858 const Lhs& lhs, const Rhs& rhs) {
859 using ProdEval = dst_ordered_product_evaluator<Lhs, Rhs, Dst>;
860 using Traits = product_packet_cascade_traits<ProdEval, Dst, Func>;
861 const ProdEval prodEval(lhs, rhs);
862 product_run_packet_cascade<Traits>(func, dst, prodEval);
863}
864template <typename Func, typename Dst, typename Lhs, typename Rhs>
865EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void product_packet_assign(std::false_type, const Func& func, Dst& dst,
866 const Lhs& lhs, const Rhs& rhs) {
867 call_assignment_no_alias(dst, lhs.lazyProduct(rhs), func);
868}
869
870template <typename Lhs, typename Rhs>
871struct generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, CoeffBasedProductMode> {
872 using Scalar = typename Product<Lhs, Rhs>::Scalar;
873
874 template <typename Dst>
875 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dst& dst, const Lhs& lhs, const Rhs& rhs) {
876 // Same as: dst.noalias() = lhs.lazyProduct(rhs);
877 // but easier on the compiler side.
878 // NB: same-scalar plain assignment stays on the generic path.
879 // The packet cascade is a measured win for the compound (+=, -=) case, but
880 // for plain assignment the generic InnerVectorized path is already well
881 // tuned -- the cascade was at best neutral there and could regress float
882 // (whose packet half-chain bottoms out at Packet4f, so odd-size tails fall
883 // to scalars). Mixed products also use the cascade when it widens their packets.
884 using ProdEval = dst_ordered_product_evaluator<Lhs, Rhs, Dst>;
885 using Func = internal::assign_op<typename Dst::Scalar, Scalar>;
886 using Traits = product_packet_cascade_traits<ProdEval, Dst, Func>;
887 product_packet_assign(bool_constant<(ProdEval::MixedRealComplex && Traits::Enable)>(), Func(), dst, lhs, rhs);
888 }
889
890 template <typename Dst>
891 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void addTo(Dst& dst, const Lhs& lhs, const Rhs& rhs) {
892 // dst.noalias() += lhs.lazyProduct(rhs);
893 using ProdEval = dst_ordered_product_evaluator<Lhs, Rhs, Dst>;
894 using Func = internal::add_assign_op<typename Dst::Scalar, Scalar>;
895 using Traits = product_packet_cascade_traits<ProdEval, Dst, Func>;
896 product_packet_assign(bool_constant<Traits::Enable>(), Func(), dst, lhs, rhs);
897 }
898
899 template <typename Dst>
900 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void subTo(Dst& dst, const Lhs& lhs, const Rhs& rhs) {
901 // dst.noalias() -= lhs.lazyProduct(rhs);
902 using ProdEval = dst_ordered_product_evaluator<Lhs, Rhs, Dst>;
903 using Func = internal::sub_assign_op<typename Dst::Scalar, Scalar>;
904 using Traits = product_packet_cascade_traits<ProdEval, Dst, Func>;
905 product_packet_assign(bool_constant<Traits::Enable>(), Func(), dst, lhs, rhs);
906 }
907
908 // This is a special evaluation path called from generic_product_impl<...,GemmProduct> in file GeneralMatrixMatrix.h
909 // This variant tries to extract scalar multiples from both the LHS and RHS and factor them out. For instance:
910 // dst {,+,-}= (s1*A)*(B*s2)
911 // will be rewritten as:
912 // dst {,+,-}= (s1*s2) * (A.lazyProduct(B))
913 // There are at least four benefits of doing so:
914 // 1 - huge performance gain for heap-allocated matrix types as it save costly allocations.
915 // 2 - it is faster than simply by-passing the heap allocation through stack allocation.
916 // 3 - it makes this fallback consistent with the heavy GEMM routine.
917 // 4 - it fully by-passes huge stack allocation attempts when multiplying huge fixed-size matrices.
918 // (see https://stackoverflow.com/questions/54738495)
919 // For small fixed sizes matrices, however, the gains are less obvious, it is sometimes x2 faster, but sometimes x3
920 // slower, and the behavior depends also a lot on the compiler... This is why this re-writing strategy is currently
921 // enabled only when falling back from the main GEMM.
922 template <typename Dst, typename Func>
923 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void eval_dynamic(Dst& dst, const Lhs& lhs, const Rhs& rhs,
924 const Func& func) {
925 enum {
926 HasScalarFactor = blas_traits<Lhs>::HasScalarFactor || blas_traits<Rhs>::HasScalarFactor,
927 ConjLhs = blas_traits<Lhs>::NeedToConjugate,
928 ConjRhs = blas_traits<Rhs>::NeedToConjugate
929 };
930 // FIXME: this should be auto, and extractScalarFactor should also return auto
931 // this is important for real*complex_mat
932 Scalar actualAlpha = combine_scalar_factors<Scalar>(lhs, rhs);
933
934 eval_dynamic_impl(dst, blas_traits<Lhs>::extract(lhs).template conjugateIf<ConjLhs>(),
935 blas_traits<Rhs>::extract(rhs).template conjugateIf<ConjRhs>(), func, actualAlpha,
936 bool_constant<HasScalarFactor>());
937 }
938
939 protected:
940 template <typename Dst, typename LhsT, typename RhsT, typename Func, typename Scalar>
941 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void eval_dynamic_impl(Dst& dst, const LhsT& lhs, const RhsT& rhs,
942 const Func& func, const Scalar& s /* == 1 */,
943 std::false_type) {
944 EIGEN_UNUSED_VARIABLE(s);
945 eigen_internal_assert(numext::is_exactly_one(s));
946 call_restricted_packet_assignment_no_alias(dst, lhs.lazyProduct(rhs), func);
947 }
948
949 template <typename Dst, typename LhsT, typename RhsT, typename Func, typename Scalar>
950 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void eval_dynamic_impl(Dst& dst, const LhsT& lhs, const RhsT& rhs,
951 const Func& func, const Scalar& s,
952 std::true_type) {
953 call_restricted_packet_assignment_no_alias(dst, s * lhs.lazyProduct(rhs), func);
954 }
955};
956
957// This specialization enforces the use of a coefficient-based evaluation strategy
958template <typename Lhs, typename Rhs>
959struct generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, LazyCoeffBasedProductMode>
960 : generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, CoeffBasedProductMode> {};
961
962template <typename Packet, typename Scalar, typename ResultScalar = typename unpacket_traits<Packet>::type,
963 bool Real = std::is_same<Scalar, float>::value || std::is_same<Scalar, double>::value>
964struct product_operand_packet {
965 using type = Packet;
966};
967
968// Preserve real operands: promoting them to complex would introduce artificial 0*inf terms.
969template <typename Packet, typename Real>
970struct product_operand_packet<Packet, Real, std::complex<Real>, true> {
971 using type = typename unpacket_traits<Packet>::as_real;
972};
973
974template <typename Packet, typename Scalar>
975using product_operand_packet_t = typename product_operand_packet<Packet, Scalar>::type;
976
977// ploaddup reads half a real packet. Only contiguous, addressable coefficients may use it.
978template <typename Xpr>
979struct product_real_packet_access {
980 using CoeffReturnType = decltype(std::declval<const evaluator<Xpr>&>().coeff(Index(0), Index(0)));
981 static constexpr bool value =
982 (evaluator<Xpr>::Flags & DirectAccessBit) != 0 && Xpr::InnerStrideAtCompileTime == 1 &&
983 std::is_same<std::remove_const_t<std::remove_reference_t<CoeffReturnType>>, typename Xpr::Scalar>::value &&
984 std::is_lvalue_reference<CoeffReturnType>::value;
985};
986
987template <typename Xpr>
988struct product_real_packet_evaluator : evaluator<Xpr> {
989 using Base = evaluator<Xpr>;
990 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit product_real_packet_evaluator(const Xpr& xpr) : Base(xpr) {}
991
992 template <int LoadMode, typename Packet>
993 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename unpacket_traits<Packet>::as_real packet(Index row, Index col) const {
994 return ploaddup<typename unpacket_traits<Packet>::as_real>(&this->coeff(row, col));
995 }
996
997 template <int LoadMode, typename Packet>
998 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename unpacket_traits<Packet>::as_real packetSegment(Index row, Index col,
999 Index begin,
1000 Index count) const {
1001 // The product evaluator handles partial packets before loading either operand.
1002 EIGEN_UNUSED_VARIABLE(begin);
1003 EIGEN_UNUSED_VARIABLE(count);
1004 eigen_internal_assert(begin == 0 && count == unpacket_traits<Packet>::size);
1005 return packet<LoadMode, Packet>(row, col);
1006 }
1007};
1008
1009template <int StorageOrder, int UnrollingIndex, typename Lhs, typename Rhs, typename Packet, int LoadMode>
1010struct etor_product_packet_impl;
1011
1012struct product_empty_packet_evaluator {
1013 template <typename Xpr>
1014 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit product_empty_packet_evaluator(const Xpr&) {}
1015};
1016
1017// Coefficient-based evaluator of lhs * rhs whose packets run along rows if EvalToRowMajor_, else along columns.
1018template <typename Lhs, typename Rhs, bool EvalToRowMajor_>
1019struct coeff_based_product_evaluator : evaluator_base<Product<Lhs, Rhs, LazyProduct>> {
1020 using XprType = Product<Lhs, Rhs, LazyProduct>;
1021 using Scalar = typename XprType::Scalar;
1022 using CoeffReturnType = typename XprType::CoeffReturnType;
1023
1024 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit coeff_based_product_evaluator(const XprType& xpr)
1025 : coeff_based_product_evaluator(xpr.lhs(), xpr.rhs()) {}
1026
1027 // Also construct directly from the product operands so packet-cascade callers
1028 // do not have to materialize a temporary Product expression first.
1029 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE coeff_based_product_evaluator(const Lhs& lhs, const Rhs& rhs)
1030 : m_lhs(lhs),
1031 m_rhs(rhs),
1032 m_lhsImpl(m_lhs), // Real evaluator objects for packet products; empty placeholders for scalar-only products.
1033 m_rhsImpl(m_rhs),
1034 m_innerDim(lhs.cols()) {
1035 EIGEN_INTERNAL_CHECK_COST_VALUE(NumTraits<Scalar>::MulCost);
1036 EIGEN_INTERNAL_CHECK_COST_VALUE(NumTraits<Scalar>::AddCost);
1037 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
1038 }
1039
1040 // Everything below here is taken from CoeffBasedProduct.h
1041
1042 using LhsNested = typename internal::nested_eval<Lhs, Rhs::ColsAtCompileTime>::type;
1043 using RhsNested = typename internal::nested_eval<Rhs, Lhs::RowsAtCompileTime>::type;
1044
1045 using LhsNestedCleaned = internal::remove_all_t<LhsNested>;
1046 using RhsNestedCleaned = internal::remove_all_t<RhsNested>;
1047
1048 using LhsScalar = typename LhsNestedCleaned::Scalar;
1049 using RhsScalar = typename RhsNestedCleaned::Scalar;
1050
1051 using LhsEtorType = evaluator<LhsNestedCleaned>;
1052 using RhsEtorType = evaluator<RhsNestedCleaned>;
1053
1054 enum {
1055 RowsAtCompileTime = LhsNestedCleaned::RowsAtCompileTime,
1056 ColsAtCompileTime = RhsNestedCleaned::ColsAtCompileTime,
1057 InnerSize = min_size_prefer_fixed(LhsNestedCleaned::ColsAtCompileTime, RhsNestedCleaned::RowsAtCompileTime),
1058 MaxRowsAtCompileTime = LhsNestedCleaned::MaxRowsAtCompileTime,
1059 MaxColsAtCompileTime = RhsNestedCleaned::MaxColsAtCompileTime
1060 };
1061
1062 using LhsVecPacketType = typename find_best_packet<Scalar, RowsAtCompileTime>::type;
1063 using RhsVecPacketType = typename find_best_packet<Scalar, ColsAtCompileTime>::type;
1064
1065 static constexpr int LhsCoeffReadCost = LhsEtorType::CoeffReadCost;
1066 static constexpr int RhsCoeffReadCost = RhsEtorType::CoeffReadCost;
1067 static constexpr int CoeffReadCost =
1068 InnerSize == 0 ? NumTraits<Scalar>::ReadCost
1069 : InnerSize == Dynamic
1070 ? HugeCost
1071 : InnerSize * (NumTraits<Scalar>::MulCost + int(LhsCoeffReadCost) + int(RhsCoeffReadCost)) +
1072 (InnerSize - 1) * NumTraits<Scalar>::AddCost;
1073
1074 static constexpr bool Unroll = CoeffReadCost <= EIGEN_UNROLLING_LIMIT;
1075
1076 static constexpr int LhsFlags = LhsEtorType::Flags;
1077 static constexpr int RhsFlags = RhsEtorType::Flags;
1078
1079 static constexpr int LhsRowMajor = LhsFlags & RowMajorBit;
1080 static constexpr int RhsRowMajor = RhsFlags & RowMajorBit;
1081
1082 static constexpr int LhsVecPacketSize = unpacket_traits<LhsVecPacketType>::size;
1083 static constexpr int RhsVecPacketSize = unpacket_traits<RhsVecPacketType>::size;
1084
1085 // Here, we don't care about alignment larger than the usable packet size.
1086 static constexpr int LhsAlignment =
1087 plain_enum_min(LhsEtorType::Alignment, LhsVecPacketSize* int(sizeof(typename LhsNestedCleaned::Scalar)));
1088 static constexpr int RhsAlignment =
1089 plain_enum_min(RhsEtorType::Alignment, RhsVecPacketSize* int(sizeof(typename RhsNestedCleaned::Scalar)));
1090
1091 static constexpr bool SameType =
1092 std::is_same<typename LhsNestedCleaned::Scalar, typename RhsNestedCleaned::Scalar>::value;
1093
1094 static constexpr bool CanVectorizeRhs = bool(RhsRowMajor) && (RhsFlags & PacketAccessBit) && (ColsAtCompileTime != 1);
1095 static constexpr bool CanVectorizeLhs = (!LhsRowMajor) && (LhsFlags & PacketAccessBit) && (RowsAtCompileTime != 1);
1096
1097 // Only the operand that is contiguous along the packets' axis can vectorize the product.
1098 static constexpr bool EvalToRowMajor = EvalToRowMajor_;
1099 using RealScalar = typename NumTraits<Scalar>::Real;
1100 static constexpr bool LhsIsReal = std::is_same<LhsScalar, RealScalar>::value;
1101 static constexpr bool RhsIsReal = std::is_same<RhsScalar, RealScalar>::value;
1102 static constexpr bool MixedScalarTypes =
1103 std::is_same<Scalar, std::complex<RealScalar>>::value &&
1104 (std::is_same<RealScalar, float>::value || std::is_same<RealScalar, double>::value) &&
1105 ((LhsIsReal && std::is_same<RhsScalar, Scalar>::value) || (RhsIsReal && std::is_same<LhsScalar, Scalar>::value));
1106 static constexpr bool RealPacketLhs = LhsIsReal && product_real_packet_access<LhsNestedCleaned>::value;
1107 static constexpr bool RealPacketRhs = RhsIsReal && product_real_packet_access<RhsNestedCleaned>::value;
1108 static constexpr bool PacketOperandVectorizable = EvalToRowMajor ? CanVectorizeRhs : CanVectorizeLhs;
1109 // Expression and evaluator storage orders must agree for consumers that index product packets.
1110 static constexpr bool MixedRealComplex =
1111 MixedScalarTypes && packet_traits<Scalar>::Vectorizable && PacketOperandVectorizable &&
1112 (EvalToRowMajor ? (!RhsIsReal || RealPacketRhs) : (!LhsIsReal || RealPacketLhs)) &&
1113 (bool(int(traits<XprType>::Flags) & RowMajorBit) == bool(EvalToRowMajor));
1114
1115 static constexpr bool Vectorize = (SameType && PacketOperandVectorizable) || MixedRealComplex;
1116 static constexpr int Flags = ((int(LhsFlags) | int(RhsFlags)) & HereditaryBits & ~RowMajorBit) |
1117 (EvalToRowMajor ? RowMajorBit : 0) | (Vectorize ? PacketAccessBit : 0) |
1118 (XprType::IsVectorAtCompileTime ? LinearAccessBit : 0);
1119 using LhsPacketEtorType =
1120 std::conditional_t<bool(int(Flags) & PacketAccessBit),
1121 std::conditional_t<MixedRealComplex && RealPacketLhs && !EvalToRowMajor,
1122 product_real_packet_evaluator<LhsNestedCleaned>, LhsEtorType>,
1123 product_empty_packet_evaluator>;
1124 using RhsPacketEtorType =
1125 std::conditional_t<bool(int(Flags) & PacketAccessBit),
1126 std::conditional_t<MixedRealComplex && RealPacketRhs && EvalToRowMajor,
1127 product_real_packet_evaluator<RhsNestedCleaned>, RhsEtorType>,
1128 product_empty_packet_evaluator>;
1129
1130 static constexpr bool SingleTermCoeff = InnerSize == 1 && Vectorize;
1131
1132 static constexpr int LhsOuterStrideBytes =
1133 int(LhsNestedCleaned::OuterStrideAtCompileTime) * int(sizeof(typename LhsNestedCleaned::Scalar));
1134 static constexpr int RhsOuterStrideBytes =
1135 int(RhsNestedCleaned::OuterStrideAtCompileTime) * int(sizeof(typename RhsNestedCleaned::Scalar));
1136
1137 // Packets come from the operand that vectorizes the product, at its outer index k.
1138 static constexpr int VecOuterStrideBytes = EvalToRowMajor ? RhsOuterStrideBytes : LhsOuterStrideBytes;
1139 static constexpr int VecAlignment = EvalToRowMajor ? RhsAlignment : LhsAlignment;
1140 static constexpr int Alignment =
1141 (Vectorize && VecOuterStrideBytes > 0 && VecOuterStrideBytes % plain_enum_max(1, VecAlignment) == 0)
1142 ? VecAlignment
1143 : 0;
1144
1145 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const CoeffReturnType coeff(Index row, Index col) const {
1146 return coeff_impl(row, col, bool_constant<SingleTermCoeff>());
1147 }
1148
1149 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE CoeffReturnType coeff_impl(Index row, Index col,
1150 std::true_type) const {
1151 return fast_mult_op<LhsScalar, RhsScalar>()(m_lhsImpl.coeff(row, Index(0)), m_rhsImpl.coeff(Index(0), col));
1152 }
1153
1154 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE CoeffReturnType coeff_impl(Index row, Index col,
1155 std::false_type) const {
1156 return coeff_reduced(row, col, bool_constant<MixedRealComplex>());
1157 }
1158
1159 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff_reduced(Index row, Index col, std::true_type) const {
1160 using SinglePacket = typename find_best_packet<Scalar, 1>::type;
1161 EIGEN_IF_CONSTEXPR (unpacket_traits<SinglePacket>::size == 1) {
1162 return pfirst(packetSegment<Unaligned, SinglePacket>(row, col, 0, 1));
1163 }
1164 return coeff_reduced(row, col, std::false_type());
1165 }
1166
1167 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE CoeffReturnType coeff_reduced(Index row, Index col,
1168 std::false_type) const {
1169 // fast_mult_op is cwiseProduct's scalar_product_op with a pmul-based scalar path, so the
1170 // reduction (and its precision) is unchanged but complex scalars avoid std::complex::operator*
1171 // (the slow libgcc __mul?c3). See fast_mult_op.
1172 return m_lhs.row(row).transpose().binaryExpr(m_rhs.col(col), fast_mult_op<LhsScalar, RhsScalar>()).sum();
1173 }
1174
1175 /* Allow index-based non-packet access. It is impossible though to allow index-based packed access,
1176 * which is why we don't set the LinearAccessBit.
1177 * TODO: this seems possible when the result is a vector
1178 */
1179 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const CoeffReturnType coeff(Index index) const {
1180 const Index row = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime == 1) ? 0 : index;
1181 const Index col = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime == 1) ? index : 0;
1182 return coeff(row, col);
1183 }
1184
1185 template <int LoadMode, typename PacketType>
1186 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packet(Index row, Index col) const {
1187 PacketType res;
1188 using PacketImpl = etor_product_packet_impl<bool(int(Flags) & RowMajorBit) ? RowMajor : ColMajor,
1189 Unroll ? int(InnerSize) : Dynamic, LhsPacketEtorType, RhsPacketEtorType,
1190 PacketType, LoadMode>;
1191 PacketImpl::run(row, col, m_lhsImpl, m_rhsImpl, m_innerDim, res);
1192 return res;
1193 }
1194
1195 template <int LoadMode, typename PacketType>
1196 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packet(Index index) const {
1197 const Index row = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime == 1) ? 0 : index;
1198 const Index col = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime == 1) ? index : 0;
1199 return packet<LoadMode, PacketType>(row, col);
1200 }
1201
1202 template <int LoadMode, typename PacketType>
1203 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packetSegment(Index row, Index col, Index begin,
1204 Index count) const {
1205 EIGEN_IF_CONSTEXPR (MixedRealComplex) {
1206 if (count == 0) return pset1<PacketType>(Scalar(0));
1207 }
1208 EIGEN_IF_CONSTEXPR (MixedRealComplex && (EvalToRowMajor ? RealPacketRhs : RealPacketLhs) &&
1209 unpacket_traits<PacketType>::size > 1) {
1210 if (count != unpacket_traits<PacketType>::size) {
1211 // The real-packet adapter's ploaddup requires a full source half-packet; a partial load could overread.
1212 Scalar values[unpacket_traits<PacketType>::size] = {};
1213 for (Index i = begin; i < begin + count; ++i) {
1214 values[i] = coeff_reduced(row + (EvalToRowMajor ? 0 : i), col + (EvalToRowMajor ? i : 0), std::false_type());
1215 }
1216 return ploadu<PacketType>(values);
1217 }
1218 }
1219 PacketType res;
1220 using PacketImpl = etor_product_packet_impl<bool(int(Flags) & RowMajorBit) ? RowMajor : ColMajor,
1221 Unroll ? int(InnerSize) : Dynamic, LhsPacketEtorType, RhsPacketEtorType,
1222 PacketType, LoadMode>;
1223 PacketImpl::run_segment(row, col, m_lhsImpl, m_rhsImpl, m_innerDim, res, begin, count);
1224 return res;
1225 }
1226
1227 template <int LoadMode, typename PacketType>
1228 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packetSegment(Index index, Index begin, Index count) const {
1229 const Index row = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime == 1) ? 0 : index;
1230 const Index col = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime == 1) ? index : 0;
1231 return packetSegment<LoadMode, PacketType>(row, col, begin, count);
1232 }
1233
1234 protected:
1235 add_const_on_value_type_t<LhsNested> m_lhs;
1236 add_const_on_value_type_t<RhsNested> m_rhs;
1237
1238 LhsPacketEtorType m_lhsImpl;
1239 RhsPacketEtorType m_rhsImpl;
1240
1241 variable_if_dynamic<Index, InnerSize> m_innerDim;
1242};
1243
1244// Evaluate in the storage order the expression reports, which consumers such as Reverse, Redux, Block and FindCoeff
1245// use to index packets and size their loops.
1246template <typename Lhs, typename Rhs, int ProductTag>
1247struct product_evaluator<Product<Lhs, Rhs, LazyProduct>, ProductTag, DenseShape, DenseShape>
1248 : coeff_based_product_evaluator<Lhs, Rhs, (int(traits<Product<Lhs, Rhs, LazyProduct>>::Flags) & RowMajorBit) != 0> {
1249 using XprType = Product<Lhs, Rhs, LazyProduct>;
1250 using Base =
1251 coeff_based_product_evaluator<Lhs, Rhs, (int(traits<Product<Lhs, Rhs, LazyProduct>>::Flags) & RowMajorBit) != 0>;
1252
1253 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit product_evaluator(const XprType& xpr) : Base(xpr) {}
1254 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE product_evaluator(const Lhs& lhs, const Rhs& rhs) : Base(lhs, rhs) {}
1255};
1256
1257// A dense assignment reads its source only through the evaluator, so it may evaluate a coefficient-based product
1258// along the destination's storage order, which the destination's packets need. It does so only where that order
1259// vectorizes; otherwise the assignment is scalar in either order, and the expression's evaluator is kept.
1260template <typename DstEvaluatorType, typename Lhs, typename Rhs>
1261struct dense_assignment_source_evaluator<
1262 DstEvaluatorType, Product<Lhs, Rhs, LazyProduct>,
1263 std::enable_if_t<std::is_same<typename evaluator_traits<Lhs>::Shape, DenseShape>::value &&
1264 std::is_same<typename evaluator_traits<Rhs>::Shape, DenseShape>::value>> {
1265 using DstOrdered = coeff_based_product_evaluator<Lhs, Rhs, (int(DstEvaluatorType::Flags) & RowMajorBit) != 0>;
1266 using type = std::conditional_t<DstOrdered::Vectorize, DstOrdered, evaluator<Product<Lhs, Rhs, LazyProduct>>>;
1267};
1268
1269template <typename Lhs, typename Rhs>
1270struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, LazyCoeffBasedProductMode, DenseShape, DenseShape>
1271 : product_evaluator<Product<Lhs, Rhs, LazyProduct>, CoeffBasedProductMode, DenseShape, DenseShape> {
1272 using XprType = Product<Lhs, Rhs, DefaultProduct>;
1273 using BaseProduct = Product<Lhs, Rhs, LazyProduct>;
1274 using Base = product_evaluator<BaseProduct, CoeffBasedProductMode, DenseShape, DenseShape>;
1275 static_assert((int(traits<XprType>::Flags) & RowMajorBit) == (int(Base::Flags) & RowMajorBit),
1276 "the evaluator must use the storage order its expression reports");
1277 enum { Flags = Base::Flags | EvalBeforeNestingBit };
1278 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit product_evaluator(const XprType& xpr)
1279 : Base(BaseProduct(xpr.lhs(), xpr.rhs())) {}
1280};
1281
1282/****************************************
1283*** Coeff based product, Packet path ***
1284****************************************/
1285
1286template <int UnrollingIndex, typename Lhs, typename Rhs, typename Packet, int LoadMode>
1287struct etor_product_packet_impl<RowMajor, UnrollingIndex, Lhs, Rhs, Packet, LoadMode> {
1288 using FactorPacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Lhs&>().coeff(0, 0))>>;
1289 using ValuePacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Rhs&>().coeff(0, 0))>>;
1290 using Op = conj_helper<FactorPacket, ValuePacket, false, false>;
1291 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Index row, Index col, const Lhs& lhs, const Rhs& rhs,
1292 Index innerDim, Packet& res) {
1293 etor_product_packet_impl<RowMajor, UnrollingIndex - 1, Lhs, Rhs, Packet, LoadMode>::run(row, col, lhs, rhs,
1294 innerDim, res);
1295 res = Op().pmadd(pset1<FactorPacket>(lhs.coeff(row, Index(UnrollingIndex - 1))),
1296 rhs.template packet<LoadMode, Packet>(Index(UnrollingIndex - 1), col), res);
1297 }
1298 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run_segment(Index row, Index col, const Lhs& lhs, const Rhs& rhs,
1299 Index innerDim, Packet& res, Index begin, Index count) {
1300 etor_product_packet_impl<RowMajor, UnrollingIndex - 1, Lhs, Rhs, Packet, LoadMode>::run_segment(
1301 row, col, lhs, rhs, innerDim, res, begin, count);
1302 res = Op().pmadd(pset1<FactorPacket>(lhs.coeff(row, Index(UnrollingIndex - 1))),
1303 rhs.template packetSegment<LoadMode, Packet>(Index(UnrollingIndex - 1), col, begin, count), res);
1304 }
1305};
1306
1307template <int UnrollingIndex, typename Lhs, typename Rhs, typename Packet, int LoadMode>
1308struct etor_product_packet_impl<ColMajor, UnrollingIndex, Lhs, Rhs, Packet, LoadMode> {
1309 using FactorPacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Rhs&>().coeff(0, 0))>>;
1310 using ValuePacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Lhs&>().coeff(0, 0))>>;
1311 using Op = conj_helper<ValuePacket, FactorPacket, false, false>;
1312 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Index row, Index col, const Lhs& lhs, const Rhs& rhs,
1313 Index innerDim, Packet& res) {
1314 etor_product_packet_impl<ColMajor, UnrollingIndex - 1, Lhs, Rhs, Packet, LoadMode>::run(row, col, lhs, rhs,
1315 innerDim, res);
1316 res = Op().pmadd(lhs.template packet<LoadMode, Packet>(row, Index(UnrollingIndex - 1)),
1317 pset1<FactorPacket>(rhs.coeff(Index(UnrollingIndex - 1), col)), res);
1318 }
1319 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run_segment(Index row, Index col, const Lhs& lhs, const Rhs& rhs,
1320 Index innerDim, Packet& res, Index begin, Index count) {
1321 etor_product_packet_impl<ColMajor, UnrollingIndex - 1, Lhs, Rhs, Packet, LoadMode>::run_segment(
1322 row, col, lhs, rhs, innerDim, res, begin, count);
1323 res = Op().pmadd(lhs.template packetSegment<LoadMode, Packet>(row, Index(UnrollingIndex - 1), begin, count),
1324 pset1<FactorPacket>(rhs.coeff(Index(UnrollingIndex - 1), col)), res);
1325 }
1326};
1327
1328template <typename Lhs, typename Rhs, typename Packet, int LoadMode>
1329struct etor_product_packet_impl<RowMajor, 1, Lhs, Rhs, Packet, LoadMode> {
1330 using FactorPacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Lhs&>().coeff(0, 0))>>;
1331 using ValuePacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Rhs&>().coeff(0, 0))>>;
1332 using Op = conj_helper<FactorPacket, ValuePacket, false, false>;
1333 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE void run(Index row, Index col, const Lhs& lhs, const Rhs& rhs,
1334 Index /*innerDim*/, Packet& res) {
1335 res =
1336 Op().pmul(pset1<FactorPacket>(lhs.coeff(row, Index(0))), rhs.template packet<LoadMode, Packet>(Index(0), col));
1337 }
1338 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run_segment(Index row, Index col, const Lhs& lhs, const Rhs& rhs,
1339 Index /*innerDim*/, Packet& res, Index begin,
1340 Index count) {
1341 res = Op().pmul(pset1<FactorPacket>(lhs.coeff(row, Index(0))),
1342 rhs.template packetSegment<LoadMode, Packet>(Index(0), col, begin, count));
1343 }
1344};
1345
1346template <typename Lhs, typename Rhs, typename Packet, int LoadMode>
1347struct etor_product_packet_impl<ColMajor, 1, Lhs, Rhs, Packet, LoadMode> {
1348 using FactorPacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Rhs&>().coeff(0, 0))>>;
1349 using ValuePacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Lhs&>().coeff(0, 0))>>;
1350 using Op = conj_helper<ValuePacket, FactorPacket, false, false>;
1351 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE void run(Index row, Index col, const Lhs& lhs, const Rhs& rhs,
1352 Index /*innerDim*/, Packet& res) {
1353 res =
1354 Op().pmul(lhs.template packet<LoadMode, Packet>(row, Index(0)), pset1<FactorPacket>(rhs.coeff(Index(0), col)));
1355 }
1356 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run_segment(Index row, Index col, const Lhs& lhs, const Rhs& rhs,
1357 Index /*innerDim*/, Packet& res, Index begin,
1358 Index count) {
1359 res = Op().pmul(lhs.template packetSegment<LoadMode, Packet>(row, Index(0), begin, count),
1360 pset1<FactorPacket>(rhs.coeff(Index(0), col)));
1361 }
1362};
1363
1364template <typename Lhs, typename Rhs, typename Packet, int LoadMode>
1365struct etor_product_packet_impl<RowMajor, 0, Lhs, Rhs, Packet, LoadMode> {
1366 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Index /*row*/, Index /*col*/, const Lhs& /*lhs*/,
1367 const Rhs& /*rhs*/, Index /*innerDim*/, Packet& res) {
1368 res = pset1<Packet>(typename unpacket_traits<Packet>::type(0));
1369 }
1370 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run_segment(Index /*row*/, Index /*col*/, const Lhs& /*lhs*/,
1371 const Rhs& /*rhs*/, Index /*innerDim*/, Packet& res,
1372 Index /*begin*/, Index /*count*/) {
1373 res = pset1<Packet>(typename unpacket_traits<Packet>::type(0));
1374 }
1375};
1376
1377template <typename Lhs, typename Rhs, typename Packet, int LoadMode>
1378struct etor_product_packet_impl<ColMajor, 0, Lhs, Rhs, Packet, LoadMode> {
1379 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Index /*row*/, Index /*col*/, const Lhs& /*lhs*/,
1380 const Rhs& /*rhs*/, Index /*innerDim*/, Packet& res) {
1381 res = pset1<Packet>(typename unpacket_traits<Packet>::type(0));
1382 }
1383 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run_segment(Index /*row*/, Index /*col*/, const Lhs& /*lhs*/,
1384 const Rhs& /*rhs*/, Index /*innerDim*/, Packet& res,
1385 Index /*begin*/, Index /*count*/) {
1386 res = pset1<Packet>(typename unpacket_traits<Packet>::type(0));
1387 }
1388};
1389
1390template <typename Packet, typename ReadLhs, typename ReadRhs>
1391EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet product_packet_accumulate(Index innerDim, const ReadLhs& lhs,
1392 const ReadRhs& rhs) {
1393 using LhsPacket = decltype(lhs(Index(0)));
1394 using RhsPacket = decltype(rhs(Index(0)));
1395 const conj_helper<LhsPacket, RhsPacket, false, false> op;
1396 Packet res = pset1<Packet>(typename unpacket_traits<Packet>::type(0));
1397 Index i = 0;
1398 // Seeding mixed products from their first term preserves single-term signed zeros.
1399 EIGEN_IF_CONSTEXPR ((!std::is_same<LhsPacket, RhsPacket>::value)) {
1400 if (innerDim > 0) {
1401 res = op.pmul(lhs(0), rhs(0));
1402 i = 1;
1403 }
1404 }
1405 for (; i < innerDim; ++i) res = op.pmadd(lhs(i), rhs(i), res);
1406 return res;
1407}
1408
1409template <typename Lhs, typename Rhs, typename Packet, int LoadMode>
1410struct etor_product_packet_impl<RowMajor, Dynamic, Lhs, Rhs, Packet, LoadMode> {
1411 using FactorPacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Lhs&>().coeff(0, 0))>>;
1412 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Index row, Index col, const Lhs& lhs, const Rhs& rhs,
1413 Index innerDim, Packet& res) {
1414 res = product_packet_accumulate<Packet>(
1415 innerDim, [&](Index i) { return pset1<FactorPacket>(lhs.coeff(row, i)); },
1416 [&](Index i) { return rhs.template packet<LoadMode, Packet>(i, col); });
1417 }
1418 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run_segment(Index row, Index col, const Lhs& lhs, const Rhs& rhs,
1419 Index innerDim, Packet& res, Index begin, Index count) {
1420 res = product_packet_accumulate<Packet>(
1421 innerDim, [&](Index i) { return pset1<FactorPacket>(lhs.coeff(row, i)); },
1422 [&](Index i) { return rhs.template packetSegment<LoadMode, Packet>(i, col, begin, count); });
1423 }
1424};
1425
1426template <typename Lhs, typename Rhs, typename Packet, int LoadMode>
1427struct etor_product_packet_impl<ColMajor, Dynamic, Lhs, Rhs, Packet, LoadMode> {
1428 using FactorPacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Rhs&>().coeff(0, 0))>>;
1429 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Index row, Index col, const Lhs& lhs, const Rhs& rhs,
1430 Index innerDim, Packet& res) {
1431 res = product_packet_accumulate<Packet>(
1432 innerDim, [&](Index i) { return lhs.template packet<LoadMode, Packet>(row, i); },
1433 [&](Index i) { return pset1<FactorPacket>(rhs.coeff(i, col)); });
1434 }
1435 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run_segment(Index row, Index col, const Lhs& lhs, const Rhs& rhs,
1436 Index innerDim, Packet& res, Index begin, Index count) {
1437 res = product_packet_accumulate<Packet>(
1438 innerDim, [&](Index i) { return lhs.template packetSegment<LoadMode, Packet>(row, i, begin, count); },
1439 [&](Index i) { return pset1<FactorPacket>(rhs.coeff(i, col)); });
1440 }
1441};
1442
1443/***************************************************************************
1444 * Triangular products
1445 ***************************************************************************/
1446template <int Mode, bool LhsIsTriangular, typename Lhs, bool LhsIsVector, typename Rhs, bool RhsIsVector>
1447struct triangular_product_impl;
1448
1449template <int ProductOrder>
1450struct diagonal_product_segment_impl;
1451
1452template <>
1453struct diagonal_product_segment_impl<OnTheLeft> {
1454 template <typename DstSegment, typename Coeffs, typename DiagonalType, typename Alpha>
1455 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(DstSegment& dst, const Coeffs& coeffs,
1456 const DiagonalType& diagonal, Index begin, Index /*col*/,
1457 const Alpha& alpha) {
1458 dst += alpha * diagonal.segment(begin, dst.size()).cwiseProduct(coeffs);
1459 }
1460 template <typename DstSegment, typename Coeffs, typename DiagonalType>
1461 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void runOverwrite(DstSegment& dst, const Coeffs& coeffs,
1462 const DiagonalType& diagonal, Index begin,
1463 Index /*col*/) {
1464 dst = diagonal.segment(begin, dst.size()).cwiseProduct(coeffs);
1465 }
1466};
1467
1468template <>
1469struct diagonal_product_segment_impl<OnTheRight> {
1470 template <typename DstSegment, typename Coeffs, typename DiagonalType, typename Alpha>
1471 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(DstSegment& dst, const Coeffs& coeffs,
1472 const DiagonalType& diagonal, Index /*begin*/, Index col,
1473 const Alpha& alpha) {
1474 dst += alpha * (coeffs * diagonal.coeff(col));
1475 }
1476 template <typename DstSegment, typename Coeffs, typename DiagonalType>
1477 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void runOverwrite(DstSegment& dst, const Coeffs& coeffs,
1478 const DiagonalType& diagonal, Index /*begin*/,
1479 Index col) {
1480 dst = coeffs * diagonal.coeff(col);
1481 }
1482};
1483
1484template <int Mode, int ProductOrder, typename MatrixType, typename DiagonalType>
1485struct triangular_diagonal_product_impl {
1486 using MatrixScalar = typename MatrixType::Scalar;
1487
1488 template <typename Dest, typename Alpha>
1489 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Dest& dst, const MatrixType& matrix,
1490 const DiagonalType& diagonal, const Alpha& alpha) {
1491 EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_AXIS_MSG(diagonal, matrix, ProductOrder == OnTheLeft, "invalid matrix product");
1492
1493 const Index rows = matrix.rows();
1494 const Index cols = matrix.cols();
1495 for (Index col = 0; col < cols; ++col) {
1496 EIGEN_IF_CONSTEXPR ((Mode & Upper) == Upper) {
1497 const Index end = (std::min)(rows, ((Mode & (UnitDiag | ZeroDiag)) ? col : col + 1));
1498 addStoredSegment(dst, matrix, diagonal, 0, end, col, alpha);
1499 } else {
1500 const Index begin = ((Mode & (UnitDiag | ZeroDiag)) ? col + 1 : col);
1501 addStoredSegment(dst, matrix, diagonal, begin, rows - begin, col, alpha);
1502 }
1503
1504 EIGEN_IF_CONSTEXPR ((Mode & UnitDiag) == UnitDiag) {
1505 if (col < rows) addUnitCoeff(dst, diagonal, col, alpha);
1506 }
1507 }
1508 }
1509
1510 private:
1511 template <typename Dest, typename Alpha>
1512 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void addStoredSegment(Dest& dst, const MatrixType& matrix,
1513 const DiagonalType& diagonal, Index begin,
1514 Index size, Index col, const Alpha& alpha) {
1515 if (size <= 0) return;
1516 auto dstSegment = dst.col(col).segment(begin, size);
1517 diagonal_product_segment_impl<ProductOrder>::run(dstSegment, matrix.col(col).segment(begin, size), diagonal, begin,
1518 col, alpha);
1519 }
1520
1521 template <typename Dest, typename Alpha>
1522 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void addUnitCoeff(Dest& dst, const DiagonalType& diagonal, Index index,
1523 const Alpha& alpha) {
1524 EIGEN_IF_CONSTEXPR (ProductOrder == OnTheLeft) {
1525 dst.coeffRef(index, index) += alpha * (diagonal.coeff(index) * MatrixScalar(1));
1526 } else {
1527 dst.coeffRef(index, index) += alpha * (MatrixScalar(1) * diagonal.coeff(index));
1528 }
1529 }
1530};
1531
1532template <typename Lhs, typename Rhs, int ProductTag>
1533struct generic_product_impl<Lhs, Rhs, TriangularShape, DenseShape, ProductTag>
1534 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, TriangularShape, DenseShape, ProductTag>> {
1535 using Scalar = typename Product<Lhs, Rhs>::Scalar;
1536
1537 template <typename Dest>
1538 static void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
1539 triangular_product_impl<Lhs::Mode, true, typename Lhs::MatrixType, false, Rhs, Rhs::ColsAtCompileTime == 1>::run(
1540 dst, lhs.nestedExpression(), rhs, alpha);
1541 }
1542};
1543
1544template <typename Lhs, typename Rhs, int ProductTag>
1545struct generic_product_impl<Lhs, Rhs, DenseShape, TriangularShape, ProductTag>
1546 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, DenseShape, TriangularShape, ProductTag>> {
1547 using Scalar = typename Product<Lhs, Rhs>::Scalar;
1548
1549 template <typename Dest>
1550 static void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
1551 triangular_product_impl<Rhs::Mode, false, Lhs, Lhs::RowsAtCompileTime == 1, typename Rhs::MatrixType, false>::run(
1552 dst, lhs, rhs.nestedExpression(), alpha);
1553 }
1554};
1555
1556template <typename Lhs, typename Rhs, int ProductTag>
1557struct generic_product_impl<Lhs, Rhs, TriangularShape, DiagonalShape, ProductTag>
1558 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, TriangularShape, DiagonalShape, ProductTag>> {
1559 using Scalar = typename Product<Lhs, Rhs>::Scalar;
1560
1561 template <typename Dest>
1562 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs,
1563 const Scalar& alpha) {
1564 triangular_diagonal_product_impl<Lhs::Mode, OnTheRight, typename Lhs::MatrixType,
1565 typename Rhs::DiagonalVectorType>::run(dst, lhs.nestedExpression(), rhs.diagonal(),
1566 alpha);
1567 }
1568};
1569
1570template <typename Lhs, typename Rhs, int ProductTag>
1571struct generic_product_impl<Lhs, Rhs, DiagonalShape, TriangularShape, ProductTag>
1572 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, DiagonalShape, TriangularShape, ProductTag>> {
1573 using Scalar = typename Product<Lhs, Rhs>::Scalar;
1574
1575 template <typename Dest>
1576 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs,
1577 const Scalar& alpha) {
1578 triangular_diagonal_product_impl<Rhs::Mode, OnTheLeft, typename Rhs::MatrixType,
1579 typename Lhs::DiagonalVectorType>::run(dst, rhs.nestedExpression(), lhs.diagonal(),
1580 alpha);
1581 }
1582};
1583
1584/***************************************************************************
1585 * SelfAdjoint products
1586 ***************************************************************************/
1587template <typename Lhs, int LhsMode, bool LhsIsVector, typename Rhs, int RhsMode, bool RhsIsVector>
1588struct selfadjoint_product_impl;
1589
1590template <int Mode, int ProductOrder, typename MatrixType, typename DiagonalType>
1591struct selfadjoint_diagonal_product_impl {
1592 // Accumulating: dst += alpha * (matrix.selfadjointView<Mode>() * diagonal[asDiagonal])
1593 // (with the diagonal on the right or left as ProductOrder dictates).
1594 template <typename Dest, typename Alpha>
1595 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Dest& dst, const MatrixType& matrix,
1596 const DiagonalType& diagonal, const Alpha& alpha) {
1597 runImpl<true>(dst, matrix, diagonal, alpha);
1598 }
1599
1600 // Overwriting: dst = matrix.selfadjointView<Mode>() * diagonal[asDiagonal].
1601 // Each output entry is written exactly once, so the caller can skip the
1602 // dst.setZero() pass that generic_product_impl_base::evalTo would do.
1603 template <typename Dest>
1604 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void runOverwrite(Dest& dst, const MatrixType& matrix,
1605 const DiagonalType& diagonal) {
1606 using Scalar = typename traits<MatrixType>::Scalar;
1607 runImpl<false>(dst, matrix, diagonal, Scalar(1));
1608 }
1609
1610 private:
1611 // Tile size for the blocked mirror pass. Tuned so that one BlockSize x
1612 // BlockSize source tile fits comfortably in L1 across common scalar / SIMD
1613 // combinations: 32x32 of double = 8 KB, of complex<double> = 16 KB, both
1614 // well under typical 32 KB L1. Smaller tiles leave SIMD work on the table
1615 // for AVX/AVX-512; larger tiles spill out of L1 on machines with smaller
1616 // caches.
1617 static constexpr Index BlockSize = 32;
1618
1619 // The mirror half writes the strict-other-triangle of dst. The naive
1620 // per-column form reads matrix.row(col).segment(...) which has stride =
1621 // leading dimension, so on an N x N source it streams cold cache lines.
1622 // We walk the mirror in BlockSize x BlockSize tiles instead: off-diagonal
1623 // tiles use a blocked conjugate-transpose, and the small diagonal tile
1624 // falls back to the per-column row-strided loop where the working set is
1625 // L1-hot.
1626 template <bool Accumulate, typename Dest, typename Alpha>
1627 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void runImpl(Dest& dst, const MatrixType& matrix,
1628 const DiagonalType& diagonal, const Alpha& alpha) {
1629 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(matrix, "SelfAdjointView is only for squared matrices");
1630 EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_ROWS_MSG(diagonal, matrix, "invalid matrix product");
1631
1632 const Index size = matrix.rows();
1633
1634 // Stored half: one column-strided segment per output column.
1635 for (Index col = 0; col < size; ++col) {
1636 EIGEN_IF_CONSTEXPR ((Mode & Upper) == Upper) {
1637 storedSegment<Accumulate>(dst, matrix, diagonal, 0, col + 1, col, alpha);
1638 } else {
1639 storedSegment<Accumulate>(dst, matrix, diagonal, col, size - col, col, alpha);
1640 }
1641 }
1642
1643 // Mirror half.
1644 for (Index ib = 0; ib < size; ib += BlockSize) {
1645 const Index ib_end = numext::mini(size, ib + BlockSize);
1646 const Index br = ib_end - ib;
1647 EIGEN_IF_CONSTEXPR ((Mode & Upper) == Upper) {
1648 // Off-diagonal: write strict-lower of dst from strict-upper of source.
1649 for (Index jb = 0; jb < ib; jb += BlockSize) {
1650 const Index bc = numext::mini(jb + BlockSize, ib) - jb;
1651 mirrorBlock<Accumulate>(dst, matrix, diagonal, ib, jb, br, bc, alpha);
1652 }
1653 // Diagonal tile: in-tile strict-lower mirror.
1654 for (Index col = ib; col < ib_end; ++col)
1655 conjugateSegment<Accumulate>(dst, matrix, diagonal, col + 1, ib_end - col - 1, col, alpha);
1656 } else {
1657 // Off-diagonal: write strict-upper of dst from strict-lower of source.
1658 for (Index jb = ib_end; jb < size; jb += BlockSize) {
1659 const Index bc = numext::mini(size, jb + BlockSize) - jb;
1660 mirrorBlock<Accumulate>(dst, matrix, diagonal, ib, jb, br, bc, alpha);
1661 }
1662 // Diagonal tile: in-tile strict-upper mirror.
1663 for (Index col = ib; col < ib_end; ++col)
1664 conjugateSegment<Accumulate>(dst, matrix, diagonal, ib, col - ib, col, alpha);
1665 }
1666 }
1667 }
1668
1669 template <bool Accumulate, typename Dest, typename Alpha>
1670 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void storedSegment(Dest& dst, const MatrixType& matrix,
1671 const DiagonalType& diagonal, Index begin, Index size,
1672 Index col, const Alpha& alpha) {
1673 if (size <= 0) return;
1674 auto dstSegment = dst.col(col).segment(begin, size);
1675 auto srcSegment = matrix.col(col).segment(begin, size);
1676 EIGEN_IF_CONSTEXPR (Accumulate) {
1677 diagonal_product_segment_impl<ProductOrder>::run(dstSegment, srcSegment, diagonal, begin, col, alpha);
1678 } else {
1679 diagonal_product_segment_impl<ProductOrder>::runOverwrite(dstSegment, srcSegment, diagonal, begin, col);
1680 }
1681 }
1682
1683 template <bool Accumulate, typename Dest, typename Alpha>
1684 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void conjugateSegment(Dest& dst, const MatrixType& matrix,
1685 const DiagonalType& diagonal, Index begin,
1686 Index size, Index col, const Alpha& alpha) {
1687 if (size <= 0) return;
1688 auto dstSegment = dst.col(col).segment(begin, size);
1689 auto srcSegment = matrix.row(col).segment(begin, size).conjugate().transpose();
1690 EIGEN_IF_CONSTEXPR (Accumulate) {
1691 diagonal_product_segment_impl<ProductOrder>::run(dstSegment, srcSegment, diagonal, begin, col, alpha);
1692 } else {
1693 diagonal_product_segment_impl<ProductOrder>::runOverwrite(dstSegment, srcSegment, diagonal, begin, col);
1694 }
1695 }
1696
1697 // dst.block(ib, jb, br, bc) [+= alpha *] matrix.block(jb, ib, bc, br).adjoint() * <diag>,
1698 // where <diag> scales each output column (OnTheRight) or row (OnTheLeft).
1699 // Loop bounds in runImpl guarantee br > 0 && bc > 0.
1700 template <bool Accumulate, typename Dest, typename Alpha>
1701 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void mirrorBlock(Dest& dst, const MatrixType& matrix,
1702 const DiagonalType& diagonal, Index ib, Index jb,
1703 Index br, Index bc, const Alpha& alpha) {
1704 auto dstBlock = dst.block(ib, jb, br, bc);
1705 auto srcAdjoint = matrix.block(jb, ib, bc, br).adjoint();
1706 EIGEN_IF_CONSTEXPR (ProductOrder == OnTheRight) {
1707 auto scaled = srcAdjoint * diagonal.segment(jb, bc).asDiagonal();
1708 EIGEN_IF_CONSTEXPR (Accumulate) {
1709 dstBlock += alpha * scaled;
1710 } else {
1711 dstBlock = scaled;
1712 }
1713 } else {
1714 auto scaled = diagonal.segment(ib, br).asDiagonal() * srcAdjoint;
1715 EIGEN_IF_CONSTEXPR (Accumulate) {
1716 dstBlock += alpha * scaled;
1717 } else {
1718 dstBlock = scaled;
1719 }
1720 }
1721 }
1722};
1723
1724template <typename Lhs, typename Rhs, int ProductTag>
1725struct generic_product_impl<Lhs, Rhs, SelfAdjointShape, DenseShape, ProductTag>
1726 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, SelfAdjointShape, DenseShape, ProductTag>> {
1727 using Scalar = typename Product<Lhs, Rhs>::Scalar;
1728
1729 template <typename Dest>
1730 static EIGEN_DEVICE_FUNC void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
1731 selfadjoint_product_impl<typename Lhs::MatrixType, Lhs::Mode, false, Rhs, 0, Rhs::ColsAtCompileTime == 1>::run(
1732 dst, lhs.nestedExpression(), rhs, alpha);
1733 }
1734};
1735
1736template <typename Lhs, typename Rhs, int ProductTag>
1737struct generic_product_impl<Lhs, Rhs, DenseShape, SelfAdjointShape, ProductTag>
1738 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, DenseShape, SelfAdjointShape, ProductTag>> {
1739 using Scalar = typename Product<Lhs, Rhs>::Scalar;
1740
1741 template <typename Dest>
1742 static void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
1743 selfadjoint_product_impl<Lhs, 0, Lhs::RowsAtCompileTime == 1, typename Rhs::MatrixType, Rhs::Mode, false>::run(
1744 dst, lhs, rhs.nestedExpression(), alpha);
1745 }
1746};
1747
1748template <typename Lhs, typename Rhs, int ProductTag>
1749struct generic_product_impl<Lhs, Rhs, SelfAdjointShape, DiagonalShape, ProductTag>
1750 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, SelfAdjointShape, DiagonalShape, ProductTag>> {
1751 using Scalar = typename Product<Lhs, Rhs>::Scalar;
1752 // The "Dense ?= scalar * Product" rewriting rule folds an outer alpha into the
1753 // SelfAdjointView via SelfAdjointView::operator*(scalar), whose nested
1754 // expression becomes (matrix * alpha). For complex alpha this is no longer
1755 // Hermitian — the mirror half of our kernel would produce conj(alpha) on the
1756 // off-triangle. Strip the scalar factor with blas_traits and re-fold it into
1757 // the kernel's alpha so the same scalar multiplies every output entry.
1758 using LhsBlasTraits = blas_traits<typename Lhs::MatrixType>;
1759 // A named constant, not bool(...): nvcc's front end re-emits that cast as a function type MSVC rejects.
1760 static constexpr bool ConjLhs = LhsBlasTraits::NeedToConjugate;
1761 // conjugateIf<false> returns a reference to its argument, which dangles when extract returns by value.
1762 using ActualLhsMatrixXpr =
1763 decltype(LhsBlasTraits::extract(std::declval<const typename Lhs::MatrixType&>()).template conjugateIf<ConjLhs>());
1764 using ActualLhsMatrix = std::conditional_t<std::is_reference<typename LhsBlasTraits::ExtractType>::value,
1765 ActualLhsMatrixXpr, remove_all_t<ActualLhsMatrixXpr>>;
1766 using ActualLhsMatrixType = remove_all_t<ActualLhsMatrix>;
1767 using Kernel =
1768 selfadjoint_diagonal_product_impl<Lhs::Mode, OnTheRight, ActualLhsMatrixType, typename Rhs::DiagonalVectorType>;
1769
1770 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ActualLhsMatrix actualLhsMatrix(const typename Lhs::MatrixType& matrix) {
1771 return LhsBlasTraits::extract(matrix).template conjugateIf<ConjLhs>();
1772 }
1773
1774 template <typename Dest>
1775 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
1776 EIGEN_IF_CONSTEXPR (LhsBlasTraits::HasScalarFactor) {
1777 // Folded scalar factor present: zero dst then accumulate at the extracted alpha.
1778 Scalar factor = LhsBlasTraits::extractScalarFactor(lhs.nestedExpression());
1779 dst.setZero();
1780 Kernel::run(dst, actualLhsMatrix(lhs.nestedExpression()), rhs.diagonal(), factor);
1781 } else {
1782 // No scalar factor: kernel writes every entry exactly once, skip setZero.
1783 Kernel::runOverwrite(dst, actualLhsMatrix(lhs.nestedExpression()), rhs.diagonal());
1784 }
1785 }
1786
1787 template <typename Dest>
1788 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs,
1789 const Scalar& alpha) {
1790 Scalar combinedAlpha = alpha * LhsBlasTraits::extractScalarFactor(lhs.nestedExpression());
1791 Kernel::run(dst, actualLhsMatrix(lhs.nestedExpression()), rhs.diagonal(), combinedAlpha);
1792 }
1793};
1794
1795template <typename Lhs, typename Rhs, int ProductTag>
1796struct generic_product_impl<Lhs, Rhs, DiagonalShape, SelfAdjointShape, ProductTag>
1797 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, DiagonalShape, SelfAdjointShape, ProductTag>> {
1798 using Scalar = typename Product<Lhs, Rhs>::Scalar;
1799 // See note on the SelfAdjointShape, DiagonalShape specialization above for why
1800 // we extract the scalar factor with blas_traits.
1801 using RhsBlasTraits = blas_traits<typename Rhs::MatrixType>;
1802 static constexpr bool ConjRhs = RhsBlasTraits::NeedToConjugate;
1803 // conjugateIf<false> returns a reference to its argument, which dangles when extract returns by value.
1804 using ActualRhsMatrixXpr =
1805 decltype(RhsBlasTraits::extract(std::declval<const typename Rhs::MatrixType&>()).template conjugateIf<ConjRhs>());
1806 using ActualRhsMatrix = std::conditional_t<std::is_reference<typename RhsBlasTraits::ExtractType>::value,
1807 ActualRhsMatrixXpr, remove_all_t<ActualRhsMatrixXpr>>;
1808 using ActualRhsMatrixType = remove_all_t<ActualRhsMatrix>;
1809 using Kernel =
1810 selfadjoint_diagonal_product_impl<Rhs::Mode, OnTheLeft, ActualRhsMatrixType, typename Lhs::DiagonalVectorType>;
1811
1812 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ActualRhsMatrix actualRhsMatrix(const typename Rhs::MatrixType& matrix) {
1813 return RhsBlasTraits::extract(matrix).template conjugateIf<ConjRhs>();
1814 }
1815
1816 template <typename Dest>
1817 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
1818 EIGEN_IF_CONSTEXPR (RhsBlasTraits::HasScalarFactor) {
1819 Scalar factor = RhsBlasTraits::extractScalarFactor(rhs.nestedExpression());
1820 dst.setZero();
1821 Kernel::run(dst, actualRhsMatrix(rhs.nestedExpression()), lhs.diagonal(), factor);
1822 } else {
1823 Kernel::runOverwrite(dst, actualRhsMatrix(rhs.nestedExpression()), lhs.diagonal());
1824 }
1825 }
1826
1827 template <typename Dest>
1828 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs,
1829 const Scalar& alpha) {
1830 Scalar combinedAlpha = alpha * RhsBlasTraits::extractScalarFactor(rhs.nestedExpression());
1831 Kernel::run(dst, actualRhsMatrix(rhs.nestedExpression()), lhs.diagonal(), combinedAlpha);
1832 }
1833};
1834
1835/***************************************************************************
1836 * Products of two triangular or self-adjoint views
1837 ***************************************************************************/
1838
1845template <typename View, typename Shape = typename evaluator_traits<View>::Shape>
1846struct densified_view_operand {
1847 using Scalar = typename View::Scalar;
1848 template <typename Dense>
1849 static Scalar run(Dense& dense, const View& view) {
1850 dense = view;
1851 return Scalar(1);
1852 }
1853};
1854
1855template <typename View>
1856struct densified_view_operand<View, SelfAdjointShape> {
1857 using Scalar = typename View::Scalar;
1858 using BlasTraits = blas_traits<typename View::MatrixType>;
1859 static constexpr unsigned int UpLo = unsigned(int(View::Mode) & int(Upper | Lower));
1860 template <typename Dense>
1861 static Scalar run(Dense& dense, const View& view) {
1862 dense = BlasTraits::extract(view.nestedExpression())
1863 .template conjugateIf<bool(BlasTraits::NeedToConjugate)>()
1864 .template selfadjointView<UpLo>();
1865 return BlasTraits::extractScalarFactor(view.nestedExpression());
1866 }
1867};
1868
1876template <typename Lhs, typename Rhs, typename LhsShape, typename RhsShape, int ProductTag, bool DensifyRhs>
1877struct structured_view_product_impl;
1878
1879template <typename Lhs, typename Rhs, typename LhsShape, typename RhsShape, int ProductTag>
1880struct structured_view_product_impl<Lhs, Rhs, LhsShape, RhsShape, ProductTag, true>
1881 : generic_product_impl_base<Lhs, Rhs,
1882 structured_view_product_impl<Lhs, Rhs, LhsShape, RhsShape, ProductTag, true>> {
1883 using Scalar = typename Product<Lhs, Rhs>::Scalar;
1884 using RhsDenseType = typename Rhs::DenseMatrixType;
1885
1886 template <typename Dest>
1887 static void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
1888 RhsDenseType rhsDense(rhs.rows(), rhs.cols());
1889 const Scalar factor = densified_view_operand<Rhs>::run(rhsDense, rhs);
1890 generic_product_impl<Lhs, RhsDenseType, LhsShape, DenseShape, ProductTag>::scaleAndAddTo(dst, lhs, rhsDense,
1891 alpha * factor);
1892 }
1893};
1894
1895template <typename Lhs, typename Rhs, typename LhsShape, typename RhsShape, int ProductTag>
1896struct structured_view_product_impl<Lhs, Rhs, LhsShape, RhsShape, ProductTag, false>
1897 : generic_product_impl_base<Lhs, Rhs,
1898 structured_view_product_impl<Lhs, Rhs, LhsShape, RhsShape, ProductTag, false>> {
1899 using Scalar = typename Product<Lhs, Rhs>::Scalar;
1900 using LhsDenseType = typename Lhs::DenseMatrixType;
1901
1902 template <typename Dest>
1903 static void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
1904 LhsDenseType lhsDense(lhs.rows(), lhs.cols());
1905 const Scalar factor = densified_view_operand<Lhs>::run(lhsDense, lhs);
1906 generic_product_impl<LhsDenseType, Rhs, DenseShape, RhsShape, ProductTag>::scaleAndAddTo(dst, lhsDense, rhs,
1907 alpha * factor);
1908 }
1909};
1910
1911template <typename Lhs, typename Rhs, int ProductTag>
1912struct generic_product_impl<Lhs, Rhs, TriangularShape, TriangularShape, ProductTag>
1913 : structured_view_product_impl<Lhs, Rhs, TriangularShape, TriangularShape, ProductTag, true> {};
1914
1915template <typename Lhs, typename Rhs, int ProductTag>
1916struct generic_product_impl<Lhs, Rhs, TriangularShape, SelfAdjointShape, ProductTag>
1917 : structured_view_product_impl<Lhs, Rhs, TriangularShape, SelfAdjointShape, ProductTag, true> {};
1918
1919template <typename Lhs, typename Rhs, int ProductTag>
1920struct generic_product_impl<Lhs, Rhs, SelfAdjointShape, TriangularShape, ProductTag>
1921 : structured_view_product_impl<Lhs, Rhs, SelfAdjointShape, TriangularShape, ProductTag, false> {};
1922
1923template <typename Lhs, typename Rhs, int ProductTag>
1924struct generic_product_impl<Lhs, Rhs, SelfAdjointShape, SelfAdjointShape, ProductTag>
1925 : structured_view_product_impl<Lhs, Rhs, SelfAdjointShape, SelfAdjointShape, ProductTag, true> {};
1926
1927/***************************************************************************
1928 * Diagonal products
1929 ***************************************************************************/
1930
1931template <typename MatrixType, typename DiagonalType, typename Derived, int ProductOrder>
1932struct diagonal_product_evaluator_base : evaluator_base<Derived> {
1933 using Scalar = typename Derived::Scalar;
1934
1935 public:
1936 enum {
1937 CoeffReadCost = int(NumTraits<Scalar>::MulCost) + int(evaluator<MatrixType>::CoeffReadCost) +
1938 int(evaluator<DiagonalType>::CoeffReadCost),
1939
1940 MatrixFlags = evaluator<MatrixType>::Flags,
1941 DiagFlags = evaluator<DiagonalType>::Flags,
1942
1943 StorageOrder_ = (Derived::MaxRowsAtCompileTime == 1 && Derived::MaxColsAtCompileTime != 1) ? RowMajor
1944 : (Derived::MaxColsAtCompileTime == 1 && Derived::MaxRowsAtCompileTime != 1) ? ColMajor
1945 : MatrixFlags & RowMajorBit ? RowMajor
1946 : ColMajor,
1947 SameStorageOrder_ = int(StorageOrder_) == ((MatrixFlags & RowMajorBit) ? RowMajor : ColMajor),
1948
1949 ScalarAccessOnDiag_ = !((int(StorageOrder_) == ColMajor && int(ProductOrder) == OnTheLeft) ||
1950 (int(StorageOrder_) == RowMajor && int(ProductOrder) == OnTheRight)),
1951 SameTypes_ = std::is_same<typename MatrixType::Scalar, typename DiagonalType::Scalar>::value,
1952 // FIXME currently we need same types, but in the future the next rule should be the one
1953 // Vectorizable_ = bool(int(MatrixFlags)&PacketAccessBit) && ((!_PacketOnDiag) || (SameTypes_ &&
1954 // bool(int(DiagFlags)&PacketAccessBit))),
1955 Vectorizable_ = bool(int(MatrixFlags) & PacketAccessBit) && SameTypes_ &&
1956 (SameStorageOrder_ || (MatrixFlags & LinearAccessBit) == LinearAccessBit) &&
1957 (ScalarAccessOnDiag_ || (bool(int(DiagFlags) & PacketAccessBit))),
1958 LinearAccessMask_ =
1959 (MatrixType::RowsAtCompileTime == 1 || MatrixType::ColsAtCompileTime == 1) ? LinearAccessBit : 0,
1960 Flags = ((HereditaryBits | LinearAccessMask_) & static_cast<unsigned int>(MatrixFlags)) |
1961 (Vectorizable_ ? PacketAccessBit : 0),
1962 Alignment = evaluator<MatrixType>::Alignment,
1963
1964 AsScalarProduct =
1965 (DiagonalType::SizeAtCompileTime == 1) ||
1966 (DiagonalType::SizeAtCompileTime == Dynamic && MatrixType::RowsAtCompileTime == 1 &&
1967 ProductOrder == OnTheLeft) ||
1968 (DiagonalType::SizeAtCompileTime == Dynamic && MatrixType::ColsAtCompileTime == 1 && ProductOrder == OnTheRight)
1969 };
1970
1971 EIGEN_DEVICE_FUNC diagonal_product_evaluator_base(const MatrixType& mat, const DiagonalType& diag)
1972 : m_diagImpl(diag), m_matImpl(mat) {
1973 EIGEN_INTERNAL_CHECK_COST_VALUE(NumTraits<Scalar>::MulCost);
1974 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
1975 }
1976
1977 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const Scalar coeff(Index idx) const {
1978 using DiagonalFirst = bool_constant<
1979 ProductOrder == OnTheLeft ||
1980 functor_is_commutative<scalar_product_op<typename MatrixType::Scalar, typename DiagonalType::Scalar>>::value>;
1981 return coeff_impl(idx, DiagonalFirst());
1982 }
1983
1984 protected:
1985 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar coeff_impl(Index idx, std::true_type) const {
1986 EIGEN_IF_CONSTEXPR (AsScalarProduct)
1987 return m_diagImpl.coeff(0) * m_matImpl.coeff(idx);
1988 else
1989 return m_diagImpl.coeff(idx) * m_matImpl.coeff(idx);
1990 }
1991
1992 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar coeff_impl(Index idx, std::false_type) const {
1993 EIGEN_IF_CONSTEXPR (AsScalarProduct)
1994 return m_matImpl.coeff(idx) * m_diagImpl.coeff(0);
1995 else
1996 return m_matImpl.coeff(idx) * m_diagImpl.coeff(idx);
1997 }
1998
1999 template <int LoadMode, typename PacketType>
2000 EIGEN_STRONG_INLINE PacketType packet_impl(Index row, Index col, Index id, std::true_type) const {
2001 return internal::pmul(m_matImpl.template packet<LoadMode, PacketType>(row, col),
2002 internal::pset1<PacketType>(m_diagImpl.coeff(id)));
2003 }
2004
2005 template <int LoadMode, typename PacketType>
2006 EIGEN_STRONG_INLINE PacketType packet_impl(Index row, Index col, Index id, std::false_type) const {
2007 enum { DiagonalPacketLoadMode = plain_enum_min(LoadMode, int(evaluator<DiagonalType>::Alignment)) };
2008 return internal::pmul(m_matImpl.template packet<LoadMode, PacketType>(row, col),
2009 m_diagImpl.template packet<DiagonalPacketLoadMode, PacketType>(id));
2010 }
2011
2012 template <int LoadMode, typename PacketType>
2013 EIGEN_STRONG_INLINE PacketType packet_segment_impl(Index row, Index col, Index id, Index begin, Index count,
2014 std::true_type) const {
2015 return internal::pmul(m_matImpl.template packetSegment<LoadMode, PacketType>(row, col, begin, count),
2016 internal::pset1<PacketType>(m_diagImpl.coeff(id)));
2017 }
2018
2019 template <int LoadMode, typename PacketType>
2020 EIGEN_STRONG_INLINE PacketType packet_segment_impl(Index row, Index col, Index id, Index begin, Index count,
2021 std::false_type) const {
2022 enum { DiagonalPacketLoadMode = plain_enum_min(LoadMode, int(evaluator<DiagonalType>::Alignment)) };
2023 return internal::pmul(m_matImpl.template packetSegment<LoadMode, PacketType>(row, col, begin, count),
2024 m_diagImpl.template packetSegment<DiagonalPacketLoadMode, PacketType>(id, begin, count));
2025 }
2026
2027 evaluator<DiagonalType> m_diagImpl;
2028 evaluator<MatrixType> m_matImpl;
2029};
2030
2031// diagonal * dense
2032template <typename Lhs, typename Rhs, int ProductKind, int ProductTag>
2033struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, DiagonalShape, DenseShape>
2034 : diagonal_product_evaluator_base<Rhs, typename Lhs::DiagonalVectorType, Product<Lhs, Rhs, LazyProduct>,
2035 OnTheLeft> {
2036 using Base =
2037 diagonal_product_evaluator_base<Rhs, typename Lhs::DiagonalVectorType, Product<Lhs, Rhs, LazyProduct>, OnTheLeft>;
2038 using Base::coeff;
2039 using Base::m_diagImpl;
2040 using Base::m_matImpl;
2041 using Scalar = typename Base::Scalar;
2042
2043 using XprType = Product<Lhs, Rhs, ProductKind>;
2044 using PlainObject = typename XprType::PlainObject;
2045
2046 static constexpr int StorageOrder = Base::StorageOrder_;
2047 using IsRowMajor_t = bool_constant<StorageOrder == RowMajor>;
2048
2049 EIGEN_DEVICE_FUNC constexpr explicit product_evaluator(const XprType& xpr) : Base(xpr.rhs(), xpr.lhs().diagonal()) {}
2050
2051 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const Scalar coeff(Index row, Index col) const {
2052 return m_diagImpl.coeff(row) * m_matImpl.coeff(row, col);
2053 }
2054
2055#ifndef EIGEN_GPUCC
2056 template <int LoadMode, typename PacketType>
2057 EIGEN_STRONG_INLINE PacketType packet(Index row, Index col) const {
2058 // FIXME: NVCC used to complain about the template keyword, but we have to check whether this is still the case.
2059 // See also similar calls below.
2060 return this->template packet_impl<LoadMode, PacketType>(row, col, row, IsRowMajor_t());
2061 }
2062
2063 template <int LoadMode, typename PacketType>
2064 EIGEN_STRONG_INLINE PacketType packet(Index idx) const {
2065 return packet<LoadMode, PacketType>(int(StorageOrder) == ColMajor ? idx : 0,
2066 int(StorageOrder) == ColMajor ? 0 : idx);
2067 }
2068
2069 template <int LoadMode, typename PacketType>
2070 EIGEN_STRONG_INLINE PacketType packetSegment(Index row, Index col, Index begin, Index count) const {
2071 // FIXME: NVCC used to complain about the template keyword, but we have to check whether this is still the case.
2072 // See also similar calls below.
2073 return this->template packet_segment_impl<LoadMode, PacketType>(row, col, row, begin, count, IsRowMajor_t());
2074 }
2075
2076 template <int LoadMode, typename PacketType>
2077 EIGEN_STRONG_INLINE PacketType packetSegment(Index idx, Index begin, Index count) const {
2078 return packetSegment<LoadMode, PacketType>(StorageOrder == ColMajor ? idx : 0, StorageOrder == ColMajor ? 0 : idx,
2079 begin, count);
2080 }
2081#endif
2082};
2083
2084// dense * diagonal
2085template <typename Lhs, typename Rhs, int ProductKind, int ProductTag>
2086struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, DenseShape, DiagonalShape>
2087 : diagonal_product_evaluator_base<Lhs, typename Rhs::DiagonalVectorType, Product<Lhs, Rhs, LazyProduct>,
2088 OnTheRight> {
2089 using Base = diagonal_product_evaluator_base<Lhs, typename Rhs::DiagonalVectorType, Product<Lhs, Rhs, LazyProduct>,
2090 OnTheRight>;
2091 using Base::coeff;
2092 using Base::m_diagImpl;
2093 using Base::m_matImpl;
2094 using Scalar = typename Base::Scalar;
2095
2096 using XprType = Product<Lhs, Rhs, ProductKind>;
2097 using PlainObject = typename XprType::PlainObject;
2098
2099 static constexpr int StorageOrder = Base::StorageOrder_;
2100 using IsColMajor_t = bool_constant<StorageOrder == ColMajor>;
2101
2102 EIGEN_DEVICE_FUNC constexpr explicit product_evaluator(const XprType& xpr) : Base(xpr.lhs(), xpr.rhs().diagonal()) {}
2103
2104 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const Scalar coeff(Index row, Index col) const {
2105 return m_matImpl.coeff(row, col) * m_diagImpl.coeff(col);
2106 }
2107
2108#ifndef EIGEN_GPUCC
2109 template <int LoadMode, typename PacketType>
2110 EIGEN_STRONG_INLINE PacketType packet(Index row, Index col) const {
2111 return this->template packet_impl<LoadMode, PacketType>(row, col, col, IsColMajor_t());
2112 }
2113
2114 template <int LoadMode, typename PacketType>
2115 EIGEN_STRONG_INLINE PacketType packet(Index idx) const {
2116 return packet<LoadMode, PacketType>(StorageOrder == ColMajor ? idx : 0, StorageOrder == ColMajor ? 0 : idx);
2117 }
2118
2119 template <int LoadMode, typename PacketType>
2120 EIGEN_STRONG_INLINE PacketType packetSegment(Index row, Index col, Index begin, Index count) const {
2121 return this->template packet_segment_impl<LoadMode, PacketType>(row, col, col, begin, count, IsColMajor_t());
2122 }
2123
2124 template <int LoadMode, typename PacketType>
2125 EIGEN_STRONG_INLINE PacketType packetSegment(Index idx, Index begin, Index count) const {
2126 return packetSegment<LoadMode, PacketType>(StorageOrder == ColMajor ? idx : 0, StorageOrder == ColMajor ? 0 : idx,
2127 begin, count);
2128 }
2129#endif
2130};
2131
2132template <typename MatrixScalar, typename DiagonalScalar>
2133EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE auto diagonal_product_coeff(const MatrixScalar& matrix,
2134 const DiagonalScalar& diagonal, true_type)
2135 -> decltype(diagonal * matrix) {
2136 return diagonal * matrix;
2137}
2138
2139template <typename MatrixScalar, typename DiagonalScalar>
2140EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE auto diagonal_product_coeff(const MatrixScalar& matrix,
2141 const DiagonalScalar& diagonal, false_type)
2142 -> decltype(matrix * diagonal) {
2143 return matrix * diagonal;
2144}
2145
2146// Lazy evaluators for triangular/selfadjoint × diagonal products.
2147//
2148// The triangular-assignment dispatcher in TriangularMatrix.h routes
2149// `triangularView() ?= structured * diagonal` through call_triangular_assignment_loop,
2150// which constructs an evaluator over the Product expression. Without these
2151// specializations, evaluator<Product<TriangularView, Diagonal>> falls back to the
2152// default product_evaluator that materializes a full PlainObject — defeating the
2153// triangular-aware dispatcher. The kernel reads only the destination's active
2154// triangle one coefficient at a time, so a coeff()-only evaluator avoids the
2155// temporary at no loss in functionality.
2156
2157template <int Mode, int ProductOrder, typename MatrixType, typename DiagonalType, typename ProductXpr>
2158struct triangular_diagonal_product_lazy_evaluator_base : evaluator_base<ProductXpr> {
2159 using Scalar = typename ProductXpr::Scalar;
2160 using DiagonalOnLeft = bool_constant<ProductOrder == OnTheLeft>;
2161 using MatrixScalar = typename MatrixType::Scalar;
2162
2163 enum {
2164 CoeffReadCost = int(NumTraits<Scalar>::MulCost) + int(evaluator<MatrixType>::CoeffReadCost) +
2165 int(evaluator<DiagonalType>::CoeffReadCost),
2166 Flags = HereditaryBits & static_cast<unsigned int>(evaluator<MatrixType>::Flags),
2167 Alignment = 0
2168 };
2169
2170 EIGEN_DEVICE_FUNC triangular_diagonal_product_lazy_evaluator_base(const MatrixType& mat, const DiagonalType& diag)
2171 : m_diagImpl(diag), m_matImpl(mat) {}
2172
2173 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index row, Index col) const {
2174 const bool inActive = ((Mode & Upper) == Upper) ? (row <= col) : (row >= col);
2175 if (!inActive) return Scalar(0);
2176 if (row == col) {
2177 EIGEN_IF_CONSTEXPR ((Mode & UnitDiag) == UnitDiag) {
2178 return Scalar(diagonal_product_coeff(MatrixScalar(1), m_diagImpl.coeff(row), DiagonalOnLeft()));
2179 }
2180 EIGEN_IF_CONSTEXPR ((Mode & ZeroDiag) == ZeroDiag) return Scalar(0);
2181 }
2182 return Scalar(diagonal_product_coeff(m_matImpl.coeff(row, col),
2183 m_diagImpl.coeff(ProductOrder == OnTheLeft ? row : col), DiagonalOnLeft()));
2184 }
2185
2186 protected:
2187 evaluator<DiagonalType> m_diagImpl;
2188 evaluator<MatrixType> m_matImpl;
2189};
2190
2191// Triangular × Diagonal
2192template <typename Lhs, typename Rhs, int ProductKind, int ProductTag>
2193struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, TriangularShape, DiagonalShape>
2194 : triangular_diagonal_product_lazy_evaluator_base<Lhs::Mode, OnTheRight, typename Lhs::MatrixType,
2195 typename Rhs::DiagonalVectorType,
2196 Product<Lhs, Rhs, ProductKind>> {
2197 using XprType = Product<Lhs, Rhs, ProductKind>;
2198 using Base = triangular_diagonal_product_lazy_evaluator_base<Lhs::Mode, OnTheRight, typename Lhs::MatrixType,
2199 typename Rhs::DiagonalVectorType, XprType>;
2200
2201 EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
2202 : Base(xpr.lhs().nestedExpression(), xpr.rhs().diagonal()) {}
2203};
2204
2205// Diagonal × Triangular
2206template <typename Lhs, typename Rhs, int ProductKind, int ProductTag>
2207struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, DiagonalShape, TriangularShape>
2208 : triangular_diagonal_product_lazy_evaluator_base<Rhs::Mode, OnTheLeft, typename Rhs::MatrixType,
2209 typename Lhs::DiagonalVectorType,
2210 Product<Lhs, Rhs, ProductKind>> {
2211 using XprType = Product<Lhs, Rhs, ProductKind>;
2212 using Base = triangular_diagonal_product_lazy_evaluator_base<Rhs::Mode, OnTheLeft, typename Rhs::MatrixType,
2213 typename Lhs::DiagonalVectorType, XprType>;
2214
2215 EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
2216 : Base(xpr.rhs().nestedExpression(), xpr.lhs().diagonal()) {}
2217};
2218
2219// Dense SelfAdjointView statically rejects the Upper|Lower mode (only one half is stored), so the
2220// off-stored coefficient is always reconstructed by conjugating its mirror.
2221template <int Mode, int ProductOrder, typename MatrixType, typename DiagonalType, typename ProductXpr,
2222 bool Materialize =
2223 NumTraits<typename MatrixType::Scalar>::IsComplex && blas_traits<MatrixType>::HasScalarFactor>
2224struct selfadjoint_diagonal_product_lazy_evaluator_base : evaluator_base<ProductXpr> {
2225 using Scalar = typename ProductXpr::Scalar;
2226 using MatrixScalar = typename MatrixType::Scalar;
2227 using DiagonalOnLeft = bool_constant<ProductOrder == OnTheLeft>;
2228
2229 enum {
2230 CoeffReadCost = int(NumTraits<Scalar>::MulCost) + int(evaluator<MatrixType>::CoeffReadCost) +
2231 int(evaluator<DiagonalType>::CoeffReadCost),
2232 Flags = HereditaryBits & static_cast<unsigned int>(evaluator<MatrixType>::Flags),
2233 Alignment = 0
2234 };
2235
2236 EIGEN_DEVICE_FUNC selfadjoint_diagonal_product_lazy_evaluator_base(const ProductXpr&, const MatrixType& mat,
2237 const DiagonalType& diag)
2238 : m_diagImpl(diag), m_matImpl(mat) {}
2239
2240 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index row, Index col) const {
2241 const bool storedHere = ((Mode & Upper) == Upper) ? (row <= col) : (row >= col);
2242 const MatrixScalar matCoeff =
2243 storedHere ? MatrixScalar(m_matImpl.coeff(row, col)) : MatrixScalar(numext::conj(m_matImpl.coeff(col, row)));
2244 return Scalar(
2245 diagonal_product_coeff(matCoeff, m_diagImpl.coeff(ProductOrder == OnTheLeft ? row : col), DiagonalOnLeft()));
2246 }
2247
2248 protected:
2249 evaluator<DiagonalType> m_diagImpl;
2250 evaluator<MatrixType> m_matImpl;
2251};
2252
2253// Extract complex factors before conjugating mirrored entries; the temporary also protects nested aliases.
2254// The parameter is not named XprType: MSVC lets the dependent base's XprType (an EvalToTemp) hide it.
2255template <int Mode, int ProductOrder, typename MatrixType, typename DiagonalType, typename ProductXpr>
2256struct selfadjoint_diagonal_product_lazy_evaluator_base<Mode, ProductOrder, MatrixType, DiagonalType, ProductXpr, true>
2257 : evaluator<EvalToTemp<Product<typename ProductXpr::Lhs, typename ProductXpr::Rhs, DefaultProduct>>> {
2258 using DefaultProductType = Product<typename ProductXpr::Lhs, typename ProductXpr::Rhs, DefaultProduct>;
2259 using Base = evaluator<EvalToTemp<DefaultProductType>>;
2260 EIGEN_DEVICE_FUNC selfadjoint_diagonal_product_lazy_evaluator_base(const ProductXpr& xpr, const MatrixType&,
2261 const DiagonalType&)
2262 : Base(DefaultProductType(xpr.lhs(), xpr.rhs())) {}
2263};
2264
2265// SelfAdjoint × Diagonal
2266template <typename Lhs, typename Rhs, int ProductKind, int ProductTag>
2267struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, SelfAdjointShape, DiagonalShape>
2268 : selfadjoint_diagonal_product_lazy_evaluator_base<Lhs::Mode, OnTheRight, typename Lhs::MatrixType,
2269 typename Rhs::DiagonalVectorType,
2270 Product<Lhs, Rhs, ProductKind>> {
2271 using XprType = Product<Lhs, Rhs, ProductKind>;
2272 using Base = selfadjoint_diagonal_product_lazy_evaluator_base<Lhs::Mode, OnTheRight, typename Lhs::MatrixType,
2273 typename Rhs::DiagonalVectorType, XprType>;
2274
2275 EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
2276 : Base(xpr, xpr.lhs().nestedExpression(), xpr.rhs().diagonal()) {}
2277};
2278
2279// Diagonal × SelfAdjoint
2280template <typename Lhs, typename Rhs, int ProductKind, int ProductTag>
2281struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, DiagonalShape, SelfAdjointShape>
2282 : selfadjoint_diagonal_product_lazy_evaluator_base<Rhs::Mode, OnTheLeft, typename Rhs::MatrixType,
2283 typename Lhs::DiagonalVectorType,
2284 Product<Lhs, Rhs, ProductKind>> {
2285 using XprType = Product<Lhs, Rhs, ProductKind>;
2286 using Base = selfadjoint_diagonal_product_lazy_evaluator_base<Rhs::Mode, OnTheLeft, typename Rhs::MatrixType,
2287 typename Lhs::DiagonalVectorType, XprType>;
2288
2289 EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
2290 : Base(xpr, xpr.rhs().nestedExpression(), xpr.lhs().diagonal()) {}
2291};
2292
2293// Fallback for indexed products whose operand shape has no direct accumulator.
2294template <typename Lhs, typename Rhs, typename Derived>
2295struct product_accumulate_through_temporary {
2296 template <typename Dest>
2297 static EIGEN_DEVICE_FUNC void addTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
2298 typename Product<Lhs, Rhs>::PlainObject tmp(lhs.rows(), rhs.cols());
2299 Derived::evalTo(tmp, lhs, rhs);
2300 dst += tmp;
2301 }
2302
2303 template <typename Dest>
2304 static EIGEN_DEVICE_FUNC void subTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
2305 typename Product<Lhs, Rhs>::PlainObject tmp(lhs.rows(), rhs.cols());
2306 Derived::evalTo(tmp, lhs, rhs);
2307 dst -= tmp;
2308 }
2309};
2310
2311template <typename Lhs, typename Rhs, typename Derived, typename MatrixShape>
2312struct indexed_product_accumulation : product_accumulate_through_temporary<Lhs, Rhs, Derived> {};
2313
2314template <typename Lhs, typename Rhs, typename Derived>
2315struct indexed_product_accumulation<Lhs, Rhs, Derived, DenseShape> {
2316 template <typename Dest>
2317 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void addTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
2318 Derived::accumulate(dst, lhs, rhs, add_assign_op<typename Dest::Scalar, typename Product<Lhs, Rhs>::Scalar>());
2319 }
2320
2321 template <typename Dest>
2322 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void subTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
2323 Derived::accumulate(dst, lhs, rhs, sub_assign_op<typename Dest::Scalar, typename Product<Lhs, Rhs>::Scalar>());
2324 }
2325};
2326
2327template <typename Lhs, typename Rhs, typename Derived>
2328struct indexed_product_accumulation<Lhs, Rhs, Derived, TriangularShape>
2329 : indexed_product_accumulation<Lhs, Rhs, Derived, DenseShape> {};
2330
2331template <typename Lhs, typename Rhs, typename Derived>
2332struct indexed_product_accumulation<Lhs, Rhs, Derived, SelfAdjointShape>
2333 : indexed_product_accumulation<Lhs, Rhs, Derived, DenseShape> {};
2334
2335template <int Side, bool Transposed, typename Dest, typename Indices, typename Expression, typename Func>
2336EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void indexed_matrix_update(Dest& dst, const Indices& indices,
2337 const Expression& xpr, const Func& func) {
2338 using MatrixType = typename nested_eval<Expression, 1>::type;
2339 using MatrixTypeCleaned = remove_all_t<MatrixType>;
2340 MatrixType mat(xpr);
2341 // An inner-dimension permutation reuses its index expression for every outer vector.
2342 typename nested_eval<Indices, Dynamic>::type actualIndices(indices);
2343 const Index n = Side == OnTheLeft ? mat.rows() : mat.cols();
2344 EIGEN_IF_CONSTEXPR (bool(Dest::IsRowMajor) == bool(MatrixTypeCleaned::IsRowMajor) &&
2345 bool(Dest::IsRowMajor) == (Side == OnTheRight)) {
2346 for (Index j = 0; j < (Side == OnTheLeft ? mat.cols() : mat.rows()); ++j) {
2347 auto target = dst.template subVector < Side == OnTheLeft ? Vertical : Horizontal > (j);
2348 const auto source = mat.template subVector < Side == OnTheLeft ? Vertical : Horizontal > (j);
2349 for (Index i = 0; i < n; ++i) {
2350 const Index p = actualIndices.coeff(i);
2351 func.assignCoeff(target.coeffRef(((Side == OnTheLeft) ^ Transposed) ? p : i),
2352 source.coeff(((Side == OnTheRight) ^ Transposed) ? p : i));
2353 }
2354 }
2355 } else {
2356 for (Index i = 0; i < n; ++i) {
2357 const Index p = actualIndices.coeff(i);
2358 auto target = dst.template subVector < Side == OnTheLeft
2359 ? Horizontal
2360 : Vertical > (((Side == OnTheLeft) ^ Transposed) ? p : i);
2361 const auto source = mat.template subVector < Side == OnTheLeft
2362 ? Horizontal
2363 : Vertical > (((Side == OnTheRight) ^ Transposed) ? p : i);
2364 call_assignment_no_alias(target, source, func);
2365 }
2366 }
2367}
2368
2369/***************************************************************************
2370 * Products with permutation matrices
2371 ***************************************************************************/
2372
2381template <typename ExpressionType, int Side, bool Transposed, typename ExpressionShape>
2382struct permutation_matrix_product {
2383 template <typename Dest, typename PermutationType>
2384 static EIGEN_DEVICE_FUNC void run(Dest&, const PermutationType&, const ExpressionType&) {
2385 static_assert(std::is_same<ExpressionShape, DenseShape>::value,
2386 "PERMUTATION_PRODUCTS_ARE_NOT_IMPLEMENTED_FOR_THIS_OPERAND_SHAPE");
2387 }
2388};
2389
2390template <typename ExpressionType, int Side, bool Transposed>
2391struct permutation_matrix_product<ExpressionType, Side, Transposed, DenseShape> {
2392 using MatrixType = typename nested_eval<ExpressionType, 1>::type;
2393 using MatrixTypeCleaned = remove_all_t<MatrixType>;
2394
2395 template <typename Dest, typename Indices, typename Func>
2396 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void accumulate(Dest& dst, const Indices& indices,
2397 const ExpressionType& xpr, const Func& func) {
2398 indexed_matrix_update<Side, Transposed>(dst, indices, xpr, func);
2399 }
2400
2401 template <typename Dest, typename PermutationType>
2402 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Dest& dst, const PermutationType& perm,
2403 const ExpressionType& xpr) {
2404 MatrixType mat(xpr);
2405 const Index n = Side == OnTheLeft ? mat.rows() : mat.cols();
2406 // FIXME we need an is_same for expression that is not sensitive to constness. For instance
2407 // is_same_xpr<Block<const Matrix>, Block<Matrix> >::value should be true.
2408 // if(std::is_same<MatrixTypeCleaned,Dest>::value && extract_data(dst) == extract_data(mat))
2409 if (is_same_dense(dst, mat)) {
2410 // apply the permutation inplace
2411 Matrix<bool, PermutationType::RowsAtCompileTime, 1, 0, PermutationType::MaxRowsAtCompileTime> mask(perm.size());
2412 mask.fill(false);
2413 Index r = 0;
2414 while (r < perm.size()) {
2415 // search for the next seed
2416 while (r < perm.size() && mask[r]) r++;
2417 if (r >= perm.size()) break;
2418 // we got one, let's follow it until we are back to the seed
2419 Index k0 = r++;
2420 Index kPrev = k0;
2421 mask.coeffRef(k0) = true;
2422 for (Index k = perm.indices().coeff(k0); k != k0; k = perm.indices().coeff(k)) {
2423 Block<Dest, Side == OnTheLeft ? 1 : Dest::RowsAtCompileTime,
2424 Side == OnTheRight ? 1 : Dest::ColsAtCompileTime>(dst, k)
2425 .swap(Block < Dest, Side == OnTheLeft ? 1 : Dest::RowsAtCompileTime,
2426 Side == OnTheRight
2427 ? 1
2428 : Dest::ColsAtCompileTime > (dst, ((Side == OnTheLeft) ^ Transposed) ? k0 : kPrev));
2429
2430 mask.coeffRef(k) = true;
2431 kPrev = k;
2432 }
2433 }
2434 } else {
2435 for (Index i = 0; i < n; ++i) {
2436 Block<Dest, Side == OnTheLeft ? 1 : Dest::RowsAtCompileTime, Side == OnTheRight ? 1 : Dest::ColsAtCompileTime>(
2437 dst, ((Side == OnTheLeft) ^ Transposed) ? perm.indices().coeff(i) : i)
2438
2439 =
2440
2441 Block < const MatrixTypeCleaned,
2442 Side == OnTheLeft ? 1 : MatrixTypeCleaned::RowsAtCompileTime,
2443 Side == OnTheRight ? 1
2444 : MatrixTypeCleaned::ColsAtCompileTime >
2445 (mat, ((Side == OnTheRight) ^ Transposed) ? perm.indices().coeff(i) : i);
2446 }
2447 }
2448 }
2449};
2450
2456template <typename ExpressionType, int Side, bool Transposed>
2457struct permutation_densified_matrix_product {
2458 using DenseType = typename ExpressionType::DenseMatrixType;
2459
2460 template <typename Dest, typename Indices, typename Func>
2461 static EIGEN_DEVICE_FUNC void accumulate(Dest& dst, const Indices& indices, const ExpressionType& xpr,
2462 const Func& func) {
2463 const DenseType dense(xpr);
2464 indexed_matrix_update<Side, Transposed>(dst, indices, dense, func);
2465 }
2466
2467 template <typename Dest, typename PermutationType>
2468 static EIGEN_DEVICE_FUNC void run(Dest& dst, const PermutationType& perm, const ExpressionType& xpr) {
2469 const DenseType dense(xpr);
2470 permutation_matrix_product<DenseType, Side, Transposed, DenseShape>::run(dst, perm, dense);
2471 }
2472};
2473
2474template <typename ExpressionType, int Side, bool Transposed>
2475struct permutation_matrix_product<ExpressionType, Side, Transposed, TriangularShape>
2476 : permutation_densified_matrix_product<ExpressionType, Side, Transposed> {};
2477
2478// The scalar factor of a self-adjoint view, folded in from s * (A.selfadjointView() * P), scales the whole view as
2479// in the other self-adjoint products, not only its stored triangle.
2480template <typename ExpressionType, int Side, bool Transposed>
2481struct permutation_matrix_product<ExpressionType, Side, Transposed, SelfAdjointShape> {
2482 using DenseType = typename ExpressionType::DenseMatrixType;
2483
2484 template <typename Dest, typename Indices, typename Func>
2485 static void accumulate(Dest& dst, const Indices& indices, const ExpressionType& xpr, const Func& func) {
2486 DenseType dense(xpr.rows(), xpr.cols());
2487 const typename ExpressionType::Scalar factor = densified_view_operand<ExpressionType>::run(dense, xpr);
2488 EIGEN_IF_CONSTEXPR (blas_traits<typename ExpressionType::MatrixType>::HasScalarFactor) dense *= factor;
2489 indexed_matrix_update<Side, Transposed>(dst, indices, dense, func);
2490 }
2491
2492 template <typename Dest, typename PermutationType>
2493 static void run(Dest& dst, const PermutationType& perm, const ExpressionType& xpr) {
2494 DenseType dense(xpr.rows(), xpr.cols());
2495 const typename ExpressionType::Scalar factor = densified_view_operand<ExpressionType>::run(dense, xpr);
2496 EIGEN_IF_CONSTEXPR (blas_traits<typename ExpressionType::MatrixType>::HasScalarFactor) dense *= factor;
2497 permutation_matrix_product<DenseType, Side, Transposed, DenseShape>::run(dst, perm, dense);
2498 }
2499};
2500
2501template <typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
2502struct generic_product_impl<Lhs, Rhs, PermutationShape, MatrixShape, ProductTag>
2503 : indexed_product_accumulation<Lhs, Rhs, generic_product_impl<Lhs, Rhs, PermutationShape, MatrixShape, ProductTag>,
2504 MatrixShape> {
2505 template <typename Dest, typename Func>
2506 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void accumulate(Dest& dst, const Lhs& lhs, const Rhs& rhs,
2507 const Func& func) {
2508 permutation_matrix_product<Rhs, OnTheLeft, false, MatrixShape>::accumulate(dst, lhs.indices(), rhs, func);
2509 }
2510 template <typename Dest>
2511 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
2512 permutation_matrix_product<Rhs, OnTheLeft, false, MatrixShape>::run(dst, lhs, rhs);
2513 }
2514};
2515
2516template <typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
2517struct generic_product_impl<Lhs, Rhs, MatrixShape, PermutationShape, ProductTag>
2518 : indexed_product_accumulation<Lhs, Rhs, generic_product_impl<Lhs, Rhs, MatrixShape, PermutationShape, ProductTag>,
2519 MatrixShape> {
2520 template <typename Dest, typename Func>
2521 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void accumulate(Dest& dst, const Lhs& lhs, const Rhs& rhs,
2522 const Func& func) {
2523 permutation_matrix_product<Lhs, OnTheRight, false, MatrixShape>::accumulate(dst, rhs.indices(), lhs, func);
2524 }
2525 template <typename Dest>
2526 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
2527 permutation_matrix_product<Lhs, OnTheRight, false, MatrixShape>::run(dst, rhs, lhs);
2528 }
2529};
2530
2531template <typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
2532struct generic_product_impl<Inverse<Lhs>, Rhs, PermutationShape, MatrixShape, ProductTag>
2533 : indexed_product_accumulation<Inverse<Lhs>, Rhs,
2534 generic_product_impl<Inverse<Lhs>, Rhs, PermutationShape, MatrixShape, ProductTag>,
2535 MatrixShape> {
2536 template <typename Dest, typename Func>
2537 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void accumulate(Dest& dst, const Inverse<Lhs>& lhs, const Rhs& rhs,
2538 const Func& func) {
2539 permutation_matrix_product<Rhs, OnTheLeft, true, MatrixShape>::accumulate(dst, lhs.nestedExpression().indices(),
2540 rhs, func);
2541 }
2542 template <typename Dest>
2543 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Inverse<Lhs>& lhs, const Rhs& rhs) {
2544 permutation_matrix_product<Rhs, OnTheLeft, true, MatrixShape>::run(dst, lhs.nestedExpression(), rhs);
2545 }
2546};
2547
2548template <typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
2549struct generic_product_impl<Lhs, Inverse<Rhs>, MatrixShape, PermutationShape, ProductTag>
2550 : indexed_product_accumulation<Lhs, Inverse<Rhs>,
2551 generic_product_impl<Lhs, Inverse<Rhs>, MatrixShape, PermutationShape, ProductTag>,
2552 MatrixShape> {
2553 template <typename Dest, typename Func>
2554 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void accumulate(Dest& dst, const Lhs& lhs, const Inverse<Rhs>& rhs,
2555 const Func& func) {
2556 permutation_matrix_product<Lhs, OnTheRight, true, MatrixShape>::accumulate(dst, rhs.nestedExpression().indices(),
2557 lhs, func);
2558 }
2559 template <typename Dest>
2560 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Lhs& lhs, const Inverse<Rhs>& rhs) {
2561 permutation_matrix_product<Lhs, OnTheRight, true, MatrixShape>::run(dst, rhs.nestedExpression(), lhs);
2562 }
2563};
2564
2565/***************************************************************************
2566 * Products with transpositions matrices
2567 ***************************************************************************/
2568
2569// Overwrite can apply swaps in place; compound updates must preserve the original source.
2570template <typename Lhs, typename Rhs, typename DstScalar, typename SrcScalar>
2571struct assignment_assume_aliasing<Product<Lhs, Rhs, AliasFreeProduct>, add_assign_op<DstScalar, SrcScalar>>
2572 : bool_constant<evaluator_assume_aliasing<Product<Lhs, Rhs, AliasFreeProduct>>::value ||
2573 std::is_same<typename evaluator_traits<Lhs>::Shape, TranspositionsShape>::value ||
2574 std::is_same<typename evaluator_traits<Rhs>::Shape, TranspositionsShape>::value> {};
2575
2576template <typename Lhs, typename Rhs, typename DstScalar, typename SrcScalar>
2577struct assignment_assume_aliasing<Product<Lhs, Rhs, AliasFreeProduct>, sub_assign_op<DstScalar, SrcScalar>>
2578 : assignment_assume_aliasing<Product<Lhs, Rhs, AliasFreeProduct>, add_assign_op<DstScalar, SrcScalar>> {};
2579
2580// FIXME: consider unifying Transpositions and Permutation into a single shape.
2581
2586template <typename ExpressionType, int Side, bool Transposed, typename ExpressionShape>
2587struct transposition_matrix_product {
2588 using MatrixType = typename nested_eval<ExpressionType, 1>::type;
2589 using MatrixTypeCleaned = remove_all_t<MatrixType>;
2590
2591 template <typename Dest, typename TranspositionType, typename Func>
2592 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void accumulate(Dest& dst, const TranspositionType& tr,
2593 const ExpressionType& xpr, const Func& func) {
2594 using SourceIndices = typename TranspositionType::IndicesType;
2595 // Swap partners can fit a narrow StorageIndex even when composed positions do not.
2596 constexpr bool FitsStack = SourceIndices::MaxSizeAtCompileTime != Dynamic &&
2597 (EIGEN_STACK_ALLOCATION_LIMIT == 0 || std::size_t(SourceIndices::MaxSizeAtCompileTime) <=
2598 EIGEN_STACK_ALLOCATION_LIMIT / sizeof(Index));
2599 using Indices = Matrix<Index, FitsStack ? SourceIndices::SizeAtCompileTime : Dynamic, 1, 0,
2600 FitsStack ? SourceIndices::MaxSizeAtCompileTime : Dynamic, 1>;
2601 Indices indices(tr.size());
2602 for (Index i = 0; i < tr.size(); ++i) indices.coeffRef(i) = i;
2603 for (Index k = tr.size(); k > 0; --k) numext::swap(indices.coeffRef(k - 1), indices.coeffRef(tr.coeff(k - 1)));
2604 // Right products apply the swap sequence in the opposite matrix-product order.
2605 indexed_matrix_update<Side, Transposed ^ (Side == OnTheRight)>(dst, indices, xpr, func);
2606 }
2607
2608 template <typename Dest, typename TranspositionType>
2609 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Dest& dst, const TranspositionType& tr,
2610 const ExpressionType& xpr) {
2611 // The in-place row/column swaps below need a dense operand.
2612 static_assert(std::is_same<ExpressionShape, DenseShape>::value,
2613 "TRANSPOSITIONS_PRODUCTS_ARE_ONLY_IMPLEMENTED_FOR_DENSE_OPERANDS");
2614 MatrixType mat(xpr);
2615 using StorageIndex = typename TranspositionType::StorageIndex;
2616 const Index size = tr.size();
2617
2618 if (!is_same_dense(dst, mat)) dst = mat;
2619
2620 for (Index k = (Transposed ? size - 1 : 0); Transposed ? k >= 0 : k < size; Transposed ? --k : ++k) {
2621 const StorageIndex j = tr.coeff(k);
2622 if (Index(j) != k) {
2623 EIGEN_IF_CONSTEXPR (Side == OnTheLeft) {
2624 dst.row(k).swap(dst.row(j));
2625 } else EIGEN_IF_CONSTEXPR (Side == OnTheRight) {
2626 dst.col(k).swap(dst.col(j));
2627 }
2628 }
2629 }
2630 }
2631};
2632
2633template <typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
2634struct generic_product_impl<Lhs, Rhs, TranspositionsShape, MatrixShape, ProductTag>
2635 : indexed_product_accumulation<
2636 Lhs, Rhs, generic_product_impl<Lhs, Rhs, TranspositionsShape, MatrixShape, ProductTag>, MatrixShape> {
2637 template <typename Dest, typename Func>
2638 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void accumulate(Dest& dst, const Lhs& lhs, const Rhs& rhs,
2639 const Func& func) {
2640 transposition_matrix_product<Rhs, OnTheLeft, false, MatrixShape>::accumulate(dst, lhs, rhs, func);
2641 }
2642 template <typename Dest>
2643 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
2644 transposition_matrix_product<Rhs, OnTheLeft, false, MatrixShape>::run(dst, lhs, rhs);
2645 }
2646};
2647
2648template <typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
2649struct generic_product_impl<Lhs, Rhs, MatrixShape, TranspositionsShape, ProductTag>
2650 : indexed_product_accumulation<
2651 Lhs, Rhs, generic_product_impl<Lhs, Rhs, MatrixShape, TranspositionsShape, ProductTag>, MatrixShape> {
2652 template <typename Dest, typename Func>
2653 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void accumulate(Dest& dst, const Lhs& lhs, const Rhs& rhs,
2654 const Func& func) {
2655 transposition_matrix_product<Lhs, OnTheRight, false, MatrixShape>::accumulate(dst, rhs, lhs, func);
2656 }
2657 template <typename Dest>
2658 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
2659 transposition_matrix_product<Lhs, OnTheRight, false, MatrixShape>::run(dst, rhs, lhs);
2660 }
2661};
2662
2663template <typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
2664struct generic_product_impl<Transpose<Lhs>, Rhs, TranspositionsShape, MatrixShape, ProductTag>
2665 : indexed_product_accumulation<
2666 Transpose<Lhs>, Rhs, generic_product_impl<Transpose<Lhs>, Rhs, TranspositionsShape, MatrixShape, ProductTag>,
2667 MatrixShape> {
2668 template <typename Dest, typename Func>
2669 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void accumulate(Dest& dst, const Transpose<Lhs>& lhs, const Rhs& rhs,
2670 const Func& func) {
2671 transposition_matrix_product<Rhs, OnTheLeft, true, MatrixShape>::accumulate(dst, lhs.nestedExpression(), rhs, func);
2672 }
2673 template <typename Dest>
2674 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Transpose<Lhs>& lhs, const Rhs& rhs) {
2675 transposition_matrix_product<Rhs, OnTheLeft, true, MatrixShape>::run(dst, lhs.nestedExpression(), rhs);
2676 }
2677};
2678
2679template <typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
2680struct generic_product_impl<Lhs, Transpose<Rhs>, MatrixShape, TranspositionsShape, ProductTag>
2681 : indexed_product_accumulation<
2682 Lhs, Transpose<Rhs>, generic_product_impl<Lhs, Transpose<Rhs>, MatrixShape, TranspositionsShape, ProductTag>,
2683 MatrixShape> {
2684 template <typename Dest, typename Func>
2685 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void accumulate(Dest& dst, const Lhs& lhs, const Transpose<Rhs>& rhs,
2686 const Func& func) {
2687 transposition_matrix_product<Lhs, OnTheRight, true, MatrixShape>::accumulate(dst, rhs.nestedExpression(), lhs,
2688 func);
2689 }
2690 template <typename Dest>
2691 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Lhs& lhs, const Transpose<Rhs>& rhs) {
2692 transposition_matrix_product<Lhs, OnTheRight, true, MatrixShape>::run(dst, rhs.nestedExpression(), lhs);
2693 }
2694};
2695
2696/***************************************************************************
2697 * skew symmetric products
2698 * The 3x3 skew-symmetric operand is densified and the product is re-dispatched on the other
2699 * operand's shape, so shapes whose products with a dense matrix are evaluator-only (DiagonalShape)
2700 * resolve too.
2701 ***************************************************************************/
2702template <typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
2703struct generic_product_impl<Lhs, Rhs, SkewSymmetricShape, MatrixShape, ProductTag> {
2704 template <typename Dest>
2705 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
2706 call_assignment_no_alias(dst, typename Lhs::DenseMatrixType(lhs) * rhs);
2707 }
2708};
2709
2710template <typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
2711struct generic_product_impl<Lhs, Rhs, MatrixShape, SkewSymmetricShape, ProductTag> {
2712 template <typename Dest>
2713 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
2714 call_assignment_no_alias(dst, lhs * typename Rhs::DenseMatrixType(rhs));
2715 }
2716};
2717
2718template <typename Lhs, typename Rhs, int ProductTag>
2719struct generic_product_impl<Lhs, Rhs, SkewSymmetricShape, SkewSymmetricShape, ProductTag> {
2720 template <typename Dest>
2721 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
2722 generic_product_impl<typename Lhs::DenseMatrixType, typename Rhs::DenseMatrixType, DenseShape, DenseShape,
2723 ProductTag>::evalTo(dst, lhs, rhs);
2724 }
2725};
2726
2727template <typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
2728struct generic_product_impl<Lhs, Rhs, MatrixShape, HomogeneousShape, ProductTag>
2729 : generic_product_impl<Lhs, typename Rhs::PlainObject, MatrixShape, DenseShape, ProductTag> {};
2730
2731template <typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
2732struct generic_product_impl<Lhs, Rhs, HomogeneousShape, MatrixShape, ProductTag>
2733 : generic_product_impl<typename Lhs::PlainObject, Rhs, DenseShape, MatrixShape, ProductTag> {};
2734
2735template <typename Lhs, typename Rhs, int ProductTag>
2736struct generic_product_impl<Lhs, Rhs, PermutationShape, HomogeneousShape, ProductTag>
2737 : generic_product_impl<Lhs, Rhs, PermutationShape, DenseShape, ProductTag> {};
2738
2739template <typename Lhs, typename Rhs, int ProductTag>
2740struct generic_product_impl<Lhs, Rhs, HomogeneousShape, PermutationShape, ProductTag>
2741 : generic_product_impl<Lhs, Rhs, DenseShape, PermutationShape, ProductTag> {};
2742
2743} // end namespace internal
2744
2745} // end namespace Eigen
2746
2747#if EIGEN_COMP_MSVC
2748#pragma warning(pop)
2749#endif
2750
2751#endif // EIGEN_PRODUCTEVALUATORS_H
@ UnitDiag
Definition Constants.h:216
@ ZeroDiag
Definition Constants.h:218
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
@ OnTheLeft
Definition Constants.h:332
@ OnTheRight
Definition Constants.h:334
@ Horizontal
Definition Constants.h:270
@ Vertical
Definition Constants.h:267
constexpr unsigned int ActualPacketAccessBit
Definition Constants.h:109
constexpr unsigned int PacketAccessBit
Definition Constants.h:98
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int EvalBeforeNestingBit
Definition Constants.h:75
constexpr unsigned int LinearAccessBit
Definition Constants.h:134
constexpr unsigned int RowMajorBit
Definition Constants.h:71
void operator()(const Dst &dst, const Src &src) const
Definition ProductEvaluators.h:450