diff --git a/image_analysis/structure_refinement/Refinement.cpp b/image_analysis/structure_refinement/Refinement.cpp index 3ba9c018d..0786337cc 100644 --- a/image_analysis/structure_refinement/Refinement.cpp +++ b/image_analysis/structure_refinement/Refinement.cpp @@ -2443,9 +2443,11 @@ std::vector RunTrials(const std::vector &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 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(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 &merged, const gemmi::UnitCell &cell, const gemmi::SpaceGroup &sg, double wavelength_A, Logger &logger, - const RefinementSettings &settings, RefinementResult &result, const std::shared_future &untwinned) { + const RefinementSettings &settings, RefinementResult &result, const std::shared_future &untwinned, + const std::function &arm_done) { const RefinementStage stage("twin"); const std::vector laws = RefinementTwinLaws(cell, sg); const std::vector domains = RefinementTwinDomains(cell, sg); @@ -4449,6 +4452,7 @@ void DecideTwin(const gemmi::Structure &placed, const std::vector 1 && !RefinementTwinLaws(cell, sg).empty(); RefinementSettings untwinned_settings = settings, twin_settings = settings; + // and each arm, done, gives its threads to the other + std::atomic 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 held_bytes = held.bytes.get_future(); @@ -4754,10 +4766,12 @@ RefinementResult RefineModel(const gemmi::Structure &placed, const std::vector *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 *spare_arm_threads = nullptr; // The user's restraint dictionary, over the embedded one (BuildRefinementTopology) RestraintDictionary dictionary; };