Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
GeneralMatrixVector_BLAS.h
1/*
2 Copyright (c) 2011, Intel Corporation. All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this
8 list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright notice,
10 this list of conditions and the following disclaimer in the documentation
11 and/or other materials provided with the distribution.
12 * Neither the name of Intel Corporation nor the names of its contributors may
13 be used to endorse or promote products derived from this software without
14 specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 ********************************************************************************
28 * Content : Eigen bindings to BLAS F77
29 * General matrix-vector product functionality based on ?GEMV.
30 ********************************************************************************
31*/
32// SPDX-License-Identifier: BSD-3-Clause
33
34#ifndef EIGEN_GENERAL_MATRIX_VECTOR_BLAS_H
35#define EIGEN_GENERAL_MATRIX_VECTOR_BLAS_H
36
37// IWYU pragma: private
38#include "../InternalHeaderCheck.h"
39
40namespace Eigen {
41
42namespace internal {
43
44/**********************************************************************
45 * This file implements general matrix-vector multiplication using BLAS
46 * gemv function via partial specialization of
47 * general_matrix_vector_product::run(..) method for float, double,
48 * std::complex<float> and std::complex<double> types
49 **********************************************************************/
50
51// gemv specialization
52
53template <typename Index, typename LhsScalar, int StorageOrder, bool ConjugateLhs, typename RhsScalar,
54 bool ConjugateRhs>
55struct general_matrix_vector_product_gemv;
56
57#define EIGEN_BLAS_GEMV_SPECIALIZE(Scalar) \
58 template <typename Index, bool ConjugateLhs, bool ConjugateRhs> \
59 struct general_matrix_vector_product<Index, Scalar, const_blas_data_mapper<Scalar, Index, ColMajor>, ColMajor, \
60 ConjugateLhs, Scalar, const_blas_data_mapper<Scalar, Index, RowMajor>, \
61 ConjugateRhs, Specialized> { \
62 static void run(Index rows, Index cols, const const_blas_data_mapper<Scalar, Index, ColMajor>& lhs, \
63 const const_blas_data_mapper<Scalar, Index, RowMajor>& rhs, Scalar* res, Index resIncr, \
64 Scalar alpha) { \
65 EIGEN_IF_CONSTEXPR (ConjugateLhs) { \
66 general_matrix_vector_product<Index, Scalar, const_blas_data_mapper<Scalar, Index, ColMajor>, ColMajor, \
67 ConjugateLhs, Scalar, const_blas_data_mapper<Scalar, Index, RowMajor>, \
68 ConjugateRhs, BuiltIn>::run(rows, cols, lhs, rhs, res, resIncr, alpha); \
69 } else { \
70 general_matrix_vector_product_gemv<Index, Scalar, ColMajor, ConjugateLhs, Scalar, ConjugateRhs>::run( \
71 rows, cols, lhs.data(), lhs.stride(), rhs.data(), rhs.stride(), res, resIncr, alpha); \
72 } \
73 } \
74 }; \
75 template <typename Index, bool ConjugateLhs, bool ConjugateRhs> \
76 struct general_matrix_vector_product<Index, Scalar, const_blas_data_mapper<Scalar, Index, RowMajor>, RowMajor, \
77 ConjugateLhs, Scalar, const_blas_data_mapper<Scalar, Index, ColMajor>, \
78 ConjugateRhs, Specialized> { \
79 static void run(Index rows, Index cols, const const_blas_data_mapper<Scalar, Index, RowMajor>& lhs, \
80 const const_blas_data_mapper<Scalar, Index, ColMajor>& rhs, Scalar* res, Index resIncr, \
81 Scalar alpha) { \
82 general_matrix_vector_product_gemv<Index, Scalar, RowMajor, ConjugateLhs, Scalar, ConjugateRhs>::run( \
83 rows, cols, lhs.data(), lhs.stride(), rhs.data(), rhs.stride(), res, resIncr, alpha); \
84 } \
85 };
86
87EIGEN_BLAS_GEMV_SPECIALIZE(double)
88EIGEN_BLAS_GEMV_SPECIALIZE(float)
89EIGEN_BLAS_GEMV_SPECIALIZE(dcomplex)
90EIGEN_BLAS_GEMV_SPECIALIZE(scomplex)
91
92#define EIGEN_BLAS_GEMV_SPECIALIZATION(EIGTYPE, BLASTYPE, BLASFUNC) \
93 template <typename Index, int LhsStorageOrder, bool ConjugateLhs, bool ConjugateRhs> \
94 struct general_matrix_vector_product_gemv<Index, EIGTYPE, LhsStorageOrder, ConjugateLhs, EIGTYPE, ConjugateRhs> { \
95 using Scalar = EIGTYPE; \
96 using GEMVVector = Matrix<EIGTYPE, Dynamic, 1, ColMajor>; \
97 \
98 static void run(Index rows, Index cols, const EIGTYPE* lhs, Index lhsStride, const EIGTYPE* rhs, Index rhsIncr, \
99 Scalar* res, Index resIncr, EIGTYPE alpha) { \
100 if (rows == 0 || cols == 0 || numext::is_exactly_zero(alpha)) return; \
101 if (!blas_compatible_stride<LhsStorageOrder>(rows, cols, lhsStride) || (cols > 1 && rhsIncr == 0) || \
102 (rows > 1 && resIncr == 0)) { \
103 using LhsMapper = const_blas_data_mapper<EIGTYPE, Index, LhsStorageOrder>; \
104 using RhsMapper = const_blas_data_mapper<EIGTYPE, Index, LhsStorageOrder == ColMajor ? RowMajor : ColMajor>; \
105 const GEMVVector rhs_copy = Map<const GEMVVector, 0, InnerStride<>>(rhs, cols, InnerStride<>(rhsIncr)); \
106 GEMVVector result = GEMVVector::Zero(rows); \
107 general_matrix_vector_product<Index, EIGTYPE, LhsMapper, LhsStorageOrder, ConjugateLhs, EIGTYPE, RhsMapper, \
108 ConjugateRhs, BuiltIn>::run(rows, cols, LhsMapper(lhs, lhsStride), \
109 RhsMapper(rhs_copy.data(), Index(1)), result.data(), \
110 Index(1), alpha); \
111 Map<GEMVVector, 0, InnerStride<>>(res, rows, InnerStride<>(resIncr)) += result; \
112 return; \
113 } \
114 BlasIndex m = convert_index<BlasIndex>(rows), n = convert_index<BlasIndex>(cols); \
115 const EIGTYPE beta(1); \
116 const EIGTYPE* x_ptr; \
117 char trans = (LhsStorageOrder == ColMajor) ? 'N' : (ConjugateLhs) ? 'C' : 'T'; \
118 EIGEN_IF_CONSTEXPR (LhsStorageOrder == RowMajor) { \
119 m = convert_index<BlasIndex>(cols); \
120 n = convert_index<BlasIndex>(rows); \
121 } \
122 const BlasIndex lda = blas_leading_dim<LhsStorageOrder>(rows, cols, lhsStride); \
123 BlasIndex incx = blas_vector_increment(cols, rhsIncr); \
124 const BlasIndex incy = blas_vector_increment(rows, resIncr); \
125 GEMVVector x_tmp; \
126 EIGEN_IF_CONSTEXPR (ConjugateRhs) { \
127 Map<const GEMVVector, 0, InnerStride<>> map_x(rhs, cols, 1, InnerStride<>(incx)); \
128 x_tmp = map_x.conjugate(); \
129 x_ptr = x_tmp.data(); \
130 incx = 1; \
131 } else { \
132 /* BLAS reads a negative increment from the lowest address, Eigen from entry 0. */ \
133 x_ptr = rhsIncr < 0 ? rhs + (cols - 1) * rhsIncr : rhs; \
134 } \
135 auto* y_ptr = resIncr < 0 ? res + (rows - 1) * resIncr : res; \
136 BLASFUNC(&trans, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)lhs, &lda, \
137 (const BLASTYPE*)x_ptr, &incx, (const BLASTYPE*)&numext::real_ref(beta), (BLASTYPE*)y_ptr, &incy); \
138 } \
139 };
140
141#ifdef EIGEN_USE_MKL
142EIGEN_BLAS_GEMV_SPECIALIZATION(double, double, dgemv)
143EIGEN_BLAS_GEMV_SPECIALIZATION(float, float, sgemv)
144EIGEN_BLAS_GEMV_SPECIALIZATION(dcomplex, MKL_Complex16, zgemv)
145EIGEN_BLAS_GEMV_SPECIALIZATION(scomplex, MKL_Complex8, cgemv)
146#else
147EIGEN_BLAS_GEMV_SPECIALIZATION(double, double, EIGEN_BLAS_SYM(dgemv))
148EIGEN_BLAS_GEMV_SPECIALIZATION(float, float, EIGEN_BLAS_SYM(sgemv))
149EIGEN_BLAS_GEMV_SPECIALIZATION(dcomplex, double, EIGEN_BLAS_SYM(zgemv))
150EIGEN_BLAS_GEMV_SPECIALIZATION(scomplex, float, EIGEN_BLAS_SYM(cgemv))
151#endif
152
153#undef EIGEN_BLAS_GEMV_SPECIALIZE
154#undef EIGEN_BLAS_GEMV_SPECIALIZATION
155} // namespace internal
156
157} // end namespace Eigen
158
159#endif // EIGEN_GENERAL_MATRIX_VECTOR_BLAS_H