// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #pragma once // AtomGradientsFromMap()'s sums (RefinementXray.cpp) on an Apple GPU (macOS only): every atom's box walked against // the FFT method's map, one SIMD group of 32 threads per atom, as RefinementGPU.cu's atom_gradient_kernel does under // CUDA. The points, Gaussians and arithmetic are the CPU's, but the sums are in single precision and in another // order, so the gradient is the CPU's to a few parts in 1e5, not bit for bit. tests/AtomGradientMetalTest.cpp holds // it to the CPU's. #include #include #include "ModelDensityMetal.h" // ModelDensityMetalAtom, ModelDensityMetalGrid // Per atom the nine sums of AtomGradientsFromMap() before occupancy and the constants: an isotropic atom's // sum v dr2 x, y, z and sum v db (s[0..3]); an anisotropic atom's sum e (M r)_t (s[0..2]) and // sum e (M + 2 (M r)(M r)^T) as u11 u22 u33 u12 u13 u23 (s[3..8]). struct AtomGradientMetalSums { float s[9]; }; // The sums of `atoms` (b with the addend 0, as the gradient takes them) over `map` (nu nv nw floats, u fastest) into // `out`, one per atom. False where the GPU does not take it - no Metal device the process may use (rugnux --no-gpu // hides it), or an atom whose box is wider than the cell; `out` is then untouched. Throws JFJochException on a // Metal failure. bool AtomGradientMetal(const ModelDensityMetalGrid &grid, const std::vector &atoms, const float *map, std::vector &out); // How many gradients the GPU has summed in this process. size_t AtomGradientMetalCalls();