From 308d1ed2580ddda551ff36680266737289bf0237 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Thu, 8 Oct 2026 12:37:29 +0200 Subject: [PATCH] RotationScaleMerge: export the merged reflections on all threads Which groups are written is decided first (KeptIndices, in group order), then each written group's MergedReflection is formed on its own, on all threads, into a result sized once - where the serial loop pushed back up to millions of records, growing a vector that each merge allocated afresh. The same records in the same order. p.mtz md5 unchanged on myob, cytc, thau, kdp, 8a1a and 8qaw (GPU build). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi --- .../scale_merge/RotationScaleMerge.cpp | 64 ++++++++++--------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/image_analysis/scale_merge/RotationScaleMerge.cpp b/image_analysis/scale_merge/RotationScaleMerge.cpp index b424c990d..838ba3c64 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.cpp +++ b/image_analysis/scale_merge/RotationScaleMerge.cpp @@ -5179,11 +5179,6 @@ RotationScaleMerge::Result RotationScaleMerge::MergeAndStats(int n_groups, bool // it with += and does need the zeroes. if (use_gpu_merge) acc.resize(n_groups); else acc.assign(n_groups, Accum{}); - result.merged.clear(); - // One reflection per group at most, and a merge runs several times per pass. Without this the - // vector doubles its way up to a few hundred megabytes on a large cell, copying everything it - // already holds each time. - result.merged.reserve(n_groups); merged_I.resize(n_groups); rejected_obs.resize(fulls.size()); ParallelChunks(n_groups, ThreadsForWork(n_groups, nthreads), [&](int lo, int hi) { @@ -5321,34 +5316,41 @@ RotationScaleMerge::Result RotationScaleMerge::MergeAndStats(int n_groups, bool }); } // ---- Export merged reflections. ---- - for (int g = 0; g < n_groups; ++g) { - const auto &a = acc[g]; - if (a.sw <= 0.0) continue; - MergedReflection mr{}; - mr.h = group_h[g]; mr.k = group_k[g]; mr.l = group_l[g]; - mr.I = static_cast(a.swI / a.sw); - // Plain inverse-variance merged sigma. The error model's systematic term (b*I)^2 is measured - // from the scatter BETWEEN a reflection's symmetry equivalents, i.e. from exactly the part of - // the error that is NOT common to them, so it averages down over the multiplicity like the - // counting part and the merge must not hold it back. XDS behaves the same way: its merged - // I/sigma runs far above its own reported ISa in the strong low-resolution shells. - mr.sigma = static_cast(1.0 / std::sqrt(a.sw)); - mr.I_half[0] = mr.I_half[1] = NAN; - mr.sigma_half[0] = mr.sigma_half[1] = NAN; - mr.d = a.d; - mr.on_ice_ring = a.on_ice; - if (a.nh[0] + a.nh[1] > 0 && a.swh[0] > 0.0 && a.swh[1] > 0.0) { - for (int i = 0; i < 2; ++i) { - mr.I_half[i] = static_cast(a.swIh[i] / a.swh[i]); - mr.sigma_half[i] = static_cast(1.0 / std::sqrt(a.swh[i])); + // Which groups are written, in group order, then each one's record on its own - on all threads. + // A group is written when it has weight and a resolution. + const std::vector written = KeptIndices(n_groups, nthreads, [&](int g) { + return !(acc[g].sw <= 0.0) && std::isfinite(acc[g].d) && acc[g].d > 0.0f; + }); + result.merged.resize(written.size()); + ParallelChunks(static_cast(written.size()), ThreadsForWork(written.size(), nthreads), [&](int lo, int hi) { + for (int j = lo; j < hi; ++j) { + const int g = written[j]; + const auto &a = acc[g]; + MergedReflection mr{}; + mr.h = group_h[g]; mr.k = group_k[g]; mr.l = group_l[g]; + mr.I = static_cast(a.swI / a.sw); + // Plain inverse-variance merged sigma. The error model's systematic term (b*I)^2 is measured + // from the scatter BETWEEN a reflection's symmetry equivalents, i.e. from exactly the part of + // the error that is NOT common to them, so it averages down over the multiplicity like the + // counting part and the merge must not hold it back. XDS behaves the same way: its merged + // I/sigma runs far above its own reported ISa in the strong low-resolution shells. + mr.sigma = static_cast(1.0 / std::sqrt(a.sw)); + mr.I_half[0] = mr.I_half[1] = NAN; + mr.sigma_half[0] = mr.sigma_half[1] = NAN; + mr.d = a.d; + mr.on_ice_ring = a.on_ice; + if (a.nh[0] + a.nh[1] > 0 && a.swh[0] > 0.0 && a.swh[1] > 0.0) { + for (int i = 0; i < 2; ++i) { + mr.I_half[i] = static_cast(a.swIh[i] / a.swh[i]); + mr.sigma_half[i] = static_cast(1.0 / std::sqrt(a.swh[i])); + } + mr.cc_weight = static_cast((1.0 / a.swh_typ[0] + 1.0 / a.swh_typ[1]) + / (1.0 / a.swh[0] + 1.0 / a.swh[1])); } - mr.cc_weight = static_cast((1.0 / a.swh_typ[0] + 1.0 / a.swh_typ[1]) - / (1.0 / a.swh[0] + 1.0 / a.swh[1])); + merged_I[g] = mr.I; + result.merged[j] = mr; } - if (!std::isfinite(a.d) || a.d <= 0.0f) continue; - merged_I[g] = mr.I; - result.merged.push_back(mr); - } + }); }; run_merge();