From 095814baed3bb280f305b800d4f776e23dfbe521 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Mon, 12 Oct 2026 23:31:49 +0200 Subject: [PATCH] rugnux: place the quality guard from the pre-pass engine's size, before either ingest On a set too large for the quality guard's engine to sit beside the main one (8qaw: 123 M observations; 8a1a), the main engine was ingested, found not to leave room, dropped and ingested again after the guard - several seconds of ingest for nothing. The pre-pass's engine merged the guard's very frames earlier in the run, so its card memory per reflection is now kept (prepass_engine_bytes_ / prepass_engine_reflections_) and both engines are reckoned from it before either ingests: the guard at its frames' reflections, the main engine at all of them (observations add in proportion; rocking events and distinct hkl at most in proportion). Beside where the two fit the card less the 3 GB reserve, otherwise the guard first. The re-ingest path is gone. 7qij, 9v16, cytc: beside; 8qaw, 8a1a: guard first. Same merges, same numbers. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01EizBKhTqrAYago9KJG3dA3 --- .../scale_merge/RotationScaleMerge.cpp | 4 +- rugnux/Rugnux.h | 5 +++ rugnux/RugnuxScaleMerge.cpp | 44 ++++++++++--------- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/image_analysis/scale_merge/RotationScaleMerge.cpp b/image_analysis/scale_merge/RotationScaleMerge.cpp index 814f715ea..0ec41c6f3 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.cpp +++ b/image_analysis/scale_merge/RotationScaleMerge.cpp @@ -702,8 +702,8 @@ size_t RotationScaleMerge::DeviceBytes() const { + BYTES_PER_HKL * rawrun_start.size(); // The resident ingest's own peak comes before any of that, over every reflection read and every run // before the resolution cut: the observation arrays, the sort key and the integrated delta_phi of - // each (66 bytes) and 24 bytes of the sort's or the cut's working arrays beside them, plus sixteen - // arrays per run at the cut (IngestSort). + // each (66 bytes) and 24 bytes of the sort's or the cut's working arrays beside them, plus sixty-four + // bytes per run at the cut (IngestSort). constexpr size_t INGEST_BYTES_PER_REFLECTION = 90, INGEST_BYTES_PER_RUN = 64; const size_t ingest = INGEST_BYTES_PER_REFLECTION * ingest_reflections + INGEST_BYTES_PER_RUN * ingest_runs; return std::max(bytes + bytes / 5, ingest); diff --git a/rugnux/Rugnux.h b/rugnux/Rugnux.h index 2d6571c8c..c0291f919 100644 --- a/rugnux/Rugnux.h +++ b/rugnux/Rugnux.h @@ -788,6 +788,11 @@ class Rugnux { // fed to the second pass's Bragg prediction. Empty outside the rotation two-pass. std::vector prepass_mosaicity_; + // The two-pass geometry pre-pass's scaling engine: its card memory (RotationScaleMerge::DeviceBytes) + // and the reflections it read. A refined pass reckons the engines it is about to build from them, + // per reflection - the quality guard's merges the pre-pass frames again. Zero outside the two-pass. + size_t prepass_engine_bytes_ = 0, prepass_engine_reflections_ = 0; + // Two-pass geometry pre-pass: the detector geometry the first pass ends on - beam_x, beam_y in px, // distance in mm, then rot1 and rot2 in rad - applied by Run() before the second pass. Empty when // the pre-pass committed no change (the second pass then reproduces the first). diff --git a/rugnux/RugnuxScaleMerge.cpp b/rugnux/RugnuxScaleMerge.cpp index 7bfea663b..63dd4c3b0 100644 --- a/rugnux/RugnuxScaleMerge.cpp +++ b/rugnux/RugnuxScaleMerge.cpp @@ -1087,15 +1087,13 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { config_.nthreads, engine_log, config_.observation_dump_path); rsm->IngestHost(); } - rsm->IngestDevice(); - release_gather(); const int32_t cards = get_gpu_count(); constexpr size_t CARD_BYTES_RESERVED = size_t(3) << 30; // With one card the guard's engine goes beside this one where the two fit the card's total, - // and otherwise has the card to itself first, this engine being ingested again after it. Its - // size is reckoned as this engine's share of the reflections its frames hold: it merges those - // frames' part of these same outcomes, so it holds no more observations per reflection, and no - // more rocking events or distinct hkl, than this engine does. Judged on the total, as the + // and otherwise has the card to itself first. Both are reckoned, per reflection, at what the + // pre-pass's engine took for the same frames' reflections: the guard merges those frames again, + // and this engine merges more of the same crystal, which adds observations in proportion and + // rocking events and distinct hkl at most in proportion. Judged on the total, as the // all-observation arm below is, so that where the guard runs does not depend on the moment. size_t guard_bytes = 0; if (merge_first_part && !guard_ahead.valid()) { @@ -1106,29 +1104,33 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { if (o < static_cast(prepass_images)) n_first += static_cast(outcomes[o].reflections.size()); } - const auto guard_estimate = static_cast(n_all > 0.0 ? rsm->DeviceBytes() * (n_first / n_all) : 0.0); - const bool fits = rsm->DeviceBytes() + guard_estimate + CARD_BYTES_RESERVED <= get_gpu_total_memory(0); + const double per_reflection = prepass_engine_reflections_ > 0 + ? static_cast(prepass_engine_bytes_) / static_cast(prepass_engine_reflections_) : 0.0; + const auto guard_estimate = static_cast(per_reflection * n_first); + const auto engine_estimate = static_cast(per_reflection * n_all); + const size_t card_bytes = cards > 0 ? get_gpu_total_memory(0) : 0; + const bool beside = cards == 1 && per_reflection > 0.0 + && engine_estimate + guard_estimate + CARD_BYTES_RESERVED <= card_bytes; if (cards == 1) - logger.Info("Two-pass: the quality guard's merge {} this pass's engine on the card ({:.1f} GB " - "beside {:.1f} GB on a {:.1f} GB card)", fits ? "runs beside" : "runs before", - guard_estimate / 1e9, rsm->DeviceBytes() / 1e9, get_gpu_total_memory(0) / 1e9); - if (cards == 1 && fits) { + logger.Info("Two-pass: the quality guard's merge runs {} this pass's engine on the card ({:.1f} GB " + "{} {:.1f} GB on a {:.1f} GB card)", beside ? "beside" : "before", + guard_estimate / 1e9, beside ? "beside" : "and", engine_estimate / 1e9, card_bytes / 1e9); + if (beside) { guard_bytes = guard_estimate; guard_ahead = std::async(std::launch::async, [&] { return merge_guard(); }); } else { - if (cards > 0) - rsm.reset(); guard_merge = merge_guard(); guard_log.ReplayInto(logger); - if (!rsm) { - engine_log = Logger::Buffered(); - rsm.emplace(experiment_, indexer->GetIntegrationOutcome(), result.consensus_cell, - static_cast(config_.scaling_iter), - config_.nthreads, engine_log, config_.observation_dump_path); - rsm->Ingest(); - } } } + rsm->IngestDevice(); + release_gather(); + if (geometry_prepass) { + prepass_engine_bytes_ = rsm->DeviceBytes(); + prepass_engine_reflections_ = 0; + for (const auto &io : indexer->GetIntegrationOutcome()) + prepass_engine_reflections_ += io.reflections.size(); + } engine_log.ReplayInto(logger); rsm->SetLogger(logger); // On a card of its own from three cards up (card 0 has this engine, card 1 the quality guard's