From 491e950e12d16776fd6ae18d8868de8301b53141 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sun, 15 Nov 2026 23:49:52 +0100 Subject: [PATCH] Refinement on the CPU: the engine and the mask take the slot's share of the threads The CPU engine and the bulk-solvent mask kept the thread count they were made with: the main slot's all of them, every other weight-trial slot's one (settings.nthreads / slots). RunTrials and the stages that hand the threads back set Context::nthreads, which the engine never read - so where fewer trials ran than there were slots (the trajectory's steps run 4, TLS/U hypotheses, the last trial of a set) the trials in the other slots ran on one thread each and the step waited for them; top showed the process at 100-500% for most of a run on 16 threads. Context::SetThreads() now passes the count on (RefinementEngine::SetThreads, a no-op on a GPU engine, and BulkSolventMask::SetThreads), and the engine's and the mask's FFTs are split over the same threads (MapToFPhi/MapFromFPhiExecute take nthreads; pocketfft splits lines, the result is the same). The outcome does not depend on the threads: cytC --mode refine --no-gpu -N 16 writes the same refined model and maps bit for bit, R-free 0.3400 / 0.3366 (two hypotheses) as before. Refinement time, with the gradient commit before it: 909.6 + 700.7 s -> 224.7 + 219.7 s (wall 1611 -> 445 s). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- .../structure_refinement/ModelFFT.cpp | 8 ++++---- image_analysis/structure_refinement/ModelFFT.h | 7 ++++--- .../structure_refinement/Refinement.cpp | 18 ++++++++++++------ .../structure_refinement/RefinementXray.cpp | 9 +++++---- .../structure_refinement/RefinementXray.h | 3 +++ 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/image_analysis/structure_refinement/ModelFFT.cpp b/image_analysis/structure_refinement/ModelFFT.cpp index 8d2dc41ca..66318286d 100644 --- a/image_analysis/structure_refinement/ModelFFT.cpp +++ b/image_analysis/structure_refinement/ModelFFT.cpp @@ -23,7 +23,7 @@ static cpu_fft::Shape MapShape(int nu, int nv, int nw) { return {static_cast(nw), static_cast(nv), static_cast(nu)}; } -gemmi::FPhiGrid MapToFPhi(const gemmi::Grid &map) { +gemmi::FPhiGrid MapToFPhi(const gemmi::Grid &map, size_t nthreads) { if (map.axis_order == gemmi::AxisOrder::ZYX) gemmi::fail("MapToFPhi(): ZYX order is not supported"); gemmi::FPhiGrid hkl; @@ -34,7 +34,7 @@ gemmi::FPhiGrid MapToFPhi(const gemmi::Grid &map) { hkl.set_size_without_checking(map.nu, map.nv, map.nw / 2 + 1); const float norm = static_cast(map.unit_cell.volume / map.point_count()); - cpu_fft::R2C(MapShape(map.nu, map.nv, map.nw), kUVW, map.data.data(), hkl.data.data()); + cpu_fft::R2C(MapShape(map.nu, map.nv, map.nw), kUVW, map.data.data(), hkl.data.data(), nthreads); // Conjugated, as gemmi does: the forward transform carries exp(-2 pi i h.x), and a structure // factor is the sum over exp(+2 pi i h.x). for (auto &x : hkl.data) @@ -42,8 +42,8 @@ gemmi::FPhiGrid MapToFPhi(const gemmi::Grid &map) { return hkl; } -void MapFromFPhiExecute(int nu, int nv, int nw, std::complex *in, float *out) { - cpu_fft::C2R(MapShape(nu, nv, nw), kUVW, in, out); +void MapFromFPhiExecute(int nu, int nv, int nw, std::complex *in, float *out, size_t nthreads) { + cpu_fft::C2R(MapShape(nu, nv, nw), kUVW, in, out, nthreads); } gemmi::FPhiGrid MapToFPhiInParallel(const gemmi::Grid &map, int reach, size_t nthreads) { diff --git a/image_analysis/structure_refinement/ModelFFT.h b/image_analysis/structure_refinement/ModelFFT.h index 1e44a294a..d5dae7501 100644 --- a/image_analysis/structure_refinement/ModelFFT.h +++ b/image_analysis/structure_refinement/ModelFFT.h @@ -9,8 +9,9 @@ // The structure factors of a real-space map on the model path (Fcalc from the model density, Fmask // from the bulk-solvent mask): what gemmi::transform_map_to_f_phi(map, true) returns - the // conjugated forward transform (F = sum rho exp(+2 pi i h.x)) scaled by V/N, kept for l >= 0 on an (nu, nv, nw/2+1) grid in the map's own XYZ order, -// ready for prepare_asu_data() - computed with common/CpuFFT. Safe to call from several threads at once. -gemmi::FPhiGrid MapToFPhi(const gemmi::Grid &map); +// ready for prepare_asu_data() - computed with common/CpuFFT, its lines split over `nthreads` (the same result on any +// number). Safe to call from several threads at once. +gemmi::FPhiGrid MapToFPhi(const gemmi::Grid &map, size_t nthreads = 1); // The inverse: the real-space map of an (nu, nv, nw/2+1) half-l coefficient grid in XYZ order, as // get_f_phi_on_grid() fills it - what gemmi::transform_f_phi_grid_to_map() returns (rho = 1/V sum F @@ -20,7 +21,7 @@ gemmi::Grid MapFromFPhi(const gemmi::FPhiGrid &hkl); // MapFromFPhi's transform itself, for a caller that fills its input: `in`, the (nu, nv, nw/2+1) half-l coefficients // u fastest as MapFromFPhi transforms them (conjugated, a NaN as zero), is overwritten, and `out` receives the // (nu, nv, nw) map before MapFromFPhi's 1/V. -void MapFromFPhiExecute(int nu, int nv, int nw, std::complex *in, float *out); +void MapFromFPhiExecute(int nu, int nv, int nw, std::complex *in, float *out, size_t nthreads = 1); // CropHalfL(MapToFPhi(map), reach) on `nthreads` threads, for a map of hundreds of millions of points (the // molecular transform's box), whose single-threaded transform is minutes of CPU: the r2c along w of every diff --git a/image_analysis/structure_refinement/Refinement.cpp b/image_analysis/structure_refinement/Refinement.cpp index 89e79549a..6da085ab2 100644 --- a/image_analysis/structure_refinement/Refinement.cpp +++ b/image_analysis/structure_refinement/Refinement.cpp @@ -172,6 +172,12 @@ struct Context { std::shared_ptr scale_gpu; // shared by the slots on the engine's card #endif size_t nthreads = 1; + // The threads of the stages and of the engine and the mask from now on. + void SetThreads(size_t n) { + nthreads = n; + engine->SetThreads(n); + mask->SetThreads(n); + } // the likelihood fit's: all of the run's, also in a trial's slot - the slots refit at the same time, the GPU // idles meanwhile, and the bins they queue on the shared worker pool are fitted by whichever thread is free size_t ml_threads = 1; @@ -1961,7 +1967,7 @@ std::vector RunTrials(const std::vector &trial_slots, // the threads shared among the slots that run (the outcome does not depend on how many each has) const size_t running = std::min(trial_slots.size(), trials.size()); for (size_t s = 0; s < running; s++) - trial_slots[s].ctx->nthreads = std::max(1, settings.nthreads / running); + trial_slots[s].ctx->SetThreads(std::max(1, settings.nthreads / running)); // The longest go to the slots first - TLS bodies, then a U per atom, then the B factors refined, then held - so // that two of them share a card only where the cards are fewer than they are: the slots are on the cards in turn // (OwnSlotPlaces), a card's second slot only after every card's first. @@ -2817,7 +2823,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector trial_slots{{&ctx, &topo}}; for (const std::unique_ptr &o : own) if (o) @@ -3124,7 +3130,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector &v) { std::string s; @@ -3828,7 +3834,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector 0) logger.Debug("Refinement: {} target evaluations in trials run ahead and not needed", ahead_evaluations); - ctx.nthreads = main_threads; + ctx.SetThreads(main_threads); if (state.ran) { ctx.SetPositions(state.pos); ctx.SetB(state.b); diff --git a/image_analysis/structure_refinement/RefinementXray.cpp b/image_analysis/structure_refinement/RefinementXray.cpp index 595771ede..871d53af1 100644 --- a/image_analysis/structure_refinement/RefinementXray.cpp +++ b/image_analysis/structure_refinement/RefinementXray.cpp @@ -115,6 +115,7 @@ public: void SetBlur(double blur) override { blur_ = blur; } void SetRowModel(const XrayRowModel &rows) override { rows_ = rows; } bool OnGPU() const override { return false; } + void SetThreads(size_t nthreads) override { nthreads_ = nthreads; } void Fcalc(const gemmi::Model &model, std::vector> &f) override { std::vector> fd; @@ -125,7 +126,7 @@ public: void Fmask(const gemmi::Model &model, std::vector> &f) override { gemmi::Grid mask = grid_; PutMaskOnGrid(mask, model, orbit_leaders_, nthreads_); - const gemmi::FPhiGrid fm = MapToFPhi(mask); + const gemmi::FPhiGrid fm = MapToFPhi(mask, nthreads_); f.clear(); for (const gemmi::Miller &h : refl_.RowHkl()) f.push_back(fm.get_value_by_hkl(h)); @@ -215,7 +216,7 @@ public: } } } - MapFromFPhiExecute(nu, nv, nw, in.data(), out.data()); + MapFromFPhiExecute(nu, nv, nw, in.data(), out.data(), nthreads_); gemmi::Grid map = grid_; const float norm = static_cast(cell_.volume / static_cast(map.point_count())); for (size_t i = 0; i < map.data.size(); i++) @@ -240,7 +241,7 @@ private: void Compose(const gemmi::Model &model, std::vector> &f) const { gemmi::DensityCalculator dc = Calculator(cell_, sg_, d_min_, blur_); PutModelDensityOnGrid(dc, model, {}, nthreads_, true, true); // one copy: the symmetry is composed below - composition_.Compose(MapToFPhi(dc.grid), blur_, f, nullptr, nthreads_); + composition_.Compose(MapToFPhi(dc.grid, nthreads_), blur_, f, nullptr, nthreads_); } gemmi::UnitCell cell_; @@ -557,7 +558,7 @@ void BulkSolventMask::Fmask(const gemmi::Model &model, const std::vector fm = MapToFPhi(mask); + const gemmi::FPhiGrid fm = MapToFPhi(mask, nthreads_); sub.reset(); f.clear(); for (const gemmi::Miller &h : hkl) diff --git a/image_analysis/structure_refinement/RefinementXray.h b/image_analysis/structure_refinement/RefinementXray.h index 3b8640d6e..66bf1dd44 100644 --- a/image_analysis/structure_refinement/RefinementXray.h +++ b/image_analysis/structure_refinement/RefinementXray.h @@ -121,6 +121,7 @@ public: void Fmask(const gemmi::Model &model, const std::vector &hkl, std::vector> &f) const; double Spacing() const { return spacing_; } + void SetThreads(size_t nthreads) { nthreads_ = nthreads; } private: gemmi::Grid grid_; @@ -205,6 +206,8 @@ public: // For an anisotropic atom (SetAniso) the B derivative is the one along dev + B / (8 pi^2) I, its shape held. virtual double Evaluate(const gemmi::Model &model, std::vector> *grad) = 0; virtual bool OnGPU() const = 0; + // The threads the engine's evaluations are spread over from now on (the CPU's; a GPU engine keeps its own). + virtual void SetThreads(size_t) {} // Which atoms are anisotropic and their shapes, in model order (AtomAniso), read at every Fcalc() and // Evaluate() until it is set again; null or empty: every atom isotropic (the model's own aniso is not read).