Eigen  5.0.1-dev+master
 
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 <Melven.Roehrig-Zoellner@DLR.de>
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// SPDX-License-Identifier: BSD-3-Clause
10/*
11 Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13
14 * Redistributions of source code must retain the above copyright notice, this
15 list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of Intel Corporation nor the names of its contributors may
20 be used to endorse or promote products derived from this software without
21 specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 ********************************************************************************
35 * Content : Eigen bindings to LAPACKe
36 * Singular Value Decomposition - SVD (divide and conquer variant)
37 ********************************************************************************
38*/
39#ifndef EIGEN_BDCSVD_LAPACKE_H
40#define EIGEN_BDCSVD_LAPACKE_H
41
42namespace Eigen {
43
44namespace internal {
45
46namespace lapacke_helpers {
47
49
50// defining a derived class to allow access to protected members
51template <typename MatrixType_, int Options>
52class BDCSVD_LAPACKE : public BDCSVD<MatrixType_, Options> {
54 using MatrixType = typename SVD::MatrixType;
55 using Scalar = typename SVD::Scalar;
56 using RealScalar = typename SVD::RealScalar;
57
58 public:
59 // construct this by moving from a parent object
60 BDCSVD_LAPACKE(SVD&& svd) : SVD(std::move(svd)) {}
61
62 template <typename Derived>
63 void compute_impl_lapacke(const MatrixBase<Derived>& matrix, unsigned int computationOptions) {
64 SVD::allocate(matrix.rows(), matrix.cols(), computationOptions);
65
66 SVD::m_nonzeroSingularValues = SVD::m_diagSize;
67 // LAPACK rejects the zero leading dimensions of an empty input, and given valid ones it returns without writing
68 // U or VT. Set the identities the native path returns.
69 if (SVD::diagSize() == 0) {
70 if (SVD::computeU()) SVD::m_matrixU.setIdentity();
71 if (SVD::computeV()) SVD::m_matrixV.setIdentity();
72 SVD::m_isInitialized = true;
73 return;
74 }
75
76 // prepare arguments to ?gesdd. A, U and VT all have MatrixType's layout, which the input
77 // expression need not share.
78 MatrixType temp;
79 temp = matrix;
80 const lapack_int matrix_order = lapack_storage_of(temp);
81 const char jobz = (SVD::m_computeFullU || SVD::m_computeFullV) ? 'A'
82 : (SVD::m_computeThinU || SVD::m_computeThinV) ? 'S'
83 : 'N';
84 const lapack_int u_cols = (jobz == 'A') ? to_lapack(SVD::rows()) : (jobz == 'S') ? to_lapack(SVD::diagSize()) : 1;
85 const lapack_int vt_rows = (jobz == 'A') ? to_lapack(SVD::cols()) : (jobz == 'S') ? to_lapack(SVD::diagSize()) : 1;
86 lapack_int ldu, ldvt;
87 Scalar *u, *vt, dummy;
88 MatrixType localU;
89 if (SVD::computeU() && !(SVD::m_computeThinU && SVD::m_computeFullV)) {
90 ldu = to_lapack(SVD::m_matrixU.outerStride());
91 u = SVD::m_matrixU.data();
92 } else if (SVD::computeV()) {
93 localU.resize(SVD::rows(), u_cols);
94 ldu = to_lapack(localU.outerStride());
95 u = localU.data();
96 } else {
97 ldu = 1;
98 u = &dummy;
99 }
100 MatrixType localV;
101 if (SVD::computeU() || SVD::computeV()) {
102 localV.resize(vt_rows, SVD::cols());
103 ldvt = to_lapack(localV.outerStride());
104 vt = localV.data();
105 } else {
106 ldvt = 1;
107 vt = &dummy;
108 }
109
110 // actual call to ?gesdd
111 lapack_int info = gesdd(matrix_order, jobz, to_lapack(SVD::rows()), to_lapack(SVD::cols()), to_lapack(temp.data()),
112 to_lapack(temp.outerStride()), (RealScalar*)SVD::m_singularValues.data(), to_lapack(u), ldu,
113 to_lapack(vt), ldvt);
114
115 // Check the result of the LAPACK call
116 if (info < 0 || !SVD::m_singularValues.allFinite()) {
117 // this includes info == -4 => NaN entry in A
118 SVD::m_info = InvalidInput;
119 } else if (info > 0) {
120 SVD::m_info = NoConvergence;
121 } else {
122 SVD::m_info = Success;
123 if (SVD::m_computeThinU && SVD::m_computeFullV) {
124 SVD::m_matrixU = localU.leftCols(SVD::m_matrixU.cols());
125 }
126 if (SVD::computeV()) {
127 SVD::m_matrixV = localV.adjoint().leftCols(SVD::m_matrixV.cols());
128 }
129 }
130 SVD::m_isInitialized = true;
131 }
132};
133
134template <typename MatrixType_, int Options, typename Derived>
135BDCSVD<MatrixType_, Options>& BDCSVD_wrapper(BDCSVD<MatrixType_, Options>& svd, const MatrixBase<Derived>& matrix,
136 int computationOptions) {
137 // we need to move to the wrapper type and back
138 BDCSVD_LAPACKE<MatrixType_, Options> tmpSvd(std::move(svd));
139 tmpSvd.compute_impl_lapacke(matrix, computationOptions);
140 svd = std::move(tmpSvd);
141 return svd;
142}
143
144} // end namespace lapacke_helpers
145
146} // end namespace internal
147
148#define EIGEN_LAPACKE_SDD(EIGTYPE, EIGCOLROW, OPTIONS) \
149 template <> \
150 template <typename Derived> \
151 inline BDCSVD<Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic>, OPTIONS>& \
152 BDCSVD<Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic>, OPTIONS>::compute_impl( \
153 const MatrixBase<Derived>& matrix, unsigned int computationOptions) { \
154 return internal::lapacke_helpers::BDCSVD_wrapper(*this, matrix, computationOptions); \
155 }
156
157#define EIGEN_LAPACK_SDD_OPTIONS(OPTIONS) \
158 EIGEN_LAPACKE_SDD(double, ColMajor, OPTIONS) \
159 EIGEN_LAPACKE_SDD(float, ColMajor, OPTIONS) \
160 EIGEN_LAPACKE_SDD(dcomplex, ColMajor, OPTIONS) \
161 EIGEN_LAPACKE_SDD(scomplex, ColMajor, OPTIONS) \
162 \
163 EIGEN_LAPACKE_SDD(double, RowMajor, OPTIONS) \
164 EIGEN_LAPACKE_SDD(float, RowMajor, OPTIONS) \
165 EIGEN_LAPACKE_SDD(dcomplex, RowMajor, OPTIONS) \
166 EIGEN_LAPACKE_SDD(scomplex, RowMajor, OPTIONS)
167
168EIGEN_LAPACK_SDD_OPTIONS(0)
169EIGEN_LAPACK_SDD_OPTIONS(ComputeThinU)
170EIGEN_LAPACK_SDD_OPTIONS(ComputeThinV)
171EIGEN_LAPACK_SDD_OPTIONS(ComputeFullU)
172EIGEN_LAPACK_SDD_OPTIONS(ComputeFullV)
173EIGEN_LAPACK_SDD_OPTIONS(ComputeThinU | ComputeThinV)
174EIGEN_LAPACK_SDD_OPTIONS(ComputeFullU | ComputeFullV)
175EIGEN_LAPACK_SDD_OPTIONS(ComputeThinU | ComputeFullV)
176EIGEN_LAPACK_SDD_OPTIONS(ComputeFullU | ComputeThinV)
177
178#undef EIGEN_LAPACK_SDD_OPTIONS
179
180#undef EIGEN_LAPACKE_SDD
181
182} // end namespace Eigen
183
184#endif // EIGEN_BDCSVD_LAPACKE_H
BDCSVD()
Definition BDCSVD.h:121
@ InvalidInput
Definition Constants.h:464
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
@ ComputeFullV
Definition Constants.h:398
@ ComputeThinV
Definition Constants.h:400
@ ComputeFullU
Definition Constants.h:394
@ ComputeThinU
Definition Constants.h:396