| // SPDX-FileCopyrightText: The Eigen Authors | |
| // SPDX-License-Identifier: MPL-2.0 | |
| #define EIGEN_USE_LAPACKE | |
| #include "main.h" | |
| #include <Eigen/Cholesky> | |
| #include <Eigen/Eigenvalues> | |
| #include <Eigen/LU> | |
| #include <Eigen/QR> | |
| // A system <lapacke.h> included after Eigen must still declare the full LAPACKE API. Eigen's bundled subset header | |
| // must neither claim the system header's include guards nor conflict with its declarations. The guard check needs no | |
| // system header, so it runs everywhere; the conflict check below needs one. Only a declaration is needed, so the test | |
| // does not link against LAPACKE. | |
| #if !defined(EIGEN_USE_MKL) && !defined(EIGEN_LAPACKE_SYSTEM) && (defined(_LAPACKE_H_) || defined(_MKL_LAPACKE_H_)) | |
| #error "the bundled lapacke.h must not define the include guard of the system header" | |
| #endif | |
| #if !defined(EIGEN_USE_MKL) && !defined(EIGEN_LAPACKE_SYSTEM) && defined(__has_include) | |
| #if __has_include(<lapacke.h>) | |
| #include <lapacke.h> | |
| static_assert(std::is_function<std::remove_pointer<decltype(&LAPACKE_dgesv)>::type>::value, | |
| "LAPACKE_dgesv is not declared after Eigen"); | |
| #endif | |
| #endif | |
| namespace { | |
| int real_schur_calls = 0; | |
| int complex_schur_calls = 0; | |
| int selfadjoint_calls = 0; | |
| int potrf_calls = 0; | |
| int getrf_calls = 0; | |
| int geqp3_calls = 0; | |
| int qr_calls = 0; | |
| // What the Schur probes return: a positive code reports that the QR algorithm failed, a negative one that LAPACKE | |
| // rejected the call and ran nothing. | |
| lapack_int gees_result = 1; | |
| const double* factor_data = nullptr; | |
| lapack_int factor_lda = 0; | |
| } // namespace | |
| // Dispatch probes return a failure code before Eigen can consume any unwritten output. | |
| extern "C" lapack_int LAPACKE_dgees(int, char, char, LAPACK_D_SELECT2, lapack_int, double*, lapack_int, lapack_int*, | |
| double*, double*, double*, lapack_int) { | |
| ++real_schur_calls; | |
| return gees_result; | |
| } | |
| extern "C" lapack_int LAPACKE_zgees(int, char, char, LAPACK_Z_SELECT1, lapack_int, lapack_complex_double*, lapack_int, | |
| lapack_int*, lapack_complex_double*, lapack_complex_double*, lapack_int) { | |
| ++complex_schur_calls; | |
| return gees_result; | |
| } | |
| extern "C" lapack_int LAPACKE_dsyev(int, char, char, lapack_int, double*, lapack_int, double*) { | |
| ++selfadjoint_calls; | |
| return 1; | |
| } | |
| // Record the matrix handed to the factorization and report success, leaving it unchanged. | |
| extern "C" lapack_int LAPACKE_dpotrf(int, char, lapack_int, double* a, lapack_int lda) { | |
| ++potrf_calls; | |
| factor_data = a; | |
| factor_lda = lda; | |
| return 0; | |
| } | |
| extern "C" lapack_int LAPACKE_dgetrf(int, lapack_int m, lapack_int n, double* a, lapack_int lda, lapack_int* ipiv) { | |
| ++getrf_calls; | |
| factor_data = a; | |
| factor_lda = lda; | |
| for (lapack_int i = 0; i < (std::min)(m, n); ++i) ipiv[i] = i + 1; | |
| return 0; | |
| } | |
| // LAPACKE rejects a NaN input with info = -4 before touching the matrix. The mock does the same for any input, so the | |
| // binding has to fall back to Eigen's own algorithm and still produce a valid decomposition. | |
| extern "C" lapack_int LAPACKE_dgeqp3(int, lapack_int, lapack_int, double*, lapack_int, lapack_int*, double*) { | |
| ++geqp3_calls; | |
| return -4; | |
| } | |
| // QR probes report success, as LAPACK does for a matrix without columns. The geqrf binding takes the address of every | |
| // scalar type's routine, so an unoptimized build needs all of them. TYPE forms declarations, not expressions. | |
| // NOLINTBEGIN(bugprone-macro-parentheses) | |
| #define EIGEN_QR_PROBES(PREFIX, TYPE) \ | |
| extern "C" lapack_int LAPACKE_##PREFIX##geqrf(int, lapack_int, lapack_int, TYPE*, lapack_int, TYPE*) { \ | |
| ++qr_calls; \ | |
| return 0; \ | |
| } | |
| #define EIGEN_PIVOTED_QR_PROBES(PREFIX, TYPE) \ | |
| extern "C" lapack_int LAPACKE_##PREFIX##geqp3(int, lapack_int, lapack_int, TYPE*, lapack_int, lapack_int*, TYPE*) { \ | |
| ++qr_calls; \ | |
| return 0; \ | |
| } | |
| // NOLINTEND(bugprone-macro-parentheses) | |
| EIGEN_QR_PROBES(s, float) | |
| EIGEN_QR_PROBES(d, double) | |
| EIGEN_QR_PROBES(c, lapack_complex_float) | |
| EIGEN_QR_PROBES(z, lapack_complex_double) | |
| EIGEN_PIVOTED_QR_PROBES(s, float) | |
| EIGEN_PIVOTED_QR_PROBES(c, lapack_complex_float) | |
| EIGEN_PIVOTED_QR_PROBES(z, lapack_complex_double) | |
| #undef EIGEN_QR_PROBES | |
| #undef EIGEN_PIVOTED_QR_PROBES | |
| void colpiv_qr_lapacke_rejected_input() { | |
| const MatrixXd A = MatrixXd::Random(6, 4); | |
| const VectorXd b = A * VectorXd::Random(4); | |
| const int geqp3Before = geqp3_calls; | |
| ColPivHouseholderQR<MatrixXd> qr(A); | |
| VERIFY_IS_EQUAL(geqp3_calls, geqp3Before + 1); | |
| VERIFY_IS_EQUAL(qr.rank(), Index(4)); | |
| const MatrixXd R = qr.matrixR().triangularView<Upper>(); | |
| VERIFY_IS_APPROX(qr.householderQ() * R, A * qr.colsPermutation()); | |
| VERIFY_IS_APPROX(A * qr.solve(b), b); | |
| } | |
| // LAPACK addresses a matrix by its leading dimension only, so an in-place factorization of a Ref with an inner stride | |
| // other than 1 must hand it a contiguous copy, and a Ref with a unit inner stride its own storage. | |
| template <int Options> | |
| void inplace_lapacke_inner_stride() { | |
| using MatrixType = Matrix<double, Dynamic, Dynamic, Options>; | |
| using StridedRef = Ref<MatrixType, 0, Stride<Dynamic, Dynamic>>; | |
| const Index size = 4; | |
| for (Index inner : {Index(1), Index(2), Index(3)}) { | |
| const Stride<Dynamic, Dynamic> stride(inner * size + 2, inner); | |
| VectorXd storage = VectorXd::Random(stride.outer() * size); | |
| const VectorXd saved = storage; | |
| Map<MatrixType, 0, Stride<Dynamic, Dynamic>> a(storage.data(), size, size, stride); | |
| StridedRef ref(a); | |
| const lapack_int expected_lda = inner == 1 ? lapack_int(stride.outer()) : lapack_int(size); | |
| const int potrfBefore = potrf_calls; | |
| LLT<StridedRef> llt(ref); | |
| VERIFY_IS_EQUAL(potrf_calls, potrfBefore + 1); | |
| VERIFY_IS_EQUAL(llt.info(), Success); | |
| VERIFY_IS_EQUAL(factor_lda, expected_lda); | |
| VERIFY((factor_data == storage.data()) == (inner == 1)); | |
| VERIFY_IS_EQUAL(storage, saved); | |
| const int getrfBefore = getrf_calls; | |
| PartialPivLU<StridedRef> lu(ref); | |
| VERIFY_IS_EQUAL(getrf_calls, getrfBefore + 1); | |
| VERIFY_IS_EQUAL(factor_lda, expected_lda); | |
| VERIFY((factor_data == storage.data()) == (inner == 1)); | |
| VERIFY_IS_EQUAL(storage, saved); | |
| } | |
| } | |
| template <int Options> | |
| void inplace_plain_lapacke_dispatch() { | |
| using RealMatrix = Matrix<double, Dynamic, Dynamic, Options>; | |
| using ComplexMatrix = Matrix<std::complex<double>, Dynamic, Dynamic, Options>; | |
| RealMatrix real = RealMatrix::Identity(3, 3); | |
| ComplexMatrix complex = ComplexMatrix::Identity(3, 3); | |
| const RealMatrix constReal = RealMatrix::Identity(3, 3); | |
| const ComplexMatrix constComplex = ComplexMatrix::Identity(3, 3); | |
| const int realBefore = real_schur_calls, complexBefore = complex_schur_calls, selfadjointBefore = selfadjoint_calls; | |
| RealSchur<RealMatrix> mutableRealSchur(real), constRealSchur(constReal); | |
| ComplexSchur<ComplexMatrix> mutableComplexSchur(complex), constComplexSchur(constComplex); | |
| SelfAdjointEigenSolver<RealMatrix> mutableSelfAdjoint(real), constSelfAdjoint(constReal); | |
| VERIFY_IS_EQUAL(real_schur_calls, realBefore + 2); | |
| VERIFY_IS_EQUAL(complex_schur_calls, complexBefore + 2); | |
| VERIFY_IS_EQUAL(selfadjoint_calls, selfadjointBefore + 2); | |
| VERIFY_IS_EQUAL(mutableRealSchur.info(), NoConvergence); | |
| VERIFY_IS_EQUAL(constRealSchur.info(), NoConvergence); | |
| VERIFY_IS_EQUAL(mutableComplexSchur.info(), NoConvergence); | |
| VERIFY_IS_EQUAL(constComplexSchur.info(), NoConvergence); | |
| VERIFY_IS_EQUAL(mutableSelfAdjoint.info(), NoConvergence); | |
| VERIFY_IS_EQUAL(constSelfAdjoint.info(), NoConvergence); | |
| VERIFY_IS_EQUAL(real, constReal); | |
| VERIFY_IS_EQUAL(complex, constComplex); | |
| } | |
| // An empty matrix is valid input for the generic solvers. LAPACK rejects a zero leading dimension, so the LAPACKE | |
| // specializations must answer it themselves instead of calling a routine that returns a failure code. | |
| template <int Options> | |
| void empty_lapacke_input() { | |
| using RealMatrix = Matrix<double, Dynamic, Dynamic, Options>; | |
| using ComplexMatrix = Matrix<std::complex<double>, Dynamic, Dynamic, Options>; | |
| const int realBefore = real_schur_calls, complexBefore = complex_schur_calls, selfadjointBefore = selfadjoint_calls; | |
| for (bool vectors : {false, true}) { | |
| SelfAdjointEigenSolver<RealMatrix> selfadjoint(RealMatrix(0, 0), vectors ? ComputeEigenvectors : EigenvaluesOnly); | |
| VERIFY_IS_EQUAL(selfadjoint.info(), Success); | |
| VERIFY_IS_EQUAL(selfadjoint.eigenvalues().size(), 0); | |
| if (vectors) VERIFY_IS_EQUAL(selfadjoint.eigenvectors().rows(), 0); | |
| RealSchur<RealMatrix> realSchur(RealMatrix(0, 0), vectors); | |
| VERIFY_IS_EQUAL(realSchur.info(), Success); | |
| VERIFY_IS_EQUAL(realSchur.matrixT().rows(), 0); | |
| ComplexSchur<ComplexMatrix> complexSchur(ComplexMatrix(0, 0), vectors); | |
| VERIFY_IS_EQUAL(complexSchur.info(), Success); | |
| VERIFY_IS_EQUAL(complexSchur.matrixT().rows(), 0); | |
| } | |
| VERIFY_IS_EQUAL(real_schur_calls, realBefore); | |
| VERIFY_IS_EQUAL(complex_schur_calls, complexBefore); | |
| VERIFY_IS_EQUAL(selfadjoint_calls, selfadjointBefore); | |
| } | |
| // A negative code leaves T and U unwritten, so the decomposition must come from Eigen's own algorithm. | |
| template <int Options> | |
| void schur_lapacke_rejected_input() { | |
| using RealMatrix = Matrix<double, Dynamic, Dynamic, Options>; | |
| using ComplexMatrix = Matrix<std::complex<double>, Dynamic, Dynamic, Options>; | |
| RealMatrix real(3, 3); | |
| real << 1, 2, 3, 4, 5, 6, 7, 8, 10; | |
| ComplexMatrix complex = real.template cast<std::complex<double>>(); | |
| const int realBefore = real_schur_calls, complexBefore = complex_schur_calls; | |
| // LAPACKE could not allocate its workspace. | |
| gees_result = LAPACK_WORK_MEMORY_ERROR; | |
| const RealSchur<RealMatrix> realSchur(real); | |
| const ComplexSchur<ComplexMatrix> complexSchur(complex); | |
| gees_result = 1; | |
| VERIFY_IS_EQUAL(realSchur.info(), Success); | |
| VERIFY_IS_APPROX(realSchur.matrixU() * realSchur.matrixT() * realSchur.matrixU().transpose(), real); | |
| VERIFY_IS_EQUAL(complexSchur.info(), Success); | |
| VERIFY_IS_APPROX(complexSchur.matrixU() * complexSchur.matrixT() * complexSchur.matrixU().adjoint(), complex); | |
| // LAPACKE found a NaN in its sixth argument, the matrix. Without LAPACKE, RealSchur does not compute U for such a | |
| // matrix and ComplexSchur propagates the NaN to T and U. | |
| real(2, 0) = std::numeric_limits<double>::quiet_NaN(); | |
| complex(2, 0) = real(2, 0); | |
| gees_result = -6; | |
| const RealSchur<RealMatrix> realSchurOfNaN(real); | |
| const ComplexSchur<ComplexMatrix> complexSchurOfNaN(complex); | |
| gees_result = 1; | |
| VERIFY_IS_EQUAL(realSchurOfNaN.info(), NoConvergence); | |
| VERIFY_RAISES_ASSERT(realSchurOfNaN.matrixU()); | |
| VERIFY_IS_EQUAL(complexSchurOfNaN.info(), NoConvergence); | |
| VERIFY(complexSchurOfNaN.matrixT().diagonal().array().isNaN().all()); | |
| VERIFY(complexSchurOfNaN.matrixU().array().isNaN().all()); | |
| VERIFY_IS_EQUAL(real_schur_calls, realBefore + 2); | |
| VERIFY_IS_EQUAL(complex_schur_calls, complexBefore + 2); | |
| } | |
| // LAPACK rejects the zero leading dimension of an empty input, and given a valid one it returns success with no pivot | |
| // written. The QR bindings must not call it, and must return the native path's rank-0 decomposition. | |
| template <typename MatrixType> | |
| void check_qr_lapacke_empty(Index rows, Index cols) { | |
| using VectorType = Matrix<typename MatrixType::Scalar, Dynamic, 1>; | |
| const MatrixType m(rows, cols); | |
| const int callsBefore = qr_calls, geqp3Before = geqp3_calls; | |
| HouseholderQR<MatrixType> qr(m); | |
| VERIFY_IS_EQUAL(qr.hCoeffs().size(), Index(0)); | |
| ColPivHouseholderQR<MatrixType> cpqr(m); | |
| VERIFY_IS_EQUAL(cpqr.info(), Success); | |
| VERIFY_IS_EQUAL(cpqr.rank(), Index(0)); | |
| VERIFY_IS_EQUAL(cpqr.hCoeffs().size(), Index(0)); | |
| VERIFY_IS_EQUAL(MatrixType(cpqr.colsPermutation()), MatrixType::Identity(cols, cols)); | |
| VERIFY_IS_EQUAL(cpqr.solve(VectorType::Ones(rows)).eval(), VectorType::Zero(cols)); | |
| CompleteOrthogonalDecomposition<MatrixType> cod(m); | |
| VERIFY_IS_EQUAL(cod.info(), Success); | |
| VERIFY_IS_EQUAL(cod.rank(), Index(0)); | |
| VERIFY_IS_EQUAL(qr_calls, callsBefore); | |
| VERIFY_IS_EQUAL(geqp3_calls, geqp3Before); | |
| } | |
| template <typename Scalar> | |
| void qr_lapacke_empty() { | |
| using ColMatrix = Matrix<Scalar, Dynamic, Dynamic>; | |
| using RowMatrix = Matrix<Scalar, Dynamic, Dynamic, RowMajor>; | |
| check_qr_lapacke_empty<ColMatrix>(0, 0); | |
| check_qr_lapacke_empty<ColMatrix>(0, 3); | |
| check_qr_lapacke_empty<ColMatrix>(3, 0); | |
| check_qr_lapacke_empty<RowMatrix>(0, 0); | |
| check_qr_lapacke_empty<RowMatrix>(0, 3); | |
| check_qr_lapacke_empty<RowMatrix>(3, 0); | |
| } | |
| EIGEN_DECLARE_TEST(inplace_decomposition_lapacke) { | |
| inplace_plain_lapacke_dispatch<ColMajor>(); | |
| inplace_plain_lapacke_dispatch<RowMajor>(); | |
| empty_lapacke_input<ColMajor>(); | |
| empty_lapacke_input<RowMajor>(); | |
| colpiv_qr_lapacke_rejected_input(); | |
| schur_lapacke_rejected_input<ColMajor>(); | |
| schur_lapacke_rejected_input<RowMajor>(); | |
| qr_lapacke_empty<float>(); | |
| qr_lapacke_empty<double>(); | |
| qr_lapacke_empty<std::complex<float>>(); | |
| qr_lapacke_empty<std::complex<double>>(); | |
| inplace_lapacke_inner_stride<ColMajor>(); | |
| inplace_lapacke_inner_stride<RowMajor>(); | |
| } |