Spread the scaling corrections and the space-group search over the cores

Two thirds of a rotation run is one thread. The image loop is not the problem -
on the heaviest crystal of the battery it is 1.8 s of 40 - and neither GPU nor
CPU is saturated, because while the corrections and the space-group search run
there is one core working and 47 idle. Mean occupancy over the whole run: 3.9 of
48.

In the correction surfaces (absorption in the goniometer frame, detector-plane
modulation, absorption against time and detector position - all one function):
the per-cell accumulation, the score reduction and the final apply are now
chunked, as are the three loops that assign a full to its cell, one of which
spends a sine and a cosine per full de-rotating it into the crystal frame. Two
full sorts of four million floats went with them: only the nine bin edges are
wanted, so they are selected instead, each selection starting where the last one
left off.

The per-group pass is deliberately left serial. The terms of one group are
spread all over the list, so the only way to give a thread groups of its own is
to walk in group order, and that trades a near-sequential read of the fulls for
a random one over a few hundred megabytes - the trade that already lost once in
the combine kernel.

The space-group search scores each candidate rotation by correlating I(h)
against I(Rh) over the whole merge. Every operator it can ask about comes from a
fixed list and none of them depend on each other, so they are scored up front,
in parallel, and the search reads the cache. The scratch that stops a pair being
counted twice is now per worker rather than shared.

Worker counts are gated on how much work there is, not on how many cores the
machine has (ThreadsForWork). Both parallel helpers start a thread per chunk, so
a small dataset on a large node would otherwise pay for 48 thread starts to sum
a few thousand terms - and this runs on 8-core laptops as well as on this node.

Measured on the heaviest crystal, idle machine, two runs each, summed over both
passes: those phases go 7.88 s -> 5.19 s. Whole-run wall time is the wrong ruler
for it - it moves +-4 s between identical runs. Battery 9m45s -> 9m23s, space
group 21/24, no failures; 16 of 24 crystals bit-identical to the previous run
and the rest inside the noise floor of running one binary twice.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
jungfrau
2026-08-23 08:25:43 -04:00
co-authored by Claude Opus 5
parent 01b619cfd0
commit ca3ca7170e
5 changed files with 175 additions and 71 deletions
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "SearchSpaceGroup.h"
#include "../../common/ParallelFor.h"
#include <algorithm>
#include <array>
@@ -315,10 +316,10 @@ SearchSpaceGroupResult SearchSpaceGroup(
key_to_index.emplace(key[i], static_cast<int>(i));
// --- Stage A: score each distinct rotation operator once ---
std::vector<uint32_t> visited(n, 0);
uint32_t epoch = 0;
auto score_operator = [&](const gemmi::Op& op) -> SpaceGroupOperatorScore {
// `visited` and `epoch` are scratch, taken as arguments rather than captured so that several
// operators can be scored at once - each worker below keeps its own pair.
auto score_operator = [&](const gemmi::Op& op, std::vector<uint32_t>& visited,
uint32_t& epoch) -> SpaceGroupOperatorScore {
++epoch;
std::vector<double> x, y;
for (size_t i = 0; i < n; ++i) {
@@ -362,12 +363,14 @@ SearchSpaceGroupResult SearchSpaceGroup(
};
std::map<std::array<int, 9>, SpaceGroupOperatorScore> op_cache;
std::vector<uint32_t> visited(n, 0);
uint32_t epoch = 0;
auto operator_score = [&](const gemmi::Op& op) -> const SpaceGroupOperatorScore& {
const auto rk = RotKey(op);
auto it = op_cache.find(rk);
if (it != op_cache.end())
return it->second;
return op_cache.emplace(rk, score_operator(op)).first->second;
return op_cache.emplace(rk, score_operator(op, visited, epoch)).first->second;
};
// Conjugate rotations (symmetry-equivalent within the point group) relate symmetry-equivalent
@@ -423,6 +426,34 @@ SearchSpaceGroupResult SearchSpaceGroup(
holohedry = HolohedryRotationSet(opt.lattice_system.value());
const auto point_groups = EnumeratePointGroups(holohedry);
// Every operator the search can ask about comes from this list, and scoring one is a pass over the
// whole merge with a hash lookup per reflection - the most expensive thing in here. They do not
// depend on each other, so score the distinct ones now and let the search below read the cache.
// One `visited` per worker, not per operator: it is as long as the merge, so allocating it per
// operator would cost more than the scoring.
{
std::vector<gemmi::Op> distinct;
std::vector<std::array<int, 9>> keys;
for (const auto& pg : point_groups)
for (const auto& rot : pg.rotations) {
const auto rk = RotKey(rot);
if (std::find(keys.begin(), keys.end(), rk) == keys.end()) {
keys.push_back(rk);
distinct.push_back(rot);
}
}
std::vector<SpaceGroupOperatorScore> scored(distinct.size());
ParallelChunks(static_cast<int>(distinct.size()),
std::min(opt.nthreads, distinct.size()), [&](int lo, int hi) {
std::vector<uint32_t> scratch(n, 0);
uint32_t ep = 0;
for (int i = lo; i < hi; ++i)
scored[i] = score_operator(distinct[i], scratch, ep);
});
for (size_t i = 0; i < distinct.size(); ++i)
op_cache.emplace(keys[i], scored[i]);
}
// Mapping every observation onto its symmetry representative under a candidate's rotations - one
// apply_to_hkl + Canonicalize per observation per operator - is the expensive half of BOTH
// quantities below, and both need exactly the same mapping. Build it once per point group.