Refinement on the CPU: an arm done gives its threads to the other
Where the untwinned and the twinned refinement run side by side on half of the threads each, the one that ends first left its half idle while the other ran on. Its threads now go to the other arm's weight trials from its next wave on. Nothing in an arm depends on its thread count. 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
91ebb7626f
commit
7276a1cf3b
2 files changed
+22
-3
No files matched your search
@@ -2443,9 +2443,11 @@ std::vector<TrialOutcome> RunTrials(const std::vector<TrialSlot> &trial_slots,
|
||||
// every thread is dealt out, and a slot that has no trial left gives its threads to those still running.
|
||||
const size_t running = std::min(trial_slots.size(), trials.size());
|
||||
const bool on_cpu = !trial_slots[0].ctx->engine->OnGPU();
|
||||
const size_t nthreads =
|
||||
settings.nthreads + (settings.spare_arm_threads != nullptr ? settings.spare_arm_threads->load() : 0);
|
||||
std::atomic<int> spare_threads{0};
|
||||
for (size_t s = 0; s < running; s++) {
|
||||
const size_t share = settings.nthreads / running + (on_cpu && s < settings.nthreads % running ? 1 : 0);
|
||||
const size_t share = nthreads / running + (on_cpu && s < nthreads % running ? 1 : 0);
|
||||
trial_slots[s].ctx->SetThreads(std::max<size_t>(1, share));
|
||||
if (on_cpu)
|
||||
trial_slots[s].ctx->spare_threads = &spare_threads;
|
||||
@@ -4386,7 +4388,8 @@ double FinalRWork(const RefinementResult &r) {
|
||||
// standard errors, each added domain at a fraction of TWIN_DOMAIN_MIN at least.
|
||||
void DecideTwin(const gemmi::Structure &placed, const std::vector<MergedReflection> &merged,
|
||||
const gemmi::UnitCell &cell, const gemmi::SpaceGroup &sg, double wavelength_A, Logger &logger,
|
||||
const RefinementSettings &settings, RefinementResult &result, const std::shared_future<void> &untwinned) {
|
||||
const RefinementSettings &settings, RefinementResult &result, const std::shared_future<void> &untwinned,
|
||||
const std::function<void()> &arm_done) {
|
||||
const RefinementStage stage("twin");
|
||||
const std::vector<gemmi::Op> laws = RefinementTwinLaws(cell, sg);
|
||||
const std::vector<gemmi::Op> domains = RefinementTwinDomains(cell, sg);
|
||||
@@ -4449,6 +4452,7 @@ void DecideTwin(const gemmi::Structure &placed, const std::vector<MergedReflecti
|
||||
}
|
||||
}
|
||||
RefinementResult twinned = RefineOnce(placed, merged, cell, sg, wavelength_A, logger, settings, {law});
|
||||
arm_done();
|
||||
if (!take_untwinned())
|
||||
return;
|
||||
result.twin_split_pairs = 0;
|
||||
@@ -4726,10 +4730,18 @@ RefinementResult RefineModel(const gemmi::Structure &placed, const std::vector<M
|
||||
const bool halves = on_cpu && settings.population <= 1 && settings.nthreads > 1 &&
|
||||
!RefinementTwinLaws(cell, sg).empty();
|
||||
RefinementSettings untwinned_settings = settings, twin_settings = settings;
|
||||
// and each arm, done, gives its threads to the other
|
||||
std::atomic<size_t> spare_for_untwinned{0}, spare_for_twin{0};
|
||||
if (halves) {
|
||||
twin_settings.nthreads = settings.nthreads / 2;
|
||||
untwinned_settings.nthreads = settings.nthreads - twin_settings.nthreads;
|
||||
untwinned_settings.spare_arm_threads = &spare_for_untwinned;
|
||||
twin_settings.spare_arm_threads = &spare_for_twin;
|
||||
}
|
||||
auto twin_done = [&]() {
|
||||
if (halves)
|
||||
spare_for_untwinned = twin_settings.nthreads;
|
||||
};
|
||||
Logger twin_logger = Logger::Buffered();
|
||||
CardHeld held;
|
||||
std::future<size_t> held_bytes = held.bytes.get_future();
|
||||
@@ -4754,10 +4766,12 @@ RefinementResult RefineModel(const gemmi::Structure &placed, const std::vector<M
|
||||
untwinned.wait();
|
||||
// A failure here (the card's memory, shared with the untwinned arm) leaves the untwinned result standing.
|
||||
try {
|
||||
DecideTwin(placed, data, cell, sg, wavelength_A, twin_logger, twin_settings, result, untwinned);
|
||||
DecideTwin(placed, data, cell, sg, wavelength_A, twin_logger, twin_settings, result, untwinned,
|
||||
twin_done);
|
||||
} catch (const std::exception &e) {
|
||||
twin_logger.Warning("Refinement: twin hypothesis not tested - {}", e.what());
|
||||
}
|
||||
twin_done(); // where it ended before its refinement
|
||||
});
|
||||
try {
|
||||
result = RefineOnce(placed, data, cell, sg, wavelength_A, logger, untwinned_settings, {}, 0, &held);
|
||||
@@ -4769,6 +4783,8 @@ RefinementResult RefineModel(const gemmi::Structure &placed, const std::vector<M
|
||||
throw;
|
||||
}
|
||||
held.Say(0); // where it stopped before placing its slots
|
||||
if (halves)
|
||||
spare_for_twin = untwinned_settings.nthreads;
|
||||
untwinned_done.set_value();
|
||||
twin_arm.get();
|
||||
twin_logger.ReplayInto(logger);
|
||||
|
||||
@@ -109,6 +109,9 @@ struct RefinementSettings {
|
||||
// iterations and before every weight trial. The trials that ran are chosen among as usual, those that did not
|
||||
// are left out (RefinementResult::cancelled); cut before the first trial, the model is the placement's.
|
||||
const std::atomic<bool> *cancel = nullptr;
|
||||
// Where two refinements run side by side on the CPU (RefineModel's arms): the threads the other one has finished
|
||||
// with, which this one's weight trials take up from then on (the result does not depend on it). Null: none.
|
||||
const std::atomic<size_t> *spare_arm_threads = nullptr;
|
||||
// The user's restraint dictionary, over the embedded one (BuildRefinementTopology)
|
||||
RestraintDictionary dictionary;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user