Loading...
Searching...
No Matches
EigenBackend.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
15
16#ifndef GPR_LINALG_EIGENBACKEND_H_
17#define GPR_LINALG_EIGENBACKEND_H_
18
19#include <Eigen/Cholesky>
20
22#include "LinalgThreads.h"
23#include "TeamFactor.h"
24#include "TrainingCholesky.h"
25
26namespace gpr {
27namespace linalg {
28
29inline bool cholesky(const EigenMatrix& C, EigenMatrix& L)
30{
31 if (C.rows() != C.cols()) return false;
34 if (!scaling.compute(C.diagonal())) return false;
35 if (trainingCholeskySelected(C.rows())) {
36 EigenMatrix lower = EigenMatrix::NullaryExpr(
37 C.rows(), C.cols(), [&](Eigen::Index i, Eigen::Index j) {
38 return i >= j ? scaling.covariance(C(i, j), i, j) : 0.0;
39 });
40 if (!trainingCholeskyLower(lower)) return false;
41 scaling.restoreFactor(lower);
42 L = std::move(lower);
43 return true;
44 }
45#if defined(USE_OPENMP)
46 // The panel factor is a different rounding from Eigen's LLT, including
47 // at one thread. Oracle counts were measured on LLT, so the panel
48 // factor runs only for a team larger than one.
49 if (teamPanelKernels()) {
50 EigenMatrix lower = EigenMatrix::NullaryExpr(
51 C.rows(), C.cols(), [&](Eigen::Index i, Eigen::Index j) {
52 return i >= j ? scaling.covariance(C(i, j), i, j) : 0.0;
53 });
54 if (!teamCholeskyLower(lower)) return false;
55 scaling.restoreFactor(lower);
56 L = std::move(lower);
57 return true;
58 }
59#endif
60 Eigen::LLT<EigenMatrix> llt;
61 llt.compute(EigenMatrix::NullaryExpr(
62 C.rows(), C.cols(), [&](Eigen::Index i, Eigen::Index j) {
63 return i >= j ? scaling.covariance(C(i, j), i, j) : 0.0;
64 }));
65 // Only Success means a usable factor. Leave L untouched on failure
66 // so the output cannot expose a partial factor.
67 if (llt.info() != Eigen::Success) return false;
68 L = llt.matrixL();
69 scaling.restoreFactor(L);
70 return true;
71}
72
73} // namespace linalg
74} // namespace gpr
75
77
78#endif // GPR_LINALG_EIGENBACKEND_H_
bool compute(const Eigen::MatrixBase< Derived > &diagonal)
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
bool teamPanelKernels() noexcept
bool trainingCholeskyLower(ProductMatrix lower)
bool teamCholeskyLower(double *lower, std::int64_t lda, std::int64_t n)
bool trainingCholeskySelected(Eigen::Index order)
bool cholesky(const EigenMatrix &C, EigenMatrix &L)
Cholesky decomposition (LAPACK equivalent: dpotrf).
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