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);
186template <
int M,
int N>
187struct product_type_selector<M, N, 1> : std::integral_constant<int, OuterProduct> {};
189struct product_type_selector<M, 1, 1> : std::integral_constant<int, LazyCoeffBasedProductMode> {};
191struct product_type_selector<1, N, 1> : std::integral_constant<int, LazyCoeffBasedProductMode> {};
193struct product_type_selector<1, 1, Depth> : std::integral_constant<int, InnerProduct> {};
195struct product_type_selector<1, 1, 1> : std::integral_constant<int, InnerProduct> {};
197struct product_type_selector<Small, 1, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
199struct product_type_selector<1, Small, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
201struct product_type_selector<Small, Small, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
203struct product_type_selector<Small, Small, 1> : std::integral_constant<int, LazyCoeffBasedProductMode> {};
205struct product_type_selector<Small, Large, 1> : std::integral_constant<int, LazyCoeffBasedProductMode> {};
207struct product_type_selector<Large, Small, 1> : std::integral_constant<int, LazyCoeffBasedProductMode> {};
209struct product_type_selector<1, Large, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
211struct product_type_selector<1, Large, Large> : std::integral_constant<int, GemvProduct> {};
213struct product_type_selector<1, Small, Large> : std::integral_constant<int, CoeffBasedProductMode> {};
215struct product_type_selector<Large, 1, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
217struct product_type_selector<Large, 1, Large> : std::integral_constant<int, GemvProduct> {};
219struct product_type_selector<Small, 1, Large> : std::integral_constant<int, CoeffBasedProductMode> {};
221struct product_type_selector<Small, Small, Large> : std::integral_constant<int, GemmProduct> {};
223struct product_type_selector<Large, Small, Large> : std::integral_constant<int, GemmProduct> {};
225struct product_type_selector<Small, Large, Large> : std::integral_constant<int, GemmProduct> {};
227struct product_type_selector<Large, Large, Large> : std::integral_constant<int, GemmProduct> {};
229struct product_type_selector<Large, Small, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
231struct product_type_selector<Small, Large, Small> : std::integral_constant<int, CoeffBasedProductMode> {};
233struct product_type_selector<Large, Large, Small> : std::integral_constant<int, GemmProduct> {};
264template <
int S
ide,
int StorageOrder,
bool BlasCompatible>
265struct gemv_dense_selector;
271template <
typename Scalar,
int Size,
int MaxSize,
bool Cond>
272struct gemv_static_vector_if;
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");
282template <
typename Scalar,
int Size>
283struct gemv_static_vector_if<Scalar, Size, Dynamic, true> {
284 EIGEN_DEVICE_FUNC
constexpr Scalar* data() {
return 0; }
287template <
typename Scalar,
int Size>
288struct gemv_static_vector_if<Scalar, Size, 0, true> {
289 EIGEN_DEVICE_FUNC
constexpr Scalar* data() {
return nullptr; }
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
299 internal::plain_array<Scalar, internal::min_size_prefer_fixed(Size, MaxSize), 0> m_data;
300 constexpr Scalar* data() {
return m_data.array; }
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);
312template <
typename ResScalar>
313using gemv_mapped_destination =
314 Map<Matrix<ResScalar, Dynamic, 1>, plain_enum_min(AlignedMax, internal::packet_traits<ResScalar>::size)>;
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
324 return gemv_mapped_destination<typename Dest::Scalar>(actual_dest_ptr, dest.size());
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;
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();
340 gemv_prepare_destination<EvalToDest>(dest, actual_dest_ptr);
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());
352template <
typename RhsScalar,
typename ResScalar,
bool EvalToDestAtCompileTime,
bool ComplexByReal>
353class gemv_destination_policy {
355 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit gemv_destination_policy(
const ResScalar& alpha) : alpha_(alpha) {}
357 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
bool eval_to_dest()
const {
358 return EvalToDestAtCompileTime && alpha_is_compatible();
361 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE RhsScalar compatible_alpha()
const {
362 return alpha_is_compatible() ? get_factor<ResScalar, RhsScalar>::run(alpha_) : RhsScalar(1);
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());
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());
375 gemv_copy_destination<EvalToDestAtCompileTime>(dest, actual_dest_ptr);
380 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
bool alpha_is_compatible()
const {
381 return !ComplexByReal || numext::is_exactly_zero(numext::imag(alpha_));
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
399 Map<typename ActualRhsTypeCleaned::PlainObject, AlignedMax>(actual_rhs_ptr, rhs.size()) = rhs;
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);
410 gemv_dense_selector<OnTheRight, OtherStorageOrder, BlasCompatible>::run(rhs.transpose(), lhs.transpose(), destT,
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>;
423 Map<const Column> column(data, rows);
424 dest.col(0).matrix() += alpha * (column.template conjugateIf<ConjugateLhs>() * rhsValue);
426 Map<const Column, Unaligned, InnerStride<>> column(data, rows, InnerStride<>(stride));
427 dest.col(0).matrix() += alpha * (column.template conjugateIf<ConjugateLhs>() * rhsValue);
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) {
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);
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;
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;
465 ActualLhsType actualLhs = LhsBlasTraits::extract(lhs);
466 ActualRhsType actualRhs = RhsBlasTraits::extract(rhs);
468 ResScalar actualAlpha = combine_scalar_factors(alpha, lhs, rhs);
470 constexpr bool DoubleScalars = std::is_same<LhsScalar, RhsScalar>::value &&
471 std::is_same<LhsScalar, ResScalar>::value && std::is_same<ResScalar, double>::value;
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))
481 using ActualDest = std::conditional_t<Dest::IsVectorAtCompileTime, Dest, typename Dest::ColXpr>;
486 EvalToDestAtCompileTime = (ActualDest::InnerStrideAtCompileTime == 1),
487 ComplexByReal = (NumTraits<LhsScalar>::IsComplex) && (!NumTraits<RhsScalar>::IsComplex),
488 MightCannotUseDest = ((!EvalToDestAtCompileTime) || ComplexByReal) && (ActualDest::MaxSizeAtCompileTime != 0)
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) {
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));
503 gemv_static_vector_if<ResScalar, ActualDest::SizeAtCompileTime, ActualDest::MaxSizeAtCompileTime,
507 gemv_destination_policy<RhsScalar, ResScalar, EvalToDestAtCompileTime, ComplexByReal> destPolicy(actualAlpha);
509 ei_declare_aligned_stack_constructed_variable(ResScalar, actualDestPtr, dest.size(),
510 destPolicy.eval_to_dest() ? dest.data() : static_dest.data());
512 destPolicy.prepare(dest, actualDestPtr);
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()),
521 destPolicy.compatible_alpha());
523 destPolicy.copy_back(dest, actualDestPtr);
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;
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>;
542 std::add_const_t<ActualLhsType> actualLhs = LhsBlasTraits::extract(lhs);
543 std::add_const_t<ActualRhsType> actualRhs = RhsBlasTraits::extract(rhs);
545 ResScalar actualAlpha = combine_scalar_factors(alpha, lhs, rhs);
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))
560 ActualRhsTypeCleaned::InnerStrideAtCompileTime == 1 || ActualRhsTypeCleaned::MaxSizeAtCompileTime == 0
563 gemv_static_vector_if<RhsScalar, ActualRhsTypeCleaned::SizeAtCompileTime,
564 ActualRhsTypeCleaned::MaxSizeAtCompileTime, !DirectlyUseRhs>
567 ei_declare_aligned_stack_constructed_variable(
568 RhsScalar, actualRhsPtr, actualRhs.size(),
569 DirectlyUseRhs ?
const_cast<RhsScalar*
>(actualRhs.data()) : static_rhs.data());
571 gemv_prepare_rhs<DirectlyUseRhs>(actualRhs, actualRhsPtr);
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(),
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);
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);
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();
624template <
typename Derived>
625template <
typename OtherDerived>
627 const MatrixBase<OtherDerived>& other)
const {
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)
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();
665template <
typename Derived>
666template <
typename OtherDerived>
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)
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)