Build Packages / Create release (push) Successful in 9s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m19s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 10m26s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 19m33s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 23m17s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 23m28s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 24m38s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 27m30s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 29m4s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 29m19s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 29m31s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 23m19s
Build Packages / Generate python client (push) Successful in 28s
Build Packages / Build documentation (push) Successful in 1m22s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 24m39s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 29m40s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 28m31s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 24m33s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 27m45s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 29m25s
Build Packages / Unit tests (push) Successful in 1h35m24s
Build Packages / Validation Battery (push) Successful in 35m45s
* Rugnux: A placed model is refined by default - restrained maximum-likelihood refinement on the GPU (CPU without one), writing `<prefix>_refined_model.cif`, `<prefix>_refined_maps.mtz` and the difference-map blobs in `<prefix>_blobs.json`; `--no-refine` skips it. * Rugnux: `--mode refine` refines a model against merged structure factors from an MTZ or mmCIF file. * Rugnux: The embedded restraint dictionary covers amino acids, nucleotides including common modified RNA nucleotides, common cofactors, crystallisation additives, waters and ions, with metal sites restrained from the model's LINK records. * Rugnux: A model or reference file can be given among the inputs in any order, recognised by its contents. * Rugnux: The geometry refinement algorithm option is now `-r, --geom-refine` (was `--refine`). * Rugnux: Serial stills resolve the indexing ambiguity from the data when no model or reference is given, reject merge outliers by default, follow the run's measured rocking curve in prediction and partiality (also in `--mode scale`), and index reproducibly on the GPU. * Rugnux: Reflections touching the beam-stop shadow are not integrated, reflections coarser than the low-resolution limit are merged and written (but kept out of the scale fit), and the polarisation plane is measured more reliably. * Rugnux: Small-molecule runs report unit-cell e.s.d.s, in a SHELX `.ins` beside the `.hkl` and in the mmCIF. * jfjoch_broker: Live Rugnux refines the uploaded model after placement; the web frontend's Rugnux live page has a hero panel with resolution, ISa, space group, placement and refined R-work/R-free, and its Mol* viewer shows the refined model and maps. * Documentation: A new front page with download pages for Rugnux and the viewer, and the technical reference reorganised below it. * Build: The fast-feedback indexer is part of the source tree instead of being downloaded at build time. Reviewed-on: #91 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
212 lines
8.9 KiB
C++
212 lines
8.9 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <deque>
|
|
#include <functional>
|
|
#include <vector>
|
|
|
|
#include "../../common/ParallelFor.h"
|
|
|
|
// Limited-memory BFGS whose memory outlives one call, so that a refinement's macro-cycles - each a few dozen
|
|
// iterations on a target whose scale and likelihood parameters were refitted in between - go on from what the
|
|
// earlier ones learnt of the curvature instead of starting each from a steepest-descent step. The initial inverse
|
|
// Hessian is a diagonal the caller gives (SetDiagonal: an estimate of the Hessian's diagonal, so that a parameter
|
|
// with ten times the curvature of another takes a tenth of its step), scaled each iteration by the latest pair
|
|
// (Oren-Shanno in that metric). The line search backtracks from the full step to sufficient decrease.
|
|
// Following Nocedal (1980) Math. Comp. 35, 773-782 and Liu & Nocedal (1989) Math. Program. 45, 503-528
|
|
class RefinementMinimiser {
|
|
public:
|
|
// f(x, g): the target at x, and its gradient in g (sized as x); false where it cannot be evaluated there.
|
|
using Target = std::function<bool(const std::vector<double> &x, double &f, std::vector<double> &g)>;
|
|
|
|
// `nthreads`: the threads the sweeps over the parameters run on (the dot products stay serial chains).
|
|
explicit RefinementMinimiser(size_t memory = 20, size_t nthreads = 1) : memory_(memory), nthreads_(nthreads) {}
|
|
|
|
// Forget the pairs (the target changed beyond what the memory still describes, e.g. another weight).
|
|
void Reset() {
|
|
s_.clear();
|
|
y_.clear();
|
|
sy_.clear();
|
|
}
|
|
// The estimated diagonal of the Hessian; every element > 0.
|
|
void SetDiagonal(std::vector<double> h) { h_ = std::move(h); }
|
|
|
|
struct Outcome {
|
|
int iterations = 0;
|
|
int evaluations = 0;
|
|
double f_start = 0, f_end = 0;
|
|
};
|
|
|
|
// At most `iterations` iterations from x (x is where it ended); stops early where an iteration lowers f by
|
|
// less than `f_tolerance` relative to |f|, or `stop` says so (checked before every iteration). `max_step`, in
|
|
// the parameters' units, is the most any parameter moves in the first trial of a line search.
|
|
Outcome Minimise(const Target &target, std::vector<double> &x, int iterations, double f_tolerance, double max_step,
|
|
const std::function<bool()> &stop = nullptr) {
|
|
const size_t n = x.size();
|
|
if (h_.size() != n)
|
|
h_.assign(n, 1.0);
|
|
Outcome out;
|
|
double f = 0;
|
|
std::vector<double> g(n), d(n), x_new(n), g_new(n);
|
|
if (!target(x, f, g))
|
|
return out;
|
|
out.evaluations++;
|
|
out.f_start = out.f_end = f;
|
|
for (int it = 0; it < iterations; it++) {
|
|
if (stop && stop())
|
|
break;
|
|
Direction(g, d);
|
|
double gd = Dot(g, d);
|
|
if (!(gd < 0)) { // not a descent direction: the memory no longer fits, start again from the diagonal
|
|
Reset();
|
|
Direction(g, d);
|
|
gd = Dot(g, d);
|
|
if (!(gd < 0))
|
|
break;
|
|
}
|
|
double d_max = 0;
|
|
for (double v : d)
|
|
d_max = std::max(d_max, std::fabs(v));
|
|
double alpha = d_max > max_step ? max_step / d_max : 1.0;
|
|
double f_new = 0;
|
|
bool accepted = false;
|
|
for (int ls = 0; ls < 10; ls++) {
|
|
Sweep(n, [&](size_t k) { x_new[k] = x[k] + alpha * d[k]; });
|
|
const bool ok = target(x_new, f_new, g_new);
|
|
out.evaluations++;
|
|
if (ok && f_new <= f + 1e-4 * alpha * gd) {
|
|
accepted = true;
|
|
break;
|
|
}
|
|
// the minimum of the quadratic through f, its slope and f_new, kept within [0.1, 0.5] of the step
|
|
const double q = ok ? -gd * alpha * alpha / (2.0 * (f_new - f - gd * alpha)) : 0.1 * alpha;
|
|
alpha = std::clamp(q, 0.1 * alpha, 0.5 * alpha);
|
|
}
|
|
if (!accepted)
|
|
break;
|
|
// the pair's vectors are the ones the memory last let go, where it did: made anew, their pages fault in
|
|
std::vector<double> s = std::move(spare_s_), y = std::move(spare_y_);
|
|
s.resize(n);
|
|
y.resize(n);
|
|
Sweep(n, [&](size_t k) {
|
|
s[k] = x_new[k] - x[k];
|
|
y[k] = g_new[k] - g[k];
|
|
});
|
|
const double sy = Dot(s, y);
|
|
if (sy > 1e-10 * std::sqrt(Dot(s, s) * Dot(y, y))) {
|
|
s_.push_back(std::move(s));
|
|
y_.push_back(std::move(y));
|
|
sy_.push_back(sy);
|
|
if (s_.size() > memory_) {
|
|
spare_s_ = std::move(s_.front());
|
|
spare_y_ = std::move(y_.front());
|
|
s_.pop_front();
|
|
y_.pop_front();
|
|
sy_.pop_front();
|
|
}
|
|
} else {
|
|
spare_s_ = std::move(s);
|
|
spare_y_ = std::move(y);
|
|
}
|
|
const double decrease = f - f_new;
|
|
x.swap(x_new);
|
|
g.swap(g_new);
|
|
f = f_new;
|
|
out.iterations++;
|
|
out.f_end = f;
|
|
if (decrease <= f_tolerance * std::max(std::fabs(f), 1.0))
|
|
break;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
private:
|
|
static double Dot(const std::vector<double> &a, const std::vector<double> &b) {
|
|
double s = 0;
|
|
for (size_t k = 0; k < a.size(); k++)
|
|
s += a[k] * b[k];
|
|
return s;
|
|
}
|
|
|
|
// fn(k) for every k below n on the threads, each element on its own: the serial loop's bits.
|
|
template <typename Fn>
|
|
void Sweep(size_t n, Fn fn) const {
|
|
ParallelChunks(static_cast<int>(n), ThreadsForWork(n, nthreads_), [&](int lo, int hi) {
|
|
for (int k = lo; k < hi; k++)
|
|
fn(static_cast<size_t>(k));
|
|
});
|
|
}
|
|
|
|
// d = -H g by the two-loop recursion, H0 = gamma diag(1/h). Every dot product is a serial chain of n
|
|
// additions, so each sweep over the parameters takes the next pair's dot product while it applies the
|
|
// previous pair's update to q - the same operations on each element in the same order as a sweep for
|
|
// each would do, with the update's memory traffic hidden under the chain.
|
|
void Direction(const std::vector<double> &g, std::vector<double> &d) const {
|
|
const size_t n = g.size(), m = s_.size();
|
|
std::vector<double> &q = q_; // kept from call to call: made anew, its pages fault in every time
|
|
q.assign(g.begin(), g.end());
|
|
std::vector<double> a(m), rho(m);
|
|
for (size_t i = 0; i < m; i++)
|
|
rho[i] = 1.0 / sy_[i];
|
|
double dot = m > 0 ? Dot(s_[m - 1], q) : 0.0, yhy = 0.0;
|
|
for (size_t i = m; i-- > 0;) {
|
|
a[i] = rho[i] * dot;
|
|
const double ai = a[i];
|
|
const double *y = y_[i].data();
|
|
dot = 0.0;
|
|
if (i > 0) {
|
|
const double *s = s_[i - 1].data();
|
|
for (size_t k = 0; k < n; k++) {
|
|
q[k] -= ai * y[k];
|
|
dot += s[k] * q[k];
|
|
}
|
|
} else {
|
|
const double *yl = y_[m - 1].data();
|
|
for (size_t k = 0; k < n; k++) {
|
|
q[k] -= ai * y[k];
|
|
yhy += yl[k] * yl[k] / h_[k];
|
|
}
|
|
}
|
|
}
|
|
const double gamma = m > 0 ? sy_[m - 1] / yhy : 1.0;
|
|
if (m > 0) {
|
|
const double *y = y_[0].data();
|
|
for (size_t k = 0; k < n; k++) {
|
|
q[k] *= gamma / h_[k];
|
|
dot += y[k] * q[k];
|
|
}
|
|
} else
|
|
for (size_t k = 0; k < n; k++)
|
|
q[k] *= gamma / h_[k];
|
|
for (size_t i = 0; i < m; i++) {
|
|
// b on its own and (a[i] - b) in the loop, as before: written as a[i] - rho[i] * dot the compiler
|
|
// contracts it into a fused multiply-add, and the bits change
|
|
const double b = rho[i] * dot;
|
|
const double *s = s_[i].data();
|
|
dot = 0.0;
|
|
if (i + 1 < m) {
|
|
const double *y = y_[i + 1].data();
|
|
for (size_t k = 0; k < n; k++) {
|
|
q[k] += s[k] * (a[i] - b);
|
|
dot += y[k] * q[k];
|
|
}
|
|
} else
|
|
for (size_t k = 0; k < n; k++)
|
|
q[k] += s[k] * (a[i] - b);
|
|
}
|
|
for (size_t k = 0; k < n; k++)
|
|
d[k] = -q[k];
|
|
}
|
|
|
|
size_t memory_, nthreads_;
|
|
std::deque<std::vector<double>> s_, y_;
|
|
std::deque<double> sy_; // s.y of each pair, as taken when it was kept
|
|
std::vector<double> spare_s_, spare_y_; // the pair the memory last let go, for the next one to reuse
|
|
mutable std::vector<double> q_; // Direction()'s working vector
|
|
std::vector<double> h_;
|
|
};
|