GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
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
17
namespace
gpr::linalg
{
18
21
class
CholeskyPanels
{
22
public
:
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
37
private
:
38
friend
class
ScalapackPDPotrfInverse
;
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
64
public
:
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
205
private
:
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
TileParallel.h
gpr::linalg::CholeskyPanels::panel_columns
static constexpr Eigen::Index panel_columns
Definition
CholeskyPanels.h:23
gpr::linalg::CholeskyPanels::panel
Eigen::Map< const Eigen::MatrixXd > panel(Eigen::Index index) const
Definition
CholeskyPanels.h:110
gpr::linalg::CholeskyPanels::row_block
static constexpr Eigen::Index row_block
Definition
CholeskyPanels.h:27
gpr::linalg::CholeskyPanels::toLower
Eigen::MatrixXd toLower() const
Definition
CholeskyPanels.h:81
gpr::linalg::CholeskyPanels::panel
Eigen::Map< Eigen::MatrixXd > panel(Eigen::Index index)
Definition
CholeskyPanels.h:97
gpr::linalg::CholeskyPanels::CholeskyPanels
CholeskyPanels()=default
gpr::linalg::CholeskyPanels::forwardSolveInPlace
void forwardSolveInPlace(Eigen::MatrixBase< Derived > &right) const
Definition
CholeskyPanels.h:136
gpr::linalg::CholeskyPanels::solveInPlace
void solveInPlace(Eigen::MatrixBase< Derived > &right) const
Definition
CholeskyPanels.h:168
gpr::linalg::CholeskyPanels::panelCount
Eigen::Index panelCount() const noexcept
Definition
CholeskyPanels.h:92
gpr::linalg::CholeskyPanels::ScalapackPDPotrfInverse
friend class ScalapackPDPotrfInverse
Definition
CholeskyPanels.h:38
gpr::linalg::CholeskyPanels::hasSharedStorage
bool hasSharedStorage() const noexcept
Immutable factor storage shared by MPI ranks on the same host.
Definition
CholeskyPanels.h:130
gpr::linalg::CholeskyPanels::rows
Eigen::Index rows() const noexcept
Definition
CholeskyPanels.h:119
gpr::linalg::CholeskyPanels::storedElements
Eigen::Index storedElements() const noexcept
Definition
CholeskyPanels.h:124
gpr::linalg::CholeskyPanels::CholeskyPanels
CholeskyPanels(Eigen::Index order, Eigen::Index width=panel_columns)
Definition
CholeskyPanels.h:31
gpr::linalg::CholeskyPanels::fromLower
static CholeskyPanels fromLower(const Eigen::MatrixXd &lower)
Definition
CholeskyPanels.h:65
gpr::linalg
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Definition
ConditionedKernel.h:14
gpr::linalg::parallelFor
void parallelFor(long count, F &&fn)
parallelFor(count, fn): see TilePool::run.
Definition
TileParallel.h:277
gpr
linalg
CholeskyPanels.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf