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