diff --git a/image_analysis/scale_merge/RotationScaleMerge.h b/image_analysis/scale_merge/RotationScaleMerge.h index ef4b730f5..6291bdf1e 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.h +++ b/image_analysis/scale_merge/RotationScaleMerge.h @@ -258,6 +258,10 @@ public: // The copy does not write its per-frame scales back (WriteBackPerFrameScale does). [[nodiscard]] std::unique_ptr CloneIngest(int device, Logger &log) const; + // The per-frame scale choice the first merge that is not a search makes (see Run), from a copy that made + // that merge in this engine's place: the merges after it keep it, as they would on this engine. + void TakeScalePartialsChoice(const RotationScaleMerge &from) { scale_partials_choice = from.scale_partials_choice; } + // The card memory an engine may hold during a merge, the device helpers its merge calls included: // what a caller goes by when it decides whether a second engine fits beside one already on the // card. Call after IngestHost(). It counts what the ingest knows - the observations, the rocking diff --git a/rugnux/Rugnux.h b/rugnux/Rugnux.h index f670373ff..8ce4222a2 100644 --- a/rugnux/Rugnux.h +++ b/rugnux/Rugnux.h @@ -997,6 +997,11 @@ class Rugnux { // describes neither geometry. The two travel together or not at all. std::optional> prepass_detector_geometry_; + // The space group a search of the geometry pre-pass's P1 merge finds. A prediction only: the canonical + // pass merges in it ahead, on a copy of its engine, and keeps that merge only where its own decision + // lands on the same group on the same ingest (ScaleMergeAndSymmetry). It decides nothing. + std::shared_future> predicted_group_; + // The goniometer rotation scale the run adopted (WalkRotationScale, in RunAllPasses), applied to the // second pass's goniometer - and, while the walk runs, the scale its current probe pass is at. // Empty where the stored angles stand. diff --git a/rugnux/RugnuxScaleMerge.cpp b/rugnux/RugnuxScaleMerge.cpp index 8d07a502e..fc1cc42de 100644 --- a/rugnux/RugnuxScaleMerge.cpp +++ b/rugnux/RugnuxScaleMerge.cpp @@ -89,6 +89,73 @@ using namespace rugnux_internal; namespace { + // The space-group search's resolution cut on a P1 merge: where the best non-increasing fit of its + // thin-shell drops under 1 (0: no cut). See the search below. + double SearchCut(const std::vector &merged) { + double d_min_search = 0.0; + std::vector> rs; // (d, I/sigma) over the full P1 merge + rs.reserve(merged.size()); + for (const auto &m : merged) + if (std::isfinite(m.I) && std::isfinite(m.sigma) && m.sigma > 0.0f && + std::isfinite(m.d) && m.d > 0.0f) + rs.emplace_back(m.d, m.I / m.sigma); + if (rs.size() >= 400) { + std::sort(rs.begin(), rs.end(), + [](const auto &a, const auto &b) { return a.first > b.first; }); // low -> high res + const int bins = std::clamp(static_cast(rs.size() / 100), 1, 40); + const size_t per = (rs.size() + bins - 1) / static_cast(bins); + // The shell means, then their best NON-INCREASING fit (pool adjacent violators, weighted + // by shell size). The cut is where the FIT drops under 1, not where the first shell + // does: on a merge whose is flat near 1 - its ISa has collapsed - the first + // single shell to dip under 1 is a coin toss that moved the cut from 0.85 to 1.19 A + // between two compiler flags on one crystal, and the coarser cut left its glide zones + // too few absences to be judged. The fit moves by as much as its input does. + std::vector mean, weight; + for (size_t lo = 0; lo < rs.size(); lo += per) { + const size_t hi = std::min(rs.size(), lo + per); + double sum = 0.0; + for (size_t j = lo; j < hi; ++j) sum += rs[j].second; + mean.push_back(sum / static_cast(hi - lo)); + weight.push_back(static_cast(hi - lo)); + } + std::vector block_mean, block_weight; + std::vector block_size; + for (size_t b = 0; b < mean.size(); ++b) { + block_mean.push_back(mean[b]); block_weight.push_back(weight[b]); block_size.push_back(1); + while (block_mean.size() > 1 && block_mean[block_mean.size() - 2] < block_mean.back()) { + const size_t k = block_mean.size() - 1; + const double w = block_weight[k - 1] + block_weight[k]; + block_mean[k - 1] = (block_mean[k - 1] * block_weight[k - 1] + block_mean[k] * block_weight[k]) / w; + block_weight[k - 1] = w; + block_size[k - 1] += block_size[k]; + block_mean.pop_back(); block_weight.pop_back(); block_size.pop_back(); + } + } + std::vector fit; + for (size_t k = 0; k < block_mean.size(); ++k) + fit.insert(fit.end(), block_size[k], block_mean[k]); + for (size_t b = 0; b < fit.size(); ++b) { + const size_t lo = b * per; + if (fit[b] < 1.0) { + // Cut the noise-dominated high-res shells. When even the lowest-res shell fails, + // keep that shell alone rather than abandoning the cut. The bound is absolute + // while the merged I/sigma it tests saturates at the merge's own ISa (times the + // square root of the multiplicity), so a merge whose ISa has collapsed towards 1 + // can fail it in every shell - and that is exactly the merge the search must not + // be handed whole, since the noise which drove ISa down is what the cut exists to + // remove. Abandoning it feeds back: more noise into the search, a weaker operator + // statistic, and a lost symmetry. + if (b > 0) + d_min_search = rs[lo].first; + else if (per >= 400) + d_min_search = rs[per - 1].first; + break; + } + } + } + return d_min_search; + } + // How precisely the measured beam centre has to be placed before the post-refinement takes it as a // second place the centre is believed to be (the bound on its beam move). The ceiling is the larger // of this and what the geometry asks of the centre (BeamCenterNeed_pxl). @@ -470,6 +537,9 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { }; phase("Scaling and merging"); Step(geometry_prepass ? "Scaling and post-refinement" : "Scaling and merging"); + // Whether the reflections the engine ingested are still indexed as they were (the final merge made + // ahead, below): every change of basis of the integrated reflections clears it. + bool spec_same_ingest = true; // Ice-ring handling (--detect-ice-rings): flag reflections sitting on a hexagonal-ice powder // ring. Their integrated intensity is contaminated by the strong, variable ice background, so @@ -684,6 +754,7 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { r.l = static_cast(std::lround(reindex.a[2][0]*h + reindex.a[2][1]*k + reindex.a[2][2]*l)); } } + spec_same_ingest = false; result.consensus_cell = cand.conventional.GetUnitCell(); result.consensus_centering = cand.centering; end_msg.unit_cell = result.consensus_cell; @@ -710,6 +781,7 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { r.h = h[0]; r.k = h[1]; r.l = h[2]; } }); + spec_same_ingest = false; result.consensus_cell = CellInBasis(*result.consensus_cell, cob); end_msg.unit_cell = result.consensus_cell; if (end_msg.rotation_lattice.has_value()) @@ -1220,6 +1292,40 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { // a search that turns out not to need it (no point group from the first arm) does not read it. // Declared before the future, which waits for the merge when it goes, so the engine outlives it. std::unique_ptr rsm_all; + // The final merge made ahead: copies of the engine merging, beside the P1 merges, the search and the + // decisions, in the groups the run is expected to adopt - the one a search of the geometry pre-pass's + // merge predicted (predicted_group_), and the first arm's where that differs - each on a spare card + // (cards_ahead). Taken where the decision lands on that group on this ingest: no merge depends on the + // merges before it (RunArm), so it is the merge the engine would make. A decision that re-indexes the + // reflections clears spec_same_ingest. A deque, so that a second one does not move the first under its + // thread; declared before anything a merge reads, as rsm_all. + struct MergeAhead { + gemmi::SpaceGroup group; + std::unique_ptr rsm; + Logger log = Logger::Buffered(); + // ... with the intensity statistics for the centre on it (below), which read that merge alone. + std::future> ahead; + }; + std::deque merges_ahead; + std::vector cards_ahead; // the cards one may go on, in the order they are taken (below) + const auto merge_ahead = [&](const gemmi::SpaceGroup &g) { + MergeAhead &m = merges_ahead.emplace_back(); + m.group = g; + m.rsm = rsm->CloneIngest(cards_ahead[merges_ahead.size() - 1], m.log); + // As scale_and_merge sets them for the merge that may be written. + m.rsm->SetExportScaledFulls(write_output && config_.write_merged); + m.rsm->SetFrenchWilson(false); + m.rsm->IngestDevice(&*rsm); + m.ahead = std::async(std::launch::async, [&m, nthreads = config_.nthreads] { + pin_gpu(m.rsm->GetGpuDevice()); + TimingMark mark(m.log, "merge " + m.group.short_name() + " (ahead)"); + auto r = m.rsm->Run(/*for_search=*/false, /*full_stats=*/true, + /*measure_cc_before_corrections=*/false, &m.group); + mark.Stop(); + CentreOfSymmetryResult centre = AnalyzeCentreOfSymmetry(r.merged, m.group, nthreads); + return std::make_pair(std::move(r), std::move(centre)); + }); + }; Logger all_arm_log = Logger::Buffered(); std::future all_arm_ahead; const auto start_all_arm = [&rsm_all, &all_arm_log] { @@ -1336,6 +1442,72 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { p.postrefine_ahead.wait(); postrefine_beside = false; } + // The cards for the merges made ahead (merges_ahead), decided here from the card count so that a run + // on one card is exactly what it is without them: spare cards only - card 3 from four cards up, then + // card 2, the all-observation arm's, whose merge is done when a second one is asked for; on two or + // three cards the last card, beside the engine already there, where one more engine fits its total + // memory. And only where one more merge fits the host budget. GPU engines only: on the host it would + // take the threads the chain runs on. + if (!geometry_prepass && !experiment_.GetGemmiSpaceGroup().has_value() && config_.reference_data.empty() + && write_output && cards >= 2 && rsm->OnGPU()) { + const bool host_fits = host_engine + (all_arm ? 3 : 2) * host_merge + + (postrefine_beside ? host_postrefine : 0) <= host_budget_; + if (cards >= 4) + cards_ahead = {3, 2}; + else if ((2 + (all_arm && all_arm_device == cards - 1 ? 1 : 0)) * rsm->DeviceBytes() + CARD_BYTES_RESERVED + <= get_gpu_total_memory(cards - 1)) + cards_ahead = {cards - 1}; + if (!host_fits) + cards_ahead.clear(); + logger.Debug("Final merge ahead: {}", !host_fits ? "no room on the host" : cards_ahead.empty() + ? "no room on a card" : fmt::format("on card {}, {} at most", cards_ahead.front(), cards_ahead.size())); + } + if (!cards_ahead.empty() && p.gpu_merge) + p.gpu_merge->Holds(((all_arm ? 2 : 1) + cards_ahead.size()) * rsm->DeviceBytes()); + // The geometry pre-pass decides no group, but a search of a P1 merge of ALL its observations predicts + // the one the canonical pass will most likely adopt, and that pass merges in it ahead (merges_ahead). + // All observations, on a copy of the engine, because the pre-pass's own merge is Lorentz-filtered and + // a screw axis is read on the weak reflections the filter discards: searched on that merge a 4(1) + // crystal was predicted as its symmorphic group every time. On the last card, beside the rest of the + // pre-pass and the second pass, under the same card and host rule as a merge ahead; set up as the + // canonical search is. It decides nothing. + if (geometry_prepass && !experiment_.GetGemmiSpaceGroup().has_value() && config_.reference_data.empty() + && cards >= 2 && rsm->OnGPU() && rsm->GetSearchMinZeta() > 0.0 + && host_engine + 2 * host_merge + (postrefine_beside ? host_postrefine : 0) <= host_budget_ + && (cards >= 4 || 2 * rsm->DeviceBytes() + CARD_BYTES_RESERVED <= get_gpu_total_memory(cards - 1))) { + struct Prediction { + Logger log = Logger::Buffered(); + std::unique_ptr rsm; + }; + auto prediction = std::make_shared(); + prediction->rsm = rsm->CloneIngest(cards - 1, prediction->log); + prediction->rsm->SetSearchMinZeta(0.0); + prediction->rsm->SetExportScaledFulls(false); + prediction->rsm->SetFrenchWilson(false); + prediction->rsm->IngestDevice(&*rsm); + SearchSpaceGroupOptions o; + o.nthreads = static_cast(std::max(1, config_.nthreads)); + o.merge_friedel = experiment_.GetScalingSettings().GetMergeFriedel(); + if (end_msg.rotation_lattice_type.has_value()) { + o.lattice_system = end_msg.rotation_lattice_type->crystal_system; + o.lattice_centring = end_msg.rotation_lattice_type->centering; + } + if (result.consensus_cell.has_value()) + o.cell = gemmi::UnitCell(*result.consensus_cell); + o.enumerate_all_settings = true; + o.enumerate_all_rotation_sets = true; + const double d_manual = experiment_.GetScalingSettings().GetHighResolutionLimit_A().value_or(0.0); + predicted_group_ = std::async(std::launch::async, [prediction, o, d_manual]() mutable + -> std::optional { + pin_gpu(prediction->rsm->GetGpuDevice()); + auto merge = prediction->rsm->Run(/*for_search=*/true, /*full_stats=*/false, + /*measure_cc_before_corrections=*/false); + prediction.reset(); // the card is given back before the search + o.d_min_limit_A = std::max(SearchCut(merge.merged), d_manual); + o.merge_isa = merge.isa; + return SearchSpaceGroup(merge.merged, o).best_space_group; + }).share(); + } bool all_arm_beside = false; if (all_arm) { rsm_all = rsm->CloneIngest(all_arm_device, all_arm_log); @@ -1359,6 +1531,19 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { if (all_arm_beside) all_arm_ahead = start_all_arm(); } + // The pre-pass's prediction, where it is in: a search still running is one the merge is not waited for. + if (!cards_ahead.empty() && predicted_group_.valid() + && predicted_group_.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { + std::optional predicted; + try { + predicted = predicted_group_.get(); + } catch (const std::exception &e) { + logger.Debug("Final merge ahead: the pre-pass search failed ({})", e.what()); + } + logger.Debug("Final merge ahead: the pre-pass predicts {}", predicted ? predicted->xhm() : "no group"); + if (predicted) + merge_ahead(*predicted); + } } // The geometry pre-pass reads the per-image reflections exactly twice more: they were just @@ -1770,66 +1955,7 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { double d_min_search = 0.0; if (search_space_group) { - std::vector> rs; // (d, I/sigma) over the full P1 merge - rs.reserve(sm.merged.size()); - for (const auto &m : sm.merged) - if (std::isfinite(m.I) && std::isfinite(m.sigma) && m.sigma > 0.0f && - std::isfinite(m.d) && m.d > 0.0f) - rs.emplace_back(m.d, m.I / m.sigma); - if (rs.size() >= 400) { - std::sort(rs.begin(), rs.end(), - [](const auto &a, const auto &b) { return a.first > b.first; }); // low -> high res - const int bins = std::clamp(static_cast(rs.size() / 100), 1, 40); - const size_t per = (rs.size() + bins - 1) / static_cast(bins); - // The shell means, then their best NON-INCREASING fit (pool adjacent violators, weighted - // by shell size). The cut is where the FIT drops under 1, not where the first shell - // does: on a merge whose is flat near 1 - its ISa has collapsed - the first - // single shell to dip under 1 is a coin toss that moved the cut from 0.85 to 1.19 A - // between two compiler flags on one crystal, and the coarser cut left its glide zones - // too few absences to be judged. The fit moves by as much as its input does. - std::vector mean, weight; - for (size_t lo = 0; lo < rs.size(); lo += per) { - const size_t hi = std::min(rs.size(), lo + per); - double sum = 0.0; - for (size_t j = lo; j < hi; ++j) sum += rs[j].second; - mean.push_back(sum / static_cast(hi - lo)); - weight.push_back(static_cast(hi - lo)); - } - std::vector block_mean, block_weight; - std::vector block_size; - for (size_t b = 0; b < mean.size(); ++b) { - block_mean.push_back(mean[b]); block_weight.push_back(weight[b]); block_size.push_back(1); - while (block_mean.size() > 1 && block_mean[block_mean.size() - 2] < block_mean.back()) { - const size_t k = block_mean.size() - 1; - const double w = block_weight[k - 1] + block_weight[k]; - block_mean[k - 1] = (block_mean[k - 1] * block_weight[k - 1] + block_mean[k] * block_weight[k]) / w; - block_weight[k - 1] = w; - block_size[k - 1] += block_size[k]; - block_mean.pop_back(); block_weight.pop_back(); block_size.pop_back(); - } - } - std::vector fit; - for (size_t k = 0; k < block_mean.size(); ++k) - fit.insert(fit.end(), block_size[k], block_mean[k]); - for (size_t b = 0; b < fit.size(); ++b) { - const size_t lo = b * per; - if (fit[b] < 1.0) { - // Cut the noise-dominated high-res shells. When even the lowest-res shell fails, - // keep that shell alone rather than abandoning the cut. The bound is absolute - // while the merged I/sigma it tests saturates at the merge's own ISa (times the - // square root of the multiplicity), so a merge whose ISa has collapsed towards 1 - // can fail it in every shell - and that is exactly the merge the search must not - // be handed whole, since the noise which drove ISa down is what the cut exists to - // remove. Abandoning it feeds back: more noise into the search, a weaker operator - // statistic, and a lost symmetry. - if (b > 0) - d_min_search = rs[lo].first; - else if (per >= 400) - d_min_search = rs[per - 1].first; - break; - } - } - } + d_min_search = SearchCut(sm.merged); const double d_manual = experiment_.GetScalingSettings().GetHighResolutionLimit_A().value_or(0.0); if (rsm && d_min_search > d_manual) { logger.Info("Space-group search: re-scaling the P1 pass at {:.2f} A ( >= 1)", d_min_search); @@ -1946,6 +2072,29 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { } wait_mark.Stop(); } + // The twinning evidence measured before any promotion (see the decisions below), on the merge the arms + // carry forward - the all-observation one wherever it is there. Where the final merge is being made + // ahead, nothing else is left to hide that measurement behind, so it starts here, on a copy of the + // all-observation merge, beside the searches, and is taken below where that merge is carried forward + // in the same cell and centring. + const auto pre_promotion_analysis = [nthreads = config_.nthreads](const std::vector &merged, + std::optional cell, char centring) { + TranslationalNCSResult tncs; + if (cell) + tncs = AnalyzeTranslationalNCS(merged, gemmi::UnitCell(*cell), nullptr, 30, 0, nthreads); + auto twinning = AnalyzeTwinning(merged, nullptr, 20, TranslationalNCSVectorForLTest(tncs), centring); + return std::make_pair(tncs, twinning); + }; + const std::optional early_cell = result.consensus_cell; + const char early_centring = end_msg.rotation_lattice_type.has_value() + ? end_msg.rotation_lattice_type->centering : 'P'; + std::future> pre_promotion_early; + bool all_merge_carried = false; + if (all_merge && !cards_ahead.empty()) + pre_promotion_early = std::async(std::launch::async, [pre_promotion_analysis, merged = all_merge->merged, + early_cell, early_centring] { + return pre_promotion_analysis(merged, early_cell, early_centring); + }); if (all_merge) { SearchSpaceGroupOptions all_opts = sg_opts; all_opts.merge_isa = all_merge->isa; // as take_merge leaves it below @@ -1955,6 +2104,16 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { } auto sg_search = first_search_ahead.get(); first_arm_mark.Stop(); + // The first arm's group, merged ahead as well where it is not the one the pre-pass predicted and a card + // is left for it: the pre-pass's merge is of a third of the frames, and a screw axis its few axial + // reflections could not settle is read here on all of them. Where the first arm has no group either, + // nothing is merged ahead - the decisions are what will make one. + if (sg_search.best_space_group && merges_ahead.size() < cards_ahead.size() + && std::none_of(merges_ahead.begin(), merges_ahead.end(), [&](const MergeAhead &m) { + return std::strcmp(m.group.hall, sg_search.best_space_group->hall) == 0; })) { + logger.Debug("Final merge ahead: the first arm finds {}", sg_search.best_space_group->xhm()); + merge_ahead(*sg_search.best_space_group); + } // The Lorentz-filtered arm's merge, kept past the point where the all-observation one // replaces it. The metric re-ask below is a search like any other and answers to the same @@ -2152,6 +2311,7 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { // second opinion reported above. merged_filtered = std::move(sm.merged); sm = std::move(sm_all); + all_merge_carried = all_merge.has_value(); } // The arm merged beside the search is done with - or was not needed - before anything below @@ -2189,15 +2349,12 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { // cell is taken now: a change of basis committed below replaces it. const char lattice_centring = end_msg.rotation_lattice_type.has_value() ? end_msg.rotation_lattice_type->centering : 'P'; - auto pre_promotion = std::async(std::launch::async, - [&merged = sm.merged, cell = result.consensus_cell, - lattice_centring, nthreads = config_.nthreads] { - TranslationalNCSResult tncs; - if (cell) - tncs = AnalyzeTranslationalNCS(merged, gemmi::UnitCell(*cell), nullptr, 30, 0, nthreads); - auto twinning = AnalyzeTwinning(merged, nullptr, 20, TranslationalNCSVectorForLTest(tncs), - lattice_centring); - return std::make_pair(tncs, twinning); + auto pre_promotion = pre_promotion_early.valid() && all_merge_carried && early_cell == result.consensus_cell + && early_centring == lattice_centring + ? std::move(pre_promotion_early) + : std::async(std::launch::async, [&merged = sm.merged, cell = result.consensus_cell, lattice_centring, + pre_promotion_analysis] { + return pre_promotion_analysis(merged, cell, lattice_centring); }); TranslationalNCSResult pre_tncs; const auto join_pre_promotion = [&] { @@ -2785,6 +2942,7 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { static_cast(config_.scaling_iter), config_.nthreads, logger, config_.observation_dump); rsm->Ingest(); + spec_same_ingest = false; } const auto &uc = *result.consensus_cell; logger.Info("{} names a cell of {}x the volume of its reference setting: the " @@ -3024,6 +3182,7 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { result.consensus_cell, static_cast(config_.scaling_iter), config_.nthreads, logger, config_.observation_dump); rsm->Ingest(); + spec_same_ingest = false; } } } @@ -3038,6 +3197,7 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { for (auto &r : io.reflections) reindex_hkl(r, *commit_reindex); } + spec_same_ingest = false; result.consensus_cell = commit_cell; result.consensus_centering = commit_lattice_type->centering; end_msg.unit_cell = commit_cell; @@ -3051,6 +3211,7 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { static_cast(config_.scaling_iter), config_.nthreads, logger, config_.observation_dump); rsm->Ingest(); + spec_same_ingest = false; } } experiment_.SetSpaceGroup(sg); @@ -3059,15 +3220,52 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { result.space_group = sg; decisions_mark.Stop(); phase("Re-scaling in space group " + sg.short_name()); - auto adopted = scale_and_merge(sg.short_name(), false); + // The merge made ahead in this group on this ingest is taken, with its per-frame scales and its + // partial-scaling choice, which the merges after it keep as they would on this engine. The others + // are left to end on their own (merges_ahead reaps them when the step is over): one started at the + // first arm's answer is still merging here, and waiting for it put the whole of it on the critical + // path - 0.9 s of a 2.8 s step on a four-card server. + std::optional> ahead; + for (MergeAhead &m : merges_ahead) { + if (!spec_same_ingest || std::strcmp(m.group.hall, sg.hall) != 0) { + logger.Debug("Final merge ahead: made in {}, the run adopts {}{} - not taken", m.group.xhm(), + sg.xhm(), spec_same_ingest ? "" : " on a new ingest"); + continue; + } + TimingMark mark(logger, "merge " + sg.short_name() + " (made ahead, wait)"); + try { + ahead = m.ahead.get(); + } catch (const std::exception &e) { + logger.Warning("The final merge made ahead failed ({}); it is made again", e.what()); + } + mark.Stop(); + m.log.ReplayInto(logger); + if (ahead) { + m.rsm->WriteBackPerFrameScale(); + rsm->TakeScalePartialsChoice(*m.rsm); + } + m.rsm.reset(); + } + ScaleMergeResult adopted; + if (ahead) { + logger.Info("Final merge: made ahead in {}", sg.xhm()); + adopted = take_merge(sg.short_name(), false, std::move(ahead->first)); + } else { + adopted = scale_and_merge(sg.short_name(), false); + } + TimingMark pre_promotion_mark(logger, "pre-promotion analyses wait"); join_pre_promotion(); + pre_promotion_mark.Stop(); sm = std::move(adopted); // The intensity statistics for the centre, on this merge - the full one, resolution-cut and // corrected, in the adopted group. Read on every run and reported, Sohncke groups included, // so a chiral small molecule's reader sees them too; they decide only between a glide // group and its partner with the same absences (see above). - result.centre_statistics = AnalyzeCentreOfSymmetry(sm.merged, sg, config_.nthreads); + { + TimingMark centre_mark(logger, "centre-of-symmetry statistics"); + result.centre_statistics = ahead ? ahead->second : AnalyzeCentreOfSymmetry(sm.merged, sg, config_.nthreads); + } if (centre_partner && sg.is_centrosymmetric() && result.centre_statistics->reads_acentric && result.centre_twinning_excluded) { logger.Info("Centre of symmetry: {} and {} predict the same absences; the intensity "