From 2091c0524a19ede5278eff25ee5ed78a9ea51a8c Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Thu, 15 Oct 2026 12:52:14 +0200 Subject: [PATCH] GPU memory: a probe pass hands its memory to the card when it ends The speculative geometry probe and the rotation-scale walk's indexing probes gave their indexers' and image workers' buffers back to the allocation pool only, which kept them reserved under the main line's merge and analyses: 5.5 GB on 8qaw and 5.3 GB on 4nwv on the RTX 5080, until the end of the run. The pool is trimmed (every card: cuda_trim_allocation_pools) when such a probe ends, as IndexerThreadPool::ReleaseIndexers now does through the same helper. Measured (RTX 5080): device memory held at the end of the final merge 8.7 -> 3.4 GB (4nwv), 14.5 -> 7.7 GB (8qaw). Output unchanged: p.hkl byte-identical on cytc_x10sa, myob_x10sa, 4nwv and 8qaw, on this card and with its total read as 8.55 GB. Co-Authored-By: Claude Opus 5.5 --- image_analysis/indexing/CUDAMemHelpers.h | 8 ++++++++ image_analysis/indexing/IndexerThreadPool.cpp | 5 +---- rugnux/Rugnux.cpp | 5 +++++ rugnux/RugnuxPasses.cpp | 5 +++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/image_analysis/indexing/CUDAMemHelpers.h b/image_analysis/indexing/CUDAMemHelpers.h index c5341a484..f0b2128bf 100644 --- a/image_analysis/indexing/CUDAMemHelpers.h +++ b/image_analysis/indexing/CUDAMemHelpers.h @@ -204,6 +204,14 @@ inline void cuda_trim_allocation_pool() { cudaGetLastError(); } +// The same on every card: for work that has ended and whose threads were pinned round-robin (pin_gpu). +inline void cuda_trim_allocation_pools() { + for (int32_t d = 0; d < get_gpu_count(); d++) { + ScopedGpuPin pin(d); + cuda_trim_allocation_pool(); + } +} + // Whether `bytes` more device memory can be had on the current device, for a step whose buffers are // sized by the data set and which ends the run when they do not fit. The free memory counts what the // pool holds with no buffer in it (up to CUDA_MEM_POOL_RELEASE_THRESHOLD) as taken, so that is handed diff --git a/image_analysis/indexing/IndexerThreadPool.cpp b/image_analysis/indexing/IndexerThreadPool.cpp index 820020e15..2707482ad 100644 --- a/image_analysis/indexing/IndexerThreadPool.cpp +++ b/image_analysis/indexing/IndexerThreadPool.cpp @@ -286,10 +286,7 @@ void IndexerThreadPool::ReleaseIndexers() { #ifdef JFJOCH_USE_CUDA // Their buffers went back to the allocation pool, which would keep them reserved for the next // buffer of whoever asks; hand them to the card. - for (int32_t d = 0; d < get_gpu_count(); d++) { - ScopedGpuPin pin(d); - cuda_trim_allocation_pool(); - } + cuda_trim_allocation_pools(); #endif } diff --git a/rugnux/Rugnux.cpp b/rugnux/Rugnux.cpp index 33483f7a0..d52b589ba 100644 --- a/rugnux/Rugnux.cpp +++ b/rugnux/Rugnux.cpp @@ -569,6 +569,11 @@ void Rugnux::StartSpeculativeGeometryProbe(std::shared_ptr run, const st Logger("Rugnux").Warning("Speculative geometry probe failed: {}", e.what()); } } +#ifdef JFJOCH_USE_CUDA + // What its indexers and image workers held went back to the allocation pool; hand it to the card, + // or it stays reserved under the merge and the validation beside which the probe ran. + cuda_trim_allocation_pools(); +#endif }); speculative_probe_ = std::move(sp); } diff --git a/rugnux/RugnuxPasses.cpp b/rugnux/RugnuxPasses.cpp index b620c9379..01ae4bc01 100644 --- a/rugnux/RugnuxPasses.cpp +++ b/rugnux/RugnuxPasses.cpp @@ -756,6 +756,11 @@ RotationScaleWalk Rugnux::WalkRotationScaleBeside(const ProcessResult &pass1, Ro // empty result reads as. logger.Info("Rotation scale {:.5f}: the probe pass did not complete ({})", k, e.what()); } +#ifdef JFJOCH_USE_CUDA + // As the speculative probe (StartSpeculativeGeometryProbe): what it held goes back to the card. + if (index_only) + cuda_trim_allocation_pools(); +#endif return res; }; const auto probe = [&](float k, bool index_only) {