GeneralMatrixMatrixTriangular_BLAS.h
Go to the documentation of this file.
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  * Level 3 BLAS SYRK/HERK implementation.
30  ********************************************************************************
31 */
32 
33 #ifndef EIGEN_GENERAL_MATRIX_MATRIX_TRIANGULAR_BLAS_H
34 #define EIGEN_GENERAL_MATRIX_MATRIX_TRIANGULAR_BLAS_H
35 
36 #include "../InternalHeaderCheck.h"
37 
38 namespace Eigen {
39 
40 namespace internal {
41 
42 template <typename Index, typename Scalar, int AStorageOrder, bool ConjugateA, int ResStorageOrder, int UpLo>
43 struct general_matrix_matrix_rankupdate :
44  general_matrix_matrix_triangular_product<
45  Index,Scalar,AStorageOrder,ConjugateA,Scalar,AStorageOrder,ConjugateA,ResStorageOrder,1,UpLo,BuiltIn> {};
46 
47 
48 // try to go to BLAS specialization
49 #define EIGEN_BLAS_RANKUPDATE_SPECIALIZE(Scalar) \
50 template <typename Index, int LhsStorageOrder, bool ConjugateLhs, \
51  int RhsStorageOrder, bool ConjugateRhs, int UpLo> \
52 struct general_matrix_matrix_triangular_product<Index,Scalar,LhsStorageOrder,ConjugateLhs, \
53  Scalar,RhsStorageOrder,ConjugateRhs,ColMajor,1,UpLo,Specialized> { \
54  static EIGEN_STRONG_INLINE void run(Index size, Index depth,const Scalar* lhs, Index lhsStride, \
55  const Scalar* rhs, Index rhsStride, Scalar* res, Index resIncr, Index resStride, Scalar alpha, level3_blocking<Scalar, Scalar>& blocking) \
56  { \
57  if ( lhs==rhs && ((UpLo&(Lower|Upper))==UpLo) ) { \
58  general_matrix_matrix_rankupdate<Index,Scalar,LhsStorageOrder,ConjugateLhs,ColMajor,UpLo> \
59  ::run(size,depth,lhs,lhsStride,rhs,rhsStride,res,resStride,alpha,blocking); \
60  } else { \
61  general_matrix_matrix_triangular_product<Index, \
62  Scalar, LhsStorageOrder, ConjugateLhs, \
63  Scalar, RhsStorageOrder, ConjugateRhs, \
64  ColMajor, 1, UpLo, BuiltIn> \
65  ::run(size,depth,lhs,lhsStride,rhs,rhsStride,res,resIncr,resStride,alpha,blocking); \
66  } \
67  } \
68 };
69 
72 // TODO handle complex cases
73 // EIGEN_BLAS_RANKUPDATE_SPECIALIZE(dcomplex)
74 // EIGEN_BLAS_RANKUPDATE_SPECIALIZE(scomplex)
75 
76 // SYRK for float/double
77 #define EIGEN_BLAS_RANKUPDATE_R(EIGTYPE, BLASTYPE, BLASFUNC) \
78 template <typename Index, int AStorageOrder, bool ConjugateA, int UpLo> \
79 struct general_matrix_matrix_rankupdate<Index,EIGTYPE,AStorageOrder,ConjugateA,ColMajor,UpLo> { \
80  enum { \
81  IsLower = (UpLo&Lower) == Lower, \
82  LowUp = IsLower ? Lower : Upper, \
83  conjA = ((AStorageOrder==ColMajor) && ConjugateA) ? 1 : 0 \
84  }; \
85  static EIGEN_STRONG_INLINE void run(Index size, Index depth,const EIGTYPE* lhs, Index lhsStride, \
86  const EIGTYPE* /*rhs*/, Index /*rhsStride*/, EIGTYPE* res, Index resStride, EIGTYPE alpha, level3_blocking<EIGTYPE, EIGTYPE>& /*blocking*/) \
87  { \
88  /* typedef Matrix<EIGTYPE, Dynamic, Dynamic, RhsStorageOrder> MatrixRhs;*/ \
89 \
90  BlasIndex lda=convert_index<BlasIndex>(lhsStride), ldc=convert_index<BlasIndex>(resStride), n=convert_index<BlasIndex>(size), k=convert_index<BlasIndex>(depth); \
91  char uplo=((IsLower) ? 'L' : 'U'), trans=((AStorageOrder==RowMajor) ? 'T':'N'); \
92  EIGTYPE beta(1); \
93  BLASFUNC(&uplo, &trans, &n, &k, (const BLASTYPE*)&numext::real_ref(alpha), lhs, &lda, (const BLASTYPE*)&numext::real_ref(beta), res, &ldc); \
94  } \
95 };
96 
97 // HERK for complex data
98 #define EIGEN_BLAS_RANKUPDATE_C(EIGTYPE, BLASTYPE, RTYPE, BLASFUNC) \
99 template <typename Index, int AStorageOrder, bool ConjugateA, int UpLo> \
100 struct general_matrix_matrix_rankupdate<Index,EIGTYPE,AStorageOrder,ConjugateA,ColMajor,UpLo> { \
101  enum { \
102  IsLower = (UpLo&Lower) == Lower, \
103  LowUp = IsLower ? Lower : Upper, \
104  conjA = (((AStorageOrder==ColMajor) && ConjugateA) || ((AStorageOrder==RowMajor) && !ConjugateA)) ? 1 : 0 \
105  }; \
106  static EIGEN_STRONG_INLINE void run(Index size, Index depth,const EIGTYPE* lhs, Index lhsStride, \
107  const EIGTYPE* /*rhs*/, Index /*rhsStride*/, EIGTYPE* res, Index resStride, EIGTYPE alpha, level3_blocking<EIGTYPE, EIGTYPE>& /*blocking*/) \
108  { \
109  typedef Matrix<EIGTYPE, Dynamic, Dynamic, AStorageOrder> MatrixType; \
110 \
111  BlasIndex lda=convert_index<BlasIndex>(lhsStride), ldc=convert_index<BlasIndex>(resStride), n=convert_index<BlasIndex>(size), k=convert_index<BlasIndex>(depth); \
112  char uplo=((IsLower) ? 'L' : 'U'), trans=((AStorageOrder==RowMajor) ? 'C':'N'); \
113  RTYPE alpha_, beta_; \
114  const EIGTYPE* a_ptr; \
115 \
116  alpha_ = alpha.real(); \
117  beta_ = 1.0; \
118 /* Copy with conjugation in some cases*/ \
119  MatrixType a; \
120  if (conjA) { \
121  Map<const MatrixType, 0, OuterStride<> > mapA(lhs,n,k,OuterStride<>(lhsStride)); \
122  a = mapA.conjugate(); \
123  lda = a.outerStride(); \
124  a_ptr = a.data(); \
125  } else a_ptr=lhs; \
126  BLASFUNC(&uplo, &trans, &n, &k, &alpha_, (BLASTYPE*)a_ptr, &lda, &beta_, (BLASTYPE*)res, &ldc); \
127  } \
128 };
129 
130 #ifdef EIGEN_USE_MKL
131 EIGEN_BLAS_RANKUPDATE_R(double, double, dsyrk)
132 EIGEN_BLAS_RANKUPDATE_R(float, float, ssyrk)
133 #else
134 EIGEN_BLAS_RANKUPDATE_R(double, double, dsyrk_)
135 EIGEN_BLAS_RANKUPDATE_R(float, float, ssyrk_)
136 #endif
137 
138 // TODO hanlde complex cases
139 // EIGEN_BLAS_RANKUPDATE_C(dcomplex, double, double, zherk_)
140 // EIGEN_BLAS_RANKUPDATE_C(scomplex, float, float, cherk_)
141 
142 
143 } // end namespace internal
144 
145 } // end namespace Eigen
146 
147 #endif // EIGEN_GENERAL_MATRIX_MATRIX_TRIANGULAR_BLAS_H
#define EIGEN_BLAS_RANKUPDATE_R(EIGTYPE, BLASTYPE, BLASFUNC)
#define EIGEN_BLAS_RANKUPDATE_SPECIALIZE(Scalar)
int BLASFUNC() ssyrk(const char *, const char *, const int *, const int *, const float *, const float *, const int *, const float *, float *, const int *)
int BLASFUNC() dsyrk(const char *, const char *, const int *, const int *, const double *, const double *, const int *, const double *, double *, const int *)
: InteropHeaders
Definition: Core:139