Loading...
Searching...
No Matches
TwoStageEigen.cpp
Go to the documentation of this file.
1#include "TwoStageEigen.h"
2
3#include "CanonicalEigen.h"
4
5// These are the Fortran symbols the build linked. EIGEN_USE_LAPACKE selects
6// Eigen's solvers and, from Eigen 5, also declares these entries. dlarft's
7// reflector matrix is const there; the Fortran ABI is unchanged.
8extern "C" {
9void dgeqrf_(int*, int*, double*, int*, double*, double*, int*, int*);
10void dlarft_(char*, char*, int*, int*, const double*, int*, const double*,
11 double*, int*);
12void dlarfb_(char*, char*, char*, char*, int*, int*, int*, const double*, int*,
13 const double*, int*, double*, int*, double*, int*);
14void dsbevd_(char*, char*, int*, int*, double*, int*, double*, double*, int*,
15 double*, int*, int*, int*, int*);
16}
17
18namespace gpr::linalg {
19
20namespace {
21
30constexpr int kSemiBandwidth = 64;
31
32int queryGeqrfWork(int m, int n, double* a, int lda, double* tau)
33{
34 double q = 0.0;
35 int lwork = -1;
36 int info = 0;
37 dgeqrf_(&m, &n, a, &lda, tau, &q, &lwork, &info);
38 if (info != 0) return -1;
39 return std::max(1, static_cast<int>(q));
40}
41
42} // namespace
43
44bool TwoStageEigenLib::allEigenpairs(const Eigen::MatrixXd& sym,
45 Eigen::Index min_order,
46 Eigen::VectorXd& evals,
47 Eigen::MatrixXd& evecs) const
48{
49 if (!available()) return false;
50 if (sym.rows() != sym.cols()) return false;
51 const Eigen::Index n_idx = sym.rows();
52 if (n_idx < min_order) return false;
53 if (n_idx > static_cast<Eigen::Index>(std::numeric_limits<int>::max()))
54 return false;
55 int n = static_cast<int>(n_idx);
56 // A band wider than the matrix is just the dense problem again.
57 // LAPACK takes these integers by pointer, so they are not const.
58 int kd = std::min(kSemiBandwidth, std::max(1, n - 1));
59
60 Eigen::MatrixXd a = sym;
61 // Q1 accumulates stage-one reflectors so stage-three can carry the
62 // band eigenvectors back to the original basis.
63 Eigen::MatrixXd q1 = Eigen::MatrixXd::Identity(n, n);
64
65 std::vector<double> tau(static_cast<std::size_t>(std::max(1, n)));
66 std::vector<double> work;
67 std::vector<double> tfac;
68
69 for (int j = 0; j + kd < n; j += kd) {
70 int rows = n - (j + kd); // rows strictly below the band
71 int cols = std::min(kd, n - j - kd);
72 if (rows <= 0 || cols <= 0) break;
73
74 double* panel =
75 a.data() + static_cast<std::ptrdiff_t>(j) * n + (j + kd);
76 int lda = n;
77
78 int lw = queryGeqrfWork(rows, cols, panel, lda, tau.data());
79 if (lw < 0) return false;
80 work.resize(static_cast<std::size_t>(lw));
81 int info = 0;
82 dgeqrf_(&rows, &cols, panel, &lda, tau.data(), work.data(), &lw, &info);
83 if (info != 0) return false;
84
85 // One triangular factor for the whole panel; both sides and the Q1
86 // accumulation reuse it.
87 tfac.assign(static_cast<std::size_t>(cols) * cols, 0.0);
88 char F = 'F', C = 'C';
89 ::dlarft_(&F, &C, &rows, &cols, panel, &lda, tau.data(), tfac.data(),
90 &cols);
91
92 // The reflector acts on index set S = [j+kd, n). The similarity
93 // A <- Q^T A Q therefore touches every entry with a row or a
94 // column in S. Columns left of j are already zero on those rows
95 // from earlier panels, and columns [j, j+cols) are the panel that
96 // dgeqrf just overwrote, so the update starts at j+cols -- NOT at
97 // j+kd. Those two differ whenever the panel is narrower than the
98 // semi-bandwidth, which is what the final panel is for any order
99 // that is not a multiple of it; skipping that gap leaves a block
100 // untransformed and the eigenvalues wrong in the third decimal.
101 int off = j + cols;
102 int span = n - off; // columns (left apply) / rows (right)
103 double* lblk =
104 a.data() + static_cast<std::ptrdiff_t>(off) * n + (j + kd);
105 double* rblk = a.data() + static_cast<std::ptrdiff_t>(j + kd) * n + off;
106 char L = 'L', R = 'R', T = 'T', N = 'N';
107 // dlarfb's workspace is ldwork x k, ldwork being C's n for a left
108 // apply and its m for a right one. n x cols covers every call here.
109 work.resize(static_cast<std::size_t>(n) * cols);
110
111 ::dlarfb_(&L, &T, &F, &C, &rows, &span, &cols, panel, &lda, tfac.data(),
112 &cols, lblk, &lda, work.data(), &span);
113 ::dlarfb_(&R, &N, &F, &C, &span, &rows, &cols, panel, &lda, tfac.data(),
114 &cols, rblk, &lda, work.data(), &span);
115
116 // Two one-sided applications leave asymmetry at roundoff; the
117 // band extraction below reads one triangle, so make the two
118 // agree rather than silently preferring the lower one.
119 Eigen::Block<Eigen::MatrixXd> tb = a.block(j + kd, j + kd, rows, rows);
120 tb = 0.5 * (tb + tb.transpose().eval());
121
122 // Carry the same reflectors into Q1.
123 double* q1c = q1.data() + static_cast<std::ptrdiff_t>(j + kd) * n;
124 ::dlarfb_(&R, &N, &F, &C, &n, &rows, &cols, panel, &lda, tfac.data(),
125 &cols, q1c, &lda, work.data(), &n);
126 }
127
128 // Lower-triangular band storage: AB(i - j, j) = A(i, j), j <= i <= j+kd.
129 int ldab = kd + 1;
130 std::vector<double> ab(static_cast<std::size_t>(ldab) * n, 0.0);
131 for (int j = 0; j < n; ++j) {
132 const int hi = std::min(j + kd, n - 1);
133 for (int i = j; i <= hi; ++i) {
134 ab[static_cast<std::size_t>(j) * ldab + (i - j)] = a(i, j);
135 }
136 }
137
138 evals.resize(n);
139 Eigen::MatrixXd z(n, n);
140 char jobz = 'V', uplo = 'L';
141 int lwork = -1, liwork = -1, info = 0;
142 double wq = 0.0;
143 int iwq = 0;
144 ::dsbevd_(&jobz, &uplo, &n, &kd, ab.data(), &ldab, evals.data(), z.data(),
145 &n, &wq, &lwork, &iwq, &liwork, &info);
146 if (info != 0) return false;
147 lwork = std::max(1, static_cast<int>(wq));
148 liwork = std::max(1, iwq);
149 std::vector<double> dwork(static_cast<std::size_t>(lwork));
150 std::vector<int> iwork(static_cast<std::size_t>(liwork));
151 ::dsbevd_(&jobz, &uplo, &n, &kd, ab.data(), &ldab, evals.data(), z.data(),
152 &n, dwork.data(), &lwork, iwork.data(), &liwork, &info);
153 if (info != 0) return false;
154
155 // Stage three. dsbevd returns eigenvalues ascending, so the product
156 // inherits that ordering and matches the one-stage contract. The
157 // signs and any near-tie order are then fixed, so a reference LAPACK
158 // and OpenBLAS hand the caller the same basis.
159 evecs.noalias() = q1 * z;
160 if (!evals.allFinite() || !evecs.allFinite()) return false;
161 canonicalizeEigenpairs(evals, evecs);
162 return true;
163}
164
165} // namespace gpr::linalg
Implementation-independent symmetric eigenpairs.
@ z
Definition Enums.h:28
void dsbevd_(char *, char *, int *, int *, double *, int *, double *, double *, int *, double *, int *, int *, int *, int *)
void dlarfb_(char *, char *, char *, char *, int *, int *, int *, const double *, int *, const double *, int *, double *, int *, double *, int *)
void dgeqrf_(int *, int *, double *, int *, double *, double *, int *, int *)
void dlarft_(char *, char *, int *, int *, const double *, int *, const double *, double *, int *)
bool available() const noexcept
bool allEigenpairs(const Eigen::MatrixXd &sym, Eigen::Index min_order, Eigen::VectorXd &evals, Eigen::MatrixXd &evecs) const
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
void canonicalizeEigenpairs(Eigen::Ref< Eigen::VectorXd > evals, Eigen::Ref< Eigen::MatrixXd > evecs)
evals has one entry per leading column of evecs. Both are rewritten.