Loading...
Searching...
No Matches
KokkosBackend.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#pragma once
4
5#include <Eigen/Core>
6#include <cstddef>
7#include <limits>
8#include <memory>
9#include <stdexcept>
10
12#include "BackendInterface.h"
13#include "KokkosWorkspace.h"
14
15#if !defined(GPR_KOKKOS_EXECUTION_KIND) || !defined(GPR_KOKKOS_MEMORY_KIND)
16#error \
17 "KokkosBackend.h needs GPR_KOKKOS_EXECUTION_KIND and GPR_KOKKOS_MEMORY_KIND from the build"
18#endif
19
20namespace gpr::linalg {
21
22template <>
23struct BackendTraits<backend::KokkosTag> {
25 using vector_type = Eigen::VectorXd;
26 static constexpr const char* name = "kokkos";
27 // Fixed by the Kokkos installation. The build reads its device list and
28 // passes the result in, because only the device translation units may
29 // include Kokkos headers (meson.build, linalg_backend == 'kokkos').
30 static constexpr ExecutionKind execution =
31 ExecutionKind::GPR_KOKKOS_EXECUTION_KIND;
32 static constexpr MemoryKind memory = MemoryKind::GPR_KOKKOS_MEMORY_KIND;
33 static constexpr ArithmeticClass arithmetic =
35 // The Backend.h operation set takes host Eigen operands, so each call
36 // stages them; KokkosWorkspace and KokkosFactor keep the physical factor
37 // in execution memory across solves.
38 static constexpr bool resident = false;
39 static constexpr bool retains_factor = true;
40 static constexpr bool distributed = false;
41};
42
45bool cholesky(const EigenMatrix& C, EigenMatrix& L);
46Eigen::VectorXd forward_solve(const EigenMatrix& factor,
47 const Eigen::VectorXd& rhs);
48Eigen::VectorXd back_solve(const EigenMatrix& factor,
49 const Eigen::VectorXd& rhs);
51 const EigenMatrix& rhs);
52EigenMatrix back_solve_mat(const EigenMatrix& factor, const EigenMatrix& rhs);
54 const EigenMatrix& factor, const EigenMatrix& rhs);
55Eigen::VectorXd bordered_cholesky_diagonal(const EigenMatrix& factor,
56 const EigenMatrix& rhs,
57 const Eigen::VectorXd& prior);
59double trace_product(const EigenMatrix& first, const EigenMatrix& second);
60double log_det_cholesky(const EigenMatrix& factor);
61
62namespace detail {
63struct KokkosDeviceFactor;
64inline int kokkosSolverExtent(std::size_t extent)
65{
66 if (extent > static_cast<std::size_t>(std::numeric_limits<int>::max()))
67 throw std::overflow_error(
68 "Kokkos factor exceeds the solver integer range");
69 return static_cast<int>(extent);
70}
71} // namespace detail
72
74 std::size_t calls = 0;
75 std::size_t bytes = 0;
76};
77
96
97void resetKokkosTransferAuditStats() noexcept;
99
105
107public:
108 explicit KokkosFactor(const EigenMatrix& factor);
109 KokkosFactor(const KokkosFactor&) = delete;
111
112 [[nodiscard]] Eigen::Index order() const noexcept
113 {
114 return order_;
115 }
116 EigenMatrix forward(const EigenMatrix& rhs) const;
117 EigenMatrix backward(const EigenMatrix& rhs) const;
118 EigenMatrix solve(const EigenMatrix& rhs) const;
119 Eigen::VectorXd squaredNorms(const EigenMatrix& rhs) const;
120 EigenMatrix inverse() const;
121 [[nodiscard]] KokkosTransferStats transferStats() const;
122 void resetTransferStats();
123 double halfLogDeterminant() const;
125 double lowerFrobeniusNorm() const;
131 double schurRelativeError() const;
132
133private:
135 explicit KokkosFactor(const detail::KokkosDeviceFactor& factor);
136 Eigen::Index order_;
137 mutable KokkosWorkspace workspace_;
138};
139} // namespace gpr::linalg
Vocabulary of the swappable dense linear-algebra backend.
KokkosFactor(const KokkosFactor &)=delete
KokkosFactor & operator=(const KokkosFactor &)=delete
KokkosFactor(const EigenMatrix &factor)
friend class KokkosMolecularPosterior
Eigen::Index order() const noexcept
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).
ExecutionKind
Which units run the operation.
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).
void resetKokkosTransferAuditStats() noexcept
KokkosTransferAuditStats kokkosTransferAuditStats() noexcept
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20
static constexpr ArithmeticClass arithmetic
KokkosTransferDirectionStats device_to_device
KokkosTransferDirectionStats host_to_host
KokkosTransferDirectionStats device_to_host
KokkosTransferDirectionStats boundary_to_execution
KokkosTransferDirectionStats host_to_device
KokkosTransferDirectionStats boundary_to_host