From b36fcd0892bcfe866631be5e9ce01a1e9eda0dd5 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Mon, 16 Nov 2026 00:58:51 +0100 Subject: [PATCH] Refinement CPU gradient: atoms in blocks taken as the threads come free AtomGradientsFromMap() gave each thread one contiguous range of atoms; an anisotropic atom or one of high B costs several times another, and the threads of the cheap ranges waited for the rest. The atoms now go in blocks of 16 off a shared counter (ParallelFor), each thread with scratch of its own; every atom's derivatives are the same. Microbenchmark (2658 atoms, 144^3 grid): 16 threads 20.4 -> 17.2 ms, 8 threads 21.8 -> 17.9 ms. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- image_analysis/structure_refinement/RefinementXray.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/image_analysis/structure_refinement/RefinementXray.cpp b/image_analysis/structure_refinement/RefinementXray.cpp index 871d53af1..e718b5d18 100644 --- a/image_analysis/structure_refinement/RefinementXray.cpp +++ b/image_analysis/structure_refinement/RefinementXray.cpp @@ -915,9 +915,13 @@ void AtomGradientsFromMap(const gemmi::Grid &map, const gemmi::Model &mod grad_u->assign(atoms.size(), {0, 0, 0, 0, 0, 0}); const int nu = map.nu, nv = map.nv, nw = map.nw; constexpr double FOUR_PI2 = 4 * refine_math::PI * refine_math::PI; - ParallelChunks(static_cast(atoms.size()), nthreads, [&](int lo, int hi) { - MapPoints p; - for (int i = lo; i < hi; i++) { + // in blocks of atoms taken as the threads come free: an anisotropic atom, or one of high B, costs several times + // another, and one contiguous range per thread left the threads of the cheap ranges waiting + constexpr int ATOM_BLOCK = 16; + const int n = static_cast(atoms.size()); + ParallelFor((n + ATOM_BLOCK - 1) / ATOM_BLOCK, nthreads, [&](int block) { + thread_local MapPoints p; + for (int i = block * ATOM_BLOCK; i < std::min(n, (block + 1) * ATOM_BLOCK); i++) { const gemmi::Atom &atom = *atoms[i]; if (atom.aniso.nonzero()) { AnisoGradient(map, dc, atom, blur, p, grad[i], grad_u != nullptr ? &(*grad_u)[i] : nullptr);