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 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, BLASPREFIX) \
     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 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 resStride, \
     62   EIGTYPE alpha, \
     63   level3_blocking<EIGTYPE, EIGTYPE>& /*blocking*/, \
     64   GemmParallelInfo<Index>* /*info = 0*/) \
     65 { \
     66   using std::conj; \
     67 \
     68   char transa, transb; \
     69   BlasIndex m, n, k, lda, ldb, ldc; \
     70   const EIGTYPE *a, *b; \
     71   EIGTYPE beta(1); \
     72   MatrixX##EIGPREFIX a_tmp, b_tmp; \
     73 \
     74 /* Set transpose options */ \
     75   transa = (LhsStorageOrder==RowMajor) ? ((ConjugateLhs) ? 'C' : 'T') : 'N'; \
     76   transb = (RhsStorageOrder==RowMajor) ? ((ConjugateRhs) ? 'C' : 'T') : 'N'; \
     77 \
     78 /* Set m, n, k */ \
     79   m = convert_index<BlasIndex>(rows);  \
     80   n = convert_index<BlasIndex>(cols);  \
     81   k = convert_index<BlasIndex>(depth); \
     82 \
     83 /* Set lda, ldb, ldc */ \
     84   lda = convert_index<BlasIndex>(lhsStride); \
     85   ldb = convert_index<BlasIndex>(rhsStride); \
     86   ldc = convert_index<BlasIndex>(resStride); \
     87 \
     88 /* Set a, b, c */ \
     89   if ((LhsStorageOrder==ColMajor) && (ConjugateLhs)) { \
     90     Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > lhs(_lhs,m,k,OuterStride<>(lhsStride)); \
     91     a_tmp = lhs.conjugate(); \
     92     a = a_tmp.data(); \
     93     lda = convert_index<BlasIndex>(a_tmp.outerStride()); \
     94   } else a = _lhs; \
     95 \
     96   if ((RhsStorageOrder==ColMajor) && (ConjugateRhs)) { \
     97     Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > rhs(_rhs,k,n,OuterStride<>(rhsStride)); \
     98     b_tmp = rhs.conjugate(); \
     99     b = b_tmp.data(); \
    100     ldb = convert_index<BlasIndex>(b_tmp.outerStride()); \
    101   } else b = _rhs; \
    102 \
    103   BLASPREFIX##gemm_(&transa, &transb, &m, &n, &k, &numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (const BLASTYPE*)b, &ldb, &numext::real_ref(beta), (BLASTYPE*)res, &ldc); \
    104 }};
    105 
    106 GEMM_SPECIALIZATION(double,   d,  double, d)
    107 GEMM_SPECIALIZATION(float,    f,  float,  s)
    108 GEMM_SPECIALIZATION(dcomplex, cd, double, z)
    109 GEMM_SPECIALIZATION(scomplex, cf, float,  c)
    110 
    111 } // end namespase internal
    112 
    113 } // end namespace Eigen
    114 
    115 #endif // EIGEN_GENERAL_MATRIX_MATRIX_BLAS_H
    116