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-vector product functionality based on ?GEMV.
     30  ********************************************************************************
     31 */
     32 
     33 #ifndef EIGEN_GENERAL_MATRIX_VECTOR_BLAS_H
     34 #define EIGEN_GENERAL_MATRIX_VECTOR_BLAS_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 StorageOrder, bool ConjugateLhs, typename RhsScalar, bool ConjugateRhs>
     50 struct general_matrix_vector_product_gemv;
     51 
     52 #define EIGEN_BLAS_GEMV_SPECIALIZE(Scalar) \
     53 template<typename Index, bool ConjugateLhs, bool ConjugateRhs> \
     54 struct general_matrix_vector_product<Index,Scalar,const_blas_data_mapper<Scalar,Index,ColMajor>,ColMajor,ConjugateLhs,Scalar,const_blas_data_mapper<Scalar,Index,RowMajor>,ConjugateRhs,Specialized> { \
     55 static void run( \
     56   Index rows, Index cols, \
     57   const const_blas_data_mapper<Scalar,Index,ColMajor> &lhs, \
     58   const const_blas_data_mapper<Scalar,Index,RowMajor> &rhs, \
     59   Scalar* res, Index resIncr, Scalar alpha) \
     60 { \
     61   if (ConjugateLhs) { \
     62     general_matrix_vector_product<Index,Scalar,const_blas_data_mapper<Scalar,Index,ColMajor>,ColMajor,ConjugateLhs,Scalar,const_blas_data_mapper<Scalar,Index,RowMajor>,ConjugateRhs,BuiltIn>::run( \
     63       rows, cols, lhs, rhs, res, resIncr, alpha); \
     64   } else { \
     65     general_matrix_vector_product_gemv<Index,Scalar,ColMajor,ConjugateLhs,Scalar,ConjugateRhs>::run( \
     66       rows, cols, lhs.data(), lhs.stride(), rhs.data(), rhs.stride(), res, resIncr, alpha); \
     67   } \
     68 } \
     69 }; \
     70 template<typename Index, bool ConjugateLhs, bool ConjugateRhs> \
     71 struct general_matrix_vector_product<Index,Scalar,const_blas_data_mapper<Scalar,Index,RowMajor>,RowMajor,ConjugateLhs,Scalar,const_blas_data_mapper<Scalar,Index,ColMajor>,ConjugateRhs,Specialized> { \
     72 static void run( \
     73   Index rows, Index cols, \
     74   const const_blas_data_mapper<Scalar,Index,RowMajor> &lhs, \
     75   const const_blas_data_mapper<Scalar,Index,ColMajor> &rhs, \
     76   Scalar* res, Index resIncr, Scalar alpha) \
     77 { \
     78     general_matrix_vector_product_gemv<Index,Scalar,RowMajor,ConjugateLhs,Scalar,ConjugateRhs>::run( \
     79       rows, cols, lhs.data(), lhs.stride(), rhs.data(), rhs.stride(), res, resIncr, alpha); \
     80 } \
     81 }; \
     82 
     83 EIGEN_BLAS_GEMV_SPECIALIZE(double)
     84 EIGEN_BLAS_GEMV_SPECIALIZE(float)
     85 EIGEN_BLAS_GEMV_SPECIALIZE(dcomplex)
     86 EIGEN_BLAS_GEMV_SPECIALIZE(scomplex)
     87 
     88 #define EIGEN_BLAS_GEMV_SPECIALIZATION(EIGTYPE,BLASTYPE,BLASPREFIX) \
     89 template<typename Index, int LhsStorageOrder, bool ConjugateLhs, bool ConjugateRhs> \
     90 struct general_matrix_vector_product_gemv<Index,EIGTYPE,LhsStorageOrder,ConjugateLhs,EIGTYPE,ConjugateRhs> \
     91 { \
     92 typedef Matrix<EIGTYPE,Dynamic,1,ColMajor> GEMVVector;\
     93 \
     94 static void run( \
     95   Index rows, Index cols, \
     96   const EIGTYPE* lhs, Index lhsStride, \
     97   const EIGTYPE* rhs, Index rhsIncr, \
     98   EIGTYPE* res, Index resIncr, EIGTYPE alpha) \
     99 { \
    100   BlasIndex m=convert_index<BlasIndex>(rows), n=convert_index<BlasIndex>(cols), \
    101             lda=convert_index<BlasIndex>(lhsStride), incx=convert_index<BlasIndex>(rhsIncr), incy=convert_index<BlasIndex>(resIncr); \
    102   const EIGTYPE beta(1); \
    103   const EIGTYPE *x_ptr; \
    104   char trans=(LhsStorageOrder==ColMajor) ? 'N' : (ConjugateLhs) ? 'C' : 'T'; \
    105   if (LhsStorageOrder==RowMajor) { \
    106     m = convert_index<BlasIndex>(cols); \
    107     n = convert_index<BlasIndex>(rows); \
    108   }\
    109   GEMVVector x_tmp; \
    110   if (ConjugateRhs) { \
    111     Map<const GEMVVector, 0, InnerStride<> > map_x(rhs,cols,1,InnerStride<>(incx)); \
    112     x_tmp=map_x.conjugate(); \
    113     x_ptr=x_tmp.data(); \
    114     incx=1; \
    115   } else x_ptr=rhs; \
    116   BLASPREFIX##gemv_(&trans, &m, &n, &numext::real_ref(alpha), (const BLASTYPE*)lhs, &lda, (const BLASTYPE*)x_ptr, &incx, &numext::real_ref(beta), (BLASTYPE*)res, &incy); \
    117 }\
    118 };
    119 
    120 EIGEN_BLAS_GEMV_SPECIALIZATION(double,   double, d)
    121 EIGEN_BLAS_GEMV_SPECIALIZATION(float,    float,  s)
    122 EIGEN_BLAS_GEMV_SPECIALIZATION(dcomplex, double, z)
    123 EIGEN_BLAS_GEMV_SPECIALIZATION(scomplex, float,  c)
    124 
    125 } // end namespase internal
    126 
    127 } // end namespace Eigen
    128 
    129 #endif // EIGEN_GENERAL_MATRIX_VECTOR_BLAS_H
    130