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