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
71 lines
3.9 KiB
C++
71 lines
3.9 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#pragma once
|
|
|
|
// The restraints' Gauss-Newton normal matrix J^T J (RestraintGaussNewton) of the Newton minimisation on a GPU (CUDA
|
|
// builds only): RestraintNormalMatrix of Refinement.cpp on the card, in single precision. J is worked out on the device,
|
|
// one thread per restraint, with the terms the engine evaluates the restraints with (RefinementMath.h); a product is two
|
|
// gathers, J v per row and J^T t per parameter atom over its entries, each summed in a fixed order without atomics, in
|
|
// double-float pairs (RefinementNewtonGPU.cu).
|
|
//
|
|
// Every restraint keeps its rows whatever its E: where RestraintGaussNewton drops a row (E = 0, a plane without a
|
|
// normal) its entries here are zero, so the index of J is worked out once, at the first At(), and the set must stay
|
|
// the same for the matrix's life.
|
|
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <memory>
|
|
#include <vector>
|
|
|
|
#include "RefinementMath.h" // RestraintRecord
|
|
|
|
struct RestraintSet;
|
|
|
|
class RestraintNormalMatrixGPU {
|
|
public:
|
|
// On the x y z (`xyz`) or the B of `param_atoms` of the model's `n_atoms`, on GPU `device`.
|
|
RestraintNormalMatrixGPU(int device, const std::vector<int> ¶m_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);
|
|
// The diagonal of J^T J.
|
|
std::vector<double> Diagonal() const;
|
|
|
|
// The conjugate gradients of ConjugateGradients (RefinementMinimiser.h) on the card, for H = J^T J + diag(h) at
|
|
// the point last given to At(), preconditioned by a block-diagonal M given by the inverses of its blocks: block g
|
|
// over the elements element[start[g]] ... element[start[g + 1] - 1] of the vector, the lower triangle of its
|
|
// M_g^-1 (symmetric) in `inverse`, row by row (W_ik, k <= i, at i (i + 1) / 2 + k from the block's first). h and M
|
|
// stay until the next SetSolve().
|
|
void SetSolve(const std::vector<double> &h, const std::vector<int> &start, const std::vector<int> &element,
|
|
const std::vector<double> &inverse);
|
|
// x = H^-1 b by `iterations` of them from x = 0 (fewer only where p H p is not positive), each residual made
|
|
// M^-1-orthogonal to the earlier ones as there; the vectors on the card and every sum in float pairs, in a fixed
|
|
// order (RefinementNewtonGPU.cu).
|
|
void Solve(const std::vector<double> &b, std::vector<double> &x, int iterations);
|
|
|
|
private:
|
|
// The set and the index of J (RefinementNewtonGPU.cu): per restraint its first entry, per row its entries
|
|
// start[r] ... start[r + 1], per entry its parameter atom (-1: none) and row, per parameter atom its entries.
|
|
void Upload(const std::vector<RestraintRecord> &records, const std::vector<int> &plane_atoms,
|
|
const std::vector<int> &record_entry, const std::vector<int> &start, const std::vector<int> ¶m,
|
|
const std::vector<int> &row_of, const std::vector<int> &entry_start, const std::vector<int> &entry);
|
|
// J at `xyzb` (x, y, z, B per atom), in float
|
|
void Fill(const std::vector<std::array<double, 4>> &xyzb);
|
|
|
|
int per_, c0_;
|
|
size_t n_params_;
|
|
std::vector<int> param_of_;
|
|
bool indexed_ = false;
|
|
struct Device;
|
|
std::unique_ptr<Device> dev_;
|
|
};
|