From 17f3c48cfc9af30ad425bf981ffe5fcad4bfeb03 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Mon, 5 Oct 2026 15:14:50 +0200 Subject: [PATCH] Rotation scaling: sort the ASU keys on all threads ComputeAsuGroups sorted one key per distinct raw hkl on one thread, once per merge (6-8 merges a run). Ties are now broken on the run index, which makes the order total, so ParallelSort returns the same sequence; and the result never depended on the order within a tie (every run in it reduces to the same ASU reflection). Measured on the GPU build, both sorts side by side on the same input in every merge of 6 in-house runs: keys identical in all calls; a cytochrome C run 0.230 -> 0.058 s, a thaumatin run 0.186 -> 0.058 s, summed over its merges. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01K5K8jvPPbmCrbqnWkddTuB --- image_analysis/scale_merge/RotationScaleMerge.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/image_analysis/scale_merge/RotationScaleMerge.cpp b/image_analysis/scale_merge/RotationScaleMerge.cpp index 6132d97db..d941e3c00 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.cpp +++ b/image_analysis/scale_merge/RotationScaleMerge.cpp @@ -1230,14 +1230,17 @@ int RotationScaleMerge::ComputeAsuGroups(const HKLKeyGenerator &keygen) { // The key travels with the run it belongs to, so the sort and the run-detection below read the // key they are standing on instead of chasing it back through `key` - an indirect compare is a // cache miss per comparison, and there are a few tens of millions of them. Which of two runs - // with the SAME key ends up first is left to the sort, and cannot matter: the packed key holds - // h, k, l and the hand exactly, so every run in a tie reduces to the same ASU reflection. + // with the SAME key ends up first cannot matter: the packed key holds h, k, l and the hand + // exactly, so every run in a tie reduces to the same ASU reflection. The tie is still broken on + // the run, so the order is a total one and the sort can go on all threads (ParallelSort). struct RunKey { uint64_t key; int32_t run; }; std::vector sorted; sorted.reserve(n_run); for (int r = 0; r < n_run; ++r) if (eligible[r]) sorted.push_back({key[r], r}); - std::sort(sorted.begin(), sorted.end(), [](const RunKey &a, const RunKey &b) { return a.key < b.key; }); + ParallelSort(sorted.begin(), sorted.end(), nthreads, [](const RunKey &a, const RunKey &b) { + return a.key < b.key || (a.key == b.key && a.run < b.run); + }); // Where a group starts is a property of the sorted keys alone, so mark the starts on all threads and // number them with a prefix - the same ids the append-as-you-go walk handed out. The gemmi ASU