GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
TileCholesky.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) gpridge developers
3
// See LICENSE for details.
4
35
36
#ifndef GPR_LINALG_TILE_CHOLESKY_H_
37
#define GPR_LINALG_TILE_CHOLESKY_H_
38
39
#include <Eigen/Cholesky>
40
#include <Eigen/Core>
41
#include <cassert>
42
#include <vector>
43
44
#include "
../../backend/Macros.h
"
45
46
#ifdef GPR_HAVE_MPI
47
#include <mpi.h>
48
#endif
49
50
namespace
gpr
{
51
namespace
linalg
{
52
53
class
TileCholesky
{
54
public
:
55
TileCholesky
(
int
n
,
int
tile_size
,
int
nprocs,
int
rank)
56
: n_(
n
),
57
mb_(
tile_size
> 0 ?
tile_size
: 64),
58
nprocs_(nprocs > 0 ? nprocs : 1),
59
rank_(rank)
60
{
61
n_tiles_ = (n_ + mb_ - 1) / mb_;
62
tiles_.assign(
static_cast<
size_t
>
(n_tiles_) * n_tiles_,
EigenMatrix
());
63
}
64
66
[[nodiscard]]
int
owner_of
(
int
i_tile)
const
noexcept
67
{
68
return
i_tile % nprocs_;
69
}
70
72
[[nodiscard]]
size_t
index
(
int
i,
int
j)
const
noexcept
73
{
74
return
static_cast<
size_t
>
(i) * n_tiles_ + j;
75
}
76
78
[[nodiscard]]
int
rows_of
(
int
i_tile)
const
noexcept
79
{
80
return
(i_tile == n_tiles_ - 1) ? n_ - i_tile * mb_ : mb_;
81
}
82
86
void
load
(
const
EigenMatrix
& C)
87
{
88
assert(C.rows() == n_ && C.cols() == n_);
89
for
(
int
i = 0; i < n_tiles_; ++i) {
90
const
int
owner =
owner_of
(i);
91
if
(owner != rank_)
continue
;
92
for
(
int
j = 0; j <= i; ++j) {
93
const
int
br =
rows_of
(i);
94
const
int
bc =
rows_of
(j);
95
tiles_[
index
(i, j)] = C.block(i * mb_, j * mb_, br, bc);
96
}
97
}
98
}
99
102
[[nodiscard]]
bool
factor
()
103
{
104
for
(
int
j = 0; j < n_tiles_; ++j) {
105
// Step 1: owner of (j,j) factors the diagonal tile in place.
106
const
int
diag_owner =
owner_of
(j);
107
const
int
bj =
rows_of
(j);
108
EigenMatrix
L_jj(bj, bj);
109
if
(diag_owner == rank_) {
110
EigenMatrix
& A_jj = tiles_[
index
(j, j)];
111
Eigen::LLT<EigenMatrix> llt(A_jj);
112
if
(llt.info() == Eigen::NumericalIssue)
return
false
;
113
A_jj = llt.matrixL();
114
L_jj = A_jj;
115
}
116
#ifdef GPR_HAVE_MPI
117
if
(nprocs_ > 1) {
118
MPI_Bcast(L_jj.data(), bj * bj, MPI_DOUBLE, diag_owner,
119
MPI_COMM_WORLD);
120
}
121
#endif
122
123
// Step 2: panel-column solve. Owner of each (i, j) for i > j
124
// computes L_ij = A_ij @ L_jj^{-T}.
125
for
(
int
i = j + 1; i < n_tiles_; ++i) {
126
const
int
panel_owner =
owner_of
(i);
127
if
(panel_owner != rank_)
continue
;
128
const
int
bi =
rows_of
(i);
129
EigenMatrix
& A_ij = tiles_[
index
(i, j)];
130
// Solve A_ij L_jj^T = L_ij <=> L_jj X^T = A_ij^T
131
A_ij = L_jj.template triangularView<Eigen::Lower>()
132
.transpose()
133
.template solve<Eigen::OnTheRight>(A_ij);
134
(void)bi;
135
}
136
137
// Step 3: trailing update. For each (i, k) with j < k <= i:
138
// A_ik -= L_ij @ L_kj^T. Each rank performs the update for the
139
// tiles it owns; the panel-column tiles needed (L_ij, L_kj) for
140
// the current j may live on other ranks, so broadcast the
141
// panel column from each row-owner.
142
std::vector<EigenMatrix> panel_col(n_tiles_);
143
for
(
int
i = j + 1; i < n_tiles_; ++i) {
144
const
int
panel_owner =
owner_of
(i);
145
const
int
bi =
rows_of
(i);
146
panel_col[i].resize(bi, bj);
147
if
(panel_owner == rank_) {
148
panel_col[i] = tiles_[
index
(i, j)];
149
}
150
#ifdef GPR_HAVE_MPI
151
if
(nprocs_ > 1) {
152
MPI_Bcast(panel_col[i].data(), bi * bj, MPI_DOUBLE,
153
panel_owner, MPI_COMM_WORLD);
154
}
155
#endif
156
}
157
158
for
(
int
i = j + 1; i < n_tiles_; ++i) {
159
const
int
row_owner =
owner_of
(i);
160
if
(row_owner != rank_)
continue
;
161
for
(
int
k = j + 1; k <= i; ++k) {
162
EigenMatrix
& A_ik = tiles_[
index
(i, k)];
163
A_ik.noalias() -= panel_col[i] * panel_col[k].transpose();
164
}
165
}
166
}
167
return
true
;
168
}
169
173
[[nodiscard]]
EigenMatrix
gather_L
()
const
174
{
175
EigenMatrix
L = EigenMatrix::Zero(n_, n_);
176
for
(
int
i = 0; i < n_tiles_; ++i) {
177
for
(
int
j = 0; j <= i; ++j) {
178
const
int
owner =
owner_of
(i);
179
const
int
br =
rows_of
(i);
180
const
int
bc =
rows_of
(j);
181
EigenMatrix
tile(br, bc);
182
if
(owner == rank_) {
183
tile = tiles_[
index
(i, j)];
184
}
185
#ifdef GPR_HAVE_MPI
186
if
(nprocs_ > 1) {
187
MPI_Bcast(tile.data(), br * bc, MPI_DOUBLE, owner,
188
MPI_COMM_WORLD);
189
}
190
#endif
191
L.block(i * mb_, j * mb_, br, bc) = tile;
192
}
193
}
194
return
L;
195
}
196
200
[[nodiscard]] Eigen::VectorXd
solve_lower
(
const
Eigen::VectorXd& b)
const
201
{
202
Eigen::VectorXd
x
= b;
203
for
(
int
i = 0; i < n_tiles_; ++i) {
204
const
int
br =
rows_of
(i);
205
const
int
row_start = i * mb_;
206
const
int
owner =
owner_of
(i);
207
Eigen::VectorXd x_i(br);
208
if
(owner == rank_) {
209
Eigen::VectorXd rhs =
x
.segment(row_start, br);
210
for
(
int
j = 0; j < i; ++j) {
211
const
int
bc =
rows_of
(j);
212
const
int
col_start = j * mb_;
213
rhs.noalias() -=
214
tiles_[
index
(i, j)] *
x
.segment(col_start, bc);
215
}
216
x_i = tiles_[
index
(i, i)]
217
.template triangularView<Eigen::Lower>()
218
.solve(rhs);
219
}
220
#ifdef GPR_HAVE_MPI
221
if
(nprocs_ > 1) {
222
MPI_Bcast(x_i.data(), br, MPI_DOUBLE, owner, MPI_COMM_WORLD);
223
}
224
#endif
225
x
.segment(row_start, br) = x_i;
226
}
227
return
x
;
228
}
229
231
[[nodiscard]] Eigen::VectorXd
solve_upper
(
const
Eigen::VectorXd& b)
const
232
{
233
Eigen::VectorXd
x
= b;
234
for
(
int
i = n_tiles_ - 1; i >= 0; --i) {
235
const
int
br =
rows_of
(i);
236
const
int
row_start = i * mb_;
237
const
int
owner =
owner_of
(i);
238
Eigen::VectorXd x_i(br);
239
if
(owner == rank_) {
240
Eigen::VectorXd rhs =
x
.segment(row_start, br);
241
for
(
int
j = i + 1; j < n_tiles_; ++j) {
242
const
int
bc =
rows_of
(j);
243
const
int
col_start = j * mb_;
244
// L^T_ij = L_ji^T (we own L_ji because owner_of(j) may
245
// differ; broadcast required if not owned)
246
EigenMatrix
L_ji_T(br, bc);
247
if
(
owner_of
(j) == rank_) {
248
L_ji_T = tiles_[
index
(j, i)].transpose();
249
}
250
#ifdef GPR_HAVE_MPI
251
if
(nprocs_ > 1) {
252
MPI_Bcast(L_ji_T.data(), br * bc, MPI_DOUBLE,
253
owner_of
(j), MPI_COMM_WORLD);
254
}
255
#endif
256
rhs.noalias() -= L_ji_T *
x
.segment(col_start, bc);
257
}
258
x_i = tiles_[
index
(i, i)]
259
.template triangularView<Eigen::Lower>()
260
.transpose()
261
.solve(rhs);
262
}
263
#ifdef GPR_HAVE_MPI
264
if
(nprocs_ > 1) {
265
MPI_Bcast(x_i.data(), br, MPI_DOUBLE, owner, MPI_COMM_WORLD);
266
}
267
#endif
268
x
.segment(row_start, br) = x_i;
269
}
270
return
x
;
271
}
272
273
[[nodiscard]]
int
n
() const noexcept
274
{
275
return
n_;
276
}
277
[[nodiscard]]
int
tile_size
() const noexcept
278
{
279
return
mb_;
280
}
281
[[nodiscard]]
int
n_tiles
() const noexcept
282
{
283
return
n_tiles_;
284
}
285
286
private
:
287
int
n_;
288
int
mb_;
289
int
nprocs_;
290
int
rank_;
291
int
n_tiles_;
292
std::vector<EigenMatrix> tiles_;
293
};
294
295
}
// namespace linalg
296
}
// namespace gpr
297
298
#endif
// GPR_LINALG_TILE_CHOLESKY_H_
x
@ x
Definition
Enums.h:28
Macros.h
gpr::linalg::TileCholesky::solve_upper
Eigen::VectorXd solve_upper(const Eigen::VectorXd &b) const
Triangular solve L^T x = b.
Definition
TileCholesky.h:231
gpr::linalg::TileCholesky::gather_L
EigenMatrix gather_L() const
Definition
TileCholesky.h:173
gpr::linalg::TileCholesky::owner_of
int owner_of(int i_tile) const noexcept
Tile owner is a deterministic function of (i, nprocs).
Definition
TileCholesky.h:66
gpr::linalg::TileCholesky::n
int n() const noexcept
Definition
TileCholesky.h:273
gpr::linalg::TileCholesky::factor
bool factor()
Definition
TileCholesky.h:102
gpr::linalg::TileCholesky::n_tiles
int n_tiles() const noexcept
Definition
TileCholesky.h:281
gpr::linalg::TileCholesky::solve_lower
Eigen::VectorXd solve_lower(const Eigen::VectorXd &b) const
Definition
TileCholesky.h:200
gpr::linalg::TileCholesky::index
size_t index(int i, int j) const noexcept
Linear index into the tiles_ vector.
Definition
TileCholesky.h:72
gpr::linalg::TileCholesky::load
void load(const EigenMatrix &C)
Definition
TileCholesky.h:86
gpr::linalg::TileCholesky::TileCholesky
TileCholesky(int n, int tile_size, int nprocs, int rank)
Definition
TileCholesky.h:55
gpr::linalg::TileCholesky::rows_of
int rows_of(int i_tile) const noexcept
Tile size at (i, j) accounting for the trailing remainder.
Definition
TileCholesky.h:78
gpr::linalg::TileCholesky::tile_size
int tile_size() const noexcept
Definition
TileCholesky.h:277
gpr::linalg
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Definition
ConditionedKernel.h:14
gpr
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition
Macros.h:16
gpr::EigenMatrix
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition
Macros.h:20
gpr
linalg
TileCholesky.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf