diff --git a/image_analysis/scale_merge/RotationScaleMerge.cpp b/image_analysis/scale_merge/RotationScaleMerge.cpp index c7e7a0d06..0422ea4a6 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.cpp +++ b/image_analysis/scale_merge/RotationScaleMerge.cpp @@ -374,6 +374,7 @@ void RotationScaleMerge::Ingest() { TimingMark mark(*logger, "rsm ingest"); scale_partials_choice.reset(); n_frames = static_cast(partials_out.size()); + if (frame_limit >= 0) n_frames = std::min(n_frames, frame_limit); partials_released = false; resident_ingest = false; #ifdef JFJOCH_USE_CUDA @@ -385,7 +386,7 @@ void RotationScaleMerge::Ingest() { #endif size_t total = 0; - for (const auto &o : partials_out) total += o.reflections.size(); + for (int o = 0; o < n_frames; ++o) total += partials_out[o].reflections.size(); frame_cell_ok.assign(n_frames, 1); g_partial.assign(n_frames, 1.0); diff --git a/image_analysis/scale_merge/RotationScaleMerge.h b/image_analysis/scale_merge/RotationScaleMerge.h index 70405ba6d..771dd3a6e 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.h +++ b/image_analysis/scale_merge/RotationScaleMerge.h @@ -143,6 +143,9 @@ public: // unmerged MTZ describe the merge that was written. void SetWriteBackPerFrameScale(bool on) { write_back_per_frame_scale = on; } + // Merge only the first n frames of the outcomes (by default all of them). Set before Ingest(). + void SetFrameLimit(int n) { frame_limit = n; } + // Whether Run() also hands back the merge's fulls unmerged (Result::scaled_fulls). Off by default: // it is a copy as long as the fulls, wanted only by a caller that writes them. void SetExportScaledFulls(bool on) { export_scaled_fulls = on; } @@ -476,6 +479,7 @@ private: bool gpu_active_ = false; #endif bool write_back_per_frame_scale = true; // see SetWriteBackPerFrameScale + int frame_limit = -1; // see SetFrameLimit bool export_scaled_fulls = false; // see SetExportScaledFulls bool french_wilson = true; // see SetFrenchWilson diff --git a/rugnux/RugnuxScaleMerge.cpp b/rugnux/RugnuxScaleMerge.cpp index ba8286506..504e20cd0 100644 --- a/rugnux/RugnuxScaleMerge.cpp +++ b/rugnux/RugnuxScaleMerge.cpp @@ -697,6 +697,33 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { // before the engine is built rather than beside it - the gathered observations and their // rocking events are gigabytes on a long axis, and beside the engine's own arrays they set // the pass's memory high-water mark. + // The quality guard judges a refined pass against the pre-pass on the search merge, and a + // pre-pass that integrated only the first part of the sweep merged only that part. Against + // it a merge of the whole sweep holds more reflections, more signal and more of the axial + // rows for no reason but the frames, and the guard could never find it worse. So the guard + // reads this pass's own integration of the same frames, merged the way the pre-pass merged + // them. It only reads the outcomes (the per-frame scale is not written back), and so does the + // post-refinement, so it runs beside it; its log lines are held and replayed where it is used. + const bool merge_first_part = is_rotation && quality_guard_pass1_ && prepass_images < images_to_process + && !experiment_.GetRefineRotationWedgeInScaling() && !rot_ss.GetRotationWedgeForScaling().has_value(); + Logger guard_log = Logger::Buffered(); + const auto merge_guard = [&] { + TimingMark mark(guard_log, "merge quality guard (pre-pass frames)"); + RotationScaleMerge first_part_rsm(experiment_, indexer->GetIntegrationOutcome(), result.consensus_cell, + static_cast(config_.scaling_iter), config_.nthreads, + guard_log, ""); + first_part_rsm.SetFrameLimit(prepass_images); + first_part_rsm.SetWriteBackPerFrameScale(false); + first_part_rsm.Ingest(); + first_part_rsm.SetExportScaledFulls(false); + first_part_rsm.SetFrenchWilson(false); + auto r = first_part_rsm.Run(!experiment_.GetGemmiSpaceGroup().has_value(), + /*full_stats=*/false, /*measure_cc_before_corrections=*/true); + return ScaleMergeResult{std::move(r.merged), std::move(r.statistics), r.cc_half_before_corrections}; + }; + std::future guard_ahead; + if (merge_first_part && !(postrefine_probe_ && !geometry_prepass && postrefine_probe_only_)) + guard_ahead = std::async(std::launch::async, merge_guard); if (postrefine_probe_ && !geometry_prepass && is_rotation) { post_refine_geometry(); if (speculate_geometry_probe_ && !cancelled_ && prepass_detector_geometry_ && result.post_refine @@ -738,38 +765,11 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Rotation scaling/merging (RotationScaleMerge) does not support " "wedge refinement"); - // The quality guard judges a refined pass against the pre-pass on the search merge, and a - // pre-pass that integrated only the first part of the sweep merged only that part. Against - // it a merge of the whole sweep holds more reflections, more signal and more of the axial - // rows for no reason but the frames, and the guard could never find it worse. So the guard - // reads this pass's own integration of the same frames, merged the way the pre-pass merged - // them. The merge writes its per-frame scale back, so it gets copies of the outcomes and - // only borrows the reflections. - if (quality_guard_pass1_ && prepass_images < images_to_process) { + if (merge_first_part) { logger.Info("Two-pass: the quality guard reads this pass's merge of the first {} images, " "the ones the pre-pass integrated", prepass_images); - auto &outcomes = indexer->GetIntegrationOutcome(); - std::vector first_part(prepass_images); - for (int i = 0; i < prepass_images; ++i) { - ReflectionVector reflections = std::move(outcomes[i].reflections); - first_part[i] = outcomes[i]; - first_part[i].reflections = std::move(reflections); - } - { - TimingMark mark(logger, "merge quality guard (pre-pass frames)"); - RotationScaleMerge first_part_rsm(experiment_, first_part, result.consensus_cell, - static_cast(config_.scaling_iter), config_.nthreads, - logger, ""); - first_part_rsm.Ingest(); - first_part_rsm.SetExportScaledFulls(false); - first_part_rsm.SetFrenchWilson(false); - auto r = first_part_rsm.Run(!experiment_.GetGemmiSpaceGroup().has_value(), - /*full_stats=*/false, /*measure_cc_before_corrections=*/true); - guard_merge = ScaleMergeResult{std::move(r.merged), std::move(r.statistics), - r.cc_half_before_corrections}; - } - for (int i = 0; i < prepass_images; ++i) - outcomes[i].reflections = std::move(first_part[i].reflections); + guard_merge = guard_ahead.valid() ? guard_ahead.get() : merge_guard(); + guard_log.ReplayInto(logger); } // A reference MTZ is allowed for rotation: it fixes the space group / cell (on the CLI) and // resolves the indexing ambiguity (below), but is NOT used to scale - the rotation merge stays