GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
SketchedHessianH0.cpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) gpridge developers
3
// See LICENSE for details.
4
5
#include "
SketchedHessianH0.h
"
6
7
#include <algorithm>
8
#include <cmath>
9
#include <stdexcept>
10
#include <vector>
11
12
namespace
gpr
{
13
namespace
optim
{
14
15
namespace
{
16
17
// splitmix64 sample stream -> Box-Muller -> standard Gaussian. Seeded
18
// identically on every rank so Omega is bit-reproducible without comm.
19
struct
GaussianSampler {
20
uint64_t state;
21
22
explicit
GaussianSampler(uint64_t seed) : state(seed != 0 ? seed : 1ULL) { }
23
24
[[nodiscard]]
double
next() noexcept
25
{
26
state += 0x9E3779B97F4A7C15ULL;
27
uint64_t z1 = state;
28
z1 = (z1 ^ (z1 >> 30)) * 0xBF58476D1CE4E5B9ULL;
29
z1 = (z1 ^ (z1 >> 27)) * 0x94D049BB133111EBULL;
30
z1 ^= z1 >> 31;
31
state += 0x9E3779B97F4A7C15ULL;
32
uint64_t z2 = state;
33
z2 = (z2 ^ (z2 >> 30)) * 0xBF58476D1CE4E5B9ULL;
34
z2 = (z2 ^ (z2 >> 27)) * 0x94D049BB133111EBULL;
35
z2 ^= z2 >> 31;
36
// Box-Muller; +1e-12 keeps u1 strictly positive for log.
37
const
double
u1 =
38
static_cast<
double
>
(z1 & 0xFFFFFFFFULL) / 4294967296.0 + 1e-12;
39
const
double
u2 =
static_cast<
double
>
(z2 >> 32) / 4294967296.0;
40
return
std::sqrt(-2.0 * std::log(u1)) * std::cos(2.0 * M_PI * u2);
41
}
42
};
43
44
}
// namespace
45
46
SketchedHessianH0::SketchedHessianH0
(
int
n_dof
,
int
sketch_rank
, uint64_t seed,
47
double
tikhonov_eps)
48
: n_dof_(
n_dof
),
49
sketch_rank_(
std
::min(
std
::max(
sketch_rank
, 1),
n_dof
)),
50
seed_(seed != 0 ? seed : 1ULL),
51
tikhonov_eps_(tikhonov_eps)
52
{
53
if
(n_dof_ <= 0) {
54
throw
std::invalid_argument(
55
"SketchedHessianH0: n_dof must be positive"
);
56
}
57
Omega_.resize(n_dof_, sketch_rank_);
58
Y_.setZero(n_dof_, sketch_rank_);
59
60
// Replicated Gaussian sketch. Same seed -> same Omega on every
61
// rank, no MPI needed.
62
GaussianSampler sampler(seed_);
63
for
(
int
j = 0; j < sketch_rank_; ++j) {
64
for
(
int
i = 0; i < n_dof_; ++i) {
65
Omega_(i, j) = sampler.next();
66
}
67
}
68
}
69
70
void
SketchedHessianH0::rebuild
(
const
HessianVectorProductFn
& hvp)
71
{
72
if
(!hvp) {
73
throw
std::invalid_argument(
74
"SketchedHessianH0::rebuild: HvP functor is null"
);
75
}
76
77
int
rank = 0;
78
int
nprocs = 1;
79
#ifdef GPR_HAVE_MPI
80
int
mpi_up = 0;
81
MPI_Initialized(&mpi_up);
82
if
(mpi_up) {
83
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
84
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
85
}
86
#endif
87
88
// Column-distribute the k Y-columns across ranks. Each rank
89
// evaluates a disjoint slice of HvPs; no rank duplicates work
90
// unless k < nprocs (then trailing ranks idle, as expected for
91
// very small sketches).
92
Eigen::MatrixXd Y_local = Eigen::MatrixXd::Zero(n_dof_, sketch_rank_);
93
for
(
int
j = 0; j < sketch_rank_; ++j) {
94
if
(j % nprocs == rank) {
95
Y_local.col(j) = hvp(Omega_.col(j));
96
}
97
}
98
99
#ifdef GPR_HAVE_MPI
100
if
(mpi_up && nprocs > 1) {
101
// Single Allreduce-SUM stitches the disjoint column stripes
102
// into a globally-replicated Y. Cheaper than Allgather with
103
// strided source offsets and degenerates correctly when
104
// sketch_rank < nprocs (idle ranks contribute zeros).
105
Y_.resize(n_dof_, sketch_rank_);
106
MPI_Allreduce(Y_local.data(), Y_.data(),
107
static_cast<
int
>
(Y_local.size()), MPI_DOUBLE, MPI_SUM,
108
MPI_COMM_WORLD);
109
}
else
110
#endif
111
{
112
Y_ = std::move(Y_local);
113
}
114
115
// Mid = Omega^T Y. Tiny (k x k); replicated on every rank from
116
// identical Omega and Y. Symmetrise to absorb FD asymmetry, then
117
// LDLT with a Tikhonov shift so the apply stays defined when the
118
// HvP range is rank-deficient (Frangella-Tropp-Udell 2023, eq.
119
// 2.4).
120
Eigen::MatrixXd mid = Omega_.transpose() * Y_;
121
mid = (0.5 * (mid + mid.transpose())).eval();
122
mid.diagonal().array() += tikhonov_eps_;
123
mid_ldlt_.compute(mid);
124
if
(mid_ldlt_.info() != Eigen::Success) {
125
throw
std::runtime_error(
126
"SketchedHessianH0::rebuild: LDLT of Nystrom mid-matrix failed"
);
127
}
128
ready_ =
true
;
129
}
130
131
Eigen::VectorXd
SketchedHessianH0::apply
(
const
Eigen::VectorXd& q,
132
double
gamma_residual)
const
133
{
134
if
(!ready_) {
135
// Fall back to the legacy scalar H0 when the sketch hasn't
136
// been built yet (first L-BFGS step of the first outer iter).
137
return
gamma_residual * q;
138
}
139
if
(q.size() != n_dof_) {
140
throw
std::invalid_argument(
141
"SketchedHessianH0::apply: q size mismatch with n_dof"
);
142
}
143
const
Eigen::VectorXd Otq = Omega_.transpose() * q;
144
const
Eigen::VectorXd Ytq = Y_.transpose() * q;
145
const
Eigen::VectorXd rhs = Otq - gamma_residual * Ytq;
146
const
Eigen::VectorXd inner = mid_ldlt_.solve(rhs);
147
return
gamma_residual * q + Omega_ * inner;
148
}
149
150
}
// namespace optim
151
}
// namespace gpr
SketchedHessianH0.h
gpr::optim::SketchedHessianH0::rebuild
void rebuild(const HessianVectorProductFn &hvp)
Definition
SketchedHessianH0.cpp:70
gpr::optim::SketchedHessianH0::sketch_rank
int sketch_rank() const noexcept
Definition
SketchedHessianH0.h:106
gpr::optim::SketchedHessianH0::HessianVectorProductFn
std::function< Eigen::VectorXd(const Eigen::VectorXd &)> HessianVectorProductFn
Definition
SketchedHessianH0.h:73
gpr::optim::SketchedHessianH0::apply
Eigen::VectorXd apply(const Eigen::VectorXd &q, double gamma_residual) const
Definition
SketchedHessianH0.cpp:131
gpr::optim::SketchedHessianH0::SketchedHessianH0
SketchedHessianH0(int n_dof, int sketch_rank, uint64_t seed, double tikhonov_eps=1e-10)
Definition
SketchedHessianH0.cpp:46
gpr::optim::SketchedHessianH0::n_dof
int n_dof() const noexcept
Definition
SketchedHessianH0.h:110
gpr::optim
Definition
ConsistentForceModel.h:13
gpr
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition
Macros.h:16
std
STL namespace.
gpr
optimization
SketchedHessianH0.cpp
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf