Loading...
Searching...
No Matches
CholeskyPanels.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#ifndef GPR_LINALG_CHOLESKYPANELS_H_
4#define GPR_LINALG_CHOLESKYPANELS_H_
5
6#include <Eigen/Core>
7#include <algorithm>
8#include <functional>
9#include <limits>
10#include <memory>
11#include <stdexcept>
12#include <utility>
13#include <vector>
14
15#include "TileParallel.h"
16
17namespace gpr::linalg {
18
22public:
23 static constexpr Eigen::Index panel_columns = 64;
27 static constexpr Eigen::Index row_block = 512;
28
29 CholeskyPanels() = default;
30
31 explicit CholeskyPanels(Eigen::Index order,
32 Eigen::Index width = panel_columns)
33 : CholeskyPanels(order, width, {})
34 {
35 }
36
37private:
39 using StorageAllocator =
40 std::function<std::shared_ptr<double>(Eigen::Index)>;
41
42 CholeskyPanels(Eigen::Index order, Eigen::Index width,
43 const StorageAllocator& allocate)
44 : order_(order)
45 {
46 if (order < 0 || width <= 0)
47 throw std::invalid_argument("invalid Cholesky panel shape");
48 Eigen::Index entries = 0;
49 for (Eigen::Index first = 0; first < order;) {
50 const Eigen::Index rows = order - first;
51 const Eigen::Index columns = std::min(width, rows);
52 if (rows >
53 (std::numeric_limits<Eigen::Index>::max() - entries) / columns)
54 throw std::length_error("Cholesky panel storage is too large");
55 panels_.push_back({first, columns, entries});
56 entries += rows * columns;
57 first += columns;
58 }
59 entries_ = entries;
60 if (allocate) shared_values_ = allocate(entries);
61 if (!shared_values_) values_.resize(entries);
62 }
63
64public:
65 static CholeskyPanels fromLower(const Eigen::MatrixXd& lower)
66 {
67 if (lower.rows() != lower.cols())
68 throw std::invalid_argument("Cholesky factor must be square");
69 CholeskyPanels result(lower.rows());
70 for (Eigen::Index i = 0; i < result.panelCount(); ++i) {
71 const auto& part = result.panels_[i];
72 result.panel(i) =
73 lower.block(part.first, part.first, lower.rows() - part.first,
74 part.columns);
75 }
76 return result;
77 }
78
81 [[nodiscard]] Eigen::MatrixXd toLower() const
82 {
83 Eigen::MatrixXd lower = Eigen::MatrixXd::Zero(order_, order_);
84 for (Eigen::Index i = 0; i < panelCount(); ++i) {
85 const auto& part = panels_[static_cast<std::size_t>(i)];
86 lower.block(part.first, part.first, order_ - part.first,
87 part.columns) = panel(i);
88 }
89 return lower;
90 }
91
92 [[nodiscard]] Eigen::Index panelCount() const noexcept
93 {
94 return static_cast<Eigen::Index>(panels_.size());
95 }
96
97 [[nodiscard]] Eigen::Map<Eigen::MatrixXd> panel(Eigen::Index index)
98 {
99 const auto& part = panels_.at(static_cast<std::size_t>(index));
100 if (shared_values_ && shared_read_only_) {
101 values_ = Eigen::Map<const Eigen::VectorXd>(shared_values_.get(),
102 entries_);
103 shared_values_.reset();
104 shared_read_only_ = false;
105 }
106 double* data = shared_values_ ? shared_values_.get() : values_.data();
107 return {data + part.offset, order_ - part.first, part.columns};
108 }
109
110 [[nodiscard]] Eigen::Map<const Eigen::MatrixXd> panel(
111 Eigen::Index index) const
112 {
113 const auto& part = panels_.at(static_cast<std::size_t>(index));
114 const double* data =
115 shared_values_ ? shared_values_.get() : values_.data();
116 return {data + part.offset, order_ - part.first, part.columns};
117 }
118
119 [[nodiscard]] Eigen::Index rows() const noexcept
120 {
121 return order_;
122 }
123
124 [[nodiscard]] Eigen::Index storedElements() const noexcept
125 {
126 return entries_;
127 }
128
130 [[nodiscard]] bool hasSharedStorage() const noexcept
131 {
132 return shared_values_ != nullptr;
133 }
134
135 template <typename Derived>
136 void forwardSolveInPlace(Eigen::MatrixBase<Derived>& right) const
137 {
138 if (right.rows() != order_)
139 throw std::invalid_argument("invalid Cholesky solve dimensions");
140 // Eigen's triangular solve always forms coeffRef(0, 0), which binds
141 // a null reference when a panel or the right-hand side is empty,
142 // and a later panel's middleRows would offset that null pointer.
143 if (order_ == 0 || right.cols() == 0) return;
144 Eigen::Index first = 0;
145 for (Eigen::Index i = 0; i < panelCount(); ++i) {
146 const auto block = panel(i);
147 const Eigen::Index columns = block.cols();
148 const Eigen::Index trailing = block.rows() - columns;
149 auto current = right.derived().middleRows(first, columns);
150 block.topRows(columns).triangularView<Eigen::Lower>().solveInPlace(
151 current);
152 if (trailing > 0) {
153 // Disjoint row blocks of the update run on the tile threads.
154 const Eigen::Index begin = first + columns;
155 const Eigen::Index chunks = blockCount(begin);
156 parallelFor(static_cast<long>(chunks), [&](long c) {
157 Eigen::Index r0, r1;
158 blockRange(begin, c, r0, r1);
159 right.derived().middleRows(r0, r1 - r0).noalias() -=
160 block.middleRows(r0 - first, r1 - r0) * current;
161 });
162 }
163 first += columns;
164 }
165 }
166
167 template <typename Derived>
168 void solveInPlace(Eigen::MatrixBase<Derived>& right) const
169 {
170 forwardSolveInPlace(right);
171 if (order_ == 0 || right.cols() == 0) return;
172 Eigen::Index first = order_;
173 std::vector<Eigen::MatrixXd> partial;
174 for (Eigen::Index i = panelCount(); i > 0;) {
175 const auto block = panel(--i);
176 const Eigen::Index columns = block.cols();
177 const Eigen::Index trailing = block.rows() - columns;
178 first -= columns;
179 auto current = right.derived().middleRows(first, columns);
180 if (trailing > 0) {
181 // The sum over the trailing rows splits into row blocks whose
182 // products are taken independently and subtracted in
183 // ascending block order.
184 const Eigen::Index begin = first + columns;
185 const Eigen::Index chunks = blockCount(begin);
186 if (static_cast<Eigen::Index>(partial.size()) < chunks)
187 partial.resize(static_cast<std::size_t>(chunks));
188 parallelFor(static_cast<long>(chunks), [&](long c) {
189 Eigen::Index r0, r1;
190 blockRange(begin, c, r0, r1);
191 partial[static_cast<std::size_t>(c)].noalias() =
192 block.middleRows(r0 - first, r1 - r0).transpose() *
193 right.derived().middleRows(r0, r1 - r0);
194 });
195 for (Eigen::Index c = 0; c < chunks; ++c)
196 current -= partial[static_cast<std::size_t>(c)];
197 }
198 block.topRows(columns)
199 .transpose()
200 .triangularView<Eigen::Upper>()
201 .solveInPlace(current);
202 }
203 }
204
205private:
207 Eigen::Index blockCount(Eigen::Index begin) const noexcept
208 {
209 return (order_ - 1) / row_block - begin / row_block + 1;
210 }
211 void blockRange(Eigen::Index begin, Eigen::Index c, Eigen::Index& r0,
212 Eigen::Index& r1) const noexcept
213 {
214 r0 = std::max(begin, (begin / row_block + c) * row_block);
215 r1 = std::min(order_, (begin / row_block + c + 1) * row_block);
216 }
217
218 struct Panel {
219 Eigen::Index first;
220 Eigen::Index columns;
221 Eigen::Index offset;
222 };
223 std::vector<Panel> panels_;
224 // One allocation keeps retired factors from fragmenting the process heap.
225 Eigen::VectorXd values_;
226 std::shared_ptr<double> shared_values_;
227 Eigen::Index entries_ = 0;
228 bool shared_read_only_ = false;
229 Eigen::Index order_ = 0;
230};
231
232} // namespace gpr::linalg
233
234#endif
static constexpr Eigen::Index panel_columns
Eigen::Map< const Eigen::MatrixXd > panel(Eigen::Index index) const
static constexpr Eigen::Index row_block
Eigen::MatrixXd toLower() const
Eigen::Map< Eigen::MatrixXd > panel(Eigen::Index index)
void forwardSolveInPlace(Eigen::MatrixBase< Derived > &right) const
void solveInPlace(Eigen::MatrixBase< Derived > &right) const
Eigen::Index panelCount() const noexcept
bool hasSharedStorage() const noexcept
Immutable factor storage shared by MPI ranks on the same host.
Eigen::Index rows() const noexcept
Eigen::Index storedElements() const noexcept
CholeskyPanels(Eigen::Index order, Eigen::Index width=panel_columns)
static CholeskyPanels fromLower(const Eigen::MatrixXd &lower)
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
void parallelFor(long count, F &&fn)
parallelFor(count, fn): see TilePool::run.