Side-chain field: the reflections split across every card
The field's search is iterated conditional modes over the residues, one after another, and each step evaluates -ln L over every working reflection for each combination of states. On a large model it ran on the main engine's card alone while the others idled: a 242k-atom model with 45 NCS copies and 663k reflections spent 1743 s of a 2392 s refinement in it on one of four T4s. -ln L is a sum over the reflections, so MakeSideChainFieldEvaluatorGPUs gives each card a contiguous part of them and adds the parts' sums in card order. The search, its order and its decisions are unchanged; only the last bits of each sum move, where the parts meet. One card keeps the evaluator as it was. On that model, 4 x T4: the field 1743 -> 456 s, the refinement 2392 -> 1119 s, the refined model bit-identical (same R-free, same md5). Two small models refine bit-identically, in the same time. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UqXYdJz9sA2BQBEgKkuVuL
This commit is contained in:
1 parent
5a2d7e5221
commit
f07493c9bf
4 files changed
+102
-4
No files matched your search
@@ -3849,13 +3849,19 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
|
||||
}
|
||||
std::unique_ptr<SideChainFieldEvaluator> evaluator;
|
||||
#ifdef JFJOCH_USE_CUDA
|
||||
// on the card where its states fit beside the engine - decided up front on the card's total, as the
|
||||
// engines are; on the CPU otherwise, rather than failing at an allocation inside a sweep
|
||||
// on every card, each with a part of the reflections, where a part's states fit beside the engines -
|
||||
// decided up front on the card's total, as the engines are; on the CPU otherwise, rather than failing at
|
||||
// an allocation inside a sweep
|
||||
if (ctx.engine->OnGPU()) {
|
||||
const size_t ops = FieldOperators(sg).size();
|
||||
const size_t wanted = SideChainFieldDeviceBytes(fr.Size(), ops), card = get_gpu_total_memory(ctx.device);
|
||||
const int cards = get_gpu_count();
|
||||
const size_t wanted = SideChainFieldDeviceBytes(fr.Size() / cards + 1, ops),
|
||||
card = get_gpu_total_memory(ctx.device);
|
||||
std::vector<int> devices;
|
||||
for (int k = 0; k < cards; k++)
|
||||
devices.push_back((ctx.device + k) % cards);
|
||||
if (wanted < card / 2)
|
||||
evaluator = MakeSideChainFieldEvaluatorGPU(ctx.device, fr, FieldOperators(sg), elements);
|
||||
evaluator = MakeSideChainFieldEvaluatorGPUs(devices, fr, FieldOperators(sg), elements);
|
||||
else
|
||||
logger.Info("Side-chain field on the CPU: {} MiB wanted on the card of {} MiB", wanted >> 20,
|
||||
card >> 20);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <future>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
@@ -368,6 +369,80 @@ size_t SideChainFieldDeviceBytes(size_t rows, size_t n_ops) {
|
||||
// the states of one variable: within the sweep budget where the copies move alone, MAX_STATES otherwise
|
||||
return rows * (16 * n_ops + 48) + 8 * std::max(static_cast<size_t>(SWEEP_BUDGET), MAX_STATES * rows);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
// The GPU evaluator over several cards, each holding a contiguous part of the reflections. -ln L is a sum over the
|
||||
// reflections, so the cards evaluate their parts side by side and each combination's value is the sum of theirs, added
|
||||
// in card order (the last bits differ from one card's, whose sum is not cut where the parts meet).
|
||||
class EvaluatorCards : public SideChainFieldEvaluator {
|
||||
public:
|
||||
EvaluatorCards(const std::vector<int> &devices, const FieldReflections &refl,
|
||||
const std::vector<std::array<float, 12>> &ops, const std::vector<FormFactor> &elements) {
|
||||
const size_t n = devices.size(), rows = refl.Size();
|
||||
for (size_t k = 0; k <= n; k++)
|
||||
begin_.push_back(rows * k / n);
|
||||
for (size_t k = 0; k < n; k++)
|
||||
parts_.push_back(MakeSideChainFieldEvaluatorGPU(devices[k], Slice(refl, begin_[k], begin_[k + 1]), ops,
|
||||
elements));
|
||||
}
|
||||
|
||||
void SetTotal(const std::vector<std::complex<float>> &f) override {
|
||||
for (size_t k = 0; k < parts_.size(); k++)
|
||||
parts_[k]->SetTotal(std::vector<std::complex<float>>(f.begin() + begin_[k], f.begin() + begin_[k + 1]));
|
||||
}
|
||||
|
||||
std::vector<double> Evaluate(const FieldStates &states) override {
|
||||
std::vector<std::future<std::vector<double>>> on;
|
||||
for (auto &p : parts_)
|
||||
on.push_back(std::async(std::launch::async, [&p, &states] { return p->Evaluate(states); }));
|
||||
std::vector<double> nll = on[0].get();
|
||||
for (size_t k = 1; k < on.size(); k++) {
|
||||
const std::vector<double> part = on[k].get();
|
||||
for (size_t c = 0; c < nll.size(); c++)
|
||||
nll[c] += part[c];
|
||||
}
|
||||
return nll;
|
||||
}
|
||||
|
||||
void Move(const FieldStates &states, int to) override {
|
||||
std::vector<std::future<void>> on;
|
||||
for (auto &p : parts_)
|
||||
on.push_back(std::async(std::launch::async, [&p, &states, to] { p->Move(states, to); }));
|
||||
for (auto &f : on)
|
||||
f.get();
|
||||
}
|
||||
|
||||
std::vector<std::complex<float>> Contribution(const FieldStates &states, int s) override {
|
||||
std::vector<std::complex<float>> f;
|
||||
for (auto &p : parts_) {
|
||||
const std::vector<std::complex<float>> part = p->Contribution(states, s);
|
||||
f.insert(f.end(), part.begin(), part.end());
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
private:
|
||||
static FieldReflections Slice(const FieldReflections &r, size_t lo, size_t hi) {
|
||||
auto part = [lo, hi](const auto &v) { return std::decay_t<decltype(v)>(v.begin() + lo, v.begin() + hi); };
|
||||
return {part(r.hkl), part(r.stol2), part(r.fo), part(r.d), part(r.var), part(r.centric), part(r.work),
|
||||
part(r.k_total), part(r.fmask)};
|
||||
}
|
||||
|
||||
std::vector<size_t> begin_; // the first row of each card's part, and the end
|
||||
std::vector<std::unique_ptr<SideChainFieldEvaluator>> parts_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<SideChainFieldEvaluator> MakeSideChainFieldEvaluatorGPUs(const std::vector<int> &devices,
|
||||
const FieldReflections &refl,
|
||||
const std::vector<std::array<float, 12>> &ops,
|
||||
const std::vector<FormFactor> &elements) {
|
||||
if (devices.size() == 1)
|
||||
return MakeSideChainFieldEvaluatorGPU(devices[0], refl, ops, elements);
|
||||
return std::make_unique<EvaluatorCards>(devices, refl, ops, elements);
|
||||
}
|
||||
#endif
|
||||
|
||||
std::unique_ptr<SideChainFieldEvaluator> MakeSideChainFieldEvaluatorCPU(const FieldReflections &refl,
|
||||
|
||||
@@ -104,6 +104,12 @@ size_t SideChainFieldDeviceBytes(size_t rows, size_t n_ops);
|
||||
std::unique_ptr<SideChainFieldEvaluator> MakeSideChainFieldEvaluatorGPU(int device, const FieldReflections &refl,
|
||||
const std::vector<std::array<float, 12>> &ops,
|
||||
const std::vector<FormFactor> &elements);
|
||||
// On every card of `devices`, the reflections split among them in contiguous parts that the cards evaluate side by
|
||||
// side; on one card, the evaluator above.
|
||||
std::unique_ptr<SideChainFieldEvaluator> MakeSideChainFieldEvaluatorGPUs(const std::vector<int> &devices,
|
||||
const FieldReflections &refl,
|
||||
const std::vector<std::array<float, 12>> &ops,
|
||||
const std::vector<FormFactor> &elements);
|
||||
#endif
|
||||
|
||||
// A residue the field moved: its atoms (index in the model's atom list) at their new positions, or absent.
|
||||
|
||||
@@ -194,6 +194,17 @@ TEST_CASE("SideChainField_recovers_the_rotamer_the_data_hold", "[Refinement]") {
|
||||
REQUIRE(g.moves.size() == 1);
|
||||
CHECK(g.moves[0].chi_after == r.moves[0].chi_after);
|
||||
CHECK(g.energy_after == Catch::Approx(r.energy_after).epsilon(1e-4));
|
||||
// on every card, the reflections split among them: the same move, the energy summed in another order
|
||||
int n = 0;
|
||||
cudaGetDeviceCount(&n);
|
||||
std::vector<int> cards;
|
||||
for (int k = 0; k < n; k++)
|
||||
cards.push_back(k);
|
||||
auto split = MakeSideChainFieldEvaluatorGPUs(cards, fr, ops, ff);
|
||||
const SideChainFieldResult s = run(*split);
|
||||
REQUIRE(s.moves.size() == 1);
|
||||
CHECK(s.moves[0].chi_after == g.moves[0].chi_after);
|
||||
CHECK(s.energy_after == Catch::Approx(g.energy_after).epsilon(1e-9));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user