Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
GeneralProduct.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-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_GENERAL_PRODUCT_H
13#define EIGEN_GENERAL_PRODUCT_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20enum { Large = 2, Small = 3 };
21
22// Runtime-size threshold for falling back from the generic matrix-matrix product
23// implementation (heavy) to the lightweight coeff-based product one. See
24// generic_product_impl<Lhs,Rhs,DenseShape,DenseShape,GemmProduct> in
25// products/GeneralMatrixMatrix.h for more details.
26// The crossover belongs to the kernel the GEMM path would select, not to the
27// build: a scalar type that falls back to the generic gebp kernel keeps this
28// value even in an SME build. The SME kernels have their own crossovers
29// -- sme_gemm_to_coeffbased_threshold and
30// EIGEN_SME_FIXED_SIZE_GEMM_TO_COEFFBASED_THRESHOLD below.
31#ifndef EIGEN_GEMM_TO_COEFFBASED_THRESHOLD
32// This default value has been obtained on a Haswell architecture.
33#define EIGEN_GEMM_TO_COEFFBASED_THRESHOLD 20
34#endif
35
36// Fixed-size products can reach the GEMM product path even when the
37// coeff-based evaluator is still faster. Keep this threshold separate so
38// runtime-size dispatch preserves the historical heuristic above. This default
39// was tuned on the same Haswell system as the runtime threshold. It does not
40// follow a user-defined EIGEN_GEMM_TO_COEFFBASED_THRESHOLD, which still applies
41// at run time, so a fixed-size product's crossover is the larger of the two.
42#ifndef EIGEN_FIXED_SIZE_GEMM_TO_COEFFBASED_THRESHOLD
43#define EIGEN_FIXED_SIZE_GEMM_TO_COEFFBASED_THRESHOLD 40
44#endif
45
46#ifdef EIGEN_VECTORIZE_SME
47// Read only for SME-kernel scalar pairs; fitted on Apple M4 Pro to today's NEON small-block bounds (re-measure: #3194).
48#ifndef EIGEN_SME_FIXED_SIZE_GEMM_TO_COEFFBASED_THRESHOLD
49#define EIGEN_SME_FIXED_SIZE_GEMM_TO_COEFFBASED_THRESHOLD 61
50#endif
51#endif
52
53// The dimension-sum bound in sme_gemm_to_coeffbased_threshold below grows with
54// the depth, so it cannot reach a small output over a long depth: a 2x2 result
55// at k=1024 sums to 1028 and takes the SME path with nearly the whole ZA grid
56// predicated off. The crossover for that shape family is an output *area* rather
57// than a dimension sum, and it tracks the scalar width.
58//
59// Measured on Apple M4 at SVL=512, every m x n with m,n in [2,12] against the
60// coeff-based path: the largest area at which *no* shape loses is 27, 14, 15 and
61// 6 for float, complex<float>, double and complex<double>. 96/sizeof (24, 12,
62// 12, 6) sits just inside all four, and holds out to k=32768 -- the worst cell
63// there is float 4x6 at 1.03x, i.e. neutral, while 2x2 is 13x and 3x3 2.0x.
64// A flat constant does not work: the value that is safe for complex<double>
65// admits only 2x2 for float, and float's would cost complex<double> up to 2x.
66#ifndef EIGEN_SME_GEMM_TO_COEFFBASED_OUTPUT_AREA_THRESHOLD
67#define EIGEN_SME_GEMM_TO_COEFFBASED_OUTPUT_AREA_THRESHOLD(Scalar) (96 / int(sizeof(Scalar)))
68#endif
69
70namespace internal {
71
72#ifdef EIGEN_VECTORIZE_SME
73// Defined in products/GeneralBlockPanelKernel.h, which Core includes after this
74// header; only the declaration is needed to form the dependent type below.
75template <typename LhsScalar, typename RhsScalar>
76struct sme_has_gebp_kernel;
77// Dimension sum below which the coeff-based product beats the SME GEMM kernel.
78// mr x nr is sized in ZA tiles, so a product too small to fill the grid wastes
79// most of the outer products the kernel issues, and the block holds fewer
80// scalars as the scalar widens: the crossover sits well above the Haswell-tuned
81// EIGEN_GEMM_TO_COEFFBASED_THRESHOLD, and it moves with the scalar type.
82//
83// First cube (m=n=k) at which the SME kernel wins, from two sweeps that each
84// built the same source twice with only this threshold changed, so that the real
85// dispatch selected the path in both:
86//
87// scalar M4 M4 Pro M4 Pro, result 64-byte aligned
88// complex<double> 14 14 15
89// double 16 18 16
90// complex<float> 20 19 19
91// float 26 22 23
92//
93// The entries below are 3n for the largest of each row, so neither host loses at
94// the crossover it gets. Over the sizes where the two disagree the paths are
95// within 1.2x on both, while at n=14 a too-low bound costs 1.7x to 2.9x.
96//
97// Defining EIGEN_SME_GEMM_TO_COEFFBASED_THRESHOLD replaces every entry with one
98// value, which is what such a sweep wants. The primary template answers with the
99// generic threshold: sme_has_gebp_kernel gates the dispatch, so a scalar pair
100// without an SME kernel never reads the table.
101// With the NEON small-block path (arch/SME/GeneralBlockPanelKernel.h) the SME
102// kernels take the generic crossover: below it the small products run the NEON
103// packers and kernel, so the SME build matches a NEON build there.
104#ifdef EIGEN_SME_GEMM_TO_COEFFBASED_THRESHOLD
105template <typename Scalar>
106struct sme_gemm_to_coeffbased_threshold : std::integral_constant<int, EIGEN_SME_GEMM_TO_COEFFBASED_THRESHOLD> {};
107#else
108template <typename Scalar>
109struct sme_gemm_to_coeffbased_threshold : std::integral_constant<int, EIGEN_GEMM_TO_COEFFBASED_THRESHOLD> {};
110#endif
111#endif
112
113template <int Rows, int Cols, int Depth>
114struct product_type_selector;
115
116template <int Size, int MaxSize>
117struct product_size_category {
118 enum {
119#ifndef EIGEN_GPU_COMPILE_PHASE
120 is_large = MaxSize == Dynamic || Size >= EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD ||
121 (Size == Dynamic && MaxSize >= EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD),
122#else
123 is_large = 0,
124#endif
125 value = is_large ? Large
126 : Size == 1 ? 1
127 : Small
128 };
129};
130
131template <typename Lhs, typename Rhs>
132struct product_type {
133 using Lhs_ = remove_all_t<Lhs>;
134 using Rhs_ = remove_all_t<Rhs>;
135 enum {
136 MaxRows = traits<Lhs_>::MaxRowsAtCompileTime,
137 Rows = traits<Lhs_>::RowsAtCompileTime,
138 MaxCols = traits<Rhs_>::MaxColsAtCompileTime,
139 Cols = traits<Rhs_>::ColsAtCompileTime,
140 MaxDepth = min_size_prefer_fixed(traits<Lhs_>::MaxColsAtCompileTime, traits<Rhs_>::MaxRowsAtCompileTime),
141 Depth = min_size_prefer_fixed(traits<Lhs_>::ColsAtCompileTime, traits<Rhs_>::RowsAtCompileTime)
142 };
143
144 static constexpr int ProductType =
145 product_type_selector<product_size_category<Rows, MaxRows>::value, product_size_category<Cols, MaxCols>::value,
146 product_size_category<Depth, MaxDepth>::value>::value;
147
148 // Same rule as the runtime dispatch in products/GeneralMatrixMatrix.h: the
149 // threshold follows the kernel the GEMM path would select, so a scalar pair
150 // without an SME kernel keeps the generic crossover in an SME build.
151 static constexpr int FixedSizeThreshold =
152#ifdef EIGEN_VECTORIZE_SME
153 sme_has_gebp_kernel<typename traits<Lhs_>::Scalar, typename traits<Rhs_>::Scalar>::value
154 ? EIGEN_SME_FIXED_SIZE_GEMM_TO_COEFFBASED_THRESHOLD
155 :
156#endif
157 EIGEN_FIXED_SIZE_GEMM_TO_COEFFBASED_THRESHOLD;
158
159 static constexpr bool FixedSizeCoeffBasedProduct = ProductType == GemmProduct && Rows != Dynamic && Cols != Dynamic &&
160 Depth != Dynamic &&
161 (int(Rows) + int(Cols) + int(Depth) < FixedSizeThreshold);
163 static constexpr int value = FixedSizeCoeffBasedProduct ? CoeffBasedProductMode : ProductType;
164#ifdef EIGEN_DEBUG_PRODUCT
165 static void debug() {
166 const int rows_select = product_size_category<Rows, MaxRows>::value;
167 const int cols_select = product_size_category<Cols, MaxCols>::value;
168 const int depth_select = product_size_category<Depth, MaxDepth>::value;
169 EIGEN_DEBUG_VAR(Rows);
170 EIGEN_DEBUG_VAR(Cols);
171 EIGEN_DEBUG_VAR(Depth);
172 EIGEN_DEBUG_VAR(rows_select);
173 EIGEN_DEBUG_VAR(cols_select);
174 EIGEN_DEBUG_VAR(depth_select);
175 EIGEN_DEBUG_VAR(ProductType);
176 EIGEN_DEBUG_VAR(FixedSizeCoeffBasedProduct);
177 EIGEN_DEBUG_VAR(value);
178 }
179#endif
180};
181
182/* The following allows to select the kind of product at compile time
183 * based on the three dimensions of the product.
184 * This is a compile time mapping from {1,Small,Large}^3 -> {product types} */
185// FIXME: the current compile-time product-type mapping may not be optimal.
186template <int M, int N>
187struct product_type_selector<M, N, 1> : std::integral_constant<int, OuterProduct> {};
188template <int M>
189struct product_type_selector<M, 1, 1> : std::integral_constant<int, LazyCoeffBasedProductMode> {};
190template <int N>
191struct product_type_selector<1, N, 1> : std::integral_constant<int, LazyCoeffBasedProductMode> {};
192template <int Depth>
193struct product_type_selector<1, 1, Depth> : std::integral_constant<int, InnerProduct> {};
194template <>
195struct product_type_selector<1, 1, 1> : std::integral_constant<int, InnerProduct> {};
196template <>
197struct product_type_selector<Small, 1, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
198template <>
199struct product_type_selector<1, Small, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
200template <>
201struct product_type_selector<Small, Small, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
202template <>
203struct product_type_selector<Small, Small, 1> : std::integral_constant<int, LazyCoeffBasedProductMode> {};
204template <>
205struct product_type_selector<Small, Large, 1> : std::integral_constant<int, LazyCoeffBasedProductMode> {};
206template <>
207struct product_type_selector<Large, Small, 1> : std::integral_constant<int, LazyCoeffBasedProductMode> {};
208template <>
209struct product_type_selector<1, Large, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
210template <>
211struct product_type_selector<1, Large, Large> : std::integral_constant<int, GemvProduct> {};
212template <>
213struct product_type_selector<1, Small, Large> : std::integral_constant<int, CoeffBasedProductMode> {};
214template <>
215struct product_type_selector<Large, 1, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
216template <>
217struct product_type_selector<Large, 1, Large> : std::integral_constant<int, GemvProduct> {};
218template <>
219struct product_type_selector<Small, 1, Large> : std::integral_constant<int, CoeffBasedProductMode> {};
220template <>
221struct product_type_selector<Small, Small, Large> : std::integral_constant<int, GemmProduct> {};
222template <>
223struct product_type_selector<Large, Small, Large> : std::integral_constant<int, GemmProduct> {};
224template <>
225struct product_type_selector<Small, Large, Large> : std::integral_constant<int, GemmProduct> {};
226template <>
227struct product_type_selector<Large, Large, Large> : std::integral_constant<int, GemmProduct> {};
228template <>
229struct product_type_selector<Large, Small, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
230template <>
231struct product_type_selector<Small, Large, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
232template <>
233struct product_type_selector<Large, Large, Small> : std::integral_constant<int, GemmProduct> {};
234
235} // end namespace internal
236
237/***********************************************************************
238 * Implementation of Inner Vector Vector Product
239 ***********************************************************************/
240
241// FIXME: consider returning a Scalar instead of a 1x1 matrix for inner products.
242// Pro: more natural for the user.
243// Con: in a meta-unrolled algorithm a matrix-matrix product may reduce to a
244// row-vector times column-vector product. To handle this, we could specialize
245// Block<MatrixType,1,1> with operator=(Scalar x).
246
247/***********************************************************************
248 * Implementation of Outer Vector Vector Product
249 ***********************************************************************/
250
251/***********************************************************************
252 * Implementation of General Matrix Vector Product
253 ***********************************************************************/
254
255/* According to the shape/flags of the matrix we have to distinguish 3 different cases:
256 * 1 - the matrix is col-major, BLAS compatible and M is large => call fast BLAS-like colmajor routine
257 * 2 - the matrix is row-major, BLAS compatible and N is large => call fast BLAS-like rowmajor routine
258 * 3 - all other cases are handled using a simple loop along the outer-storage direction.
259 * Therefore we need a lower level meta selector.
260 * Furthermore, if the matrix is the rhs, then the product has to be transposed.
261 */
262namespace internal {
263
264template <int Side, int StorageOrder, bool BlasCompatible>
265struct gemv_dense_selector;
266
267} // end namespace internal
268
269namespace internal {
270
271template <typename Scalar, int Size, int MaxSize, bool Cond>
272struct gemv_static_vector_if;
273
274template <typename Scalar, int Size, int MaxSize>
275struct gemv_static_vector_if<Scalar, Size, MaxSize, false> {
276 EIGEN_DEVICE_FUNC constexpr Scalar* data() {
277 eigen_internal_assert(false && "should never be called");
278 return 0;
279 }
280};
281
282template <typename Scalar, int Size>
283struct gemv_static_vector_if<Scalar, Size, Dynamic, true> {
284 EIGEN_DEVICE_FUNC constexpr Scalar* data() { return 0; }
285};
286
287template <typename Scalar, int Size>
288struct gemv_static_vector_if<Scalar, Size, 0, true> {
289 EIGEN_DEVICE_FUNC constexpr Scalar* data() { return nullptr; }
290};
291
292template <typename Scalar, int Size, int MaxSize>
293struct gemv_static_vector_if<Scalar, Size, MaxSize, true> {
294#if EIGEN_MAX_STATIC_ALIGN_BYTES != 0
295 internal::plain_array<Scalar, internal::min_size_prefer_fixed(Size, MaxSize), 0, AlignedMax> m_data;
296 constexpr Scalar* data() { return m_data.array; }
297#elif EIGEN_MAX_ALIGN_BYTES == 0
298 // No alignment is required: ~(0 - 1) below would clear every bit and return a null pointer.
299 internal::plain_array<Scalar, internal::min_size_prefer_fixed(Size, MaxSize), 0> m_data;
300 constexpr Scalar* data() { return m_data.array; }
301#else
302 // Some architectures cannot align on the stack,
303 // => let's manually enforce alignment by allocating more data and return the address of the first aligned element.
304 internal::plain_array<Scalar, internal::min_size_prefer_fixed(Size, MaxSize) + EIGEN_MAX_ALIGN_BYTES, 0> m_data;
305 constexpr Scalar* data() {
306 return reinterpret_cast<Scalar*>((std::uintptr_t(m_data.array) & ~(std::size_t(EIGEN_MAX_ALIGN_BYTES - 1))) +
307 EIGEN_MAX_ALIGN_BYTES);
308 }
309#endif
310};
311
312template <typename ResScalar>
313using gemv_mapped_destination =
314 Map<Matrix<ResScalar, Dynamic, 1>, plain_enum_min(AlignedMax, internal::packet_traits<ResScalar>::size)>;
315
316template <typename Dest>
317EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE gemv_mapped_destination<typename Dest::Scalar> gemv_construct_mapped_destination(
318 Dest& dest, typename Dest::Scalar* actual_dest_ptr) {
319#ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
320 constexpr int Size = Dest::SizeAtCompileTime;
321 Index size = dest.size();
322 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
323#endif
324 return gemv_mapped_destination<typename Dest::Scalar>(actual_dest_ptr, dest.size());
325}
326
327// Prepares the temporary destination shared by general, triangular, and selfadjoint GEMV kernels.
328template <bool EvalToDest, typename Dest>
329EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void gemv_prepare_destination(Dest& dest,
330 typename Dest::Scalar* actual_dest_ptr) {
331 EIGEN_IF_CONSTEXPR (!EvalToDest) gemv_construct_mapped_destination(dest, actual_dest_ptr) = dest;
332}
333
334template <bool EvalToDest, typename Dest>
335EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void gemv_prepare_destination(Dest& dest, typename Dest::Scalar* actual_dest_ptr,
336 bool initialize_to_zero) {
337 if (initialize_to_zero) {
338 gemv_construct_mapped_destination(dest, actual_dest_ptr).setZero();
339 } else {
340 gemv_prepare_destination<EvalToDest>(dest, actual_dest_ptr);
341 }
342}
343
344template <bool EvalToDest, typename Dest>
345EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void gemv_copy_destination(Dest& dest, typename Dest::Scalar* actual_dest_ptr) {
346 EIGEN_IF_CONSTEXPR (!EvalToDest) {
347 dest = gemv_mapped_destination<typename Dest::Scalar>(actual_dest_ptr, dest.size());
348 }
349}
350
351// Adds complex-by-real scalar adaptation to the shared destination handling when necessary.
352template <typename RhsScalar, typename ResScalar, bool EvalToDestAtCompileTime, bool ComplexByReal>
353class gemv_destination_policy {
354 public:
355 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit gemv_destination_policy(const ResScalar& alpha) : alpha_(alpha) {}
356
357 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool eval_to_dest() const {
358 return EvalToDestAtCompileTime && alpha_is_compatible();
359 }
360
361 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE RhsScalar compatible_alpha() const {
362 return alpha_is_compatible() ? get_factor<ResScalar, RhsScalar>::run(alpha_) : RhsScalar(1);
363 }
364
365 template <typename Dest>
366 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void prepare(Dest& dest, ResScalar* actual_dest_ptr) const {
367 gemv_prepare_destination<EvalToDestAtCompileTime>(dest, actual_dest_ptr, !alpha_is_compatible());
368 }
369
370 template <typename Dest>
371 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void copy_back(Dest& dest, ResScalar* actual_dest_ptr) const {
372 if (!alpha_is_compatible()) {
373 dest.matrix() += alpha_ * gemv_mapped_destination<ResScalar>(actual_dest_ptr, dest.size());
374 } else {
375 gemv_copy_destination<EvalToDestAtCompileTime>(dest, actual_dest_ptr);
376 }
377 }
378
379 private:
380 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool alpha_is_compatible() const {
381 return !ComplexByReal || numext::is_exactly_zero(numext::imag(alpha_));
382 }
383
384 ResScalar alpha_;
385};
386
387// Copies a non-contiguous GEMV right-hand side into the aligned buffer allocated by the caller.
388template <bool DirectlyUseRhs, typename ActualRhsType>
389EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void gemv_prepare_rhs(
390 const ActualRhsType& rhs, typename remove_all_t<ActualRhsType>::Scalar* actual_rhs_ptr) {
391 using ActualRhsTypeCleaned = remove_all_t<ActualRhsType>;
392 EIGEN_IF_CONSTEXPR (!DirectlyUseRhs) {
393#ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
394 constexpr int Size = ActualRhsTypeCleaned::SizeAtCompileTime;
395 Index size = rhs.size();
396 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
397#endif
398 // The caller obtains this temporary from ei_declare_aligned_stack_constructed_variable.
399 Map<typename ActualRhsTypeCleaned::PlainObject, AlignedMax>(actual_rhs_ptr, rhs.size()) = rhs;
400 }
401}
402
403// The caller has already established commutative coefficient multiplication.
404template <int StorageOrder, bool BlasCompatible>
405struct gemv_dense_selector<OnTheLeft, StorageOrder, BlasCompatible> {
406 template <typename Lhs, typename Rhs, typename Dest>
407 static void run(const Lhs& lhs, const Rhs& rhs, Dest& dest, const typename Dest::Scalar& alpha) {
408 Transpose<Dest> destT(dest);
409 enum { OtherStorageOrder = StorageOrder == RowMajor ? ColMajor : RowMajor };
410 gemv_dense_selector<OnTheRight, OtherStorageOrder, BlasCompatible>::run(rhs.transpose(), lhs.transpose(), destT,
411 alpha);
412 }
413};
414
415// Keep the shortcut's expression out of the general GEMV caller's inlining budget.
416template <bool ConjugateLhs, typename LhsScalar, typename RhsScalar, typename Dest>
417EIGEN_DONT_INLINE void gemv_single_column_update(const LhsScalar* data, Index rows, Index stride, RhsScalar rhsValue,
418 typename Dest::Scalar alpha, Dest& dest) {
419 using Column = Matrix<LhsScalar, Dynamic, 1>;
420 // A runtime stride of one must be visible to the assignment evaluator to enable packets.
421 // Preserve alpha * (lhs * rhs): folding alpha into rhs can overflow before the product.
422 if (stride == 1) {
423 Map<const Column> column(data, rows);
424 dest.col(0).matrix() += alpha * (column.template conjugateIf<ConjugateLhs>() * rhsValue);
425 } else {
426 Map<const Column, Unaligned, InnerStride<>> column(data, rows, InnerStride<>(stride));
427 dest.col(0).matrix() += alpha * (column.template conjugateIf<ConjugateLhs>() * rhsValue);
428 }
429}
430
431template <bool Enable, bool ConjugateLhs, bool ConjugateRhs>
432struct gemv_single_column {
433 template <typename Lhs, typename Rhs, typename Scalar, typename Dest>
434 static EIGEN_STRONG_INLINE bool run(const Lhs&, const Rhs&, const Scalar&, Dest&, bool) {
435 return false;
436 }
437};
438
439template <bool ConjugateLhs, bool ConjugateRhs>
440struct gemv_single_column<true, ConjugateLhs, ConjugateRhs> {
441 template <typename Lhs, typename Rhs, typename Scalar, typename Dest>
442 static EIGEN_STRONG_INLINE bool run(const Lhs& lhs, const Rhs& rhs, const Scalar& alpha, Dest& dest, bool eligible) {
443 if (!eligible || lhs.cols() != 1) return false;
444 if (!is_identically_zero(alpha)) {
445 gemv_single_column_update<ConjugateLhs>(lhs.data(), lhs.rows(), lhs.col(0).innerStride(),
446 conj_if<ConjugateRhs>()(rhs.coeff(0, 0)), alpha, dest);
447 }
448 return true;
449 }
450};
451
452template <>
453struct gemv_dense_selector<OnTheRight, ColMajor, true> {
454 template <typename Lhs, typename Rhs, typename Dest>
455 static inline void run(const Lhs& lhs, const Rhs& rhs, Dest& dest, const typename Dest::Scalar& alpha) {
456 using LhsScalar = typename Lhs::Scalar;
457 using RhsScalar = typename Rhs::Scalar;
458 using ResScalar = typename Dest::Scalar;
459
460 using LhsBlasTraits = internal::blas_traits<Lhs>;
461 using ActualLhsType = typename LhsBlasTraits::DirectLinearAccessType;
462 using RhsBlasTraits = internal::blas_traits<Rhs>;
463 using ActualRhsType = typename RhsBlasTraits::DirectLinearAccessType;
464
465 ActualLhsType actualLhs = LhsBlasTraits::extract(lhs);
466 ActualRhsType actualRhs = RhsBlasTraits::extract(rhs);
467
468 ResScalar actualAlpha = combine_scalar_factors(alpha, lhs, rhs);
469
470 constexpr bool DoubleScalars = std::is_same<LhsScalar, RhsScalar>::value &&
471 std::is_same<LhsScalar, ResScalar>::value && std::is_same<ResScalar, double>::value;
472 // Float updates can be slower than the buffered packet kernel; bypass it only for matching double scalars.
473 constexpr bool EnableSingleColumn =
474 Lhs::ColsAtCompileTime == Dynamic && DoubleScalars && Dest::InnerStrideAtCompileTime != 1;
475 if (gemv_single_column<EnableSingleColumn, LhsBlasTraits::NeedToConjugate, RhsBlasTraits::NeedToConjugate>::run(
476 actualLhs, actualRhs, actualAlpha, dest,
477 Dest::InnerStrideAtCompileTime != Dynamic || dest.innerStride() != 1))
478 return;
479
480 // make sure Dest is a compile-time vector type (bug 1166)
481 using ActualDest = std::conditional_t<Dest::IsVectorAtCompileTime, Dest, typename Dest::ColXpr>;
482
483 enum {
484 // FIXME: find a way to allow an inner stride on the result if packet_traits<Scalar>::size==1
485 // on the other hand it is good for the cache to pack the vector anyways...
486 EvalToDestAtCompileTime = (ActualDest::InnerStrideAtCompileTime == 1),
487 ComplexByReal = (NumTraits<LhsScalar>::IsComplex) && (!NumTraits<RhsScalar>::IsComplex),
488 MightCannotUseDest = ((!EvalToDestAtCompileTime) || ComplexByReal) && (ActualDest::MaxSizeAtCompileTime != 0)
489 };
490
491 using LhsMapper = const_blas_data_mapper<LhsScalar, Index, ColMajor>;
492 using RhsMapper = const_blas_data_mapper<RhsScalar, Index, RowMajor>;
493 EIGEN_IF_CONSTEXPR (!MightCannotUseDest) {
494 // shortcut if we are sure to be able to use dest directly,
495 // this eases the compiler to generate cleaner and more optimized code for most common cases
496 general_matrix_vector_product<
497 Index, LhsScalar, LhsMapper, ColMajor, LhsBlasTraits::NeedToConjugate, RhsScalar, RhsMapper,
498 RhsBlasTraits::NeedToConjugate>::run(actualLhs.rows(), actualLhs.cols(),
499 LhsMapper(actualLhs.data(), actualLhs.outerStride()),
500 RhsMapper(actualRhs.data(), actualRhs.innerStride()), dest.data(), 1,
501 get_factor<ResScalar, RhsScalar>::run(actualAlpha));
502 } else {
503 gemv_static_vector_if<ResScalar, ActualDest::SizeAtCompileTime, ActualDest::MaxSizeAtCompileTime,
504 MightCannotUseDest>
505 static_dest;
506
507 gemv_destination_policy<RhsScalar, ResScalar, EvalToDestAtCompileTime, ComplexByReal> destPolicy(actualAlpha);
508
509 ei_declare_aligned_stack_constructed_variable(ResScalar, actualDestPtr, dest.size(),
510 destPolicy.eval_to_dest() ? dest.data() : static_dest.data());
511
512 destPolicy.prepare(dest, actualDestPtr);
513
514 general_matrix_vector_product<Index, LhsScalar, LhsMapper, ColMajor, LhsBlasTraits::NeedToConjugate, RhsScalar,
515 RhsMapper, RhsBlasTraits::NeedToConjugate>::run(actualLhs.rows(), actualLhs.cols(),
516 LhsMapper(actualLhs.data(),
517 actualLhs.outerStride()),
518 RhsMapper(actualRhs.data(),
519 actualRhs.innerStride()),
520 actualDestPtr, 1,
521 destPolicy.compatible_alpha());
522
523 destPolicy.copy_back(dest, actualDestPtr);
524 }
525 }
526};
527
528template <>
529struct gemv_dense_selector<OnTheRight, RowMajor, true> {
530 template <typename Lhs, typename Rhs, typename Dest>
531 static void run(const Lhs& lhs, const Rhs& rhs, Dest& dest, const typename Dest::Scalar& alpha) {
532 using LhsScalar = typename Lhs::Scalar;
533 using RhsScalar = typename Rhs::Scalar;
534 using ResScalar = typename Dest::Scalar;
535
536 using LhsBlasTraits = internal::blas_traits<Lhs>;
537 using ActualLhsType = typename LhsBlasTraits::DirectLinearAccessType;
538 using RhsBlasTraits = internal::blas_traits<Rhs>;
539 using ActualRhsType = typename RhsBlasTraits::DirectLinearAccessType;
540 using ActualRhsTypeCleaned = internal::remove_all_t<ActualRhsType>;
541
542 std::add_const_t<ActualLhsType> actualLhs = LhsBlasTraits::extract(lhs);
543 std::add_const_t<ActualRhsType> actualRhs = RhsBlasTraits::extract(rhs);
544
545 ResScalar actualAlpha = combine_scalar_factors(alpha, lhs, rhs);
546
547 // Cost heuristic: broadcasts may allocate for owning scalars. Complex shortcuts can worsen GCC's kernel spilling.
548 constexpr bool EnableSingleColumn = Lhs::ColsAtCompileTime == Dynamic && !NumTraits<ResScalar>::IsComplex &&
549 std::is_trivially_copyable<LhsScalar>::value &&
550 std::is_trivially_copyable<RhsScalar>::value &&
551 std::is_trivially_copyable<ResScalar>::value;
552 if (gemv_single_column<EnableSingleColumn, LhsBlasTraits::NeedToConjugate, RhsBlasTraits::NeedToConjugate>::run(
553 actualLhs, actualRhs, actualAlpha, dest, true))
554 return;
555
556 enum {
557 // FIXME: find a way to allow an inner stride on the result if packet_traits<Scalar>::size==1
558 // on the other hand it is good for the cache to pack the vector anyways...
559 DirectlyUseRhs =
560 ActualRhsTypeCleaned::InnerStrideAtCompileTime == 1 || ActualRhsTypeCleaned::MaxSizeAtCompileTime == 0
561 };
562
563 gemv_static_vector_if<RhsScalar, ActualRhsTypeCleaned::SizeAtCompileTime,
564 ActualRhsTypeCleaned::MaxSizeAtCompileTime, !DirectlyUseRhs>
565 static_rhs;
566
567 ei_declare_aligned_stack_constructed_variable(
568 RhsScalar, actualRhsPtr, actualRhs.size(),
569 DirectlyUseRhs ? const_cast<RhsScalar*>(actualRhs.data()) : static_rhs.data());
570
571 gemv_prepare_rhs<DirectlyUseRhs>(actualRhs, actualRhsPtr);
572
573 using LhsMapper = const_blas_data_mapper<LhsScalar, Index, RowMajor>;
574 using RhsMapper = const_blas_data_mapper<RhsScalar, Index, ColMajor>;
575 general_matrix_vector_product<Index, LhsScalar, LhsMapper, RowMajor, LhsBlasTraits::NeedToConjugate, RhsScalar,
576 RhsMapper, RhsBlasTraits::NeedToConjugate>::
577 run(actualLhs.rows(), actualLhs.cols(), LhsMapper(actualLhs.data(), actualLhs.outerStride()),
578 RhsMapper(actualRhsPtr, 1), dest.data(),
579 dest.col(0).innerStride(), // NOTE if dest is not a vector at compile-time, then dest.innerStride() might
580 // be wrong. (bug 1166)
581 actualAlpha);
582 }
583};
584
585template <>
586struct gemv_dense_selector<OnTheRight, ColMajor, false> {
587 template <typename Lhs, typename Rhs, typename Dest>
588 static void run(const Lhs& lhs, const Rhs& rhs, Dest& dest, const typename Dest::Scalar& alpha) {
589 EIGEN_STATIC_ASSERT((!nested_eval<Lhs, 1>::Evaluate),
590 EIGEN_INTERNAL_COMPILATION_ERROR_OR_YOU_MADE_A_PROGRAMMING_MISTAKE);
591 // TODO: if rhs is large enough it might be beneficial to make sure that dest is sequentially stored in memory,
592 // otherwise use a temp
593 typename nested_eval<Rhs, 1>::type actual_rhs(rhs);
594 const Index size = rhs.rows();
595 for (Index k = 0; k < size; ++k) dest.matrix() += (alpha * actual_rhs.coeff(k)) * lhs.col(k);
596 }
597};
598
599template <>
600struct gemv_dense_selector<OnTheRight, RowMajor, false> {
601 template <typename Lhs, typename Rhs, typename Dest>
602 static void run(const Lhs& lhs, const Rhs& rhs, Dest& dest, const typename Dest::Scalar& alpha) {
603 EIGEN_STATIC_ASSERT((!nested_eval<Lhs, 1>::Evaluate),
604 EIGEN_INTERNAL_COMPILATION_ERROR_OR_YOU_MADE_A_PROGRAMMING_MISTAKE);
605 typename nested_eval<Rhs, Lhs::RowsAtCompileTime>::type actual_rhs(rhs);
606 const Index rows = dest.rows();
607 for (Index i = 0; i < rows; ++i)
608 dest.coeffRef(i) += alpha * (lhs.row(i).cwiseProduct(actual_rhs.transpose())).sum();
609 }
610};
611
612} // end namespace internal
613
614/***************************************************************************
615 * Implementation of matrix base methods
616 ***************************************************************************/
617
624template <typename Derived>
625template <typename OtherDerived>
626EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Product<Derived, OtherDerived> MatrixBase<Derived>::operator*(
627 const MatrixBase<OtherDerived>& other) const {
628 // A note regarding the function declaration: In MSVC, this function will sometimes
629 // not be inlined since DenseStorage is an unwindable object for dynamic
630 // matrices and product types are holding a member to store the result.
631 // Thus it does not help tagging this function with EIGEN_STRONG_INLINE.
632 enum {
633 ProductIsValid = Derived::ColsAtCompileTime == Dynamic || OtherDerived::RowsAtCompileTime == Dynamic ||
634 int(Derived::ColsAtCompileTime) == int(OtherDerived::RowsAtCompileTime),
635 AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
636 SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived, OtherDerived)
637 };
638 // note to the lost user:
639 // * for a dot product use: v1.dot(v2)
640 // * for a coeff-wise product use: v1.cwiseProduct(v2)
641 EIGEN_STATIC_ASSERT(
642 ProductIsValid || !(AreVectors && SameSizes),
643 INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
644 EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
645 INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
646 EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
647#ifdef EIGEN_DEBUG_PRODUCT
648 internal::product_type<Derived, OtherDerived>::debug();
649#endif
650
651 return Product<Derived, OtherDerived>(derived(), other.derived());
652}
653
665template <typename Derived>
666template <typename OtherDerived>
667EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Product<Derived, OtherDerived, LazyProduct>
668MatrixBase<Derived>::lazyProduct(const MatrixBase<OtherDerived>& other) const {
669 enum {
670 ProductIsValid = Derived::ColsAtCompileTime == Dynamic || OtherDerived::RowsAtCompileTime == Dynamic ||
671 int(Derived::ColsAtCompileTime) == int(OtherDerived::RowsAtCompileTime),
672 AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
673 SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived, OtherDerived)
674 };
675 // note to the lost user:
676 // * for a dot product use: v1.dot(v2)
677 // * for a coeff-wise product use: v1.cwiseProduct(v2)
678 EIGEN_STATIC_ASSERT(
679 ProductIsValid || !(AreVectors && SameSizes),
680 INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
681 EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
682 INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
683 EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
684
685 return Product<Derived, OtherDerived, LazyProduct>(derived(), other.derived());
686}
687
688} // end namespace Eigen
689
690#endif // EIGEN_GENERAL_PRODUCT_H
CoeffReturnType value() const
Definition DenseBase.h:454
const Product< Derived, OtherDerived, LazyProduct > lazyProduct(const MatrixBase< OtherDerived > &other) const
Definition GeneralProduct.h:668
const Product< Derived, OtherDerived > operator*(const MatrixBase< OtherDerived > &other) const
Definition GeneralProduct.h:626
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:225
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
@ OnTheLeft
Definition Constants.h:332
@ OnTheRight
Definition Constants.h:334