Rigid body GPU: a zone's observations and composition kept per pool

The null's replicates are all fitted to the same reflections, so the zone
with its composition table (in the device's half-u layout) and the scale's
points is built once per validation and zone and shared; each fit only
uploads it. 8t7r-sized fit 0.45 -> 0.39 s, 6oel-sized 0.27 -> 0.20 s.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D1G8gJVAy6gp1K5Dz3NE5C
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-09-28 19:23:14 +02:00
1 parent c8fc2a7067
commit 11417642a7
4 files changed
+92 -67

No files matched your search

+65 -26
View File
@@ -107,20 +107,27 @@ RigidBodyGPUZone ModelZone(const gemmi::Model &model, const gemmi::UnitCell &cel
}
// The zone's observations and the composition of Fcalc at them.
void AddObservations(RigidBodyGPUZone &zone, const gemmi::UnitCell &cell, const SymmetryComposition *composition,
const gemmi::AsuData<gemmi::ValueSigma<float>> *fobs) {
zone.ops = composition->Ops();
for (size_t m = 0; m < composition->Hkl().size(); m++) {
const gemmi::Miller &h = composition->Hkl()[m];
void AddObservations(RigidBodyGPUZone &zone, const gemmi::UnitCell &cell, const SymmetryComposition &composition,
const gemmi::AsuData<gemmi::ValueSigma<float>> &fobs) {
zone.ops = composition.Ops();
const size_t rows = composition.Hkl().size();
zone.row_hkl.reserve(rows);
zone.row_scale.reserve(rows);
zone.row_stol2.reserve(rows);
for (size_t m = 0; m < rows; m++) {
const gemmi::Miller &h = composition.Hkl()[m];
zone.row_hkl.push_back({h[0], h[1], h[2]});
// prepare_asu_data()'s unblur, exp(B_blur |s|^2 / 4), as Compose() applies it
zone.row_scale.push_back(composition->Centring() * std::exp(zone.blur * 0.25 * composition->InvD2()[m]));
zone.row_scale.push_back(composition.Centring() * std::exp(zone.blur * 0.25 * composition.InvD2()[m]));
zone.row_stol2.push_back(cell.calculate_stol_sq(h));
}
for (const SymmetryComposition::Term &t : composition->Terms()) {
zone.terms.reserve(composition.Terms().size());
for (const SymmetryComposition::Term &t : composition.Terms()) {
RigidBodyGPUTerm g{};
for (int k = 0; k < 3; k++)
g.k[k] = t.k[k];
// The half-u transform holds h >= 0, and F1(-k) = conj F1(k) for a real map.
g.conj = t.k[0] < 0;
const int ku = g.conj ? -t.k[0] : t.k[0], kv = g.conj ? -t.k[1] : t.k[1], kw = g.conj ? -t.k[2] : t.k[2];
g.index = ku + (zone.nu / 2 + 1) * (gemmi::modulo(kv, zone.nv) + zone.nv * gemmi::modulo(kw, zone.nw));
g.phase[0] = t.phase.real();
g.phase[1] = t.phase.imag();
g.s[0] = t.s.x;
@@ -129,18 +136,34 @@ void AddObservations(RigidBodyGPUZone &zone, const gemmi::UnitCell &cell, const
zone.terms.push_back(g);
}
double sum = 0;
for (size_t i = 0; i < fobs->v.size(); i++) {
const int m = composition->Row()[i];
const gemmi::ValueSigma<float> &vs = fobs->v[i].value;
for (size_t i = 0; i < fobs.v.size(); i++) {
const int m = composition.Row()[i];
const gemmi::ValueSigma<float> &vs = fobs.v[i].value;
zone.obs_row.push_back(m);
zone.obs_fobs.push_back(vs.value);
zone.obs_sigma.push_back(vs.sigma);
sum += vs.value;
// the points gemmi's prepare_points() takes
if (m >= 0 && !std::isnan(vs.value) && !std::isnan(vs.sigma))
if (m >= 0 && !std::isnan(vs.value) && !std::isnan(vs.sigma)) {
zone.point_obs.push_back(static_cast<int>(i));
zone.point_hkl.push_back(zone.row_hkl[m]);
zone.point_stol2.push_back(zone.row_stol2[m]);
zone.point_fobs.push_back(vs.value);
zone.point_sigma.push_back(vs.sigma);
}
}
zone.f_mean = fobs->v.empty() ? 1.0 : sum / static_cast<double>(fobs->v.size());
zone.f_mean = fobs.v.empty() ? 1.0 : sum / static_cast<double>(fobs.v.size());
}
bool SameObservations(const gemmi::AsuData<gemmi::ValueSigma<float>> &a,
const gemmi::AsuData<gemmi::ValueSigma<float>> &b) {
if (a.v.size() != b.v.size())
return false;
for (size_t i = 0; i < a.v.size(); i++)
if (a.v[i].hkl != b.v[i].hkl || !(a.v[i].value.value == b.v[i].value.value) ||
!(a.v[i].value.sigma == b.v[i].value.sigma))
return false;
return true;
}
} // namespace
@@ -204,6 +227,26 @@ std::unique_ptr<RigidBodyGPUPool> RigidBodyGPUPool::Create(const gemmi::Model &m
RigidBodyGPUPool::~RigidBodyGPUPool() = default;
std::shared_ptr<const RigidBodyGPUZone> RigidBodyGPUPool::ObservedZone(
const gemmi::Model &model, const gemmi::UnitCell &cell, const gemmi::SpaceGroup &sg,
const gemmi::AsuData<gemmi::ValueSigma<float>> &fobs, double d_min) {
const RigidBodyGPUZone &model_zone = Zone(model, cell, sg, d_min);
std::lock_guard lock(observed_m_);
for (const ObservedEntry &o : observed_)
if (o.d_min == d_min && SameObservations(o.fobs, fobs))
return o.zone;
const gemmi::Grid<float> grid = RigidBodyZoneGrid(cell, sg, d_min);
std::vector<gemmi::Miller> hkl;
hkl.reserve(fobs.v.size());
for (const auto &hv : fobs.v)
hkl.push_back(hv.hkl);
const SymmetryComposition composition(grid, d_min, hkl);
auto zone = std::make_shared<RigidBodyGPUZone>(model_zone);
AddObservations(*zone, cell, composition, fobs);
observed_.push_back({d_min, fobs, zone});
return zone;
}
const RigidBodyGPUZone &RigidBodyGPUPool::Zone(const gemmi::Model &model, const gemmi::UnitCell &cell,
const gemmi::SpaceGroup &sg, double d_min) {
std::lock_guard lock(zones_m_);
@@ -264,18 +307,11 @@ void RigidBodyTargetGPU::Placement(const double q[6], double rotation[9], double
}
void RigidBodyTargetGPU::SetZone(const gemmi::AsuData<gemmi::ValueSigma<float>> &fobs, double d_min) {
fobs_ = fobs;
d_min_ = d_min;
solvent_fitted_ = false;
host_scale_ = false;
have_point_ = false;
const gemmi::Grid<float> grid = RigidBodyZoneGrid(cell_, sg_, d_min);
std::vector<gemmi::Miller> hkl;
for (const auto &hv : fobs_.v)
hkl.push_back(hv.hkl);
const SymmetryComposition composition(grid, d_min, hkl);
zone_ = std::make_unique<RigidBodyGPUZone>(pool_.Zone(model_, cell_, sg_, d_min));
AddObservations(*zone_, cell_, &composition, &fobs_);
zone_ = pool_.ObservedZone(model_, cell_, sg_, fobs, d_min);
try {
engine_.SetZone(*zone_);
} catch (const JFJochException &e) {
@@ -292,10 +328,9 @@ void RigidBodyTargetGPU::HostScale() {
gemmi::Scaling<float> scaling(cell_, &sg_);
scaling.use_solvent = true;
for (size_t p = 0; p < zone_->point_obs.size(); p++) {
const auto &o = fobs_.v[zone_->point_obs[p]];
const int m = zone_->obs_row[zone_->point_obs[p]];
scaling.points.push_back({o.hkl, zone_->row_stol2[m], std::complex<float>(fc[p][0], fc[p][1]),
std::complex<float>(fm[p][0], fm[p][1]), o.value.value, o.value.sigma});
const std::array<int, 3> &h = zone_->point_hkl[p];
scaling.points.push_back({{h[0], h[1], h[2]}, zone_->point_stol2[p], std::complex<float>(fc[p][0], fc[p][1]),
std::complex<float>(fm[p][0], fm[p][1]), zone_->point_fobs[p], zone_->point_sigma[p]});
}
if (!solvent_fitted_) {
FitModelScale(scaling, {}, nthreads_);
@@ -399,3 +434,7 @@ bool RigidBodyTargetGPU::Jacobian(const double q[6], double *jacobian) {
throw RigidBodyGPUFailure(e.what());
}
}
size_t RigidBodyTargetGPU::NumObservations() const {
return zone_ ? zone_->obs_row.size() : 0;
}
+7 -37
View File
@@ -53,13 +53,6 @@ struct Placement {
double frac[9];
};
struct DeviceTerm {
int index; // of F1(k), or of F1(-k), in the half-u transform
int conj; // read as the Friedel mate
double phase[2];
double s[3];
};
// The scale a residual or a Jacobian row is taken at.
struct Scale {
double k_overall;
@@ -275,14 +268,14 @@ __global__ void gather_kernel(const RigidBodyGPUAtom *atoms, const float4 *pos,
// F at the zone's rows composed from the transform of one copy (SymmetryComposition::Compose), with
// dF/dt where `df_dt` is not null. `f1` is cuFFT's forward transform, which carries exp(-2 pi i h.x):
// conjugated and scaled by V/N it is gemmi's F1.
__global__ void compose_kernel(const float2 *f1, float norm, const DeviceTerm *terms, const double *row_scale,
__global__ void compose_kernel(const float2 *f1, float norm, const RigidBodyGPUTerm *terms, const double *row_scale,
int rows, int ops, double2 *f, double2 *df_dt) {
const int m = blockIdx.x * blockDim.x + threadIdx.x;
if (m >= rows)
return;
double sr = 0, si = 0, d[3][2] = {};
for (int o = 0; o < ops; o++) {
const DeviceTerm t = terms[static_cast<size_t>(m) * ops + o];
const RigidBodyGPUTerm t = terms[static_cast<size_t>(m) * ops + o];
const float2 out = f1[t.index];
double vr = out.x * norm, vi = -out.y * norm; // F1 at the index read
if (t.conj)
@@ -516,7 +509,7 @@ struct RigidBodyGPUEngineImpl {
CudaDevicePtr<char> cub_temp;
size_t cub_bytes = 0;
CudaDevicePtr<DeviceTerm> terms;
CudaDevicePtr<RigidBodyGPUTerm> terms;
CudaDevicePtr<int> row_hkl;
CudaDevicePtr<double> row_scale, row_stol2;
CudaDevicePtr<double2> f, df_dt, shifted;
@@ -570,7 +563,7 @@ struct RigidBodyGPUEngineImpl {
cub_bytes = CubBytes(na, np);
cub_temp = CudaDevicePtr<char>(std::max<size_t>(cub_bytes, 1), sync);
const size_t nr = std::max<size_t>(cap.rows, 1), no = std::max<size_t>(cap.observations, 1);
terms = CudaDevicePtr<DeviceTerm>(std::max<size_t>(cap.terms, 1), sync);
terms = CudaDevicePtr<RigidBodyGPUTerm>(std::max<size_t>(cap.terms, 1), sync);
row_hkl = CudaDevicePtr<int>(3 * nr, sync);
row_scale = CudaDevicePtr<double>(nr, sync);
row_stol2 = CudaDevicePtr<double>(nr, sync);
@@ -746,7 +739,7 @@ size_t RigidBodyGPUEngine::DeviceBytes(const RigidBodyGPUCapacity &c) {
sizeof(int) + sizeof(float) + 2 * sizeof(int));
bytes += 3 * c.grid_points * sizeof(float) + 3 * c.complex_points * sizeof(float2) + c.fft_work_bytes;
bytes += 4 * c.pairs * sizeof(int) + 2 * c.bricks * sizeof(int);
bytes += c.terms * sizeof(DeviceTerm);
bytes += c.terms * sizeof(RigidBodyGPUTerm);
bytes += nr * (3 * sizeof(int) + 2 * sizeof(double) + 7 * sizeof(double2) + sizeof(float2));
bytes += no * (2 * sizeof(int) + sizeof(float) + (1 + 6 + MAX_SCALE_PARAMS) * sizeof(double) + 2 * sizeof(float2));
bytes += CubBytes(na, c.pairs + 1);
@@ -817,19 +810,6 @@ void RigidBodyGPUEngine::SetZone(const RigidBodyGPUZone &zone) {
radius[zone.mask_atom[j]] = zone.mask_radius[j];
}
std::vector<DeviceTerm> terms(zone.terms.size());
for (size_t i = 0; i < zone.terms.size(); i++) {
const RigidBodyGPUTerm &t = zone.terms[i];
DeviceTerm &d = terms[i];
d.conj = t.k[0] < 0; // the half-u transform holds h >= 0, and F1(-k) = conj F1(k) for a real map
const int ku = d.conj ? -t.k[0] : t.k[0], kv = d.conj ? -t.k[1] : t.k[1], kw = d.conj ? -t.k[2] : t.k[2];
d.index = ku + (zone.nu / 2 + 1) * (((kv % zone.nv) + zone.nv) % zone.nv +
zone.nv * (((kw % zone.nw) + zone.nw) % zone.nw));
d.phase[0] = t.phase[0];
d.phase[1] = t.phase[1];
std::copy(t.s, t.s + 3, d.s);
}
auto up = [&](void *dst, const void *src, size_t bytes) {
if (bytes > 0)
cuda_err(cudaMemcpyAsync(dst, src, bytes, cudaMemcpyHostToDevice, e.stream));
@@ -838,23 +818,13 @@ void RigidBodyGPUEngine::SetZone(const RigidBodyGPUZone &zone) {
up(e.box, box.data(), box.size() * sizeof(int));
up(e.mask_slot, slot.data(), slot.size() * sizeof(int));
up(e.mask_radius, radius.data(), radius.size() * sizeof(float));
up(e.terms, terms.data(), terms.size() * sizeof(DeviceTerm));
up(e.terms, zone.terms.data(), zone.terms.size() * sizeof(RigidBodyGPUTerm));
up(e.row_hkl, zone.row_hkl.data(), zone.row_hkl.size() * 3 * sizeof(int));
up(e.row_scale, zone.row_scale.data(), zone.row_scale.size() * sizeof(double));
up(e.row_stol2, zone.row_stol2.data(), zone.row_stol2.size() * sizeof(double));
up(e.obs_row, zone.obs_row.data(), zone.obs_row.size() * sizeof(int));
up(e.obs_fobs, zone.obs_fobs.data(), zone.obs_fobs.size() * sizeof(float));
up(e.point_obs, zone.point_obs.data(), zone.point_obs.size() * sizeof(int));
std::vector<std::array<int, 3>> point_hkl;
std::vector<double> point_stol2;
std::vector<float> point_fobs, point_sigma;
for (int i : zone.point_obs) {
const int m = zone.obs_row[i];
point_hkl.push_back(zone.row_hkl[m]);
point_stol2.push_back(zone.row_stol2[m]);
point_fobs.push_back(zone.obs_fobs[i]);
point_sigma.push_back(zone.obs_sigma[i]);
}
ModelMaskGrid mask_grid;
mask_grid.nu = zone.nu;
mask_grid.nv = zone.nv;
@@ -867,7 +837,7 @@ void RigidBodyGPUEngine::SetZone(const RigidBodyGPUZone &zone) {
std::copy(zone.images[i].begin() + 9, zone.images[i].end(), images[i].tran);
}
e.mask->SetGrid(mask_grid, images);
e.scale->SetPoints(point_hkl, point_stol2, point_fobs, point_sigma, zone.constraints, zone.frac);
e.scale->SetPoints(zone.point_hkl, zone.point_stol2, zone.point_fobs, zone.point_sigma, zone.constraints, zone.frac);
e.Plan(1);
e.Plan(3);
cuda_err(cudaStreamSynchronize(e.stream));
+15 -3
View File
@@ -55,6 +55,12 @@ public:
// The zone to d_min without its observations, for this pool's model.
const RigidBodyGPUZone &Zone(const gemmi::Model &model, const gemmi::UnitCell &cell, const gemmi::SpaceGroup &sg,
double d_min);
// The zone with these observations and the composition of Fcalc at them. The null's replicates are all
// fitted to the same reflections, so they share it.
std::shared_ptr<const RigidBodyGPUZone> ObservedZone(const gemmi::Model &model, const gemmi::UnitCell &cell,
const gemmi::SpaceGroup &sg,
const gemmi::AsuData<gemmi::ValueSigma<float>> &fobs,
double d_min);
private:
RigidBodyGPUPool() = default;
@@ -64,6 +70,13 @@ private:
std::condition_variable cv_;
std::map<double, std::unique_ptr<RigidBodyGPUZone>> zones_;
std::mutex zones_m_;
struct ObservedEntry {
double d_min;
gemmi::AsuData<gemmi::ValueSigma<float>> fobs;
std::shared_ptr<const RigidBodyGPUZone> zone;
};
std::vector<ObservedEntry> observed_;
std::mutex observed_m_;
};
class RigidBodyTargetGPU : public RigidBodyTargetBase {
@@ -75,7 +88,7 @@ public:
~RigidBodyTargetGPU() override;
void SetZone(const gemmi::AsuData<gemmi::ValueSigma<float>> &fobs, double d_min) override;
size_t NumObservations() const override { return fobs_.v.size(); }
size_t NumObservations() const override;
bool Residuals(const double q[6], double *residuals) override;
bool Jacobian(const double q[6], double *jacobian) override;
@@ -92,9 +105,8 @@ private:
const gemmi::SpaceGroup &sg_;
size_t nthreads_;
gemmi::AsuData<gemmi::ValueSigma<float>> fobs_;
double d_min_ = 0;
std::unique_ptr<RigidBodyGPUZone> zone_;
std::shared_ptr<const RigidBodyGPUZone> zone_;
bool solvent_fitted_ = false;
bool host_scale_ = false; // the zone's scale is fitted on the host (HostScale)
+5 -1
View File
@@ -26,7 +26,8 @@ struct RigidBodyGPUAtom {
// One term of SymmetryComposition: F1 at k = hR, times the phase of the operator's translation.
struct RigidBodyGPUTerm {
int k[3];
int index; // of F1(k) in the half-u transform, or of F1(-k) where k is outside the stored half
int conj; // read as the Friedel mate, conj F1(-k)
double phase[2]; // exp(+2 pi i h.t), real and imaginary
double s[3]; // k as a Cartesian reciprocal vector
};
@@ -58,6 +59,9 @@ struct RigidBodyGPUZone {
double f_mean = 1;
// The scale's points: the observations gemmi's prepare_points() would take, in order.
std::vector<int> point_obs;
std::vector<std::array<int, 3>> point_hkl;
std::vector<double> point_stol2;
std::vector<float> point_fobs, point_sigma;
std::vector<std::array<double, 6>> constraints; // adp_symmetry_constraints()
};