From c44056b3f9cf77fc4305ae15749a6f029d2e4fda Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Fri, 20 Nov 2026 04:20:47 +0100 Subject: [PATCH] PostRefine: bound its fixed 16 Ceres threads by the machine's hardware threads Post-refinement asks Ceres for a fixed 16 threads (so the pieces Ceres sums the cost in do not move with -N). On a machine with fewer hardware threads, Ceres bounds the request itself and prints, through abseil logging, "Specified options.num_threads: 16 exceeds maximum available from the threading model Ceres was compiled with: 14" for every solve, along with the "All log messages before absl::InitializeLog()" banner. The count is now bounded first, by the same std::thread::hardware_concurrency() Ceres uses, so Ceres runs exactly as it did before and stays silent. Machines with 16 or more hardware threads are unchanged. Checked by making a run see 14 hardware threads (get_nprocs shimmed): the previous binary printed the warning 6 times on an open rotation set, the new one prints it 0 times. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HVeDQrPqZ52Tp6rzzZGxf3 --- image_analysis/geom_refinement/PostRefine.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/image_analysis/geom_refinement/PostRefine.cpp b/image_analysis/geom_refinement/PostRefine.cpp index ff43c94bc..fcb1f15ff 100644 --- a/image_analysis/geom_refinement/PostRefine.cpp +++ b/image_analysis/geom_refinement/PostRefine.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include "../../common/JFJochMath.h" // PI #include "XtalResidual.h" // the positional detector<->reciprocal residual, and the cell it is parameterised by @@ -710,10 +711,14 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations, // A fixed thread count, not -N: Ceres sums the cost and gradient in 4 * num_threads // pieces, so the steps it accepts and where it stops would otherwise move with -N. It // hands the pieces to its threads in scheduling order, which only one thread makes - // exact - and one thread costs a second or more here - so that part is left. + // exact - and one thread costs a second or more here - so that part is left. Ceres bounds + // it by the machine's hardware threads, with a warning, so it is bounded here first. constexpr int POSTREFINE_CERES_THREADS = 16; + const int hardware_threads = static_cast(std::thread::hardware_concurrency()); ceres::Solver::Options o; o.linear_solver_type = ceres::DENSE_QR; o.max_num_iterations = 60; - o.num_threads = POSTREFINE_CERES_THREADS; o.logging_type = ceres::LoggingType::SILENT; + o.num_threads = hardware_threads > 0 ? std::min(POSTREFINE_CERES_THREADS, hardware_threads) + : POSTREFINE_CERES_THREADS; + o.logging_type = ceres::LoggingType::SILENT; ceres::Solver::Summary sum; ceres::Solve(o, &p, &sum); if (distance_corr && !fix_distance && sum.IsSolutionUsable()) { std::fill(distance_corr, distance_corr + 4, std::numeric_limits::quiet_NaN());