diff --git a/image_analysis/scale_merge/RotationScaleMerge.cpp b/image_analysis/scale_merge/RotationScaleMerge.cpp index 10c5829d6..1178fe5eb 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.cpp +++ b/image_analysis/scale_merge/RotationScaleMerge.cpp @@ -1336,12 +1336,13 @@ int RotationScaleMerge::ComputeAsuGroups(const HKLKeyGenerator &keygen) { return n_groups; } -double RotationScaleMerge::ReferenceWeight(const Obs &o, double min_partiality, float &I_corr) { - if (o.group < 0) return 0.0; - if (!(o.corr > 0.0f) || !std::isfinite(o.corr)) return 0.0; - if (o.partiality < min_partiality) return 0.0; - const float Ic = o.I * o.corr; - const float sigma_corr = o.sigma * o.corr; +double RotationScaleMerge::ReferenceWeight(int32_t group, float corr, float partiality, float I, float sigma, + double min_partiality, float &I_corr) { + if (group < 0) return 0.0; + if (!(corr > 0.0f) || !std::isfinite(corr)) return 0.0; + if (partiality < min_partiality) return 0.0; + const float Ic = I * corr; + const float sigma_corr = sigma * corr; if (!std::isfinite(Ic) || !std::isfinite(sigma_corr) || sigma_corr <= 0.0f) return 0.0; I_corr = Ic; return 1.0 / (static_cast(sigma_corr) * sigma_corr); @@ -1383,7 +1384,30 @@ void RotationScaleMerge::ReducePartialGroupMeans(int n_groups, std::vector &obs, const std::vector &fstart, +void RotationScaleMerge::ReduceScalingGroupMeans(int n_groups, const std::vector &refs, + const std::vector &corr, + std::vector &out_mean) const { + out_mean.assign(n_groups, NAN); + ParallelChunks(n_groups, ThreadsForWork(corr.size(), nthreads), [&](int glo, int ghi) { + for (int g = glo; g < ghi; ++g) { + double sw = 0.0, swI = 0.0; + const int lo = partial_group_start[g], hi = lo + partial_group_count[g]; + for (int p = lo; p < hi; ++p) { + const ScalingRef &r = refs[p]; + float I_corr = 0.0f; + const double w = ReferenceWeight(g, corr[partial_group_perm[p]], r.partiality, r.I, r.sigma, + min_partiality, I_corr); + if (w <= 0.0) continue; + sw += w; + swI += w * I_corr; + } + if (sw > 0.0) out_mean[g] = swI / sw; + } + }); +} + +template +void RotationScaleMerge::FitPerFrameG(const std::vector &obs, const std::vector &fstart, const std::vector &fcount, const std::vector &group_mean_in, bool unity, std::vector &g) { @@ -5560,6 +5584,30 @@ RotationScaleMerge::Result RotationScaleMerge::Run(bool for_search, bool full_st logger.Info("Per-frame scaling (partials): the same grouping and settings as an earlier merge, " "so its {} iterations stand", partial_loop.iterations); } else { + // The loop works on its own compact copy of the partials (ScalingObs in the header) and hands + // corr back to them when it is done. + const int n_partials = static_cast(partials.size()); + const size_t partial_threads = ThreadsForWork(partials.size(), nthreads); + std::vector scaling_obs(n_partials); + std::vector scaling_corr(n_partials); + ParallelChunks(n_partials, partial_threads, [&](int lo, int hi) { + for (int i = lo; i < hi; ++i) { + const Obs &o = partials[i]; + scaling_obs[i] = {o.frame, o.group, o.I, o.sigma, o.partiality, o.prescaling_corr, o.zeta, + o.on_ice}; + scaling_corr[i] = o.corr; + } + }); + std::vector scaling_refs(partial_group_start.empty() + ? 0 : partial_group_start.back() + partial_group_count.back()); + ParallelChunks(n_groups, partial_threads, [&](int glo, int ghi) { + for (int g = glo; g < ghi; ++g) + for (int p = partial_group_start[g]; p < partial_group_start[g] + partial_group_count[g]; ++p) { + const ScalingObs &o = scaling_obs[partial_group_perm[p]]; + scaling_refs[p] = {o.I, o.sigma, o.partiality}; + } + }); + // RunScalingLoop rescales after every iteration before anything reads corr, so the iteration's // UpdateCorr is folded into that pass over the partials: the G and the fitted frames as the // iteration left them, then the rescale on top - the same two roundings, one pass instead of two. @@ -5567,34 +5615,32 @@ RotationScaleMerge::Result RotationScaleMerge::Run(bool for_search, bool full_st std::vector frame_fitted; partial_loop = RunScalingLoop("partials", g_partial, frame_count, smooth_window, /*release_restraint=*/false, [&] { - ReducePartialGroupMeans(n_groups, partial_mean); - FitPerFrameG(partials, frame_start, frame_count, partial_mean, /*unity=*/false, g_partial); + ReduceScalingGroupMeans(n_groups, scaling_refs, scaling_corr, partial_mean); + FitPerFrameG(scaling_obs, frame_start, frame_count, partial_mean, /*unity=*/false, g_partial); g_fitted = g_partial; frame_fitted = frame_scaled_scratch; }, [&](const std::vector &apply, const std::vector &ratio) { - ParallelChunks(static_cast(partials.size()), ThreadsForWork(partials.size(), nthreads), - [&](int lo, int hi) { + ParallelChunks(n_partials, partial_threads, [&](int lo, int hi) { for (int i = lo; i < hi; ++i) { - Obs &o = partials[i]; + const ScalingObs &o = scaling_obs[i]; + float &corr = scaling_corr[i]; if (frame_fitted[o.frame]) { // UpdateCorr const double denom = static_cast(o.partiality) * g_fitted[o.frame]; if (std::isfinite(o.prescaling_corr) && std::isfinite(denom) && denom > 0.0) - o.corr = static_cast(o.prescaling_corr / denom); + corr = static_cast(o.prescaling_corr / denom); else - o.corr = NAN; + corr = NAN; } - if (apply[o.frame] && std::isfinite(o.corr)) - o.corr = static_cast(o.corr * ratio[o.frame]); + if (apply[o.frame] && std::isfinite(corr)) + corr = static_cast(corr * ratio[o.frame]); } }); }); - PartialLoopMemo m{key, partial_loop, frame_scaled_scratch, g_partial, {}}; - m.corr.resize(partials.size()); - ParallelChunks(static_cast(partials.size()), ThreadsForWork(partials.size(), nthreads), - [&](int lo, int hi) { - for (int i = lo; i < hi; ++i) m.corr[i] = partials[i].corr; + ParallelChunks(n_partials, partial_threads, [&](int lo, int hi) { + for (int i = lo; i < hi; ++i) partials[i].corr = scaling_corr[i]; }); + PartialLoopMemo m{key, partial_loop, frame_scaled_scratch, g_partial, std::move(scaling_corr)}; if (partial_loop_memos.size() == MAX_PARTIAL_LOOP_MEMOS) partial_loop_memos.erase(partial_loop_memos.begin()); partial_loop_memos.push_back(std::move(m)); diff --git a/image_analysis/scale_merge/RotationScaleMerge.h b/image_analysis/scale_merge/RotationScaleMerge.h index ebef3de15..ea1947c8f 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.h +++ b/image_analysis/scale_merge/RotationScaleMerge.h @@ -446,17 +446,40 @@ private: std::vector partial_group_start, partial_group_count; std::unique_ptr partial_group_perm; + // The CPU partial scaling loop's working copy of the partials. It passes over them several times + // per iteration and reads a handful of fields each time, so it works on these rather than on the + // eighty-byte Obs: ScalingObs holds, in the partials' own order, what FitPerFrameG and the corr + // update read; ScalingRef holds, in the CSR's group order, what the group means read besides corr, + // so that only corr is gathered. The same values, so the same arithmetic in the same order. + struct ScalingObs { + int32_t frame, group; + float I, sigma, partiality, prescaling_corr, zeta; + uint8_t on_ice; + }; + struct ScalingRef { + float I, sigma, partiality; + }; + // The observation's inverse-variance weight in the scaling reference, and its scaled intensity; // 0 when the observation is not in the reference at all. - static double ReferenceWeight(const Obs &o, double min_partiality, float &I_corr); + static double ReferenceWeight(int32_t group, float corr, float partiality, float I, float sigma, + double min_partiality, float &I_corr); + static double ReferenceWeight(const Obs &o, double min_partiality, float &I_corr) { + return ReferenceWeight(o.group, o.corr, o.partiality, o.I, o.sigma, min_partiality, I_corr); + } // Inverse-variance per-group mean of I*corr over `obs` (the merge reference). void ReduceGroupMeans(const std::vector &obs, int n_groups, std::vector &out_mean) const; // The same over the partials, using the CSR above - the same sums in the same order. void ReducePartialGroupMeans(int n_groups, std::vector &out_mean) const; + // The same again over the scaling loop's copy: refs in group order, corr in the partials' order. + void ReduceScalingGroupMeans(int n_groups, const std::vector &refs, + const std::vector &corr, std::vector &out_mean) const; // Robust per-frame G fit (IRLS, Cauchy k=3), unity=false uses the rotation partiality, unity=true the // scale-fulls (partiality already folded in). Reads out_mean[group] as the reference intensity. - void FitPerFrameG(std::vector &obs, const std::vector &fstart, + // O is Obs or ScalingObs. + template + void FitPerFrameG(const std::vector &obs, const std::vector &fstart, const std::vector &fcount, const std::vector &group_mean_in, bool unity, std::vector &g);