Files
Filip LeonarskiandClaude Opus 5.5 88330f395f Refinement gradient on the Apple GPU: the sums over the derivative map in Metal
RESULT-CHANGING (ulp level; owner approved for Metal ports where exactness costs speed)

AtomGradientsFromMap() takes a `gpu` flag; the CPU engine sets it, and on macOS the sums of every
atom's box against the FFT method's map then run on the Apple GPU (AtomGradientMetal.mm/.metal):
one SIMD group of 32 threads per atom, the lanes taking the box's points in turn, as
RefinementGPU.cu's atom_gradient_kernel does under CUDA. Each atom's box, radius and Gaussians are
the CPU's (built on the host as the CPU loops build them), and a point's place and the radius test
are spelled as GatherMapPoints() spells them with fast math and contraction off, so the GPU takes
the same points; the sums are in single precision and in another order, so the gradient is not the
CPU's bit for bit. Where the GPU does not take it (rugnux --no-gpu, no device, a box wider than the
cell) or fails (one warning, then the CPU from there on) the CPU loops run as before.

Parity (AtomGradientMetal_MatchesCPU, [portable][Metal], 2000 atoms, a quarter anisotropic, P1
triclinic and P212121): worst |GPU - CPU| over the largest |CPU| of that derivative 0.6-1.0e-6 for
x, y, z, B and 2.1-7.6e-6 for U; held to 1e-5 and 5e-5.

M6 Mac mini, rugnux --mode refine, an open-arm set (9zlo, its merged MTZ and refined model):
90.6 s -> 45.8 s wall; R-work 0.2697 both, R-free 0.2936 -> 0.2938. With --no-gpu the refined model
is byte-identical to the build before (115 s: --no-gpu also takes the density gather off the GPU).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK
2026-11-16 07:21:12 +01:00

32 lines
1.7 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// 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 <cstddef>
#include <vector>
#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<ModelDensityMetalAtom> &atoms,
const float *map, std::vector<AtomGradientMetalSums> &out);
// How many gradients the GPU has summed in this process.
size_t AtomGradientMetalCalls();