Merge branch 'rc188-g' into rc188-int

This commit is contained in:
leonarski_f committed 2026-11-17 16:59:28 +01:00
commit 25bb143daa
5 files changed
+79 -10

No files matched your search

@@ -2000,7 +2000,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
@@ -2014,7 +2014,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;
@@ -3072,7 +3072,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;
@@ -4272,7 +4292,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);