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