Loading...
Searching...
No Matches
CholmodBackend.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3
4#ifndef GPR_LINALG_CHOLMODBACKEND_H_
5#define GPR_LINALG_CHOLMODBACKEND_H_
6
7#include <cholmod.h>
8
9#include <cmath>
10#include <limits>
11#include <utility>
12
15#include "TeamFactor.h"
16
17namespace gpr::linalg {
18namespace detail {
19
20// Numeric extraction consumes the CHOLMOD factor. All CHOLMOD objects belong
21// to one call; only the physical dense lower triangle leaves this workspace.
22struct CholmodWorkspace {
23 cholmod_common common{};
24 cholmod_sparse* covariance = nullptr;
25 cholmod_factor* factor = nullptr;
26 cholmod_sparse* lower = nullptr;
27 bool started;
28
29 CholmodWorkspace() : started(cholmod_l_start(&common) != 0) { }
30 CholmodWorkspace(const CholmodWorkspace&) = delete;
31 CholmodWorkspace& operator=(const CholmodWorkspace&) = delete;
32 ~CholmodWorkspace()
33 {
34 if (!started) return;
35 if (lower) cholmod_l_free_sparse(&lower, &common);
36 if (factor) cholmod_l_free_factor(&factor, &common);
37 if (covariance) cholmod_l_free_sparse(&covariance, &common);
38 cholmod_l_finish(&common);
39 }
40};
41
42} // namespace detail
43
44inline bool cholesky(const EigenMatrix& C, EigenMatrix& L)
45{
46 if (C.rows() != C.cols()) return false;
49 if (!scaling.compute(C.diagonal())) return false;
50 const auto n = static_cast<std::size_t>(C.rows());
51 const auto max_index =
52 static_cast<std::size_t>(std::numeric_limits<SuiteSparse_long>::max());
53 if (n >= max_index) return false;
54 if (n == 0) {
55 L.resize(0, 0);
56 return true;
57 }
58
59 std::size_t entries = 0;
60 for (Eigen::Index column = 0; column < C.cols(); ++column) {
61 for (Eigen::Index row = column; row < C.rows(); ++row) {
62 const double value =
63 scaling.covariance(C(row, column), row, column);
64 if (!std::isfinite(value)) return false;
65 if (C(row, column) != 0.0 && ++entries > max_index) return false;
66 }
67 }
68
69 detail::CholmodWorkspace workspace;
70 if (!workspace.started) return false;
71 auto& common = workspace.common;
72 common.print = 0;
73 // The supernodal factor runs dense supernodes through dpotrf, dsyrk and
74 // dgemm on the OpenBLAS kernel the CPU selects, so a dense covariance
75 // of order 64 already factored to different bits per OPENBLAS_CORETYPE.
76 // The simplicial up-looking factor calls no BLAS.
77 common.supernodal = CHOLMOD_SIMPLICIAL;
78 common.nmethods = 1;
79 common.method[0].ordering = CHOLMOD_NATURAL;
80 common.postorder = 0;
81 common.final_asis = 0;
82 common.final_ll = 1;
83 common.final_pack = 1;
84 common.final_monotonic = 1;
85 workspace.covariance = cholmod_l_allocate_sparse(n, n, entries, 1, 1, -1,
86 CHOLMOD_REAL, &common);
87 if (!workspace.covariance) return false;
88 auto* column_offsets =
89 static_cast<SuiteSparse_long*>(workspace.covariance->p);
90 auto* rows = static_cast<SuiteSparse_long*>(workspace.covariance->i);
91 auto* values = static_cast<double*>(workspace.covariance->x);
92 std::size_t offset = 0;
93 for (Eigen::Index column = 0; column < C.cols(); ++column) {
94 column_offsets[column] = static_cast<SuiteSparse_long>(offset);
95 for (Eigen::Index row = column; row < C.rows(); ++row) {
96 // Exact zeros are absent from CSC; no tolerance changes the matrix.
97 if (C(row, column) == 0.0) continue;
98 rows[offset] = static_cast<SuiteSparse_long>(row);
99 values[offset] = scaling.covariance(C(row, column), row, column);
100 ++offset;
101 }
102 }
103 column_offsets[n] = static_cast<SuiteSparse_long>(offset);
104 workspace.factor = cholmod_l_analyze(workspace.covariance, &common);
105 if (!workspace.factor || common.status != CHOLMOD_OK) return false;
106 if (!cholmod_l_factorize(workspace.covariance, workspace.factor, &common) ||
107 common.status != CHOLMOD_OK || workspace.factor->minor != n ||
108 !workspace.factor->is_ll || workspace.factor->itype != CHOLMOD_LONG)
109 return false;
110 const auto* permutation =
111 static_cast<const SuiteSparse_long*>(workspace.factor->Perm);
112 if (permutation) {
113 for (std::size_t i = 0; i < n; ++i)
114 if (permutation[i] != static_cast<SuiteSparse_long>(i))
115 return false;
116 }
117 workspace.lower = cholmod_l_factor_to_sparse(workspace.factor, &common);
118 if (!workspace.lower || common.status != CHOLMOD_OK ||
119 workspace.lower->itype != CHOLMOD_LONG ||
120 workspace.lower->xtype != CHOLMOD_REAL ||
121 workspace.lower->dtype != CHOLMOD_DOUBLE || !workspace.lower->packed)
122 return false;
123
124 const auto* lower_offsets =
125 static_cast<const SuiteSparse_long*>(workspace.lower->p);
126 const auto* lower_rows =
127 static_cast<const SuiteSparse_long*>(workspace.lower->i);
128 const auto* lower_values = static_cast<const double*>(workspace.lower->x);
129 EigenMatrix candidate = EigenMatrix::Zero(C.rows(), C.cols());
130 for (Eigen::Index column = 0; column < C.cols(); ++column) {
131 for (auto k = lower_offsets[column]; k < lower_offsets[column + 1];
132 ++k) {
133 const auto row = lower_rows[k];
134 if (row < column || row >= C.rows()) return false;
135 candidate(row, column) =
136 scaling.physicalFactor(lower_values[k], row);
137 }
138 }
139 if (!candidate.allFinite() || !(candidate.diagonal().array() > 0.0).all())
140 return false;
141 L = std::move(candidate);
142 return true;
143}
144
145} // namespace gpr::linalg
146
148
149#endif // GPR_LINALG_CHOLMODBACKEND_H_
bool compute(const Eigen::MatrixBase< Derived > &diagonal)
double physicalFactor(double value, Eigen::Index row) const noexcept
double covariance(double value, Eigen::Index row, Eigen::Index column) const noexcept
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
bool cholesky(const EigenMatrix &C, EigenMatrix &L)
Cholesky decomposition (LAPACK equivalent: dpotrf).
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20