Eigen  5.0.1
 
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 typedef Matrix<EIGTYPE, Dynamic, 1, ColMajor> GEMVVector; \
96 \
97 static void run(Index rows, Index cols, const EIGTYPE* lhs, Index lhsStride, const EIGTYPE* rhs, Index rhsIncr, \
98 EIGTYPE* res, Index resIncr, EIGTYPE alpha) { \
99 if (rows == 0 || cols == 0) return; \
100 BlasIndex m = convert_index<BlasIndex>(rows), n = convert_index<BlasIndex>(cols), \
101 lda = convert_index<BlasIndex>(lhsStride), incx = convert_index<BlasIndex>(rhsIncr), \
102 incy = convert_index<BlasIndex>(resIncr); \
103 const EIGTYPE beta(1); \
104 const EIGTYPE* x_ptr; \
105 char trans = (LhsStorageOrder == ColMajor) ? 'N' : (ConjugateLhs) ? 'C' : 'T'; \
106 EIGEN_IF_CONSTEXPR (LhsStorageOrder == RowMajor) { \
107 m = convert_index<BlasIndex>(cols); \
108 n = convert_index<BlasIndex>(rows); \
109 } \
110 GEMVVector x_tmp; \
111 EIGEN_IF_CONSTEXPR (ConjugateRhs) { \
112 Map<const GEMVVector, 0, InnerStride<> > map_x(rhs, cols, 1, InnerStride<>(incx)); \
113 x_tmp = map_x.conjugate(); \
114 x_ptr = x_tmp.data(); \
115 incx = 1; \
116 } else { \
117 x_ptr = rhs; \
118 } \
119 BLASFUNC(&trans, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)lhs, &lda, \
120 (const BLASTYPE*)x_ptr, &incx, (const BLASTYPE*)&numext::real_ref(beta), (BLASTYPE*)res, &incy); \
121 } \
122 };
123
124#ifdef EIGEN_USE_MKL
125EIGEN_BLAS_GEMV_SPECIALIZATION(double, double, dgemv)
126EIGEN_BLAS_GEMV_SPECIALIZATION(float, float, sgemv)
127EIGEN_BLAS_GEMV_SPECIALIZATION(dcomplex, MKL_Complex16, zgemv)
128EIGEN_BLAS_GEMV_SPECIALIZATION(scomplex, MKL_Complex8, cgemv)
129#else
130EIGEN_BLAS_GEMV_SPECIALIZATION(double, double, EIGEN_BLAS_SYM(dgemv))
131EIGEN_BLAS_GEMV_SPECIALIZATION(float, float, EIGEN_BLAS_SYM(sgemv))
132EIGEN_BLAS_GEMV_SPECIALIZATION(dcomplex, double, EIGEN_BLAS_SYM(zgemv))
133EIGEN_BLAS_GEMV_SPECIALIZATION(scomplex, float, EIGEN_BLAS_SYM(cgemv))
134#endif
135
136#undef EIGEN_BLAS_GEMV_SPECIALIZE
137#undef EIGEN_BLAS_GEMV_SPECIALIZATION
138} // namespace internal
139
140} // end namespace Eigen
141
142#endif // EIGEN_GENERAL_MATRIX_VECTOR_BLAS_H