diff --git a/image_analysis/scale_merge/RotationScaleMerge.cpp b/image_analysis/scale_merge/RotationScaleMerge.cpp index 838ba3c64..f1319ccf5 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.cpp +++ b/image_analysis/scale_merge/RotationScaleMerge.cpp @@ -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 - | / 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 rmeas(n_groups); + std::vector &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 rabsdev(n_groups), rsumI(n_groups), rwabsdev(n_groups), rwsumI(n_groups), - rsumv(n_groups), rsumv2(n_groups); - std::vector 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(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> blk_obs(nb, std::vector(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(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 diff --git a/image_analysis/scale_merge/RotationScaleMerge.h b/image_analysis/scale_merge/RotationScaleMerge.h index 70dbf3546..15f2e2725 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.h +++ b/image_analysis/scale_merge/RotationScaleMerge.h @@ -436,6 +436,19 @@ private: std::vector merge_rejected, merge_half_set; std::vector merge_obs_key; std::vector 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 merge_rmeas; + struct RmeasStaging { + std::vector absdev, sumI, wabsdev, wsumI, sumv, sumv2; + std::vector 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.