Loading...
Searching...
No Matches
StdParBackend.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
28
29#ifndef GPR_LINALG_STDPARBACKEND_H_
30#define GPR_LINALG_STDPARBACKEND_H_
31
32#include <cblas.h>
33#include <lapacke.h>
34
35#include <algorithm>
36#include <cmath>
37#include <execution>
38#include <numeric>
39
41#include "ExactSchur.h"
42#include "ExactSum.h"
43#include "LocalExecutor.h"
44
45namespace gpr {
46namespace linalg {
47
48inline bool cholesky(const EigenMatrix& C, EigenMatrix& L)
49{
50 if (C.rows() != C.cols()) return false;
52 if (!scaling.compute(C.diagonal())) return false;
53 const int n = static_cast<int>(C.rows());
54 EigenMatrix factor = C; // dpotrf works in-place
55 scaling.scaleLower(factor);
56 int info = LAPACKE_dpotrf(LAPACK_COL_MAJOR, 'L', n, factor.data(), n);
57 // Leave the caller's L untouched on failure; dpotrf has already
58 // overwritten part of `factor` by the time it reports non-zero info.
59 if (info != 0) return false;
60 scaling.restoreFactor(factor);
61 L = std::move(factor);
62 // Zero upper triangle (dpotrf leaves original values there)
63 for (int j = 1; j < n; ++j)
64 for (int i = 0; i < j; ++i)
65 L(i, j) = 0.0;
66 return true;
67}
68
69inline Eigen::VectorXd forward_solve(const EigenMatrix& L,
70 const Eigen::VectorXd& b)
71{
72 Eigen::VectorXd x = b;
73 const int n = static_cast<int>(L.rows());
74 // Solve L x = b (Lower, NoTrans, NonUnit)
75 cblas_dtrsv(CblasColMajor, CblasLower, CblasNoTrans, CblasNonUnit, n,
76 L.data(), n, x.data(), 1);
77 return x;
78}
79
80inline Eigen::VectorXd back_solve(const EigenMatrix& L,
81 const Eigen::VectorXd& b)
82{
83 Eigen::VectorXd x = b;
84 const int n = static_cast<int>(L.rows());
85 // Solve L^T x = b (Lower, Trans, NonUnit)
86 cblas_dtrsv(CblasColMajor, CblasLower, CblasTrans, CblasNonUnit, n,
87 L.data(), n, x.data(), 1);
88 return x;
89}
90
92{
93 EigenMatrix X = B;
94 const int m = static_cast<int>(L.rows());
95 const int n = static_cast<int>(B.cols());
96 // Solve L X = B (Left, Lower, NoTrans, NonUnit)
97 cblas_dtrsm(CblasColMajor, CblasLeft, CblasLower, CblasNoTrans,
98 CblasNonUnit, m, n, 1.0, L.data(), m, X.data(), m);
99 return X;
100}
101
102inline EigenMatrix back_solve_mat(const EigenMatrix& L, const EigenMatrix& B)
103{
104 EigenMatrix X = B;
105 const int m = static_cast<int>(L.rows());
106 const int n = static_cast<int>(B.cols());
107 // Solve L^T X = B (Left, Lower, Trans, NonUnit)
108 cblas_dtrsm(CblasColMajor, CblasLeft, CblasLower, CblasTrans, CblasNonUnit,
109 m, n, 1.0, L.data(), m, X.data(), m);
110 return X;
111}
112
113inline Eigen::VectorXd forward_solve_mat_colwise_squared_norm(
114 const EigenMatrix& L, const EigenMatrix& B)
115{
116 const EigenMatrix X = forward_solve_mat(L, B);
117 return X.colwise().squaredNorm().transpose();
118}
119
120inline Eigen::VectorXd bordered_cholesky_diagonal(const EigenMatrix& L,
121 const EigenMatrix& B,
122 const Eigen::VectorXd& prior)
123{
124 return borderedCholeskyDiagonal(prior, forward_solve_mat(L, B), L);
125}
126
128{
129 const int n = static_cast<int>(L.rows());
130 EigenMatrix invC = L;
131 // dpotri computes the inverse from the Cholesky factor in-place.
132 // It fills only the lower triangle.
133 int info = LAPACKE_dpotri(LAPACK_COL_MAJOR, 'L', n, invC.data(), n);
134 (void)info;
135 // Symmetrise: copy lower triangle to upper
136 for (int j = 0; j < n; ++j)
137 for (int i = 0; i < j; ++i)
138 invC(i, j) = invC(j, i);
139 return invC;
140}
141
142inline double trace_product(const EigenMatrix& A, const EigenMatrix& B)
143{
144 const auto n = static_cast<std::size_t>(A.size());
145 if (n == 0) return 0.0;
146 constexpr std::size_t kChunk = 256;
147 const std::size_t n_chunks = (n + kChunk - 1) / kChunk;
148 std::vector<ExactSum> chunks(n_chunks);
149 const double* a_ptr = A.data();
150 const double* b_ptr = B.data();
152 n_chunks,
153 [&](std::size_t c) {
154 const std::size_t begin = c * kChunk;
155 const std::size_t end = std::min(n, begin + kChunk);
156 ExactSum acc;
157 for (std::size_t i = begin; i < end; ++i)
158 acc.addProduct(a_ptr[i], b_ptr[i]);
159 chunks[c] = acc;
160 },
162 ExactSum total;
163 for (std::size_t c = 0; c < n_chunks; ++c)
164 total.merge(chunks[c]);
165 return total.value();
166}
167
168inline double log_det_cholesky(const EigenMatrix& L)
169{
170 const int n = static_cast<int>(L.rows());
171 const int stride = n + 1; // diagonal stride in ColMajor
172 double sum = 0.0;
173 for (int i = 0; i < n; ++i)
174 sum += std::log(L.data()[i * stride]);
175 return sum;
176}
177
178} // namespace linalg
179} // namespace gpr
180
181#endif // GPR_LINALG_STDPARBACKEND_H_
@ x
Definition Enums.h:28
Correctly rounded Schur-complement diagonals from Cholesky solves.
Exact floating-point accumulator (Kulisch long accumulator).
void scaleLower(Eigen::MatrixBase< Derived > &matrix) const
void addProduct(double a, double b) noexcept
Definition ExactSum.h:70
void merge(const ExactSum &other) noexcept
Adds another accumulator exactly.
Definition ExactSum.h:146
void forEach(std::size_t count, Operation &&operation, Grain grain=Grain::Fine) const
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
EigenMatrix back_solve_mat(const EigenMatrix &L, const EigenMatrix &B)
Back triangular solve with matrix RHS (BLAS equivalent: dtrsm with side=L, uplo=L,...
Eigen::VectorXd forward_solve(const EigenMatrix &L, const Eigen::VectorXd &b)
Forward triangular solve with vector RHS (BLAS equivalent: dtrsv).
EigenMatrix forward_solve_mat(const EigenMatrix &L, const EigenMatrix &B)
Forward triangular solve with matrix RHS (BLAS equivalent: dtrsm).
Eigen::VectorXd forward_solve_mat_colwise_squared_norm(const EigenMatrix &L, const EigenMatrix &B)
Forward triangular solve followed by columnwise squared norms.
double log_det_cholesky(const EigenMatrix &L)
Half log-determinant from Cholesky factor.
Eigen::VectorXd back_solve(const EigenMatrix &L, const Eigen::VectorXd &b)
Back triangular solve with vector RHS (BLAS equivalent: dtrsv).
double trace_product(const EigenMatrix &A, const EigenMatrix &B)
Frobenius inner product / trace of matrix product.
Eigen::VectorXd bordered_cholesky_diagonal(const EigenMatrix &L, const EigenMatrix &B, const Eigen::VectorXd &prior)
Posterior diagonal from bordered Cholesky pivots.
EigenMatrix cholesky_inverse(const EigenMatrix &L)
Symmetric inverse from Cholesky factor (LAPACK equivalent: dpotri).
bool cholesky(const EigenMatrix &C, EigenMatrix &L)
Cholesky decomposition (LAPACK equivalent: dpotrf).
Eigen::VectorXd borderedCholeskyDiagonal(const Eigen::VectorXd &prior, const Eigen::MatrixBase< Derived > &whitened)
borderedCholeskyDiagonal for a whitened block L^-1 K_tq with no factor in hand: the allowance is the ...
Definition ExactSchur.h:444
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20