Refinement on the CPU without a population: the untwinned and the twinned arm side by side

The twinned arm waited for the untwinned one on the CPU ("a second arm only shares its threads"), so a crystal
with a twin law refined twice in a row, and each arm's phases with few trials left threads idle. Where nothing in
an arm depends on its threads - no population, whose members are as many as the trial slots (the CPU protocol of
RefinementSettings::lean_on_cpu) - the arms now run side by side on half of the threads each. The same refined
models bit for bit (cytC, 7ris, myob and the live cytC placement); --mode refine --no-gpu -N 16: cytC 150 -> 137 s,
the live cytC set 182 -> 146 s, 7ris 154 -> 141 s, myob 72 -> 65 s (the last three also with the commit before).

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 05:36:50 +01:00
1 parent 55972f4b67
commit 783b10ad36
1 file changed
+13 -4
@@ -4230,10 +4230,18 @@ RefinementResult RefineModel(const gemmi::Structure &placed, const std::vector<M
// one has placed its engines and slots - and where twice what it holds of its busiest card does not fit the
// budget, after it, so that the twinned arm's engines are made as they would be alone. Its log is kept and
// follows the untwinned one's, as when they ran one after the other.
// On the CPU, where nothing in an arm depends on how many threads it has once there is no population (whose
// members are as many as the trial slots), the arms run side by side, each on half of the threads.
RefinementResult result;
if (!settings.twin)
result = RefineOnce(placed, data, cell, sg, wavelength_A, logger, settings);
else {
const bool halves = on_cpu && settings.population <= 1 && settings.nthreads > 1;
RefinementSettings untwinned_settings = settings, twin_settings = settings;
if (halves) {
twin_settings.nthreads = settings.nthreads / 2;
untwinned_settings.nthreads = settings.nthreads - twin_settings.nthreads;
}
Logger twin_logger = Logger::Buffered();
CardHeld held;
std::future<size_t> held_bytes = held.bytes.get_future();
@@ -4251,19 +4259,20 @@ RefinementResult RefineModel(const gemmi::Structure &placed, const std::vector<M
#ifdef JFJOCH_USE_CUDA
const ScopedGpuPin pin(twin_card);
#endif
// Nothing held on a card: the untwinned arm is on the CPU, where a second arm only shares its threads.
// Nothing held on a card: the untwinned arm is on the CPU, where a second arm only shares its threads -
// unless each arm has half of them (halves).
const size_t held_on_card = held_bytes.get();
if (held_on_card == 0 || 2 * held_on_card > budget)
if (!halves && (held_on_card == 0 || 2 * held_on_card > budget))
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, settings, result, untwinned);
DecideTwin(placed, data, cell, sg, wavelength_A, twin_logger, twin_settings, result, untwinned);
} catch (const std::exception &e) {
twin_logger.Warning("Refinement: twin hypothesis not tested - {}", e.what());
}
});
try {
result = RefineOnce(placed, data, cell, sg, wavelength_A, logger, settings, {}, 0, &held);
result = RefineOnce(placed, data, cell, sg, wavelength_A, logger, untwinned_settings, {}, 0, &held);
} catch (...) {
result = RefinementResult(); // not ok: the twinned arm decides nothing
held.Say(0);