$darkmode
Eigen  5.0.1-dev
GeneralMatrixMatrix_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-matrix product functionality based on ?GEMM.
30  ********************************************************************************
31 */
32 
33 #ifndef EIGEN_GENERAL_MATRIX_MATRIX_BLAS_H
34 #define EIGEN_GENERAL_MATRIX_MATRIX_BLAS_H
35 
36 // IWYU pragma: private
37 #include "../InternalHeaderCheck.h"
38 
39 namespace Eigen {
40 
41 namespace internal {
42 
43 /**********************************************************************
44  * This file implements general matrix-matrix multiplication using BLAS
45  * gemm function via partial specialization of
46  * general_matrix_matrix_product::run(..) method for float, double,
47  * std::complex<float> and std::complex<double> types
48  **********************************************************************/
49 
50 // gemm specialization
51 
52 #define GEMM_SPECIALIZATION(EIGTYPE, EIGPREFIX, BLASTYPE, BLASFUNC) \
53  template <typename Index, int LhsStorageOrder, bool ConjugateLhs, int RhsStorageOrder, bool ConjugateRhs> \
54  struct general_matrix_matrix_product<Index, EIGTYPE, LhsStorageOrder, ConjugateLhs, EIGTYPE, RhsStorageOrder, \
55  ConjugateRhs, ColMajor, 1> { \
56  typedef gebp_traits<EIGTYPE, EIGTYPE> Traits; \
57  \
58  static void run(Index rows, Index cols, Index depth, const EIGTYPE* lhs_, Index lhsStride, const EIGTYPE* rhs_, \
59  Index rhsStride, EIGTYPE* res, Index resIncr, Index resStride, EIGTYPE alpha, \
60  level3_blocking<EIGTYPE, EIGTYPE>& /*blocking*/, GemmParallelInfo<Index>* /*info = 0*/) { \
61  using std::conj; \
62  if (rows == 0 || cols == 0 || depth == 0) return; \
63  EIGEN_ONLY_USED_FOR_DEBUG(resIncr); \
64  eigen_assert(resIncr == 1); \
65  char transa, transb; \
66  BlasIndex m, n, k, lda, ldb, ldc; \
67  const EIGTYPE *a, *b; \
68  EIGTYPE beta(1); \
69  MatrixX##EIGPREFIX a_tmp, b_tmp; \
70  \
71  /* Set transpose options */ \
72  transa = (LhsStorageOrder == RowMajor) ? ((ConjugateLhs) ? 'C' : 'T') : 'N'; \
73  transb = (RhsStorageOrder == RowMajor) ? ((ConjugateRhs) ? 'C' : 'T') : 'N'; \
74  \
75  /* Set m, n, k */ \
76  m = convert_index<BlasIndex>(rows); \
77  n = convert_index<BlasIndex>(cols); \
78  k = convert_index<BlasIndex>(depth); \
79  \
80  /* Set lda, ldb, ldc */ \
81  lda = convert_index<BlasIndex>(lhsStride); \
82  ldb = convert_index<BlasIndex>(rhsStride); \
83  ldc = convert_index<BlasIndex>(resStride); \
84  \
85  /* Set a, b, c */ \
86  if ((LhsStorageOrder == ColMajor) && (ConjugateLhs)) { \
87  Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > lhs(lhs_, m, k, OuterStride<>(lhsStride)); \
88  a_tmp = lhs.conjugate(); \
89  a = a_tmp.data(); \
90  lda = convert_index<BlasIndex>(a_tmp.outerStride()); \
91  } else \
92  a = lhs_; \
93  \
94  if ((RhsStorageOrder == ColMajor) && (ConjugateRhs)) { \
95  Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > rhs(rhs_, k, n, OuterStride<>(rhsStride)); \
96  b_tmp = rhs.conjugate(); \
97  b = b_tmp.data(); \
98  ldb = convert_index<BlasIndex>(b_tmp.outerStride()); \
99  } else \
100  b = rhs_; \
101  \
102  BLASFUNC(&transa, &transb, &m, &n, &k, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, &lda, \
103  (const BLASTYPE*)b, &ldb, (const BLASTYPE*)&numext::real_ref(beta), (BLASTYPE*)res, &ldc); \
104  } \
105  };
106 
107 #ifdef EIGEN_USE_MKL
108 GEMM_SPECIALIZATION(double, d, double, dgemm)
109 GEMM_SPECIALIZATION(float, f, float, sgemm)
110 GEMM_SPECIALIZATION(dcomplex, cd, MKL_Complex16, zgemm)
111 GEMM_SPECIALIZATION(scomplex, cf, MKL_Complex8, cgemm)
112 #else
113 GEMM_SPECIALIZATION(double, d, double, dgemm_)
114 GEMM_SPECIALIZATION(float, f, float, sgemm_)
115 GEMM_SPECIALIZATION(dcomplex, cd, double, zgemm_)
116 GEMM_SPECIALIZATION(scomplex, cf, float, cgemm_)
117 #endif
118 
119 // If OpenBLAS with BUILD_BFLOAT16=1 support is available,
120 // use sbgemm for bfloat16.
121 #if EIGEN_USE_OPENBLAS_BFLOAT16
122 
123 extern "C" {
124 // OpenBLAS prototype.
125 void sbgemm_(const char* trans_a, const char* trans_b, const int* M, const int* N, const int* K, const float* alpha,
126  const Eigen::bfloat16* A, const int* lda, const Eigen::bfloat16* B, const int* ldb, const float* beta,
127  float* C, const int* ldc);
128 } // extern "C"
129 
130 template <typename Index, int LhsStorageOrder, bool ConjugateLhs, int RhsStorageOrder, bool ConjugateRhs>
131 struct general_matrix_matrix_product<Index, Eigen::bfloat16, LhsStorageOrder, ConjugateLhs, Eigen::bfloat16,
132  RhsStorageOrder, ConjugateRhs, ColMajor, 1> {
133  typedef gebp_traits<Eigen::bfloat16, Eigen::bfloat16> Traits;
134 
135  static void run(Index rows, Index cols, Index depth, const Eigen::bfloat16* lhs_, Index lhsStride,
136  const Eigen::bfloat16* rhs_, Index rhsStride, Eigen::bfloat16* res, Index resIncr, Index resStride,
137  Eigen::bfloat16 alpha, level3_blocking<Eigen::bfloat16, Eigen::bfloat16>& /*blocking*/,
138  GemmParallelInfo<Index>* /*info = 0*/) {
139  using std::conj;
140  if (rows == 0 || cols == 0 || depth == 0) return;
141  EIGEN_ONLY_USED_FOR_DEBUG(resIncr);
142  eigen_assert(resIncr == 1);
143  char transa, transb;
144  BlasIndex m, n, k, lda, ldb, ldc;
145  const Eigen::bfloat16 *a, *b;
146 
147  float falpha = static_cast<float>(alpha);
148  float fbeta = float(1.0);
149 
150  using MatrixXbf16 = Matrix<Eigen::bfloat16, Dynamic, Dynamic>;
151  MatrixXbf16 a_tmp, b_tmp;
152  MatrixXf r_tmp;
153 
154  /* Set transpose options */
155  transa = (LhsStorageOrder == RowMajor) ? ((ConjugateLhs) ? 'C' : 'T') : 'N';
156  transb = (RhsStorageOrder == RowMajor) ? ((ConjugateRhs) ? 'C' : 'T') : 'N';
157 
158  /* Set m, n, k */
159  m = convert_index<BlasIndex>(rows);
160  n = convert_index<BlasIndex>(cols);
161  k = convert_index<BlasIndex>(depth);
162 
163  /* Set lda, ldb, ldc */
164  lda = convert_index<BlasIndex>(lhsStride);
165  ldb = convert_index<BlasIndex>(rhsStride);
166  ldc = convert_index<BlasIndex>(m);
167 
168  /* Set a, b, c */
169  if ((LhsStorageOrder == ColMajor) && (ConjugateLhs)) {
170  Map<const MatrixXbf16, 0, OuterStride<> > lhs(lhs_, m, k, OuterStride<>(lhsStride));
171  a_tmp = lhs.conjugate();
172  a = a_tmp.data();
173  lda = convert_index<BlasIndex>(a_tmp.outerStride());
174  } else {
175  a = lhs_;
176  }
177 
178  if ((RhsStorageOrder == ColMajor) && (ConjugateRhs)) {
179  Map<const MatrixXbf16, 0, OuterStride<> > rhs(rhs_, k, n, OuterStride<>(rhsStride));
180  b_tmp = rhs.conjugate();
181  b = b_tmp.data();
182  ldb = convert_index<BlasIndex>(b_tmp.outerStride());
183  } else {
184  b = rhs_;
185  }
186 
187  // Evaluate to a temporary intermediate array.
188  r_tmp.resize(m, n);
189 
190  sbgemm_(&transa, &transb, &m, &n, &k, (const float*)&numext::real_ref(falpha), a, &lda, b, &ldb,
191  (const float*)&numext::real_ref(fbeta), r_tmp.data(), &ldc);
192 
193  // Cast to the output.
194  Map<MatrixXbf16, 0, OuterStride<> > result(res, m, n, OuterStride<>(resStride));
195  result = r_tmp.cast<Eigen::bfloat16>();
196  }
197 };
198 
199 #endif // EIGEN_USE_OPENBLAS_SBGEMM
200 
201 } // namespace internal
202 
203 } // end namespace Eigen
204 
205 #endif // EIGEN_GENERAL_MATRIX_MATRIX_BLAS_H
Definition: Constants.h:318
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
Matrix< float, Dynamic, Dynamic > MatrixXf
Dynamic×Dynamic matrix of type float.
Definition: Matrix.h:478
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Definition: Constants.h:320