34#ifndef EIGEN_TRIANGULAR_MATRIX_VECTOR_BLAS_H
35#define EIGEN_TRIANGULAR_MATRIX_VECTOR_BLAS_H
38#include "../InternalHeaderCheck.h"
50template <
typename Index,
int Mode,
typename LhsScalar,
bool ConjLhs,
typename RhsScalar,
bool ConjRhs,
52struct triangular_matrix_vector_product_trmv
53 : triangular_matrix_vector_product<Index, Mode, LhsScalar, ConjLhs, RhsScalar, ConjRhs, StorageOrder, BuiltIn> {};
55template <
typename Index,
int Mode,
typename Scalar,
bool ConjLhs,
bool ConjRhs,
int Order>
56void trmv_builtin(Index rows, Index cols,
const Scalar* lhs, Index lhsStride,
const Scalar* rhs, Index rhsIncr,
57 Scalar* res, Index resIncr, Scalar alpha) {
58 using Vector = Matrix<Scalar, Dynamic, 1>;
61 Index output_stride = resIncr;
62 const Index size = (std::min)(rows, cols);
63 const Index output_size = (Mode &
Lower) ? rows : size;
65 EIGEN_IF_CONSTEXPR (Order ==
RowMajor) {
67 rhs_copy = Map<const Vector, 0, InnerStride<>>(rhs, (Mode &
Lower) ? size : cols, InnerStride<>(rhsIncr));
68 rhs = rhs_copy.data();
73 result = Vector::Zero(output_size);
74 output = result.data();
78 triangular_matrix_vector_product<Index, Mode, Scalar, ConjLhs, Scalar, ConjRhs, Order, BuiltIn>::run(
79 rows, cols, lhs, lhsStride, rhs, rhsIncr, output, output_stride, alpha);
80 if (result.size() != 0) Map<Vector, 0, InnerStride<>>(res, output_size, InnerStride<>(resIncr)) += result;
83#define EIGEN_BLAS_TRMV_SPECIALIZE(Scalar) \
84 template <typename Index, int Mode, bool ConjLhs, bool ConjRhs> \
85 struct triangular_matrix_vector_product<Index, Mode, Scalar, ConjLhs, Scalar, ConjRhs, ColMajor, Specialized> { \
86 static void run(Index rows_, Index cols_, const Scalar* lhs_, Index lhsStride, const Scalar* rhs_, Index rhsIncr, \
87 Scalar* res_, Index resIncr, Scalar alpha) { \
88 triangular_matrix_vector_product_trmv<Index, Mode, Scalar, ConjLhs, Scalar, ConjRhs, ColMajor>::run( \
89 rows_, cols_, lhs_, lhsStride, rhs_, rhsIncr, res_, resIncr, alpha); \
92 template <typename Index, int Mode, bool ConjLhs, bool ConjRhs> \
93 struct triangular_matrix_vector_product<Index, Mode, Scalar, ConjLhs, Scalar, ConjRhs, RowMajor, Specialized> { \
94 static void run(Index rows_, Index cols_, const Scalar* lhs_, Index lhsStride, const Scalar* rhs_, Index rhsIncr, \
95 Scalar* res_, Index resIncr, Scalar alpha) { \
96 triangular_matrix_vector_product_trmv<Index, Mode, Scalar, ConjLhs, Scalar, ConjRhs, RowMajor>::run( \
97 rows_, cols_, lhs_, lhsStride, rhs_, rhsIncr, res_, resIncr, alpha); \
101EIGEN_BLAS_TRMV_SPECIALIZE(
double)
102EIGEN_BLAS_TRMV_SPECIALIZE(
float)
103EIGEN_BLAS_TRMV_SPECIALIZE(dcomplex)
104EIGEN_BLAS_TRMV_SPECIALIZE(scomplex)
107#define EIGEN_BLAS_TRMV_CM(EIGTYPE, BLASTYPE, EIGPREFIX, BLASPREFIX, BLASPOSTFIX) \
108 template <typename Index, int Mode, bool ConjLhs, bool ConjRhs> \
109 struct triangular_matrix_vector_product_trmv<Index, Mode, EIGTYPE, ConjLhs, EIGTYPE, ConjRhs, ColMajor> { \
110 using Scalar = EIGTYPE; \
112 IsLower = (Mode & Lower) == Lower, \
113 SetDiag = (Mode & (ZeroDiag | UnitDiag)) ? 0 : 1, \
114 IsUnitDiag = (Mode & UnitDiag) ? 1 : 0, \
115 IsZeroDiag = (Mode & ZeroDiag) ? 1 : 0, \
116 LowUp = IsLower ? Lower : Upper \
118 static void run(Index rows_, Index cols_, const EIGTYPE* lhs_, Index lhsStride, const EIGTYPE* rhs_, \
119 Index rhsIncr, Scalar* res_, Index resIncr, EIGTYPE alpha) { \
120 if (rows_ == 0 || cols_ == 0 || numext::is_exactly_zero(alpha)) return; \
121 if (!blas_compatible_stride<ColMajor>(rows_, cols_, lhsStride) || (rows_ > 1 && resIncr == 0)) { \
122 trmv_builtin<Index, Mode, EIGTYPE, ConjLhs, ConjRhs, ColMajor>(rows_, cols_, lhs_, lhsStride, rhs_, rhsIncr, \
123 res_, resIncr, alpha); \
126 EIGEN_IF_CONSTEXPR (ConjLhs || IsZeroDiag) { \
127 trmv_builtin<Index, Mode, EIGTYPE, ConjLhs, ConjRhs, ColMajor>(rows_, cols_, lhs_, lhsStride, rhs_, rhsIncr, \
128 res_, resIncr, alpha); \
131 Index size = (std::min)(rows_, cols_); \
132 Index rows = IsLower ? rows_ : size; \
133 Index cols = IsLower ? size : cols_; \
135 using VectorRhs = VectorX##EIGPREFIX; \
139 Map<const VectorRhs, 0, InnerStride<>> rhs(rhs_, cols, InnerStride<>(rhsIncr)); \
141 EIGEN_IF_CONSTEXPR (ConjRhs) \
142 x_tmp = rhs.conjugate(); \
149 char trans, uplo, diag; \
150 BlasIndex m, n, lda, incx, incy; \
155 n = convert_index<BlasIndex>(size); \
156 lda = blas_leading_dim<ColMajor>(rows, cols, lhsStride); \
158 incy = blas_vector_increment(rows, resIncr); \
162 uplo = IsLower ? 'L' : 'U'; \
163 diag = IsUnitDiag ? 'U' : 'N'; \
166 EIGEN_CAT(EIGEN_CAT(BLASPREFIX, trmv), BLASPOSTFIX) \
167 (&uplo, &trans, &diag, &n, (const BLASTYPE*)lhs_, &lda, (BLASTYPE*)x, &incx); \
171 EIGEN_CAT(EIGEN_CAT(BLASPREFIX, axpy), BLASPOSTFIX) \
172 (&n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)x, &incx, \
173 (BLASTYPE*)(resIncr < 0 ? res_ + (size - 1) * resIncr : res_), &incy); \
175 if (size < (std::max)(rows, cols)) { \
176 EIGEN_IF_CONSTEXPR (ConjRhs) \
177 x_tmp = rhs.conjugate(); \
182 y = res_ + size * resIncr; \
184 m = convert_index<BlasIndex>(rows - size); \
185 n = convert_index<BlasIndex>(size); \
189 a = lhs_ + size * lda; \
190 m = convert_index<BlasIndex>(size); \
191 n = convert_index<BlasIndex>(cols - size); \
193 if (resIncr < 0) y += (m - 1) * resIncr; \
194 EIGEN_CAT(EIGEN_CAT(BLASPREFIX, gemv), BLASPOSTFIX) \
195 (&trans, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (const BLASTYPE*)x, \
196 &incx, (const BLASTYPE*)&numext::real_ref(beta), (BLASTYPE*)y, &incy); \
202EIGEN_BLAS_TRMV_CM(
double,
double, d, d, )
203EIGEN_BLAS_TRMV_CM(dcomplex, MKL_Complex16, cd, z, )
204EIGEN_BLAS_TRMV_CM(
float,
float, f, s, )
205EIGEN_BLAS_TRMV_CM(scomplex, MKL_Complex8, cf, c, )
207EIGEN_BLAS_TRMV_CM(
double,
double, d, d, EIGEN_BLAS_POSTFIX)
208EIGEN_BLAS_TRMV_CM(dcomplex,
double, cd, z, EIGEN_BLAS_POSTFIX)
209EIGEN_BLAS_TRMV_CM(
float,
float, f, s, EIGEN_BLAS_POSTFIX)
210EIGEN_BLAS_TRMV_CM(scomplex,
float, cf, c, EIGEN_BLAS_POSTFIX)
214#define EIGEN_BLAS_TRMV_RM(EIGTYPE, BLASTYPE, EIGPREFIX, BLASPREFIX, BLASPOSTFIX) \
215 template <typename Index, int Mode, bool ConjLhs, bool ConjRhs> \
216 struct triangular_matrix_vector_product_trmv<Index, Mode, EIGTYPE, ConjLhs, EIGTYPE, ConjRhs, RowMajor> { \
217 using Scalar = EIGTYPE; \
219 IsLower = (Mode & Lower) == Lower, \
220 SetDiag = (Mode & (ZeroDiag | UnitDiag)) ? 0 : 1, \
221 IsUnitDiag = (Mode & UnitDiag) ? 1 : 0, \
222 IsZeroDiag = (Mode & ZeroDiag) ? 1 : 0, \
223 LowUp = IsLower ? Lower : Upper \
225 static void run(Index rows_, Index cols_, const EIGTYPE* lhs_, Index lhsStride, const EIGTYPE* rhs_, \
226 Index rhsIncr, Scalar* res_, Index resIncr, EIGTYPE alpha) { \
227 if (rows_ == 0 || cols_ == 0 || numext::is_exactly_zero(alpha)) return; \
228 if (!blas_compatible_stride<RowMajor>(rows_, cols_, lhsStride) || (rows_ > 1 && resIncr == 0)) { \
229 trmv_builtin<Index, Mode, EIGTYPE, ConjLhs, ConjRhs, RowMajor>(rows_, cols_, lhs_, lhsStride, rhs_, rhsIncr, \
230 res_, resIncr, alpha); \
233 EIGEN_IF_CONSTEXPR (IsZeroDiag) { \
234 trmv_builtin<Index, Mode, EIGTYPE, ConjLhs, ConjRhs, RowMajor>(rows_, cols_, lhs_, lhsStride, rhs_, rhsIncr, \
235 res_, resIncr, alpha); \
238 Index size = (std::min)(rows_, cols_); \
239 Index rows = IsLower ? rows_ : size; \
240 Index cols = IsLower ? size : cols_; \
242 using VectorRhs = VectorX##EIGPREFIX; \
246 Map<const VectorRhs, 0, InnerStride<>> rhs(rhs_, cols, InnerStride<>(rhsIncr)); \
248 EIGEN_IF_CONSTEXPR (ConjRhs) \
249 x_tmp = rhs.conjugate(); \
256 char trans, uplo, diag; \
257 BlasIndex m, n, lda, incx, incy; \
262 n = convert_index<BlasIndex>(size); \
263 lda = blas_leading_dim<RowMajor>(rows, cols, lhsStride); \
265 incy = blas_vector_increment(rows, resIncr); \
268 trans = ConjLhs ? 'C' : 'T'; \
269 uplo = IsLower ? 'U' : 'L'; \
270 diag = IsUnitDiag ? 'U' : 'N'; \
273 EIGEN_CAT(EIGEN_CAT(BLASPREFIX, trmv), BLASPOSTFIX) \
274 (&uplo, &trans, &diag, &n, (const BLASTYPE*)lhs_, &lda, (BLASTYPE*)x, &incx); \
278 EIGEN_CAT(EIGEN_CAT(BLASPREFIX, axpy), BLASPOSTFIX) \
279 (&n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)x, &incx, \
280 (BLASTYPE*)(resIncr < 0 ? res_ + (size - 1) * resIncr : res_), &incy); \
282 if (size < (std::max)(rows, cols)) { \
283 EIGEN_IF_CONSTEXPR (ConjRhs) \
284 x_tmp = rhs.conjugate(); \
289 y = res_ + size * resIncr; \
290 a = lhs_ + size * lda; \
291 m = convert_index<BlasIndex>(rows - size); \
292 n = convert_index<BlasIndex>(size); \
297 m = convert_index<BlasIndex>(size); \
298 n = convert_index<BlasIndex>(cols - size); \
300 if (resIncr < 0) y += (m - 1) * resIncr; \
301 EIGEN_CAT(EIGEN_CAT(BLASPREFIX, gemv), BLASPOSTFIX) \
302 (&trans, &n, &m, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (const BLASTYPE*)x, \
303 &incx, (const BLASTYPE*)&numext::real_ref(beta), (BLASTYPE*)y, &incy); \
309EIGEN_BLAS_TRMV_RM(
double,
double, d, d, )
310EIGEN_BLAS_TRMV_RM(dcomplex, MKL_Complex16, cd, z, )
311EIGEN_BLAS_TRMV_RM(
float,
float, f, s, )
312EIGEN_BLAS_TRMV_RM(scomplex, MKL_Complex8, cf, c, )
314EIGEN_BLAS_TRMV_RM(
double,
double, d, d, EIGEN_BLAS_POSTFIX)
315EIGEN_BLAS_TRMV_RM(dcomplex,
double, cd, z, EIGEN_BLAS_POSTFIX)
316EIGEN_BLAS_TRMV_RM(
float,
float, f, s, EIGEN_BLAS_POSTFIX)
317EIGEN_BLAS_TRMV_RM(scomplex,
float, cf, c, EIGEN_BLAS_POSTFIX)
320#undef EIGEN_BLAS_TRMV_RM
321#undef EIGEN_BLAS_TRMV_SPECIALIZE
322#undef EIGEN_BLAS_TRMV_CM
@ Lower
Definition Constants.h:212
@ RowMajor
Definition Constants.h:321
Matrix< Type, Size, 1 > Vector
SizeĆ1 vector of type Type.
Definition Matrix.h:532