LLT_LAPACKE.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 LAPACKe
29  * LLt decomposition based on LAPACKE_?potrf function.
30  ********************************************************************************
31 */
32 
33 #ifndef EIGEN_LLT_LAPACKE_H
34 #define EIGEN_LLT_LAPACKE_H
35 
36 #include "./InternalHeaderCheck.h"
37 
38 namespace Eigen {
39 
40 namespace internal {
41 
42 namespace lapacke_helpers {
43  // -------------------------------------------------------------------------------------------------------------------
44  // Dispatch for rank update handling upper and lower parts
45  // -------------------------------------------------------------------------------------------------------------------
46 
47  template<UpLoType Mode>
48  struct rank_update {};
49 
50  template<>
51  struct rank_update<Lower> {
52  template<typename MatrixType, typename VectorType>
53  static Index run(MatrixType &mat, const VectorType &vec, const typename MatrixType::RealScalar &sigma) {
54  return Eigen::internal::llt_rank_update_lower(mat, vec, sigma);
55  }
56  };
57 
58  template<>
59  struct rank_update<Upper> {
60  template<typename MatrixType, typename VectorType>
61  static Index run(MatrixType &mat, const VectorType &vec, const typename MatrixType::RealScalar &sigma) {
62  Transpose<MatrixType> matt(mat);
63  return Eigen::internal::llt_rank_update_lower(matt, vec.conjugate(), sigma);
64  }
65  };
66 
67  // -------------------------------------------------------------------------------------------------------------------
68  // Generic lapacke llt implementation that hands of to the dispatches
69  // -------------------------------------------------------------------------------------------------------------------
70 
71  template<typename Scalar, UpLoType Mode>
72  struct lapacke_llt {
73  EIGEN_STATIC_ASSERT(((Mode == Lower) || (Mode == Upper)),MODE_MUST_BE_UPPER_OR_LOWER)
74  template<typename MatrixType>
75  static Index blocked(MatrixType& m)
76  {
77  eigen_assert(m.rows() == m.cols());
78  if(m.rows() == 0) {
79  return -1;
80  }
81  /* Set up parameters for ?potrf */
82  lapack_int size = to_lapack(m.rows());
83  lapack_int matrix_order = lapack_storage_of(m);
84  constexpr char uplo = Mode == Upper ? 'U' : 'L';
85  Scalar* a = &(m.coeffRef(0,0));
86  lapack_int lda = to_lapack(m.outerStride());
87 
88  lapack_int info = potrf(matrix_order, uplo, size, to_lapack(a), lda );
89  info = (info==0) ? -1 : info>0 ? info-1 : size;
90  return info;
91  }
92 
93  template<typename MatrixType, typename VectorType>
94  static Index rankUpdate(MatrixType& mat, const VectorType& vec, const typename MatrixType::RealScalar& sigma)
95  {
96  return rank_update<Mode>::run(mat, vec, sigma);
97  }
98  };
99 }
100 // end namespace lapacke_helpers
101 
102 /*
103  * Here, we just put the generic implementation from lapacke_llt into a full specialization of the llt_inplace
104  * type. By being a full specialization, the versions defined here thus get precedence over the generic implementation
105  * in LLT.h for double, float and complex double, complex float types.
106  */
107 
108 #define EIGEN_LAPACKE_LLT(EIGTYPE) \
109 template<> struct llt_inplace<EIGTYPE, Lower> : public lapacke_helpers::lapacke_llt<EIGTYPE, Lower> {}; \
110 template<> struct llt_inplace<EIGTYPE, Upper> : public lapacke_helpers::lapacke_llt<EIGTYPE, Upper> {};
111 
112 EIGEN_LAPACKE_LLT(double)
113 EIGEN_LAPACKE_LLT(float)
114 EIGEN_LAPACKE_LLT(std::complex<double>)
115 EIGEN_LAPACKE_LLT(std::complex<float>)
116 
117 #undef EIGEN_LAPACKE_LLT
118 
119 } // end namespace internal
120 
121 } // end namespace Eigen
122 
123 #endif // EIGEN_LLT_LAPACKE_H
Matrix3f m
#define EIGEN_LAPACKE_LLT(EIGTYPE)
Definition: LLT_LAPACKE.h:108
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
Matrix< float, 1, Dynamic > MatrixType
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:68
@ Lower
Definition: Constants.h:211
@ Upper
Definition: Constants.h:213
#define lapack_int
Definition: lapacke.h:52
static Index llt_rank_update_lower(MatrixType &mat, const VectorType &vec, const typename MatrixType::RealScalar &sigma)
Definition: LLT.h:239
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82