11#ifndef EIGEN_SELFADJOINT_MATRIX_VECTOR_H
12#define EIGEN_SELFADJOINT_MATRIX_VECTOR_H
15#include "../InternalHeaderCheck.h"
29template <
typename Scalar,
typename Index,
int StorageOrder,
int UpLo,
bool ConjugateLhs,
bool ConjugateRhs,
30 int Version = Specialized>
31struct selfadjoint_matrix_vector_product;
33template <
typename Scalar,
typename Index,
int StorageOrder,
int UpLo,
bool ConjugateLhs,
bool ConjugateRhs,
35struct selfadjoint_matrix_vector_product
38 static EIGEN_DONT_INLINE EIGEN_DEVICE_FUNC
void run(Index size,
const Scalar* lhs, Index lhsStride,
const Scalar* rhs,
39 Scalar* res, Scalar alpha);
42template <
typename Scalar,
typename Index,
int StorageOrder,
int UpLo,
bool ConjugateLhs,
bool ConjugateRhs,
44EIGEN_DONT_INLINE EIGEN_DEVICE_FUNC
void
45selfadjoint_matrix_vector_product<Scalar, Index, StorageOrder, UpLo, ConjugateLhs, ConjugateRhs, Version>::run(
46 Index size,
const Scalar* lhs, Index lhsStride,
const Scalar* rhs, Scalar* res, Scalar alpha) {
47 using Packet =
typename packet_traits<Scalar>::type;
48 using RealScalar =
typename NumTraits<Scalar>::Real;
49 const Index PacketSize =
sizeof(Packet) /
sizeof(Scalar);
52 IsRowMajor = StorageOrder ==
RowMajor ? 1 : 0,
53 IsLower = UpLo ==
Lower ? 1 : 0,
54 FirstTriangular = IsRowMajor == IsLower
57 conj_helper<Scalar, Scalar, NumTraits<Scalar>::IsComplex && logical_xor(ConjugateLhs, IsRowMajor), ConjugateRhs> cj0;
58 conj_helper<Scalar, Scalar, NumTraits<Scalar>::IsComplex && logical_xor(ConjugateLhs, !IsRowMajor), ConjugateRhs> cj1;
59 conj_helper<RealScalar, Scalar, false, ConjugateRhs> cjd;
61 conj_helper<Packet, Packet, NumTraits<Scalar>::IsComplex && logical_xor(ConjugateLhs, IsRowMajor), ConjugateRhs> pcj0;
62 conj_helper<Packet, Packet, NumTraits<Scalar>::IsComplex && logical_xor(ConjugateLhs, !IsRowMajor), ConjugateRhs>
65 Scalar cjAlpha = ConjugateRhs ? numext::conj(alpha) : alpha;
69 Index n4 = (numext::maxi(Index(0), size - 8) / 4) * 4;
70 Index n2 = ((size - n4) / 2) * 2;
78 Index jStart = FirstTriangular ? (size - n4) : 0;
79 Index jEnd = FirstTriangular ? size : n4;
81 for (Index j = jStart; j < jEnd; j += 4) {
82 const Scalar* EIGEN_RESTRICT A0 = lhs + j * lhsStride;
83 const Scalar* EIGEN_RESTRICT A1 = lhs + (j + 1) * lhsStride;
84 const Scalar* EIGEN_RESTRICT A2 = lhs + (j + 2) * lhsStride;
85 const Scalar* EIGEN_RESTRICT A3 = lhs + (j + 3) * lhsStride;
87 Scalar t0 = cjAlpha * rhs[j];
88 Scalar t1 = cjAlpha * rhs[j + 1];
89 Scalar t2 = cjAlpha * rhs[j + 2];
90 Scalar t3 = cjAlpha * rhs[j + 3];
91 Packet ptmp0 = pset1<Packet>(t0);
92 Packet ptmp1 = pset1<Packet>(t1);
93 Packet ptmp2 = pset1<Packet>(t2);
94 Packet ptmp3 = pset1<Packet>(t3);
96 Scalar t4(0), t5(0), t6(0), t7(0);
97 Packet ptmp4 = pzero(Packet{});
98 Packet ptmp5 = pzero(Packet{});
99 Packet ptmp6 = pzero(Packet{});
100 Packet ptmp7 = pzero(Packet{});
102 Index starti = FirstTriangular ? 0 : j + 4;
103 Index endi = FirstTriangular ? j : size;
104 Index alignedStart = starti + internal::first_default_aligned(&res[starti], endi - starti);
105 Index alignedEnd = alignedStart + ((endi - alignedStart) / PacketSize) * PacketSize;
108 res[j] += cjd.pmul(numext::real(A0[j]), t0);
109 res[j + 1] += cjd.pmul(numext::real(A1[j + 1]), t1);
110 res[j + 2] += cjd.pmul(numext::real(A2[j + 2]), t2);
111 res[j + 3] += cjd.pmul(numext::real(A3[j + 3]), t3);
114 EIGEN_IF_CONSTEXPR (FirstTriangular) {
116 res[j] += cj0.pmul(A1[j], t1) + cj0.pmul(A2[j], t2) + cj0.pmul(A3[j], t3);
117 res[j + 1] += cj0.pmul(A2[j + 1], t2) + cj0.pmul(A3[j + 1], t3);
118 res[j + 2] += cj0.pmul(A3[j + 2], t3);
120 t5 += cj1.pmul(A1[j], rhs[j]);
121 t6 += cj1.pmul(A2[j], rhs[j]) + cj1.pmul(A2[j + 1], rhs[j + 1]);
122 t7 += cj1.pmul(A3[j], rhs[j]) + cj1.pmul(A3[j + 1], rhs[j + 1]) + cj1.pmul(A3[j + 2], rhs[j + 2]);
125 res[j + 1] += cj0.pmul(A0[j + 1], t0);
126 res[j + 2] += cj0.pmul(A0[j + 2], t0) + cj0.pmul(A1[j + 2], t1);
127 res[j + 3] += cj0.pmul(A0[j + 3], t0) + cj0.pmul(A1[j + 3], t1) + cj0.pmul(A2[j + 3], t2);
129 t4 += cj1.pmul(A0[j + 1], rhs[j + 1]) + cj1.pmul(A0[j + 2], rhs[j + 2]) + cj1.pmul(A0[j + 3], rhs[j + 3]);
130 t5 += cj1.pmul(A1[j + 2], rhs[j + 2]) + cj1.pmul(A1[j + 3], rhs[j + 3]);
131 t6 += cj1.pmul(A2[j + 3], rhs[j + 3]);
135 for (Index i = starti; i < alignedStart; ++i) {
136 res[i] += cj0.pmul(A0[i], t0) + cj0.pmul(A1[i], t1) + cj0.pmul(A2[i], t2) + cj0.pmul(A3[i], t3);
137 t4 += cj1.pmul(A0[i], rhs[i]);
138 t5 += cj1.pmul(A1[i], rhs[i]);
139 t6 += cj1.pmul(A2[i], rhs[i]);
140 t7 += cj1.pmul(A3[i], rhs[i]);
144 const Scalar* EIGEN_RESTRICT a0It = A0 + alignedStart;
145 const Scalar* EIGEN_RESTRICT a1It = A1 + alignedStart;
146 const Scalar* EIGEN_RESTRICT a2It = A2 + alignedStart;
147 const Scalar* EIGEN_RESTRICT a3It = A3 + alignedStart;
148 const Scalar* EIGEN_RESTRICT rhsIt = rhs + alignedStart;
149 Scalar* EIGEN_RESTRICT resIt = res + alignedStart;
150 for (Index i = alignedStart; i < alignedEnd; i += PacketSize) {
151 Packet A0i = ploadu<Packet>(a0It);
153 Packet A1i = ploadu<Packet>(a1It);
155 Packet A2i = ploadu<Packet>(a2It);
157 Packet A3i = ploadu<Packet>(a3It);
159 Packet Bi = ploadu<Packet>(rhsIt);
161 Packet Xi = pload<Packet>(resIt);
163 Xi = pcj0.pmadd(A0i, ptmp0, Xi);
164 Xi = pcj0.pmadd(A1i, ptmp1, Xi);
165 Xi = pcj0.pmadd(A2i, ptmp2, Xi);
166 Xi = pcj0.pmadd(A3i, ptmp3, Xi);
170 ptmp4 = pcj1.pmadd(A0i, Bi, ptmp4);
171 ptmp5 = pcj1.pmadd(A1i, Bi, ptmp5);
172 ptmp6 = pcj1.pmadd(A2i, Bi, ptmp6);
173 ptmp7 = pcj1.pmadd(A3i, Bi, ptmp7);
177 for (Index i = alignedEnd; i < endi; ++i) {
178 res[i] += cj0.pmul(A0[i], t0) + cj0.pmul(A1[i], t1) + cj0.pmul(A2[i], t2) + cj0.pmul(A3[i], t3);
179 t4 += cj1.pmul(A0[i], rhs[i]);
180 t5 += cj1.pmul(A1[i], rhs[i]);
181 t6 += cj1.pmul(A2[i], rhs[i]);
182 t7 += cj1.pmul(A3[i], rhs[i]);
185 res[j] += alpha * (t4 + predux(ptmp4));
186 res[j + 1] += alpha * (t5 + predux(ptmp5));
187 res[j + 2] += alpha * (t6 + predux(ptmp6));
188 res[j + 3] += alpha * (t7 + predux(ptmp7));
194 Index jStart = FirstTriangular ? (size - n4 - n2) : n4;
195 Index jEnd = FirstTriangular ? (size - n4) : (n4 + n2);
197 for (Index j = jStart; j < jEnd; j += 2) {
198 const Scalar* EIGEN_RESTRICT A0 = lhs + j * lhsStride;
199 const Scalar* EIGEN_RESTRICT A1 = lhs + (j + 1) * lhsStride;
201 Scalar t0 = cjAlpha * rhs[j];
202 Packet ptmp0 = pset1<Packet>(t0);
203 Scalar t1 = cjAlpha * rhs[j + 1];
204 Packet ptmp1 = pset1<Packet>(t1);
207 Packet ptmp2 = pzero(Packet{});
209 Packet ptmp3 = pzero(Packet{});
211 Index starti = FirstTriangular ? 0 : j + 2;
212 Index endi = FirstTriangular ? j : size;
213 Index alignedStart = starti + internal::first_default_aligned(&res[starti], endi - starti);
214 Index alignedEnd = alignedStart + ((endi - alignedStart) / PacketSize) * PacketSize;
216 res[j] += cjd.pmul(numext::real(A0[j]), t0);
217 res[j + 1] += cjd.pmul(numext::real(A1[j + 1]), t1);
218 EIGEN_IF_CONSTEXPR (FirstTriangular) {
219 res[j] += cj0.pmul(A1[j], t1);
220 t3 += cj1.pmul(A1[j], rhs[j]);
222 res[j + 1] += cj0.pmul(A0[j + 1], t0);
223 t2 += cj1.pmul(A0[j + 1], rhs[j + 1]);
226 for (Index i = starti; i < alignedStart; ++i) {
227 res[i] += cj0.pmul(A0[i], t0) + cj0.pmul(A1[i], t1);
228 t2 += cj1.pmul(A0[i], rhs[i]);
229 t3 += cj1.pmul(A1[i], rhs[i]);
231 const Scalar* EIGEN_RESTRICT a0It = A0 + alignedStart;
232 const Scalar* EIGEN_RESTRICT a1It = A1 + alignedStart;
233 const Scalar* EIGEN_RESTRICT rhsIt = rhs + alignedStart;
234 Scalar* EIGEN_RESTRICT resIt = res + alignedStart;
235 for (Index i = alignedStart; i < alignedEnd; i += PacketSize) {
236 Packet A0i = ploadu<Packet>(a0It);
238 Packet A1i = ploadu<Packet>(a1It);
240 Packet Bi = ploadu<Packet>(rhsIt);
242 Packet Xi = pload<Packet>(resIt);
244 Xi = pcj0.pmadd(A0i, ptmp0, pcj0.pmadd(A1i, ptmp1, Xi));
245 ptmp2 = pcj1.pmadd(A0i, Bi, ptmp2);
246 ptmp3 = pcj1.pmadd(A1i, Bi, ptmp3);
250 for (Index i = alignedEnd; i < endi; i++) {
251 res[i] += cj0.pmul(A0[i], t0) + cj0.pmul(A1[i], t1);
252 t2 += cj1.pmul(A0[i], rhs[i]);
253 t3 += cj1.pmul(A1[i], rhs[i]);
256 res[j] += alpha * (t2 + predux(ptmp2));
257 res[j + 1] += alpha * (t3 + predux(ptmp3));
263 Index jStart = FirstTriangular ? 0 : (n4 + n2);
264 Index jEnd = FirstTriangular ? (size - n4 - n2) : size;
266 for (Index j = jStart; j < jEnd; j++) {
267 const Scalar* EIGEN_RESTRICT A0 = lhs + j * lhsStride;
269 Scalar t1 = cjAlpha * rhs[j];
271 Packet ptmp1 = pset1<Packet>(t1);
272 Packet ptmp2 = pzero(Packet{});
274 res[j] += cjd.pmul(numext::real(A0[j]), t1);
276 Index starti = FirstTriangular ? 0 : j + 1;
277 Index endi = FirstTriangular ? j : size;
278 Index alignedStart = starti + internal::first_default_aligned(&res[starti], endi - starti);
279 Index alignedEnd = alignedStart + ((endi - alignedStart) / PacketSize) * PacketSize;
281 for (Index i = starti; i < alignedStart; ++i) {
282 res[i] += cj0.pmul(A0[i], t1);
283 t2 += cj1.pmul(A0[i], rhs[i]);
285 const Scalar* EIGEN_RESTRICT a0It = A0 + alignedStart;
286 const Scalar* EIGEN_RESTRICT rhsIt = rhs + alignedStart;
287 Scalar* EIGEN_RESTRICT resIt = res + alignedStart;
288 for (Index i = alignedStart; i < alignedEnd; i += PacketSize) {
289 Packet A0i = ploadu<Packet>(a0It);
291 Packet Bi = ploadu<Packet>(rhsIt);
293 Packet Xi = pload<Packet>(resIt);
295 Xi = pcj0.pmadd(A0i, ptmp1, Xi);
299 ptmp2 = pcj1.pmadd(A0i, Bi, ptmp2);
301 for (Index i = alignedEnd; i < endi; i++) {
302 res[i] += cj0.pmul(A0[i], t1);
303 t2 += cj1.pmul(A0[i], rhs[i]);
305 res[j] += alpha * (t2 + predux(ptmp2));
318template <
typename Lhs,
int LhsMode,
typename Rhs>
319struct selfadjoint_product_impl<Lhs, LhsMode, false, Rhs, 0, true> {
320 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
322 using LhsBlasTraits = internal::blas_traits<Lhs>;
323 using ActualLhsType =
typename LhsBlasTraits::DirectLinearAccessType;
324 using ActualLhsTypeCleaned = internal::remove_all_t<ActualLhsType>;
326 using RhsBlasTraits = internal::blas_traits<Rhs>;
327 using ActualRhsType =
typename RhsBlasTraits::DirectLinearAccessType;
328 using ActualRhsTypeCleaned = internal::remove_all_t<ActualRhsType>;
335 static_assert(Rhs::ColsAtCompileTime == 1,
"The RHS must be a column vector.");
337 template <
typename Dest>
338 static EIGEN_DEVICE_FUNC
void run(Dest& dest,
const Lhs& a_lhs,
const Rhs& a_rhs,
const Scalar& alpha) {
339 using ResScalar =
typename Dest::Scalar;
340 using RhsScalar =
typename Rhs::Scalar;
342 eigen_assert(dest.rows() == a_lhs.rows() && dest.cols() == a_rhs.cols());
344 add_const_on_value_type_t<ActualLhsType> lhs = LhsBlasTraits::extract(a_lhs);
345 add_const_on_value_type_t<ActualRhsType> rhs = RhsBlasTraits::extract(a_rhs);
349 if (lhs.size() == 0)
return;
351 Scalar actualAlpha = combine_scalar_factors(alpha, a_lhs, a_rhs);
354 EvalToDest = (Dest::InnerStrideAtCompileTime == 1),
355 UseRhs = (ActualRhsTypeCleaned::InnerStrideAtCompileTime == 1)
358 internal::gemv_static_vector_if<ResScalar, Dest::SizeAtCompileTime, Dest::MaxSizeAtCompileTime, !EvalToDest>
360 internal::gemv_static_vector_if<RhsScalar, ActualRhsTypeCleaned::SizeAtCompileTime,
361 ActualRhsTypeCleaned::MaxSizeAtCompileTime, !UseRhs>
364 ei_declare_aligned_stack_constructed_variable(ResScalar, actualDestPtr, dest.size(),
365 EvalToDest ? dest.data() : static_dest.data());
367 ei_declare_aligned_stack_constructed_variable(RhsScalar, actualRhsPtr, rhs.size(),
368 UseRhs ?
const_cast<RhsScalar*
>(rhs.data()) : static_rhs.data());
370 internal::gemv_prepare_destination<EvalToDest>(dest, actualDestPtr);
371 internal::gemv_prepare_rhs<UseRhs>(rhs, actualRhsPtr);
373 internal::selfadjoint_matrix_vector_product<
375 int(LhsUpLo), bool(LhsBlasTraits::NeedToConjugate),
376 bool(RhsBlasTraits::NeedToConjugate)>::run(lhs.rows(),
377 &lhs.coeffRef(0, 0), lhs.outerStride(),
383 internal::gemv_copy_destination<EvalToDest>(dest, actualDestPtr);
387template <
typename Lhs,
typename Rhs,
int RhsMode>
388struct selfadjoint_product_impl<Lhs, 0, true, Rhs, RhsMode, false> {
389 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
392 template <
typename Dest>
393 static void run(Dest& dest,
const Lhs& a_lhs,
const Rhs& a_rhs,
const Scalar& alpha) {
395 Transpose<Dest> destT(dest);
396 selfadjoint_product_impl<Transpose<const Rhs>, int(RhsUpLo) ==
Upper ?
Lower :
Upper,
false, Transpose<const Lhs>,
397 0,
true>::run(destT, a_rhs.transpose(), a_lhs.transpose(), alpha);
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
constexpr unsigned int RowMajorBit
Definition Constants.h:71