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());