Loading...
Searching...
No Matches
DenseFactorOperations.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3
4#ifndef GPR_LINALG_DENSE_FACTOR_OPERATIONS_H_
5#define GPR_LINALG_DENSE_FACTOR_OPERATIONS_H_
6
7#include <chrono>
8#include <limits>
9
11#include "DenseFactorTrace.h"
12#include "ExactSchur.h"
13#include "LinalgThreads.h"
14#include "TeamFactor.h"
15
16namespace gpr::linalg {
17
18// Every operation uses the supplied physical factor. Factorization workspaces
19// have no role in solving a system or forming its inverse.
20// A vector solve uses Eigen's triangular solve at every OpenMP team
21// size, including one thread, so those bits match a serial build. A
22// matrix substitution uses the fixed-order panel kernels only when the
23// team is larger than one. One configured thread keeps Eigen's solve,
24// which the oracle counts were measured on. The panel substitution does
25// not match that one-thread solve entry for entry.
26inline Eigen::VectorXd forward_solve(const EigenMatrix& L,
27 const Eigen::VectorXd& b)
28{
29#if defined(USE_OPENMP)
30 if (!librarySerialKernels()) {
31 Eigen::VectorXd x = b;
32 teamSolveLowerVector(L, true, x);
33 return x;
34 }
35 const auto solve_start = std::chrono::steady_clock::now();
36 Eigen::VectorXd x = L.triangularView<Eigen::Lower>().solve(b);
37 teamAddSolveWall(std::chrono::duration<double>(
38 std::chrono::steady_clock::now() - solve_start)
39 .count());
40 return x;
41#else
43 return L.triangularView<Eigen::Lower>().solve(b);
44#endif
45}
46
47inline Eigen::VectorXd back_solve(const EigenMatrix& L,
48 const Eigen::VectorXd& b)
49{
50#if defined(USE_OPENMP)
51 if (!librarySerialKernels()) {
52 Eigen::VectorXd x = b;
53 teamSolveLowerVector(L, false, x);
54 return x;
55 }
56 const auto solve_start = std::chrono::steady_clock::now();
57 Eigen::VectorXd x = L.transpose().triangularView<Eigen::Upper>().solve(b);
58 teamAddSolveWall(std::chrono::duration<double>(
59 std::chrono::steady_clock::now() - solve_start)
60 .count());
61 return x;
62#else
64 return L.transpose().triangularView<Eigen::Upper>().solve(b);
65#endif
66}
67
69{
70#if defined(USE_OPENMP)
71 if (teamPanelKernels()) {
72 EigenMatrix X = B;
73 teamSolveLower(L, true, X);
74 return X;
75 }
76 const auto solve_start = std::chrono::steady_clock::now();
77 EigenMatrix X = L.triangularView<Eigen::Lower>().solve(B);
78 teamAddSolveWall(std::chrono::duration<double>(
79 std::chrono::steady_clock::now() - solve_start)
80 .count());
81 return X;
82#else
84 return L.triangularView<Eigen::Lower>().solve(B);
85#endif
86}
87
88inline EigenMatrix back_solve_mat(const EigenMatrix& L, const EigenMatrix& B)
89{
90#if defined(USE_OPENMP)
91 if (teamPanelKernels()) {
92 EigenMatrix X = B;
93 teamSolveLower(L, false, X);
94 return X;
95 }
96 const auto solve_start = std::chrono::steady_clock::now();
97 EigenMatrix X = L.transpose().triangularView<Eigen::Upper>().solve(B);
98 teamAddSolveWall(std::chrono::duration<double>(
99 std::chrono::steady_clock::now() - solve_start)
100 .count());
101 return X;
102#else
104 return L.transpose().triangularView<Eigen::Upper>().solve(B);
105#endif
106}
107
108inline Eigen::VectorXd forward_solve_mat_colwise_squared_norm(
109 const EigenMatrix& L, const EigenMatrix& B)
110{
111 const EigenMatrix X = forward_solve_mat(L, B);
112 return X.colwise().squaredNorm().transpose();
113}
114
115inline Eigen::VectorXd bordered_cholesky_diagonal(const EigenMatrix& L,
116 const EigenMatrix& B,
117 const Eigen::VectorXd& prior)
118{
119 return borderedCholeskyDiagonal(prior, forward_solve_mat(L, B), L);
120}
121
123{
124#if defined(USE_OPENMP)
125 EigenMatrix L_inv;
126 if (teamPanelKernels()) {
127 L_inv = EigenMatrix::Identity(L.rows(), L.cols());
128 teamSolveLower(L, true, L_inv);
129 } else {
130 const auto solve_start = std::chrono::steady_clock::now();
131 L_inv = L.triangularView<Eigen::Lower>().solve(
132 EigenMatrix::Identity(L.rows(), L.cols()));
133 teamAddSolveWall(std::chrono::duration<double>(
134 std::chrono::steady_clock::now() - solve_start)
135 .count());
136 }
137#else
139 EigenMatrix L_inv = L.triangularView<Eigen::Lower>().solve(
140 EigenMatrix::Identity(L.rows(), L.cols()));
141#endif
142 EigenMatrix invC(L.rows(), L.cols());
143#if defined(USE_OPENMP)
144 if (teamPanelKernels()) {
145 // Row panels of B^T B. One thread uses a single dgemm. The panels
146 // write the same bits, so the Gram does not follow the team.
147 teamSymmetricGram(L_inv.data(), L_inv.outerStride(), invC.data(),
148 invC.outerStride(), L_inv.rows());
149 return invC;
150 }
151#endif
152 const auto gram_start = std::chrono::steady_clock::now();
153 invC.noalias() = L_inv.transpose() * L_inv;
154#if defined(USE_OPENMP)
155 teamAddSolveWall(std::chrono::duration<double>(
156 std::chrono::steady_clock::now() - gram_start)
157 .count());
158#else
159 (void)gram_start;
160#endif
161 return invC;
162}
163
164inline double trace_product(const EigenMatrix& A, const EigenMatrix& B)
165{
166 return A.cwiseProduct(B).sum();
167}
168
169inline double log_det_cholesky(const EigenMatrix& L)
170{
171 return L.diagonal().array().log().sum();
172}
173
174} // namespace gpr::linalg
175
176#endif // GPR_LINALG_DENSE_FACTOR_OPERATIONS_H_
@ x
Definition Enums.h:28
Correctly rounded Schur-complement diagonals from Cholesky solves.
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
void teamSolveLowerVector(const double *lower, std::int64_t lda, bool lower_factor, double *rhs, std::int64_t n)
EigenMatrix back_solve_mat(const EigenMatrix &L, const EigenMatrix &B)
Back triangular solve with matrix RHS (BLAS equivalent: dtrsm with side=L, uplo=L,...
void teamSolveLower(const double *lower, std::int64_t lda, bool lower_factor, double *rhs, std::int64_t ldb, std::int64_t n, std::int64_t cols)
Eigen::VectorXd forward_solve(const EigenMatrix &L, const Eigen::VectorXd &b)
Forward triangular solve with vector RHS (BLAS equivalent: dtrsv).
bool teamPanelKernels() noexcept
EigenMatrix forward_solve_mat(const EigenMatrix &L, const EigenMatrix &B)
Forward triangular solve with matrix RHS (BLAS equivalent: dtrsm).
void teamSymmetricGram(const double *b, std::int64_t ldb, double *c, std::int64_t ldc, std::int64_t n)
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).
bool librarySerialKernels() noexcept
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).
void teamAddSolveWall(double seconds)
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
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20