Refinement: the engine polls for its evaluations and the pair count comes back pinned; the slots beyond one per card go to the cards the ladder's long trials leave free

The GPU engine's waits on its stream (every evaluation, the refits' transforms) and the model density's wait for
the (brick, atom) pair count were blocking event and stream synchronisations; where a weight trial has a card to
itself, as on a box with a card per trial, the wake-up of the sleeping thread is on the critical path of every
evaluation. Both now poll, and the pair count and overflow flag come back into pinned memory instead of a
pageable int (a pageable copy back is staged and blocks the host for the whole stream).

OwnSlotDevices places the slots beyond one per card on the cards the ladder's long trials (those refining B)
leave free, so that on four cards the three B rungs have a card each and the three B-held rungs share the fourth,
instead of two cards each carrying a B rung beside a B-held one. The outcome does not depend on the placement.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu
This commit is contained in:
leonarski_fandClaude Fable 5.1 committed 2026-10-26 22:41:43 +01:00
1 parent 68e567bd02
commit 3189f71e8f
4 files changed
+32 -10

No files matched your search

@@ -1020,19 +1020,25 @@ std::unique_ptr<OwnSlot> MakeOwnSlot(const gemmi::Structure &before_topology, co
}
// The devices of the slots beside the main one, `wanted` of them at most: the GPUs in turn from the main
// engine's, each taking engines while they fit in its budget. -1 for each one on the CPU, where the main one and
// those beside it are no more than the threads.
// engine's, each taking engines while they fit in its budget. The slots beyond one per card go to the cards the
// ladder's long trials (those refining B) leave free: RunTrials gives the long trials to the first slots, one
// card each, so a short trial then shares a card with a short one, not with a long one. -1 for each one on the
// CPU, where the main one and those beside it are no more than the threads.
std::vector<int> OwnSlotDevices(const Context &main, int wanted, const RefinementSettings &settings) {
#ifdef JFJOCH_USE_CUDA
if (main.engine->OnGPU()) {
const int n = get_gpu_count();
const int longest = static_cast<int>(settings.weight_ladder.size()) *
static_cast<int>(std::count_if(settings.adp_ladder.begin(), settings.adp_ladder.end(),
[](double adp) { return adp > 0; }));
const int free = n - longest; // cards without a long trial, after the first slots' cards
// a slot's engine and its scale pool, the robust fit's buffers included
const size_t slot_bytes = main.engine_bytes + ModelScaleGPUPool::DeviceBytes(main.refl.Size(), 1, true);
std::vector<size_t> used(n, 0);
used[main.device] = slot_bytes;
std::vector<int> devices;
for (int k = 1; k <= wanted; k++) {
const int d = (main.device + k) % n;
const int d = (k < n || free <= 0) ? (main.device + k) % n : (main.device + longest + (k - n) % free) % n;
const size_t usable = get_gpu_total_memory(d) / 5 * 4;
const size_t budget = usable > settings.gpu_resident_bytes ? usable - settings.gpu_resident_bytes : 0;
if (used[d] + slot_bytes <= budget) {
@@ -1149,7 +1155,7 @@ std::vector<TrialOutcome> RunTrials(const std::vector<TrialSlot> &trial_slots,
for (size_t s = 0; s < running; s++)
trial_slots[s].ctx->nthreads = std::max<size_t>(1, settings.nthreads / running);
// The trials that refine B are the longest: they go to the slots first, so that two of them share a slot (or a
// card) only where the slots are fewer than they are.
// card) only where the slots are fewer than they are; the first slots are on cards of their own (OwnSlotDevices).
std::vector<size_t> order(trials.size());
std::iota(order.begin(), order.end(), 0);
std::stable_sort(order.begin(), order.end(),