// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // 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 #include #include #include #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 ¶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 &block_atoms, int iterations); void At(const RestraintSet &set, const std::vector> &xyzb); // hv = J^T J v void Multiply(const std::vector &v, std::vector &hv); // The diagonal of J^T J. std::vector 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 &h, const std::vector &start, const std::vector &element, const std::vector &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 &b, std::vector &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 &records, const std::vector &plane_atoms, const std::vector &record_entry, const std::vector &start, const std::vector ¶m, const std::vector &row_of, const std::vector &entry_start, const std::vector &entry); // J at `xyzb` (x, y, z, B per atom), in float void Fill(const std::vector> &xyzb); int per_, c0_; size_t n_params_; std::vector param_of_; bool indexed_ = false; struct Device; std::unique_ptr dev_; };