From b67f48848fbcc4fc4581df29c9c81ab5f61de6dc Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Mon, 16 Nov 2026 15:24:40 +0100 Subject: [PATCH] Refinement minimiser: sweeps threaded only from 65536 parameters a thread Below that waking the workers costs more than the pass (the dot products' fixed blocks with them). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- image_analysis/structure_refinement/RefinementMinimiser.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/image_analysis/structure_refinement/RefinementMinimiser.h b/image_analysis/structure_refinement/RefinementMinimiser.h index b79bd1c36..6bedb58fc 100644 --- a/image_analysis/structure_refinement/RefinementMinimiser.h +++ b/image_analysis/structure_refinement/RefinementMinimiser.h @@ -14,22 +14,24 @@ // Sweeps over the parameters on `nthreads` threads: each element on its own, and dot products folded in fixed // blocks (ParallelBlocks), so that the bits do not depend on the number of threads. namespace refine_vec { +// a pass over fewer than this many parameters per thread costs less than waking the threads +constexpr size_t SWEEP_PER_THREAD = 65536; template void Each(size_t n, size_t nthreads, Fn fn) { - ParallelChunks(static_cast(n), ThreadsForWork(n, nthreads, 16384), [&](int lo, int hi) { + ParallelChunks(static_cast(n), ThreadsForWork(n, nthreads, SWEEP_PER_THREAD), [&](int lo, int hi) { for (int k = lo; k < hi; k++) fn(static_cast(k)); }); } inline double Dot(const std::vector &a, const std::vector &b, size_t nthreads) { const int n = static_cast(a.size()); - std::vector part(ReductionBlocks(n), 0.0); + std::vector part(ReductionBlocks(n, SWEEP_PER_THREAD), 0.0); ParallelBlocks(n, nthreads, [&](int block, int lo, int hi) { double s = 0; for (int k = lo; k < hi; k++) s += a[k] * b[k]; part[block] = s; - }); + }, SWEEP_PER_THREAD); double s = 0; for (double v : part) s += v;