Eigen  5.0.1-dev+60122df6
 
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/*
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
48
49// defining a derived class to allow access to protected members
50template <typename MatrixType_, int Options>
51class BDCSVD_LAPACKE : public BDCSVD<MatrixType_, Options> {
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 template <typename Derived>
62 void compute_impl_lapacke(const MatrixBase<Derived>& matrix, unsigned int computationOptions) {
63 SVD::allocate(matrix.rows(), matrix.cols(), computationOptions);
64
65 SVD::m_nonzeroSingularValues = SVD::m_diagSize;
66
67 // prepare arguments to ?gesdd
68 const lapack_int matrix_order = lapack_storage_of(matrix);
69 const char jobz = (SVD::m_computeFullU || SVD::m_computeFullV) ? 'A'
70 : (SVD::m_computeThinU || SVD::m_computeThinV) ? 'S'
71 : 'N';
72 const lapack_int u_cols = (jobz == 'A') ? to_lapack(SVD::rows()) : (jobz == 'S') ? to_lapack(SVD::diagSize()) : 1;
73 const lapack_int vt_rows = (jobz == 'A') ? to_lapack(SVD::cols()) : (jobz == 'S') ? to_lapack(SVD::diagSize()) : 1;
74 lapack_int ldu, ldvt;
75 Scalar *u, *vt, dummy;
76 MatrixType localU;
77 if (SVD::computeU() && !(SVD::m_computeThinU && SVD::m_computeFullV)) {
78 ldu = to_lapack(SVD::m_matrixU.outerStride());
79 u = SVD::m_matrixU.data();
80 } else if (SVD::computeV()) {
81 localU.resize(SVD::rows(), u_cols);
82 ldu = to_lapack(localU.outerStride());
83 u = localU.data();
84 } else {
85 ldu = 1;
86 u = &dummy;
87 }
88 MatrixType localV;
89 if (SVD::computeU() || SVD::computeV()) {
90 localV.resize(vt_rows, SVD::cols());
91 ldvt = to_lapack(localV.outerStride());
92 vt = localV.data();
93 } else {
94 ldvt = 1;
95 vt = &dummy;
96 }
97 MatrixType temp;
98 temp = matrix;
99
100 // actual call to ?gesdd
101 lapack_int info = gesdd(matrix_order, jobz, to_lapack(SVD::rows()), to_lapack(SVD::cols()), to_lapack(temp.data()),
102 to_lapack(temp.outerStride()), (RealScalar*)SVD::m_singularValues.data(), to_lapack(u), ldu,
103 to_lapack(vt), ldvt);
104
105 // Check the result of the LAPACK call
106 if (info < 0 || !SVD::m_singularValues.allFinite()) {
107 // this includes info == -4 => NaN entry in A
108 SVD::m_info = InvalidInput;
109 } else if (info > 0) {
110 SVD::m_info = NoConvergence;
111 } else {
112 SVD::m_info = Success;
113 if (SVD::m_computeThinU && SVD::m_computeFullV) {
114 SVD::m_matrixU = localU.leftCols(SVD::m_matrixU.cols());
115 }
116 if (SVD::computeV()) {
117 SVD::m_matrixV = localV.adjoint().leftCols(SVD::m_matrixV.cols());
118 }
119 }
120 SVD::m_isInitialized = true;
121 }
122};
123
124template <typename MatrixType_, int Options, typename Derived>
125BDCSVD<MatrixType_, Options>& BDCSVD_wrapper(BDCSVD<MatrixType_, Options>& svd, const MatrixBase<Derived>& matrix,
126 int computationOptions) {
127 // we need to move to the wrapper type and back
128 BDCSVD_LAPACKE<MatrixType_, Options> tmpSvd(std::move(svd));
129 tmpSvd.compute_impl_lapacke(matrix, computationOptions);
130 svd = std::move(tmpSvd);
131 return svd;
132}
133
134} // end namespace lapacke_helpers
135
136} // end namespace internal
137
138#define EIGEN_LAPACKE_SDD(EIGTYPE, EIGCOLROW, OPTIONS) \
139 template <> \
140 template <typename Derived> \
141 inline BDCSVD<Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic>, OPTIONS>& \
142 BDCSVD<Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic>, OPTIONS>::compute_impl( \
143 const MatrixBase<Derived>& matrix, unsigned int computationOptions) { \
144 return internal::lapacke_helpers::BDCSVD_wrapper(*this, matrix, computationOptions); \
145 }
146
147#define EIGEN_LAPACK_SDD_OPTIONS(OPTIONS) \
148 EIGEN_LAPACKE_SDD(double, ColMajor, OPTIONS) \
149 EIGEN_LAPACKE_SDD(float, ColMajor, OPTIONS) \
150 EIGEN_LAPACKE_SDD(dcomplex, ColMajor, OPTIONS) \
151 EIGEN_LAPACKE_SDD(scomplex, ColMajor, OPTIONS) \
152 \
153 EIGEN_LAPACKE_SDD(double, RowMajor, OPTIONS) \
154 EIGEN_LAPACKE_SDD(float, RowMajor, OPTIONS) \
155 EIGEN_LAPACKE_SDD(dcomplex, RowMajor, OPTIONS) \
156 EIGEN_LAPACKE_SDD(scomplex, RowMajor, OPTIONS)
157
158EIGEN_LAPACK_SDD_OPTIONS(0)
159EIGEN_LAPACK_SDD_OPTIONS(ComputeThinU)
160EIGEN_LAPACK_SDD_OPTIONS(ComputeThinV)
161EIGEN_LAPACK_SDD_OPTIONS(ComputeFullU)
162EIGEN_LAPACK_SDD_OPTIONS(ComputeFullV)
163EIGEN_LAPACK_SDD_OPTIONS(ComputeThinU | ComputeThinV)
164EIGEN_LAPACK_SDD_OPTIONS(ComputeFullU | ComputeFullV)
165EIGEN_LAPACK_SDD_OPTIONS(ComputeThinU | ComputeFullV)
166EIGEN_LAPACK_SDD_OPTIONS(ComputeFullU | ComputeThinV)
167
168#undef EIGEN_LAPACK_SDD_OPTIONS
169
170#undef EIGEN_LAPACKE_SDD
171
172} // end namespace Eigen
173
174#endif // EIGEN_BDCSVD_LAPACKE_H
BDCSVD()
Definition BDCSVD.h:130
@ InvalidInput
Definition Constants.h:447
@ Success
Definition Constants.h:440
@ NoConvergence
Definition Constants.h:444
@ ComputeFullV
Definition Constants.h:393
@ ComputeThinV
Definition Constants.h:395
@ ComputeFullU
Definition Constants.h:389
@ ComputeThinU
Definition Constants.h:391
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1