From 3a5340696c6a39ffd57fdbc6ae435e4108a988e0 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 3 Oct 2026 12:33:41 +0200 Subject: [PATCH] Pre-scan: measure the spots on the card in GPU builds The pre-scan's spot measurement (spot width and bandwidth, powder rings, spot-symmetry beam centre) ran the CPU preprocessor and adaptive spot finder on every sampled frame, ~13-19 core-seconds per 16M-pixel run. In a GPU build it now uses the same engines as the image loops: the frame is decoded and preprocessed on the device (with the host decoder as fallback), the spots are found and extracted there, and the preprocessed image comes back only when the spot width needs it. This is a choice of where, not of what: the device finders reproduce the host's spot list exactly (integer ring sums, the same connected components in the same order). Checked frame by frame with both engines side by side on myob/cytc/thau x10sa and all 21 smoke-tier sets (~540k spots, HDF5, CBF, marCCD, SMV, pink beam, 3.8 keV): identical spot lists in identical order on every frame, and an identical preprocessed image on every width frame. p.mtz md5 identical to the oracle on myob/cytc/thau, GPU and CPU builds. Measured on a loaded box (load 20-30): the spot measurement takes 0.8-0.95 s instead of 2.5-3.6 s and no longer competes for the cores the beam-stop mask and the background beam-centre fit run on; the pre-scan falls from 3.0-4.9 s to 1.7-2.4 s. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01K5K8jvPPbmCrbqnWkddTuB --- rugnux/Rugnux.cpp | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/rugnux/Rugnux.cpp b/rugnux/Rugnux.cpp index d99d8d8e1..736298cb8 100644 --- a/rugnux/Rugnux.cpp +++ b/rugnux/Rugnux.cpp @@ -57,6 +57,7 @@ #ifdef JFJOCH_USE_CUDA #include "../image_analysis/image_preprocessing/ImagePreprocessorGPU.h" #include "../image_analysis/image_preprocessing/ImagePreprocessorBufferGPU.h" +#include "../image_analysis/spot_finding/AdaptiveSpotFinderGPU.h" #endif #include "../image_analysis/scale_merge/Merge.h" #include "../image_analysis/scale_merge/RfreeFlags.h" @@ -929,12 +930,32 @@ void Rugnux::PreScan(int start_image, int images_to_process, int frame_count, Ru struct PreScanWorker { std::vector decompression_buffer; JFJochReaderRawImage raw_image; - std::unique_ptr preprocessor; + std::unique_ptr preprocessor; std::unique_ptr preprocessed; std::unique_ptr spot_finder; }; const auto make_worker = [&] { PreScanWorker w; +#ifdef JFJOCH_USE_CUDA + // On the card where there is one, as in the image loops: the frame is decoded and preprocessed + // there and the spots are found and extracted there. The device finders give the host's spot + // list to the bit - integer ring sums, the same connected components in the same order + // (AdaptiveSpotFinderGPU, SpotExtractorGPU) - so this is a choice of where, not of what. The + // preprocessed image comes back only for the spot width, which reads pixels around the spots. + if (want_spots && get_gpu_count() > 0) { + auto stream = std::make_shared(); + w.preprocessor = std::make_unique(prescan_x, prescan_mask, stream, + /*copy_image_to_host=*/want_width); + w.preprocessed = std::make_unique(prescan_x.GetPixelsNum(), + /*host_mirror=*/want_width); + if (config_.spot_finding.adaptive_threshold) + w.spot_finder = std::make_unique(*prescan_mapping, stream); + else + w.spot_finder = std::make_unique(prescan_x.GetXPixelsNumConv(), + prescan_x.GetYPixelsNumConv(), stream); + return w; + } +#endif if (want_spots) { w.preprocessor = std::make_unique(prescan_x, prescan_mask); w.preprocessed = std::make_unique(prescan_x.GetPixelsNum()); @@ -956,8 +977,20 @@ void Rugnux::PreScan(int start_image, int images_to_process, int frame_count, Ru bool for_width, std::vector &curves, std::vector &spot_q) { try { - w.preprocessor->Analyze(*w.preprocessed, - image.GetUncompressedPtr(w.decompression_buffer), image.GetMode()); + // As in the image loops: a frame the device cannot decode goes to the host decoder. + ImageStatistics stats; + bool decoded_on_device = false; + try { + decoded_on_device = w.preprocessor->AnalyzeCompressed(*w.preprocessed, image, stats); + } catch (const JFJochException &e) { + cuda_throw_if_context_lost(); + logger.Warning("Pre-scan: device decoding of image {} failed ({}), decompressing it on " + "the host", image_idx, e.what()); + cuda_clear_error(); + } + if (!decoded_on_device) + w.preprocessor->Analyze(*w.preprocessed, + image.GetUncompressedPtr(w.decompression_buffer), image.GetMode()); } catch (const std::exception &e) { if (IsFatalResourceError(e)) throw; logger.Warning("Pre-scan: failed to preprocess image {}: {}", image_idx, e.what());