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

39 lines
1.8 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
// The side-chain field's direct sums (SideChainField.cpp, EvaluatorCPU::Contribution) on an Apple GPU (macOS only):
// one thread per reflection and state, as SideChainFieldGPU.cu's contribution_kernel does under CUDA, with the CPU's
// single-precision arithmetic in its order (SideChainFieldMetal.metal spells it out). tests/SideChainFieldMetalTest.cpp
// holds it to the CPU's.
#include <complex>
#include <cstddef>
#include <memory>
#include <vector>
#include "SideChainField.h" // FieldStates
class SideChainFieldMetal {
public:
// The evaluator's tables, kept on the GPU: per operator and row (k * rows + m) R^T h and h . t, per element and
// row the form factor, per row stol2. Null where there is no Metal device the process may use (rugnux --no-gpu
// hides it). Throws JFJochException on a Metal failure.
static std::unique_ptr<SideChainFieldMetal> Make(size_t rows, size_t n_ops, const std::vector<float> &hx,
const std::vector<float> &hy, const std::vector<float> &hz,
const std::vector<float> &ht, const std::vector<float> &f0,
const std::vector<float> &stol2);
~SideChainFieldMetal();
// The contributions of states first .. first + out.size() - 1, each `rows` values into its vector.
void Contributions(const FieldStates &states, int first, std::vector<std::vector<std::complex<float>>> &out);
// How many states the GPU has summed in this process.
static size_t States();
struct Impl;
private:
explicit SideChainFieldMetal(std::unique_ptr<Impl> impl);
std::unique_ptr<Impl> impl_;
};