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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-11-16 15:24:40 +01:00
1 parent 00e9e0fcc7
commit b67f48848f
1 file changed
+5 -3
@@ -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 <typename Fn>
void Each(size_t n, size_t nthreads, Fn fn) {
ParallelChunks(static_cast<int>(n), ThreadsForWork(n, nthreads, 16384), [&](int lo, int hi) {
ParallelChunks(static_cast<int>(n), ThreadsForWork(n, nthreads, SWEEP_PER_THREAD), [&](int lo, int hi) {
for (int k = lo; k < hi; k++)
fn(static_cast<size_t>(k));
});
}
inline double Dot(const std::vector<double> &a, const std::vector<double> &b, size_t nthreads) {
const int n = static_cast<int>(a.size());
std::vector<double> part(ReductionBlocks(n), 0.0);
std::vector<double> 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;