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