Files
Filip LeonarskiandClaude Opus 5.5 3a4dcba3bd Side-chain field on the Apple GPU: the states' direct sums in Metal, bit for bit
RESULT-CHANGING on the CPU (ulp level): the B factor's exp in EvaluatorCPU::Contribution is
BraggExp (bragg_integration/BraggExp.h, at most 0.53 ulp) instead of the libm expf, so that the
Apple GPU, whose exp no host function reproduces, computes the same bits.

MakeSideChainFieldEvaluatorCPU takes a `gpu` flag (the refinement passes settings.gpu); on macOS the
evaluator's Contribution() and the per-state sums of Evaluate() then run on the Apple GPU
(SideChainFieldMetal.mm/.metal): one thread per reflection and state, as SideChainFieldGPU.cu's
contribution_kernel, the evaluator's tables uploaded once, states in batches of 32 so that the
GPU's buffer stays small beside the states' own. Fast math and contraction off - also over the
BraggExp prepended to the kernel source, which a first version left contracted (91 of 60000
values 1-2 ulp off). The likelihood over the combinations stays on the CPU (double). Where the
GPU is hidden (--no-gpu) or fails (one warning) the CPU does the same sums.

SideChainFieldMetal_BitIdenticalToCPU ([portable][Metal]): 20000 reflections, P212121, 70
six-atom states (three batches): every contribution the same bits, every combination's -ln L equal.

M6 Mac mini, rugnux --mode refine of an open-arm set's merged MTZ and refined model, on top of the
Metal gradient (previous commit):
  9s02 (20355 atoms, 306298 reflections, 5 NCS copies): side-chain field 149.0 -> 30.4 s, refinement
       309 -> 184 s wall; R-work 0.2254 / R-free 0.2390 both.
  9zlo: field 7.6 -> 2.4 s, 45.8 -> 41.3 s; R unchanged.
  --no-gpu on 9zlo: the refined model byte-identical to the build before (the field moved nothing).
Not run here: SideChainFieldTest.cpp's [Refinement] cases (jfjoch_test is not built on macOS).

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

59 lines
2.6 KiB
Metal

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
// The side-chain field's direct sums (SideChainFieldMetal.h), compiled when first used from this source, which
// CMakeLists.txt copies into the binary after bragg_integration/BraggExp.h - with fast math off and no a*b+c
// contracted, as on the host, so that every operation below rounds as EvaluatorCPU::Contribution's does.
#include <metal_stdlib>
using namespace metal;
#pragma clang fp contract(off)
struct Atom { // FieldAtom
float x, y, z, b, occ;
int element;
};
struct Params {
uint rows, n_ops, first;
};
// SideChainField.cpp's SinCos2Pi(): sin and cos of 2 pi v, v taken to [-1/2, 1/2] by its nearest integer, the half
// angle by its Taylor series, then the double-angle formulas.
static void sincos_2pi(float v, thread float &sn, thread float &cs) {
const float x = 3.14159265f * (v - rint(v));
const float x2 = x * x;
const float s = x * (1 + x2 * (-1.f / 6 + x2 * (1.f / 120 + x2 * (-1.f / 5040 + x2 * (1.f / 362880 + x2 * (-1.f / 39916800))))));
const float c = 1 + x2 * (-1.f / 2 + x2 * (1.f / 24 + x2 * (-1.f / 720 + x2 * (1.f / 40320 + x2 * (-1.f / 3628800 + x2 * (1.f / 479001600))))));
sn = 2 * s * c;
cs = c * c - s * s;
}
// One thread per row m and state first + s: the state's atoms in order, each over the operators in order.
kernel void contribution(device const float *hx [[buffer(0)]], device const float *hy [[buffer(1)]],
device const float *hz [[buffer(2)]], device const float *ht [[buffer(3)]],
device const float *f0 [[buffer(4)]], device const float *stol2 [[buffer(5)]],
device const Atom *atoms [[buffer(6)]], device const int *start [[buffer(7)]],
constant Params &p [[buffer(8)]], device float2 *out [[buffer(9)]],
uint2 gid [[thread_position_in_grid]]) {
const uint m = gid.x, s = gid.y;
if (m >= p.rows)
return;
const float st = stol2[m];
float re = 0, im = 0;
for (int a = start[p.first + s]; a < start[p.first + s + 1]; a++) {
const Atom at = atoms[a];
const float fa = f0[uint(at.element) * p.rows + m] * at.occ * BraggExp(-at.b * st);
for (uint k = 0; k < p.n_ops; k++) {
const uint o = k * p.rows + m;
const float v = hx[o] * at.x + hy[o] * at.y + hz[o] * at.z + ht[o];
float sn, cs;
sincos_2pi(v, sn, cs);
re += fa * cs;
im += fa * sn;
}
}
out[s * p.rows + m] = float2(re, im);
}