Home | History | Annotate | Download | only in products
      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-matrix product functionality based on ?GEMM.
     30  ********************************************************************************
     31 */
     32 
     33 #ifndef EIGEN_GENERAL_MATRIX_MATRIX_MKL_H
     34 #define EIGEN_GENERAL_MATRIX_MATRIX_MKL_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, MKLTYPE, MKLPREFIX) \
     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> \
     55 { \
     56 static void run(Index rows, Index cols, Index depth, \
     57   const EIGTYPE* _lhs, Index lhsStride, \
     58   const EIGTYPE* _rhs, Index rhsStride, \
     59   EIGTYPE* res, Index resStride, \
     60   EIGTYPE alpha, \
     61   level3_blocking<EIGTYPE, EIGTYPE>& /*blocking*/, \
     62   GemmParallelInfo<Index>* /*info = 0*/) \
     63 { \
     64   using std::conj; \
     65 \
     66   char transa, transb; \
     67   MKL_INT m, n, k, lda, ldb, ldc; \
     68   const EIGTYPE *a, *b; \
     69   MKLTYPE alpha_, beta_; \
     70   MatrixX##EIGPREFIX a_tmp, b_tmp; \
     71   EIGTYPE myone(1);\
     72 \
     73 /* Set transpose options */ \
     74   transa = (LhsStorageOrder==RowMajor) ? ((ConjugateLhs) ? 'C' : 'T') : 'N'; \
     75   transb = (RhsStorageOrder==RowMajor) ? ((ConjugateRhs) ? 'C' : 'T') : 'N'; \
     76 \
     77 /* Set m, n, k */ \
     78   m = (MKL_INT)rows;  \
     79   n = (MKL_INT)cols;  \
     80   k = (MKL_INT)depth; \
     81 \
     82 /* Set alpha_ & beta_ */ \
     83   assign_scalar_eig2mkl(alpha_, alpha); \
     84   assign_scalar_eig2mkl(beta_, myone); \
     85 \
     86 /* Set lda, ldb, ldc */ \
     87   lda = (MKL_INT)lhsStride; \
     88   ldb = (MKL_INT)rhsStride; \
     89   ldc = (MKL_INT)resStride; \
     90 \
     91 /* Set a, b, c */ \
     92   if ((LhsStorageOrder==ColMajor) && (ConjugateLhs)) { \
     93     Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > lhs(_lhs,m,k,OuterStride<>(lhsStride)); \
     94     a_tmp = lhs.conjugate(); \
     95     a = a_tmp.data(); \
     96     lda = a_tmp.outerStride(); \
     97   } else a = _lhs; \
     98 \
     99   if ((RhsStorageOrder==ColMajor) && (ConjugateRhs)) { \
    100     Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > rhs(_rhs,k,n,OuterStride<>(rhsStride)); \
    101     b_tmp = rhs.conjugate(); \
    102     b = b_tmp.data(); \
    103     ldb = b_tmp.outerStride(); \
    104   } else b = _rhs; \
    105 \
    106   MKLPREFIX##gemm(&transa, &transb, &m, &n, &k, &alpha_, (const MKLTYPE*)a, &lda, (const MKLTYPE*)b, &ldb, &beta_, (MKLTYPE*)res, &ldc); \
    107 }};
    108 
    109 GEMM_SPECIALIZATION(double,   d,  double,        d)
    110 GEMM_SPECIALIZATION(float,    f,  float,         s)
    111 GEMM_SPECIALIZATION(dcomplex, cd, MKL_Complex16, z)
    112 GEMM_SPECIALIZATION(scomplex, cf, MKL_Complex8,  c)
    113 
    114 } // end namespase internal
    115 
    116 } // end namespace Eigen
    117 
    118 #endif // EIGEN_GENERAL_MATRIX_MATRIX_MKL_H
    119