From 0b86ef5dab175badc214822916c78288753b6033 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Tue, 13 Oct 2026 16:10:38 +0200 Subject: [PATCH] Bragg prediction: rotation partiality in the tail from erfc, the same on CPU and GPU Last-bit change, not bit-exact: every partial whose frame does not contain its exact diffracting condition gets a partiality - and so a slice-centroid walk along its Debye ring - that differs in the last bits. The partiality was (erf(b) - erf(a)) / 2 in float. Where the frame's slice lies in one tail of the rocking curve both erf are near +-1, and their difference keeps only a few ulps of 1: glibc erf and CUDA erff, one ulp apart, gave partialities ~1e-3 apart (relative) at p ~ 3e-5. The slice centroid (RockingSliceCentroid_rad) divides by that partiality, and at zeta ~ 0.05 the walk it scales is hundreds of pixels, so CPU and GPU placed such a reflection up to 0.1 px apart - BraggPrediction_CPU_GPU_consistency_large_cell failed on one of them once --fmad=false (7ed970503) moved the ulps. A slice in one tail now takes the same difference from erfcf, which keeps its relative precision there; one helper (RockingSlicePartiality) serves both predictors. Reflections placed more than 0.01 px apart by CPU and GPU in that test: 65 -> 0. BraggPrediction*, BraggPredictionRot*, BraggPredictionRotGPU*, RockingSlice* pass. 7qij image passes 5.68/7.90 s -> 5.26/7.50 s (one lock hold; no cost). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01EizBKhTqrAYago9KJG3dA3 --- .../bragg_prediction/BraggPredictionRot.cpp | 3 +-- .../bragg_prediction/BraggPredictionRotGPU.cu | 3 +-- image_analysis/bragg_prediction/RockingSlice.h | 12 ++++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/image_analysis/bragg_prediction/BraggPredictionRot.cpp b/image_analysis/bragg_prediction/BraggPredictionRot.cpp index 181b7077b..022d9e596 100644 --- a/image_analysis/bragg_prediction/BraggPredictionRot.cpp +++ b/image_analysis/bragg_prediction/BraggPredictionRot.cpp @@ -249,8 +249,7 @@ int BraggPredictionRot::Calc(const DiffractionExperiment &experiment, const Crys const float lorentz_reciprocal = std::fabs(m2 * (S % S0)) / (S.Length() * S0.Length()); const float c1 = zeta_abs / (std::sqrt(2.0f) * mos_eff_rad); - const float partiality = (std::erf((phi + half_wedge_angle_rad) * c1) - - std::erf((phi - half_wedge_angle_rad) * c1)) / 2.0f; + const float partiality = RockingSlicePartiality(phi, half_wedge_angle_rad, c1); // Place the partial where this frame recorded it: at the centre of the frame's slice of // the rocking curve, which is the exact-condition position walked along its Debye ring by // the rotation between the two (RockingSlice.h). Walking along the ring is turning S diff --git a/image_analysis/bragg_prediction/BraggPredictionRotGPU.cu b/image_analysis/bragg_prediction/BraggPredictionRotGPU.cu index 31353f61f..1e5d9a39f 100644 --- a/image_analysis/bragg_prediction/BraggPredictionRotGPU.cu +++ b/image_analysis/bragg_prediction/BraggPredictionRotGPU.cu @@ -154,8 +154,7 @@ namespace { // c1 = sqrt(2) * sigma / zeta, where sigma = mosaicity float c1 = zeta_abs / (sqrtf(2.0f) * mos_eff_rad); float half_wedge = C.wedge_angle_rad / 2.0f; - float partiality = (erff((phi + half_wedge) * c1) - - erff((phi - half_wedge) * c1)) / 2.0f; + float partiality = RockingSlicePartiality(phi, half_wedge, c1); // The partial sits at the centre of the frame's slice of its rocking curve, walked along diff --git a/image_analysis/bragg_prediction/RockingSlice.h b/image_analysis/bragg_prediction/RockingSlice.h index 6178f6bac..adc514e9b 100644 --- a/image_analysis/bragg_prediction/RockingSlice.h +++ b/image_analysis/bragg_prediction/RockingSlice.h @@ -32,6 +32,18 @@ #define ROCKING_SLICE_HD inline #endif +// The frame's slice of the rocking curve, (erf(b) - erf(a)) / 2 with a = (phi - w/2) c1, b = (phi + w/2) c1. +// Where the slice lies in one tail of the curve both erf are near +-1 and their difference cancels to a +// few float ulps of 1, so the same difference is taken from erfc, which keeps its relative precision there. +ROCKING_SLICE_HD float RockingSlicePartiality(float phi, float half_wedge, float c1) { + const float a = (phi - half_wedge) * c1, b = (phi + half_wedge) * c1; + if (a > 0.0f) + return (erfcf(a) - erfcf(b)) / 2.0f; + if (b < 0.0f) + return (erfcf(-b) - erfcf(-a)) / 2.0f; + return (erff(b) - erff(a)) / 2.0f; +} + // Flux-weighted centre of the frame's slice of the rocking curve, relative to the exact diffracting // condition, in radians of rotation. partiality is the slice's own mass (the caller has it already). ROCKING_SLICE_HD float RockingSliceCentroid_rad(float phi, float half_wedge, float c1, float partiality) {