Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
BDCSVD_LAPACKE.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2022 Melven Roehrig-Zoellner <[email protected]>
5// Copyright (c) 2011, Intel Corporation. All rights reserved.
6//
7// This file is based on the JacobiSVD_LAPACKE.h originally from Intel -
8// see license notice below:
9/*
10 Redistribution and use in source and binary forms, with or without modification,
11 are permitted provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright notice, this
14 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18 * Neither the name of Intel Corporation nor the names of its contributors may
19 be used to endorse or promote products derived from this software without
20 specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 ********************************************************************************
34 * Content : Eigen bindings to LAPACKe
35 * Singular Value Decomposition - SVD (divide and conquer variant)
36 ********************************************************************************
37*/
38#ifndef EIGEN_BDCSVD_LAPACKE_H
39#define EIGEN_BDCSVD_LAPACKE_H
40
41namespace Eigen {
42
43namespace internal {
44
45namespace lapacke_helpers {
46
49// defining a derived class to allow access to protected members
50template <typename MatrixType_, int Options>
51class BDCSVD_LAPACKE : public BDCSVD<MatrixType_, Options> {
52 typedef BDCSVD<MatrixType_, Options> SVD;
53 typedef typename SVD::MatrixType MatrixType;
54 typedef typename SVD::Scalar Scalar;
55 typedef typename SVD::RealScalar RealScalar;
56
57 public:
58 // construct this by moving from a parent object
59 BDCSVD_LAPACKE(SVD&& svd) : SVD(std::move(svd)) {}
60
61 void compute_impl_lapacke(const MatrixType& matrix, unsigned int computationOptions) {
62 SVD::allocate(matrix.rows(), matrix.cols(), computationOptions);
63
64 SVD::m_nonzeroSingularValues = SVD::m_diagSize;
65
66 // prepare arguments to ?gesdd
67 const lapack_int matrix_order = lapack_storage_of(matrix);
68 const char jobz = (SVD::m_computeFullU || SVD::m_computeFullV) ? 'A'
69 : (SVD::m_computeThinU || SVD::m_computeThinV) ? 'S'
70 : 'N';
71 const lapack_int u_cols = (jobz == 'A') ? to_lapack(SVD::rows()) : (jobz == 'S') ? to_lapack(SVD::diagSize()) : 1;
72 const lapack_int vt_rows = (jobz == 'A') ? to_lapack(SVD::cols()) : (jobz == 'S') ? to_lapack(SVD::diagSize()) : 1;
73 lapack_int ldu, ldvt;
74 Scalar *u, *vt, dummy;
75 MatrixType localU;
76 if (SVD::computeU() && !(SVD::m_computeThinU && SVD::m_computeFullV)) {
77 ldu = to_lapack(SVD::m_matrixU.outerStride());
78 u = SVD::m_matrixU.data();
79 } else if (SVD::computeV()) {
80 localU.resize(SVD::rows(), u_cols);
81 ldu = to_lapack(localU.outerStride());
82 u = localU.data();
83 } else {
84 ldu = 1;
85 u = &dummy;
86 }
87 MatrixType localV;
88 if (SVD::computeU() || SVD::computeV()) {
89 localV.resize(vt_rows, SVD::cols());
90 ldvt = to_lapack(localV.outerStride());
91 vt = localV.data();
92 } else {
93 ldvt = 1;
94 vt = &dummy;
95 }
96 MatrixType temp;
97 temp = matrix;
98
99 // actual call to ?gesdd
100 lapack_int info = gesdd(matrix_order, jobz, to_lapack(SVD::rows()), to_lapack(SVD::cols()), to_lapack(temp.data()),
101 to_lapack(temp.outerStride()), (RealScalar*)SVD::m_singularValues.data(), to_lapack(u), ldu,
102 to_lapack(vt), ldvt);
103
104 // Check the result of the LAPACK call
105 if (info < 0 || !SVD::m_singularValues.allFinite()) {
106 // this includes info == -4 => NaN entry in A
107 SVD::m_info = InvalidInput;
108 } else if (info > 0) {
109 SVD::m_info = NoConvergence;
110 } else {
111 SVD::m_info = Success;
112 if (SVD::m_computeThinU && SVD::m_computeFullV) {
113 SVD::m_matrixU = localU.leftCols(SVD::m_matrixU.cols());
114 }
115 if (SVD::computeV()) {
116 SVD::m_matrixV = localV.adjoint().leftCols(SVD::m_matrixV.cols());
117 }
118 }
119 SVD::m_isInitialized = true;
120 }
121};
122
123template <typename MatrixType_, int Options>
124BDCSVD<MatrixType_, Options>& BDCSVD_wrapper(BDCSVD<MatrixType_, Options>& svd, const MatrixType_& matrix,
125 int computationOptions) {
126 // we need to move to the wrapper type and back
127 BDCSVD_LAPACKE<MatrixType_, Options> tmpSvd(std::move(svd));
128 tmpSvd.compute_impl_lapacke(matrix, computationOptions);
129 svd = std::move(tmpSvd);
130 return svd;
131}
132
133} // end namespace lapacke_helpers
134
135} // end namespace internal
136
137#define EIGEN_LAPACKE_SDD(EIGTYPE, EIGCOLROW, OPTIONS) \
138 template <> \
139 inline BDCSVD<Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic>, OPTIONS>& \
140 BDCSVD<Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic>, OPTIONS>::compute_impl( \
141 const Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic>& matrix, unsigned int computationOptions) { \
142 return internal::lapacke_helpers::BDCSVD_wrapper(*this, matrix, computationOptions); \
143 }
144
145#define EIGEN_LAPACK_SDD_OPTIONS(OPTIONS) \
146 EIGEN_LAPACKE_SDD(double, ColMajor, OPTIONS) \
147 EIGEN_LAPACKE_SDD(float, ColMajor, OPTIONS) \
148 EIGEN_LAPACKE_SDD(dcomplex, ColMajor, OPTIONS) \
149 EIGEN_LAPACKE_SDD(scomplex, ColMajor, OPTIONS) \
150 \
151 EIGEN_LAPACKE_SDD(double, RowMajor, OPTIONS) \
152 EIGEN_LAPACKE_SDD(float, RowMajor, OPTIONS) \
153 EIGEN_LAPACKE_SDD(dcomplex, RowMajor, OPTIONS) \
154 EIGEN_LAPACKE_SDD(scomplex, RowMajor, OPTIONS)
155
156EIGEN_LAPACK_SDD_OPTIONS(0)
157EIGEN_LAPACK_SDD_OPTIONS(ComputeThinU)
158EIGEN_LAPACK_SDD_OPTIONS(ComputeThinV)
159EIGEN_LAPACK_SDD_OPTIONS(ComputeFullU)
160EIGEN_LAPACK_SDD_OPTIONS(ComputeFullV)
161EIGEN_LAPACK_SDD_OPTIONS(ComputeThinU | ComputeThinV)
162EIGEN_LAPACK_SDD_OPTIONS(ComputeFullU | ComputeFullV)
163EIGEN_LAPACK_SDD_OPTIONS(ComputeThinU | ComputeFullV)
164EIGEN_LAPACK_SDD_OPTIONS(ComputeFullU | ComputeThinV)
165
166#undef EIGEN_LAPACK_SDD_OPTIONS
167
168#undef EIGEN_LAPACKE_SDD
169
170} // end namespace Eigen
171
172#endif // EIGEN_BDCSVD_LAPACKE_H
bool computeV() const
Definition SVDBase.h:275
bool computeU() const
Definition SVDBase.h:273
ComputationInfo info() const
Definition SVDBase.h:300
@ InvalidInput
Definition Constants.h:447
@ Success
Definition Constants.h:440
@ NoConvergence
Definition Constants.h:444
Namespace containing all symbols from the Eigen library.
Definition Core:137