// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #pragma once // The device half of restrained refinement on a GPU (RefinementGPU.h, CUDA builds only). The host half works // out with gemmi everything that depends on the cell, the group and the reflections; this half grids the atoms, // transforms, composes the symmetry, evaluates the likelihood target, takes its derivative back to every atom // by the FFT method, and evaluates the geometry restraints. Every kernel is deterministic - gathers and fixed // reduction trees, no floating-point atomics - so the same input gives the same bits on every run. #include #include #include #include #include "ModelDensityGPU.h" #include "ModelMaskGPU.h" #include "RefinementMath.h" // Everything an engine needs that does not depend on the atoms. struct RefinementGPUSetup { ModelDensityGrid grid; // the structure-factor grid, u fastest double volume = 0; std::vector> sym_ops; // gemmi's Op: rot row-major and tran, in units of 1 / den int den = 24; double centring = 1; // the number of centring vectors std::vector mask_ops; // every operator with every centring vector std::vector> rows; // the reflections, in the order their numbers come back std::vector inv_d2; std::vector fo; std::vector work, centric; // The adjoint of the composition, as a gather: the half-u spectrum points the gradient's coefficients // land on (ascending), and for each the terms (row * ops + op) * 2 + conj adding to it, in order. std::vector scatter_point, scatter_start, scatter_term; size_t max_atoms = 0, max_pairs = 0; }; struct RefinementGPUEngineImpl; class RefinementGPUEngine { public: static size_t DeviceBytes(const RefinementGPUSetup &setup, size_t fft_work_bytes, size_t max_records, size_t max_slots); static size_t FFTWorkBytes(int device, const ModelDensityGrid &grid); RefinementGPUEngine(int device, const RefinementGPUSetup &setup, size_t fft_work_bytes); ~RefinementGPUEngine(); // n_cen exp(blur s^2 / 4) per reflection: prepare_asu_data()'s unblur. void SetBlur(double blur); // Per macro-cycle: F_model = k_total (F + fmask), and the likelihood's D and variance. void SetRowModel(const std::vector &k_total, const std::vector> &fmask, const std::vector &d, const std::vector &var); // The atoms, as ModelStructureFactorsGPU takes them: densities, grid positions (fractional times the grid // size, not wrapped) and the boxes of ModelDensityGPU::Boxes(). void SetAtoms(const std::vector &atoms, const std::vector> &pos); // F of the atoms at the rows. void Fcalc(std::vector> &f); // The bulk-solvent mask's F at the rows (the mask atoms fractional, wrapped). void Fmask(const std::vector &mask_atoms, std::vector> &f); // -ln L over the working rows for the atoms last set and, with `grad`, dT/d(x, y, z, B) per atom. double Evaluate(std::vector> *grad); // Restraint set `which` (0 or 1), as RestraintSet holds it. void SetRestraints(int which, const std::vector &records, const std::vector &plane_atoms, const std::vector &slot_start, const std::vector &slot_index, int slots); // The sum of E of set `which` at `xyzb` (x, y, z, B per atom) and dE/d(x, y, z, B) per atom. double EvaluateRestraints(int which, const std::vector> &xyzb, std::vector> *grad); // The most atoms of a plane restraint the kernel takes. static constexpr int MAX_PLANE_ATOMS = 32; private: std::unique_ptr impl_; };