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 Intel(R) MKL 29 * General matrix-vector product functionality based on ?GEMV. 30 ******************************************************************************** 31 */ 32 33 #ifndef EIGEN_GENERAL_MATRIX_VECTOR_MKL_H 34 #define EIGEN_GENERAL_MATRIX_VECTOR_MKL_H 35 36 namespace Eigen { 37 38 namespace internal { 39 40 /********************************************************************** 41 * This file implements general matrix-vector multiplication using BLAS 42 * gemv function via partial specialization of 43 * general_matrix_vector_product::run(..) method for float, double, 44 * std::complex<float> and std::complex<double> types 45 **********************************************************************/ 46 47 // gemv specialization 48 49 template<typename Index, typename LhsScalar, int LhsStorageOrder, bool ConjugateLhs, typename RhsScalar, bool ConjugateRhs> 50 struct general_matrix_vector_product_gemv : 51 general_matrix_vector_product<Index,LhsScalar,LhsStorageOrder,ConjugateLhs,RhsScalar,ConjugateRhs,BuiltIn> {}; 52 53 #define EIGEN_MKL_GEMV_SPECIALIZE(Scalar) \ 54 template<typename Index, bool ConjugateLhs, bool ConjugateRhs> \ 55 struct general_matrix_vector_product<Index,Scalar,ColMajor,ConjugateLhs,Scalar,ConjugateRhs,Specialized> { \ 56 static void run( \ 57 Index rows, Index cols, \ 58 const Scalar* lhs, Index lhsStride, \ 59 const Scalar* rhs, Index rhsIncr, \ 60 Scalar* res, Index resIncr, Scalar alpha) \ 61 { \ 62 if (ConjugateLhs) { \ 63 general_matrix_vector_product<Index,Scalar,ColMajor,ConjugateLhs,Scalar,ConjugateRhs,BuiltIn>::run( \ 64 rows, cols, lhs, lhsStride, rhs, rhsIncr, res, resIncr, alpha); \ 65 } else { \ 66 general_matrix_vector_product_gemv<Index,Scalar,ColMajor,ConjugateLhs,Scalar,ConjugateRhs>::run( \ 67 rows, cols, lhs, lhsStride, rhs, rhsIncr, res, resIncr, alpha); \ 68 } \ 69 } \ 70 }; \ 71 template<typename Index, bool ConjugateLhs, bool ConjugateRhs> \ 72 struct general_matrix_vector_product<Index,Scalar,RowMajor,ConjugateLhs,Scalar,ConjugateRhs,Specialized> { \ 73 static void run( \ 74 Index rows, Index cols, \ 75 const Scalar* lhs, Index lhsStride, \ 76 const Scalar* rhs, Index rhsIncr, \ 77 Scalar* res, Index resIncr, Scalar alpha) \ 78 { \ 79 general_matrix_vector_product_gemv<Index,Scalar,RowMajor,ConjugateLhs,Scalar,ConjugateRhs>::run( \ 80 rows, cols, lhs, lhsStride, rhs, rhsIncr, res, resIncr, alpha); \ 81 } \ 82 }; \ 83 84 EIGEN_MKL_GEMV_SPECIALIZE(double) 85 EIGEN_MKL_GEMV_SPECIALIZE(float) 86 EIGEN_MKL_GEMV_SPECIALIZE(dcomplex) 87 EIGEN_MKL_GEMV_SPECIALIZE(scomplex) 88 89 #define EIGEN_MKL_GEMV_SPECIALIZATION(EIGTYPE,MKLTYPE,MKLPREFIX) \ 90 template<typename Index, int LhsStorageOrder, bool ConjugateLhs, bool ConjugateRhs> \ 91 struct general_matrix_vector_product_gemv<Index,EIGTYPE,LhsStorageOrder,ConjugateLhs,EIGTYPE,ConjugateRhs> \ 92 { \ 93 typedef Matrix<EIGTYPE,Dynamic,1,ColMajor> GEMVVector;\ 94 \ 95 static void run( \ 96 Index rows, Index cols, \ 97 const EIGTYPE* lhs, Index lhsStride, \ 98 const EIGTYPE* rhs, Index rhsIncr, \ 99 EIGTYPE* res, Index resIncr, EIGTYPE alpha) \ 100 { \ 101 MKL_INT m=rows, n=cols, lda=lhsStride, incx=rhsIncr, incy=resIncr; \ 102 MKLTYPE alpha_, beta_; \ 103 const EIGTYPE *x_ptr, myone(1); \ 104 char trans=(LhsStorageOrder==ColMajor) ? 'N' : (ConjugateLhs) ? 'C' : 'T'; \ 105 if (LhsStorageOrder==RowMajor) { \ 106 m=cols; \ 107 n=rows; \ 108 }\ 109 assign_scalar_eig2mkl(alpha_, alpha); \ 110 assign_scalar_eig2mkl(beta_, myone); \ 111 GEMVVector x_tmp; \ 112 if (ConjugateRhs) { \ 113 Map<const GEMVVector, 0, InnerStride<> > map_x(rhs,cols,1,InnerStride<>(incx)); \ 114 x_tmp=map_x.conjugate(); \ 115 x_ptr=x_tmp.data(); \ 116 incx=1; \ 117 } else x_ptr=rhs; \ 118 MKLPREFIX##gemv(&trans, &m, &n, &alpha_, (const MKLTYPE*)lhs, &lda, (const MKLTYPE*)x_ptr, &incx, &beta_, (MKLTYPE*)res, &incy); \ 119 }\ 120 }; 121 122 EIGEN_MKL_GEMV_SPECIALIZATION(double, double, d) 123 EIGEN_MKL_GEMV_SPECIALIZATION(float, float, s) 124 EIGEN_MKL_GEMV_SPECIALIZATION(dcomplex, MKL_Complex16, z) 125 EIGEN_MKL_GEMV_SPECIALIZATION(scomplex, MKL_Complex8, c) 126 127 } // end namespase internal 128 129 } // end namespace Eigen 130 131 #endif // EIGEN_GENERAL_MATRIX_VECTOR_MKL_H 132