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 <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 00:58:51 +01:00
1 parent 491e950e12
commit b36fcd0892
1 file changed
+7 -3
@@ -915,9 +915,13 @@ void AtomGradientsFromMap(const gemmi::Grid<float> &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<int>(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<int>(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);