RotationScaleMerge: the R_meas staging as members, its group pass on all threads

The R_meas accumulators per group and the device's per-group sums were eight
group-length locals plus a 56-byte record per group, value-initialised on one
thread for every merge that reports statistics. They are now members, reset in
parallel, and the device branch's pass that assigns each group its shell and
copies its sums runs on all threads; the per-shell observation counts are
integers, gathered per block. The shells' R_meas sums are still added in group
order, as before.

p.mtz md5 unchanged on myob, cytc, thau, kdp, 8a1a and 8qaw (GPU build).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-10-08 12:37:29 +02:00
1 parent 308d1ed258
commit 3fa95eea29
2 files changed
+47 -25

No files matched your search

@@ -5647,36 +5647,45 @@ RotationScaleMerge::Result RotationScaleMerge::MergeAndStats(int n_groups, bool
// n_eff = (sum v)^2 / sum v^2, which is n where the sigmas are equal, and there this is the
// ordinary R_meas = sum sqrt(n/(n-1)) sum|I_i - <I>| / sum I_i.
// The unweighted sums beside them give the ordinary r_meas; the weighted ones r_meas_weighted.
struct RmeasObs { double sum_abs_dev = 0, sum_I = 0, w_abs_dev = 0, w_I = 0, sum_v = 0, sum_v2 = 0;
int n = 0, shell = -1; };
std::vector<RmeasObs> rmeas(n_groups);
std::vector<RmeasObs> &rmeas = merge_rmeas;
rmeas.resize(n_groups);
ParallelChunks(n_groups, ThreadsForWork(n_groups, nthreads), [&](int lo, int hi) {
std::fill(rmeas.begin() + lo, rmeas.begin() + hi, RmeasObs{});
});
bool did_gpu_rmeas = false;
#ifdef JFJOCH_USE_CUDA
if (use_gpu_merge) {
// Per-group R_meas + usable count on the GPU; the shell is assigned per group (its fulls share d).
std::vector<double> rabsdev(n_groups), rsumI(n_groups), rwabsdev(n_groups), rwsumI(n_groups),
rsumv(n_groups), rsumv2(n_groups);
std::vector<int32_t> rn(n_groups), rnusable(n_groups);
gpu_->MergeRmeas(merged_I.data(), rabsdev.data(), rsumI.data(), rwabsdev.data(), rwsumI.data(),
rsumv.data(), rsumv2.data(), rn.data(), rnusable.data());
for (int g = 0; g < n_groups; ++g) {
if (rnusable[g] == 0 || acc[g].d < d_floor) continue;
const auto shell = shells.GetShell(acc[g].d);
if (!shell || *shell < 0 || *shell >= n_shells) continue;
// Count the MERGED population, not the R_meas one. The R_meas re-walk deliberately
// ignores the ice flag on a search pass, so its count includes observations that never
// entered `unique` - which inflates the reported multiplicity of whatever shell they
// land in. acc[g].nh is what actually went into this group's mean, and it is zero for a
// group the merge dropped entirely. (acc[g].d is NaN for such a group, so GetShell above
// already declines it; this is the same statement made where it counts.)
sa[*shell].total_obs += static_cast<int>(acc[g].nh[0] + acc[g].nh[1]);
if (std::isfinite(merged_I[g]) && rn[g] > 0) {
auto &r = rmeas[g];
r.sum_abs_dev = rabsdev[g]; r.sum_I = rsumI[g]; r.w_abs_dev = rwabsdev[g]; r.w_I = rwsumI[g];
r.sum_v = rsumv[g]; r.sum_v2 = rsumv2[g];
r.n = rn[g]; r.shell = *shell;
RmeasStaging &rs = merge_rmeas_staging;
rs.Resize(n_groups);
gpu_->MergeRmeas(merged_I.data(), rs.absdev.data(), rs.sumI.data(), rs.wabsdev.data(), rs.wsumI.data(),
rs.sumv.data(), rs.sumv2.data(), rs.n.data(), rs.nusable.data());
// Each group on its own, on all threads; the observation counts are integers, so the blocks
// may add theirs up in any split.
const int nb = ReductionBlocks(n_groups, 4096);
std::vector<std::vector<int>> blk_obs(nb, std::vector<int>(n_shells, 0));
ParallelBlocks(n_groups, nthreads, [&](int blk, int g0, int g1) {
for (int g = g0; g < g1; ++g) {
if (rs.nusable[g] == 0 || acc[g].d < d_floor) continue;
const auto shell = shells.GetShell(acc[g].d);
if (!shell || *shell < 0 || *shell >= n_shells) continue;
// Count the MERGED population, not the R_meas one. The R_meas re-walk deliberately
// ignores the ice flag on a search pass, so its count includes observations that never
// entered `unique` - which inflates the reported multiplicity of whatever shell they
// land in. acc[g].nh is what actually went into this group's mean, and it is zero for a
// group the merge dropped entirely. (acc[g].d is NaN for such a group, so GetShell above
// already declines it; this is the same statement made where it counts.)
blk_obs[blk][*shell] += static_cast<int>(acc[g].nh[0] + acc[g].nh[1]);
if (std::isfinite(merged_I[g]) && rs.n[g] > 0) {
auto &r = rmeas[g];
r.sum_abs_dev = rs.absdev[g]; r.sum_I = rs.sumI[g]; r.w_abs_dev = rs.wabsdev[g];
r.w_I = rs.wsumI[g]; r.sum_v = rs.sumv[g]; r.sum_v2 = rs.sumv2[g];
r.n = rs.n[g]; r.shell = *shell;
}
}
}
});
for (const auto &c : blk_obs)
for (int sh = 0; sh < n_shells; ++sh) sa[sh].total_obs += c[sh];
did_gpu_rmeas = true;
}
#endif
@@ -436,6 +436,19 @@ private:
std::vector<uint8_t> merge_rejected, merge_half_set;
std::vector<uint64_t> merge_obs_key;
std::vector<WilsonObservation> merge_wobs;
// The R_meas accumulators per group (see MergeAndStats' statistics), and the device's per-group sums.
struct RmeasObs { double sum_abs_dev = 0, sum_I = 0, w_abs_dev = 0, w_I = 0, sum_v = 0, sum_v2 = 0;
int n = 0, shell = -1; };
std::vector<RmeasObs> merge_rmeas;
struct RmeasStaging {
std::vector<double> absdev, sumI, wabsdev, wsumI, sumv, sumv2;
std::vector<int32_t> n, nusable;
void Resize(int g) {
absdev.resize(g); sumI.resize(g); wabsdev.resize(g); wsumI.resize(g); sumv.resize(g); sumv2.resize(g);
n.resize(g); nusable.resize(g);
}
};
RmeasStaging merge_rmeas_staging;
// Per-group scatter for the strong-reflection ISa asymptote (see MergeAndStats). A member for the
// same reason: 32 bytes a group, once per merge.