GeneralMatrixMatrix_BLAS.h
Go to the documentation of this file.
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 #include "../InternalHeaderCheck.h"
37 
38 namespace Eigen {
39 
40 namespace internal {
41 
42 
49 // gemm specialization
50 
51 #define GEMM_SPECIALIZATION(EIGTYPE, EIGPREFIX, BLASTYPE, BLASFUNC) \
52 template< \
53  typename Index, \
54  int LhsStorageOrder, bool ConjugateLhs, \
55  int RhsStorageOrder, bool ConjugateRhs> \
56 struct general_matrix_matrix_product<Index,EIGTYPE,LhsStorageOrder,ConjugateLhs,EIGTYPE,RhsStorageOrder,ConjugateRhs,ColMajor,1> \
57 { \
58 typedef gebp_traits<EIGTYPE,EIGTYPE> Traits; \
59 \
60 static void run(Index rows, Index cols, Index depth, \
61  const EIGTYPE* _lhs, Index lhsStride, \
62  const EIGTYPE* _rhs, Index rhsStride, \
63  EIGTYPE* res, Index resIncr, Index resStride, \
64  EIGTYPE alpha, \
65  level3_blocking<EIGTYPE, EIGTYPE>& /*blocking*/, \
66  GemmParallelInfo<Index>* /*info = 0*/) \
67 { \
68  using std::conj; \
69 \
70  EIGEN_ONLY_USED_FOR_DEBUG(resIncr); \
71  eigen_assert(resIncr == 1); \
72  char transa, transb; \
73  BlasIndex m, n, k, lda, ldb, ldc; \
74  const EIGTYPE *a, *b; \
75  EIGTYPE beta(1); \
76  MatrixX##EIGPREFIX a_tmp, b_tmp; \
77 \
78 /* Set transpose options */ \
79  transa = (LhsStorageOrder==RowMajor) ? ((ConjugateLhs) ? 'C' : 'T') : 'N'; \
80  transb = (RhsStorageOrder==RowMajor) ? ((ConjugateRhs) ? 'C' : 'T') : 'N'; \
81 \
82 /* Set m, n, k */ \
83  m = convert_index<BlasIndex>(rows); \
84  n = convert_index<BlasIndex>(cols); \
85  k = convert_index<BlasIndex>(depth); \
86 \
87 /* Set lda, ldb, ldc */ \
88  lda = convert_index<BlasIndex>(lhsStride); \
89  ldb = convert_index<BlasIndex>(rhsStride); \
90  ldc = convert_index<BlasIndex>(resStride); \
91 \
92 /* Set a, b, c */ \
93  if ((LhsStorageOrder==ColMajor) && (ConjugateLhs)) { \
94  Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > lhs(_lhs,m,k,OuterStride<>(lhsStride)); \
95  a_tmp = lhs.conjugate(); \
96  a = a_tmp.data(); \
97  lda = convert_index<BlasIndex>(a_tmp.outerStride()); \
98  } else a = _lhs; \
99 \
100  if ((RhsStorageOrder==ColMajor) && (ConjugateRhs)) { \
101  Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > rhs(_rhs,k,n,OuterStride<>(rhsStride)); \
102  b_tmp = rhs.conjugate(); \
103  b = b_tmp.data(); \
104  ldb = convert_index<BlasIndex>(b_tmp.outerStride()); \
105  } else b = _rhs; \
106 \
107  BLASFUNC(&transa, &transb, &m, &n, &k, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (const BLASTYPE*)b, &ldb, (const BLASTYPE*)&numext::real_ref(beta), (BLASTYPE*)res, &ldc); \
108 }};
109 
110 #ifdef EIGEN_USE_MKL
111 GEMM_SPECIALIZATION(double, d, double, dgemm)
112 GEMM_SPECIALIZATION(float, f, float, sgemm)
113 GEMM_SPECIALIZATION(dcomplex, cd, MKL_Complex16, zgemm)
114 GEMM_SPECIALIZATION(scomplex, cf, MKL_Complex8, cgemm)
115 #else
116 GEMM_SPECIALIZATION(double, d, double, dgemm_)
117 GEMM_SPECIALIZATION(float, f, float, sgemm_)
118 GEMM_SPECIALIZATION(dcomplex, cd, double, zgemm_)
119 GEMM_SPECIALIZATION(scomplex, cf, float, cgemm_)
120 #endif
121 
122 } // end namespase internal
123 
124 } // end namespace Eigen
125 
126 #endif // EIGEN_GENERAL_MATRIX_MATRIX_BLAS_H
#define GEMM_SPECIALIZATION(EIGTYPE, EIGPREFIX, BLASTYPE, BLASFUNC)
int BLASFUNC() sgemm(const char *, const char *, const int *, const int *, const int *, const float *, const float *, const int *, const float *, const int *, const float *, float *, const int *)
int BLASFUNC() cgemm(const char *, const char *, const int *, const int *, const int *, const float *, const float *, const int *, const float *, const int *, const float *, float *, const int *)
int BLASFUNC() zgemm(const char *, const char *, const int *, const int *, const int *, const double *, const double *, const int *, const double *, const int *, const double *, double *, const int *)
int BLASFUNC() dgemm(const char *, const char *, const int *, const int *, const int *, const double *, const double *, const int *, const double *, const int *, const double *, double *, const int *)
: InteropHeaders
Definition: Core:139
std::complex< double > dcomplex
Definition: MKL_support.h:127
std::complex< float > scomplex
Definition: MKL_support.h:128