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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK
This commit is contained in:
1 parent
9705845de7
commit
491e950e12
5 files changed
+28
-17
No files matched your search
@@ -23,7 +23,7 @@ static cpu_fft::Shape MapShape(int nu, int nv, int nw) {
|
||||
return {static_cast<size_t>(nw), static_cast<size_t>(nv), static_cast<size_t>(nu)};
|
||||
}
|
||||
|
||||
gemmi::FPhiGrid<float> MapToFPhi(const gemmi::Grid<float> &map) {
|
||||
gemmi::FPhiGrid<float> MapToFPhi(const gemmi::Grid<float> &map, size_t nthreads) {
|
||||
if (map.axis_order == gemmi::AxisOrder::ZYX)
|
||||
gemmi::fail("MapToFPhi(): ZYX order is not supported");
|
||||
gemmi::FPhiGrid<float> hkl;
|
||||
@@ -34,7 +34,7 @@ gemmi::FPhiGrid<float> MapToFPhi(const gemmi::Grid<float> &map) {
|
||||
hkl.set_size_without_checking(map.nu, map.nv, map.nw / 2 + 1);
|
||||
const float norm = static_cast<float>(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<float> MapToFPhi(const gemmi::Grid<float> &map) {
|
||||
return hkl;
|
||||
}
|
||||
|
||||
void MapFromFPhiExecute(int nu, int nv, int nw, std::complex<float> *in, float *out) {
|
||||
cpu_fft::C2R(MapShape(nu, nv, nw), kUVW, in, out);
|
||||
void MapFromFPhiExecute(int nu, int nv, int nw, std::complex<float> *in, float *out, size_t nthreads) {
|
||||
cpu_fft::C2R(MapShape(nu, nv, nw), kUVW, in, out, nthreads);
|
||||
}
|
||||
|
||||
gemmi::FPhiGrid<float> MapToFPhiInParallel(const gemmi::Grid<float> &map, int reach, size_t nthreads) {
|
||||
|
||||
@@ -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<float> MapToFPhi(const gemmi::Grid<float> &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<float> MapToFPhi(const gemmi::Grid<float> &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<float> MapFromFPhi(const gemmi::FPhiGrid<float> &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<float> *in, float *out);
|
||||
void MapFromFPhiExecute(int nu, int nv, int nw, std::complex<float> *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
|
||||
|
||||
@@ -172,6 +172,12 @@ struct Context {
|
||||
std::shared_ptr<ModelScaleGPUPool> 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<TrialOutcome> RunTrials(const std::vector<TrialSlot> &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<size_t>(1, settings.nthreads / running);
|
||||
trial_slots[s].ctx->SetThreads(std::max<size_t>(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<Me
|
||||
for (std::thread &t : slot_builders)
|
||||
t.join();
|
||||
const size_t main_threads = ctx.nthreads;
|
||||
ctx.nthreads = slot_threads;
|
||||
ctx.SetThreads(slot_threads);
|
||||
std::vector<TrialSlot> trial_slots{{&ctx, &topo}};
|
||||
for (const std::unique_ptr<OwnSlot> &o : own)
|
||||
if (o)
|
||||
@@ -3124,7 +3130,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
|
||||
}
|
||||
// the maps and the audit of where the trajectory ended, on all of the threads (the trials left the
|
||||
// main slot its share of them)
|
||||
ctx.nthreads = main_threads;
|
||||
ctx.SetThreads(main_threads);
|
||||
ctx.SetPositions(state.pos);
|
||||
ctx.SetB(state.b);
|
||||
PlaceRiders(ctx, topo);
|
||||
@@ -3366,7 +3372,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
|
||||
std::none_of(state.aniso.begin(), state.aniso.end(), [](const AtomAniso &a) { return a.on; })) {
|
||||
const RefinementStage field_stage("side_chain_field");
|
||||
const auto field_start = std::chrono::steady_clock::now();
|
||||
ctx.nthreads = main_threads;
|
||||
ctx.SetThreads(main_threads);
|
||||
ctx.SetPositions(state.pos);
|
||||
ctx.SetB(state.b);
|
||||
ctx.SetAniso(state.aniso);
|
||||
@@ -3444,7 +3450,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
|
||||
const SideChainFieldResult field = OptimiseSideChainField(ctx.st, element_of, topo.refinable, ncs_group,
|
||||
state.trial.weight, fr.Size(), *evaluator);
|
||||
evaluator.reset();
|
||||
ctx.nthreads = slot_threads;
|
||||
ctx.SetThreads(slot_threads);
|
||||
result.side_chain_field_candidates = field.candidates;
|
||||
auto chis = [](const std::vector<double> &v) {
|
||||
std::string s;
|
||||
@@ -3828,7 +3834,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
|
||||
ctx.evaluations -= ahead_evaluations;
|
||||
if (ahead_evaluations > 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);
|
||||
|
||||
@@ -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<std::complex<float>> &f) override {
|
||||
std::vector<std::complex<double>> fd;
|
||||
@@ -125,7 +126,7 @@ public:
|
||||
void Fmask(const gemmi::Model &model, std::vector<std::complex<float>> &f) override {
|
||||
gemmi::Grid<float> mask = grid_;
|
||||
PutMaskOnGrid(mask, model, orbit_leaders_, nthreads_);
|
||||
const gemmi::FPhiGrid<float> fm = MapToFPhi(mask);
|
||||
const gemmi::FPhiGrid<float> 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<float> map = grid_;
|
||||
const float norm = static_cast<float>(cell_.volume / static_cast<double>(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<std::complex<double>> &f) const {
|
||||
gemmi::DensityCalculator<Table, float> 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<gemmi::
|
||||
mask.data.resize(mask.point_count());
|
||||
PutMaskOnGrid(mask, model, orbit_leaders_, nthreads_);
|
||||
sub.emplace("fmask.fft");
|
||||
const gemmi::FPhiGrid<float> fm = MapToFPhi(mask);
|
||||
const gemmi::FPhiGrid<float> fm = MapToFPhi(mask, nthreads_);
|
||||
sub.reset();
|
||||
f.clear();
|
||||
for (const gemmi::Miller &h : hkl)
|
||||
|
||||
@@ -121,6 +121,7 @@ public:
|
||||
void Fmask(const gemmi::Model &model, const std::vector<gemmi::Miller> &hkl,
|
||||
std::vector<std::complex<float>> &f) const;
|
||||
double Spacing() const { return spacing_; }
|
||||
void SetThreads(size_t nthreads) { nthreads_ = nthreads; }
|
||||
|
||||
private:
|
||||
gemmi::Grid<float> 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<std::array<double, 4>> *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).
|
||||
|
||||
Reference in new issue
Block a user