SelfadjointMatrixVector.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SELFADJOINT_MATRIX_VECTOR_H
11 #define EIGEN_SELFADJOINT_MATRIX_VECTOR_H
12 
13 // IWYU pragma: private
14 #include "../InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 /* Optimized selfadjoint matrix * vector product:
21  * This algorithm processes 2 columns at once that allows to both reduce
22  * the number of load/stores of the result by a factor 2 and to reduce
23  * the instruction dependency.
24  */
25 
26 template <typename Scalar, typename Index, int StorageOrder, int UpLo, bool ConjugateLhs, bool ConjugateRhs,
27  int Version = Specialized>
28 struct selfadjoint_matrix_vector_product;
29 
30 template <typename Scalar, typename Index, int StorageOrder, int UpLo, bool ConjugateLhs, bool ConjugateRhs,
31  int Version>
33 
34 {
35  static EIGEN_DONT_INLINE EIGEN_DEVICE_FUNC void run(Index size, const Scalar* lhs, Index lhsStride, const Scalar* rhs,
37 };
38 
39 template <typename Scalar, typename Index, int StorageOrder, int UpLo, bool ConjugateLhs, bool ConjugateRhs,
40  int Version>
43  Index size, const Scalar* lhs, Index lhsStride, const Scalar* rhs, Scalar* res, Scalar alpha) {
44  typedef typename packet_traits<Scalar>::type Packet;
45  typedef typename NumTraits<Scalar>::Real RealScalar;
46  const Index PacketSize = sizeof(Packet) / sizeof(Scalar);
47 
48  enum {
49  IsRowMajor = StorageOrder == RowMajor ? 1 : 0,
50  IsLower = UpLo == Lower ? 1 : 0,
51  FirstTriangular = IsRowMajor == IsLower
52  };
53 
54  conj_helper<Scalar, Scalar, NumTraits<Scalar>::IsComplex && logical_xor(ConjugateLhs, IsRowMajor), ConjugateRhs> cj0;
55  conj_helper<Scalar, Scalar, NumTraits<Scalar>::IsComplex && logical_xor(ConjugateLhs, !IsRowMajor), ConjugateRhs> cj1;
57 
58  conj_helper<Packet, Packet, NumTraits<Scalar>::IsComplex && logical_xor(ConjugateLhs, IsRowMajor), ConjugateRhs> pcj0;
59  conj_helper<Packet, Packet, NumTraits<Scalar>::IsComplex && logical_xor(ConjugateLhs, !IsRowMajor), ConjugateRhs>
60  pcj1;
61 
62  Scalar cjAlpha = ConjugateRhs ? numext::conj(alpha) : alpha;
63 
64  Index bound = numext::maxi(Index(0), size - 8) & 0xfffffffe;
65  if (FirstTriangular) bound = size - bound;
66 
67  for (Index j = FirstTriangular ? bound : 0; j < (FirstTriangular ? size : bound); j += 2) {
68  const Scalar* EIGEN_RESTRICT A0 = lhs + j * lhsStride;
69  const Scalar* EIGEN_RESTRICT A1 = lhs + (j + 1) * lhsStride;
70 
71  Scalar t0 = cjAlpha * rhs[j];
72  Packet ptmp0 = pset1<Packet>(t0);
73  Scalar t1 = cjAlpha * rhs[j + 1];
74  Packet ptmp1 = pset1<Packet>(t1);
75 
76  Scalar t2(0);
77  Packet ptmp2 = pset1<Packet>(t2);
78  Scalar t3(0);
79  Packet ptmp3 = pset1<Packet>(t3);
80 
81  Index starti = FirstTriangular ? 0 : j + 2;
82  Index endi = FirstTriangular ? j : size;
83  Index alignedStart = (starti) + internal::first_default_aligned(&res[starti], endi - starti);
84  Index alignedEnd = alignedStart + ((endi - alignedStart) / (PacketSize)) * (PacketSize);
85 
86  res[j] += cjd.pmul(numext::real(A0[j]), t0);
87  res[j + 1] += cjd.pmul(numext::real(A1[j + 1]), t1);
88  if (FirstTriangular) {
89  res[j] += cj0.pmul(A1[j], t1);
90  t3 += cj1.pmul(A1[j], rhs[j]);
91  } else {
92  res[j + 1] += cj0.pmul(A0[j + 1], t0);
93  t2 += cj1.pmul(A0[j + 1], rhs[j + 1]);
94  }
95 
96  for (Index i = starti; i < alignedStart; ++i) {
97  res[i] += cj0.pmul(A0[i], t0) + cj0.pmul(A1[i], t1);
98  t2 += cj1.pmul(A0[i], rhs[i]);
99  t3 += cj1.pmul(A1[i], rhs[i]);
100  }
101  // Yes this an optimization for gcc 4.3 and 4.4 (=> huge speed up)
102  // gcc 4.2 does this optimization automatically.
103  const Scalar* EIGEN_RESTRICT a0It = A0 + alignedStart;
104  const Scalar* EIGEN_RESTRICT a1It = A1 + alignedStart;
105  const Scalar* EIGEN_RESTRICT rhsIt = rhs + alignedStart;
106  Scalar* EIGEN_RESTRICT resIt = res + alignedStart;
107  for (Index i = alignedStart; i < alignedEnd; i += PacketSize) {
108  Packet A0i = ploadu<Packet>(a0It);
109  a0It += PacketSize;
110  Packet A1i = ploadu<Packet>(a1It);
111  a1It += PacketSize;
112  Packet Bi = ploadu<Packet>(rhsIt);
113  rhsIt += PacketSize; // FIXME should be aligned in most cases
114  Packet Xi = pload<Packet>(resIt);
115 
116  Xi = pcj0.pmadd(A0i, ptmp0, pcj0.pmadd(A1i, ptmp1, Xi));
117  ptmp2 = pcj1.pmadd(A0i, Bi, ptmp2);
118  ptmp3 = pcj1.pmadd(A1i, Bi, ptmp3);
119  pstore(resIt, Xi);
120  resIt += PacketSize;
121  }
122  for (Index i = alignedEnd; i < endi; i++) {
123  res[i] += cj0.pmul(A0[i], t0) + cj0.pmul(A1[i], t1);
124  t2 += cj1.pmul(A0[i], rhs[i]);
125  t3 += cj1.pmul(A1[i], rhs[i]);
126  }
127 
128  res[j] += alpha * (t2 + predux(ptmp2));
129  res[j + 1] += alpha * (t3 + predux(ptmp3));
130  }
131  for (Index j = FirstTriangular ? 0 : bound; j < (FirstTriangular ? bound : size); j++) {
132  const Scalar* EIGEN_RESTRICT A0 = lhs + j * lhsStride;
133 
134  Scalar t1 = cjAlpha * rhs[j];
135  Scalar t2(0);
136  res[j] += cjd.pmul(numext::real(A0[j]), t1);
137  for (Index i = FirstTriangular ? 0 : j + 1; i < (FirstTriangular ? j : size); i++) {
138  res[i] += cj0.pmul(A0[i], t1);
139  t2 += cj1.pmul(A0[i], rhs[i]);
140  }
141  res[j] += alpha * t2;
142  }
143 }
144 
145 } // end namespace internal
146 
147 /***************************************************************************
148  * Wrapper to product_selfadjoint_vector
149  ***************************************************************************/
150 
151 namespace internal {
152 
153 template <typename Lhs, int LhsMode, typename Rhs>
154 struct selfadjoint_product_impl<Lhs, LhsMode, false, Rhs, 0, true> {
156 
160 
164 
165  enum { LhsUpLo = LhsMode & (Upper | Lower) };
166 
167  template <typename Dest>
168  static EIGEN_DEVICE_FUNC void run(Dest& dest, const Lhs& a_lhs, const Rhs& a_rhs, const Scalar& alpha) {
169  typedef typename Dest::Scalar ResScalar;
170  typedef typename Rhs::Scalar RhsScalar;
172  MappedDest;
173 
174  eigen_assert(dest.rows() == a_lhs.rows() && dest.cols() == a_rhs.cols());
175 
176  add_const_on_value_type_t<ActualLhsType> lhs = LhsBlasTraits::extract(a_lhs);
177  add_const_on_value_type_t<ActualRhsType> rhs = RhsBlasTraits::extract(a_rhs);
178 
179  Scalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(a_lhs) * RhsBlasTraits::extractScalarFactor(a_rhs);
180 
181  enum {
182  EvalToDest = (Dest::InnerStrideAtCompileTime == 1),
183  UseRhs = (ActualRhsTypeCleaned::InnerStrideAtCompileTime == 1)
184  };
185 
187  static_dest;
188  internal::gemv_static_vector_if<RhsScalar, ActualRhsTypeCleaned::SizeAtCompileTime,
189  ActualRhsTypeCleaned::MaxSizeAtCompileTime, !UseRhs>
190  static_rhs;
191 
192  ei_declare_aligned_stack_constructed_variable(ResScalar, actualDestPtr, dest.size(),
193  EvalToDest ? dest.data() : static_dest.data());
194 
195  ei_declare_aligned_stack_constructed_variable(RhsScalar, actualRhsPtr, rhs.size(),
196  UseRhs ? const_cast<RhsScalar*>(rhs.data()) : static_rhs.data());
197 
198  if (!EvalToDest) {
199 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
200  constexpr int Size = Dest::SizeAtCompileTime;
201  Index size = dest.size();
202  EIGEN_DENSE_STORAGE_CTOR_PLUGIN
203 #endif
204  MappedDest(actualDestPtr, dest.size()) = dest;
205  }
206 
207  if (!UseRhs) {
208 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
209  constexpr int Size = ActualRhsTypeCleaned::SizeAtCompileTime;
210  Index size = rhs.size();
211  EIGEN_DENSE_STORAGE_CTOR_PLUGIN
212 #endif
213  Map<typename ActualRhsTypeCleaned::PlainObject>(actualRhsPtr, rhs.size()) = rhs;
214  }
215 
218  int(LhsUpLo), bool(LhsBlasTraits::NeedToConjugate),
219  bool(RhsBlasTraits::NeedToConjugate)>::run(lhs.rows(), // size
220  &lhs.coeffRef(0, 0), lhs.outerStride(), // lhs info
221  actualRhsPtr, // rhs info
222  actualDestPtr, // result info
223  actualAlpha // scale factor
224  );
225 
226  if (!EvalToDest) dest = MappedDest(actualDestPtr, dest.size());
227  }
228 };
229 
230 template <typename Lhs, typename Rhs, int RhsMode>
231 struct selfadjoint_product_impl<Lhs, 0, true, Rhs, RhsMode, false> {
233  enum { RhsUpLo = RhsMode & (Upper | Lower) };
234 
235  template <typename Dest>
236  static void run(Dest& dest, const Lhs& a_lhs, const Rhs& a_rhs, const Scalar& alpha) {
237  // let's simply transpose the product
238  Transpose<Dest> destT(dest);
240  0, true>::run(destT, a_rhs.transpose(), a_lhs.transpose(), alpha);
241  }
242 };
243 
244 } // end namespace internal
245 
246 } // end namespace Eigen
247 
248 #endif // EIGEN_SELFADJOINT_MATRIX_VECTOR_H
AnnoyingScalar conj(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:133
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_RESTRICT
Definition: Macros.h:1067
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_DONT_INLINE
Definition: Macros.h:853
#define eigen_assert(x)
Definition: Macros.h:910
#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER)
Definition: Memory.h:806
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
internal::packet_traits< Scalar >::type Packet
Definition: benchmark-blocking-sizes.cpp:54
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:202
Expression of the transpose of a matrix.
Definition: Transpose.h:56
float real
Definition: datatypes.h:10
@ Lower
Definition: Constants.h:211
@ Upper
Definition: Constants.h:213
@ AlignedMax
Definition: Constants.h:254
@ Specialized
Definition: Constants.h:311
@ ColMajor
Definition: Constants.h:318
@ RowMajor
Definition: Constants.h:320
const unsigned int RowMajorBit
Definition: Constants.h:70
return int(ret)+1
RealScalar alpha
Definition: level1_cplx_impl.h:151
constexpr int plain_enum_min(A a, B b)
Definition: Meta.h:649
@ Lhs
Definition: TensorContractionMapper.h:20
@ Rhs
Definition: TensorContractionMapper.h:20
constexpr bool logical_xor(bool a, bool b)
Definition: Meta.h:737
static Index first_default_aligned(const DenseBase< Derived > &m)
Definition: DenseCoeffsBase.h:539
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
EIGEN_DEVICE_FUNC void pstore(Scalar *to, const Packet &from)
Definition: GenericPacketMath.h:891
EIGEN_DEVICE_FUNC unpacket_traits< Packet >::type predux(const Packet &a)
Definition: GenericPacketMath.h:1232
typename add_const_on_value_type< T >::type add_const_on_value_type_t
Definition: Meta.h:274
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
Definition: MathFunctions.h:926
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
auto run(Kernel kernel, Args &&... args) -> decltype(kernel(args...))
Definition: gpu_test_helper.h:414
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
double Bi
Biot number.
Definition: thermal_fibre.cc:85
Definition: Eigen_Colamd.h:49
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: BlasUtil.h:459
std::conditional_t< bool(HasUsableDirectAccess), ExtractType, typename ExtractType_::PlainObject > DirectLinearAccessType
Definition: BlasUtil.h:475
Definition: ConjHelper.h:71
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ResultType pmadd(const LhsType &x, const RhsType &y, const ResultType &c) const
Definition: ConjHelper.h:74
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ResultType pmul(const LhsType &x, const RhsType &y) const
Definition: ConjHelper.h:79
Definition: GeneralProduct.h:228
Definition: GenericPacketMath.h:108
Definition: SelfadjointMatrixVector.h:34
static EIGEN_DONT_INLINE EIGEN_DEVICE_FUNC void run(Index size, const Scalar *lhs, Index lhsStride, const Scalar *rhs, Scalar *res, Scalar alpha)
Definition: SelfadjointMatrixVector.h:42
static void run(Dest &dest, const Lhs &a_lhs, const Rhs &a_rhs, const Scalar &alpha)
Definition: SelfadjointMatrixVector.h:236
Product< Lhs, Rhs >::Scalar Scalar
Definition: SelfadjointMatrixVector.h:232
static EIGEN_DEVICE_FUNC void run(Dest &dest, const Lhs &a_lhs, const Rhs &a_rhs, const Scalar &alpha)
Definition: SelfadjointMatrixVector.h:168
Product< Lhs, Rhs >::Scalar Scalar
Definition: SelfadjointMatrixVector.h:155
LhsBlasTraits::DirectLinearAccessType ActualLhsType
Definition: SelfadjointMatrixVector.h:158
internal::blas_traits< Lhs > LhsBlasTraits
Definition: SelfadjointMatrixVector.h:157
RhsBlasTraits::DirectLinearAccessType ActualRhsType
Definition: SelfadjointMatrixVector.h:162
internal::remove_all_t< ActualRhsType > ActualRhsTypeCleaned
Definition: SelfadjointMatrixVector.h:163
internal::remove_all_t< ActualLhsType > ActualLhsTypeCleaned
Definition: SelfadjointMatrixVector.h:159
internal::blas_traits< Rhs > RhsBlasTraits
Definition: SelfadjointMatrixVector.h:161
Definition: ProductEvaluators.h:768
Definition: ForwardDeclarations.h:21
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
Definition: ZVector/PacketMath.h:50