Refinement on the GPU: the normal matrix in the engine's memory budget

The positions' normal matrix a trial holds on its card while it minimises (J and its index, the
CG vectors, the per-residue block inverses, the kept residuals of the reorthogonalisation) was
outside the GPU budget that places the trials: on a 16 GB card a large model placed three trials
by a 12.1 GB count and peaked at 14.2 GB (a run died there with an out-of-memory once the card
had less to spare). RestraintNormalMatrixGPU::DeviceBytes counts what its allocations come to,
from the geometry restraints as the engine's restraint count takes them and the residues'
parameter atoms; RefineOnce adds it to each engine's own bytes, so the slot placement and the CPU
fallback, both decided up front, see it.

Ribosome case (open arm, 16 GB card, -N 16): 0.91 GB per trial; two trials at once instead of
three; predicted peak 11.74 GB (11196 MiB), measured 11348 MiB. Without the count the measured
14198 MiB matches the old placement plus three normal matrices (14124 MiB). With the card
reported as 7 GB the run goes to the CPU before the first trial, with 12 GB it places one trial.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-11-17 16:24:38 +01:00
1 parent c582a48131
commit 9caad1487d
5 files changed
+79 -10

No files matched your search

@@ -1997,7 +1997,7 @@ void TwinNull(const Context &ctx, RefinementResult &result, size_t first = 0) {
// on the CPU otherwise. `restraint_records`, `restraint_slots`: what its restraint sets hold at most (0: the GPU
// engine's estimate per atom).
void MakeEngine(Context &ctx, bool gpu, const RefinementSettings &settings, Logger &logger, size_t restraint_records = 0,
size_t restraint_slots = 0) {
size_t restraint_slots = 0, size_t normal_bytes = 0) {
const RefinementStage stage("engine");
ctx.engine.reset();
#ifdef JFJOCH_USE_CUDA
@@ -2011,7 +2011,7 @@ void MakeEngine(Context &ctx, bool gpu, const RefinementSettings &settings, Logg
RefinementGPUBytes bytes;
ctx.engine = MakeRefinementEngineGPU(device, ctx.cell, *ctx.sg, ctx.d_min, ctx.refl, ctx.Model(),
ctx.b_ceiling, budget, &bytes, settings.nthreads, restraint_records,
restraint_slots);
restraint_slots, normal_bytes);
ctx.device = device;
ctx.engine_bytes = bytes.engine;
ctx.card_bytes = bytes.card;
@@ -3066,7 +3066,27 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
restraint_records += restraint_records / 4;
restraint_slots += restraint_slots / 4;
}
MakeEngine(ctx, settings.gpu, settings, logger, restraint_records, restraint_slots);
// The positions' normal matrix a trial holds on the engine's card (MinimiseAtoms): the geometry as above, the
// parameter atoms in their residues' blocks.
size_t normal_bytes = 0;
#ifdef JFJOCH_USE_CUDA
if (restrained) {
std::vector<int> block_atoms;
size_t i = 0;
for (const gemmi::Chain &ch : ctx.Model().chains)
for (const gemmi::Residue &r : ch.residues) {
int a = 0;
for (size_t k = 0; k < r.atoms.size(); k++, i++)
if (ctx.rider_parent.empty() || ctx.rider_parent[i] < 0)
a++;
block_atoms.push_back(a);
}
normal_bytes = RestraintNormalMatrixGPU::DeviceBytes(RestraintsNow(ctx, topo, ncs_p, 1.0), ctx.atoms.size(),
block_atoms, NORMAL_CG_ITERATIONS);
logger.Debug("Refinement: the positions' normal matrix {:.2f} GB per trial on a GPU", normal_bytes / 1e9);
}
#endif
MakeEngine(ctx, settings.gpu, settings, logger, restraint_records, restraint_slots, normal_bytes);
result.on_gpu = ctx.engine->OnGPU();
std::vector<TlsBody> input_tls;
@@ -4260,7 +4280,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
if (o)
o->ctx.engine.reset();
ctx.b_ceiling = ceiling;
MakeEngine(ctx, settings.gpu, settings, logger, restraint_records, restraint_slots);
MakeEngine(ctx, settings.gpu, settings, logger, restraint_records, restraint_slots, normal_bytes);
ctx.engine->SetRestraints(0, RestraintsNow(ctx, topo));
ctx.engine->SetRestraints(1, topo.adp);
for (size_t k = 0; k < own.size(); k++)
@@ -102,7 +102,7 @@ class RefinementEngineGPU : public RefinementEngine {
public:
RefinementEngineGPU(int device, const gemmi::UnitCell &cell, const gemmi::SpaceGroup &sg, double d_min,
const RefinementReflections &refl, const gemmi::Model &model, double b_max, size_t nthreads,
size_t restraint_records, size_t restraint_slots)
size_t restraint_records, size_t restraint_slots, size_t normal_bytes)
: device_(device), cell_(cell), sg_(sg), d_min_(d_min), nthreads_(nthreads),
setup_(std::make_shared<RefinementGPUSetup>()), cards_(std::make_shared<Cards>()) {
const gemmi::Grid<float> grid = RigidBodyZoneGrid(cell, sg, d_min);
@@ -215,7 +215,7 @@ public:
restraint_records = 30 * widest.size();
restraint_slots = 60 * widest.size();
}
bytes_.engine = RefinementGPUEngine::DeviceBytes(*setup_, restraint_records, restraint_slots);
bytes_.engine = RefinementGPUEngine::DeviceBytes(*setup_, restraint_records, restraint_slots) + normal_bytes;
bytes_.card = RefinementGPUEngine::CardBytes(*setup_);
bytes_.lane = RefinementGPUEngine::LaneBytes(*setup_, fft_work_);
}
@@ -465,9 +465,10 @@ std::unique_ptr<RefinementEngine> MakeRefinementEngineGPU(int device, const gemm
const RefinementReflections &refl,
const gemmi::Model &model, double b_max, size_t budget,
RefinementGPUBytes *bytes, size_t nthreads,
size_t restraint_records, size_t restraint_slots) {
size_t restraint_records, size_t restraint_slots,
size_t normal_bytes) {
auto engine = std::make_unique<RefinementEngineGPU>(device, cell, sg, d_min, refl, model, b_max, nthreads,
restraint_records, restraint_slots);
restraint_records, restraint_slots, normal_bytes);
if (bytes != nullptr)
*bytes = engine->Bytes();
if (!engine->Supported() || engine->Bytes().engine + engine->Bytes().card + engine->Bytes().lane > budget)
@@ -25,13 +25,16 @@ struct RefinementGPUBytes {
// `b_max`: the largest B an atom may reach, which the gather's reserve is sized for. Throws JFJochException on a
// CUDA failure. `nthreads`: the host threads that work out the atoms' densities. `restraint_records`,
// `restraint_slots`: the most records (plane atoms included) and gradient slots its two restraint sets hold together,
// which the memory is worked out for (0: about thirty records and sixty slots per atom).
// which the memory is worked out for (0: about thirty records and sixty slots per atom). `normal_bytes`: what the
// minimiser's normal matrix holds beside it while a trial refines on the engine (RestraintNormalMatrixGPU::DeviceBytes),
// counted as the engine's own.
std::unique_ptr<RefinementEngine> MakeRefinementEngineGPU(int device, const gemmi::UnitCell &cell,
const gemmi::SpaceGroup &sg, double d_min,
const RefinementReflections &refl,
const gemmi::Model &model, double b_max, size_t budget,
RefinementGPUBytes *bytes = nullptr, size_t nthreads = 1,
size_t restraint_records = 0, size_t restraint_slots = 0);
size_t restraint_records = 0, size_t restraint_slots = 0,
size_t normal_bytes = 0);
// An engine like `like` (one the function above made) on `device`: the same case, and on a card where an engine
// like it is, that card's shared part with it - so it holds RefinementGPUBytes::engine there, and ::lane beside where
// `own_lane` (else it shares the card's least used lane). Throws JFJochException on a CUDA failure.
@@ -5,6 +5,45 @@
#include "RefinementRestraints.h"
size_t RestraintNormalMatrixGPU::DeviceBytes(const RestraintSet &set, size_t n_atoms,
const std::vector<int> &block_atoms, int iterations) {
size_t entries = 0, rows = 0, params = 0, inverse = 0;
for (const RestraintRecord &r : set.records) // as At() lays out J
switch (static_cast<RestraintKind>(r.kind)) {
case RestraintKind::Plane:
entries += static_cast<size_t>(r.atom[1]) * r.atom[1];
rows += r.atom[1];
break;
case RestraintKind::Angle:
entries += 3;
rows++;
break;
case RestraintKind::Torsion:
case RestraintKind::Chiral:
case RestraintKind::NcsDistance:
entries += 4;
rows++;
break;
default:
entries += 2;
rows++;
break;
}
for (int a : block_atoms) {
params += a;
inverse += static_cast<size_t>(3 * a) * (3 * a + 1) / 2;
}
const size_t n = 3 * params;
return n_atoms * 4 * sizeof(float) // the positions
+ set.records.size() * (sizeof(RestraintRecord) + sizeof(int)) + set.plane_atoms.size() * sizeof(int)
+ rows * (sizeof(int) + 2 * sizeof(float)) // row starts, J v
+ entries * (4 * sizeof(float) + 3 * sizeof(int)) + params * sizeof(int) // J, its index
+ n * 15 * sizeof(float) // seven vectors, diagonal
+ n * (2 * sizeof(float) + 2 * sizeof(int)) + 2 * block_atoms.size() * sizeof(int) // h, the blocks
+ inverse * 2 * sizeof(float) // their inverses
+ static_cast<size_t>(iterations) * n * 3 * sizeof(float); // the kept residuals
}
// The host half: the index of J, as RestraintGaussNewton lays out its rows, with every row kept.
void RestraintNormalMatrixGPU::At(const RestraintSet &set, const std::vector<std::array<double, 4>> &xyzb) {
if (!indexed_) {
@@ -28,6 +28,12 @@ public:
RestraintNormalMatrixGPU(int device, const std::vector<int> &param_atoms, size_t n_atoms, bool xyz);
~RestraintNormalMatrixGPU();
// The device memory a matrix on the x y z of the model's `n_atoms` holds with the restraints of `set`, its
// conjugate gradients of `iterations` and its preconditioner's blocks of `block_atoms` parameter atoms each (every
// parameter atom in one): what the allocations below come to, the few small ones left out.
static size_t DeviceBytes(const RestraintSet &set, size_t n_atoms, const std::vector<int> &block_atoms,
int iterations);
void At(const RestraintSet &set, const std::vector<std::array<double, 4>> &xyzb);
// hv = J^T J v
void Multiply(const std::vector<double> &v, std::vector<double> &hv);