From ef4090c830351c69f58aa9117bf9ca7e624f88d2 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 26 Sep 2026 20:48:08 +0200 Subject: [PATCH] CPU FFT indexer and beam-centre shortlist: less serial work - FFTIndexerCPU::ExecuteFFT built the per-direction histograms and scanned the per-direction spectra for their most prominent peak in one thread; every direction has its own histogram and spectrum, so the directions are now split over the refinement threads, each filled and scanned in the same order as before. - BeamCenterShortlist2D scanned the whole padded surface (73 M points on a 16M detector) once per candidate. It now keeps each row's maximum and the first index holding it and rescans only the rows a suppression touched; rows in order, first index within a row, is the same first maximum. md5-identical output; CPU-only 177 -> 169 s and 101 -> 96 s. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01D1G8gJVAy6gp1K5Dz3NE5C --- .../geom_refinement/BeamCenterFFT.cpp | 31 +++++- image_analysis/indexing/FFTIndexerCPU.cpp | 98 ++++++++++--------- 2 files changed, 79 insertions(+), 50 deletions(-) diff --git a/image_analysis/geom_refinement/BeamCenterFFT.cpp b/image_analysis/geom_refinement/BeamCenterFFT.cpp index 8c2f60366..7df072664 100644 --- a/image_analysis/geom_refinement/BeamCenterFFT.cpp +++ b/image_analysis/geom_refinement/BeamCenterFFT.cpp @@ -150,22 +150,43 @@ std::vector BeamCenterShortlist2D(std::vector &su int64_t w, float nms_pxl, int budget) { std::vector out; const int64_t d = std::lround(2.0f * nms_pxl); + // Each round takes the first maximum of the whole surface in index order. Kept per row - its + // maximum and the first index holding it - so a round reads the rows' maxima, and only the rows + // the suppression touched are scanned again. Rows in order and the first index within a row is + // the first index overall, the same pick as one scan of the whole surface. + std::vector row_v(h); + std::vector row_i(h); + const auto scan_row = [&](int64_t y) { + float v = -std::numeric_limits::infinity(); + int64_t at = -1; + for (int64_t x = 0; x < w; x++) + if (surface[static_cast(y * w + x)] > v) { + v = surface[static_cast(y * w + x)]; + at = y * w + x; + } + row_v[y] = v; + row_i[y] = at; + }; + for (int64_t y = 0; y < h; y++) + scan_row(y); for (int k = 0; k < budget; k++) { int64_t best = -1; float best_v = -std::numeric_limits::infinity(); - for (int64_t i = 0; i < h * w; i++) - if (surface[i] > best_v) { - best_v = surface[i]; - best = i; + for (int64_t y = 0; y < h; y++) + if (row_i[y] >= 0 && row_v[y] > best_v) { + best_v = row_v[y]; + best = row_i[y]; } if (best < 0 || !std::isfinite(best_v)) break; const int64_t iy = best / w, ix = best % w; out.push_back({static_cast(ix) / 2.0f, static_cast(iy) / 2.0f, best_v}); - for (int64_t y = std::max(0, iy - d); y <= std::min(h - 1, iy + d); y++) + for (int64_t y = std::max(0, iy - d); y <= std::min(h - 1, iy + d); y++) { for (int64_t x = std::max(0, ix - d); x <= std::min(w - 1, ix + d); x++) surface[static_cast(y * w + x)] = -std::numeric_limits::infinity(); + scan_row(y); + } } return out; } diff --git a/image_analysis/indexing/FFTIndexerCPU.cpp b/image_analysis/indexing/FFTIndexerCPU.cpp index 218c95cef..43f663d17 100644 --- a/image_analysis/indexing/FFTIndexerCPU.cpp +++ b/image_analysis/indexing/FFTIndexerCPU.cpp @@ -11,6 +11,7 @@ #include #include "../../common/FFTWPlannerLock.h" +#include "../../common/ParallelFor.h" static inline double dot_abs(const Coord& a, const Coord& b) { return std::fabs(a.x * b.x + a.y * b.y + a.z * b.z); @@ -61,18 +62,23 @@ void FFTIndexerCPU::ExecuteFFT(const std::vector &coord, size_t nspots) { std::fill(h_input_fft.begin(), h_input_fft.end(), 0.0); - for (int d = 0; d < D; ++d) { - float* hist = h_input_fft.data() + static_cast(d) * H; + // Every direction has a histogram and a spectrum of its own, so the directions are split over the + // refinement threads; each one is still filled and scanned in the same order. + const size_t nthreads = std::max(1u, refine_threads); + ParallelChunks(D, nthreads, [&](int d0, int d1) { + for (int d = d0; d < d1; ++d) { + float* hist = h_input_fft.data() + static_cast(d) * H; - for (size_t i = 0; i < nspots; i++) { - const auto& r = coord[i]; - double dot = dot_abs(direction_vectors[d], r); - long long bin = static_cast(dot / histogram_spacing); - if (bin >= 0 && bin < H) { - hist[bin] += 1.0; + for (size_t i = 0; i < nspots; i++) { + const auto& r = coord[i]; + double dot = dot_abs(direction_vectors[d], r); + long long bin = static_cast(dot / histogram_spacing); + if (bin >= 0 && bin < H) { + hist[bin] += 1.0; + } } } - } + }); // Plan and execute batched R2C FFT with FFTW fftwf_execute(plan); @@ -92,44 +98,46 @@ void FFTIndexerCPU::ExecuteFFT(const std::vector &coord, size_t nspots) { constexpr double BG_HALF_WIDTH_A = 15.0; const int bg_half = std::max(1, static_cast(std::lround(BG_HALF_WIDTH_A / len_coeff))); - std::vector mag(out_len), pref(out_len + 1); + ParallelChunks(D, nthreads, [&](int d0, int d1) { + std::vector mag(out_len), pref(out_len + 1); - for (int d = 0; d < D; ++d) { - const auto* spec = h_output_fft.data() + static_cast(d) * out_len; + for (int d = d0; d < d1; ++d) { + const auto* spec = h_output_fft.data() + static_cast(d) * out_len; - pref[0] = 0.0; - for (int j = 0; j < out_len; ++j) { - mag[j] = std::hypot(spec[j][0], spec[j][1]); - pref[j + 1] = pref[j] + mag[j]; - } - - double best_prom = 0.0; - double best_len = -1.0; - - for (int j = 0; j < out_len; ++j) { - double len = len_coeff * static_cast(j); - if (len <= static_cast(min_length_A)) continue; - - // Slide the background window inward at the ends rather than truncating it. A peak within - // bg_half bins of either end - which is where the LONGEST cells sit, the last usable bin - // being max_length_A itself - otherwise gets its background from a one-sided window, and - // the prominence it is judged on is biased by however much the spectrum slopes there. - int lo = j - bg_half, hi = j + bg_half + 1; - if (lo < 0) { hi = std::min(out_len, hi - lo); lo = 0; } - if (hi > out_len) { lo = std::max(0, lo - (hi - out_len)); hi = out_len; } - const double background = (pref[hi] - pref[lo]) / static_cast(hi - lo); - const double prominence = mag[j] - background; - - if (prominence > best_prom) { - best_prom = prominence; - best_len = len; + pref[0] = 0.0; + for (int j = 0; j < out_len; ++j) { + mag[j] = std::hypot(spec[j][0], spec[j][1]); + pref[j + 1] = pref[j] + mag[j]; } - } - result_fft[d] = FFTResult{ - .magnitude = static_cast(best_prom), - .direction = d, - .length = static_cast(best_len) - }; - } + double best_prom = 0.0; + double best_len = -1.0; + + for (int j = 0; j < out_len; ++j) { + double len = len_coeff * static_cast(j); + if (len <= static_cast(min_length_A)) continue; + + // Slide the background window inward at the ends rather than truncating it. A peak within + // bg_half bins of either end - which is where the LONGEST cells sit, the last usable bin + // being max_length_A itself - otherwise gets its background from a one-sided window, and + // the prominence it is judged on is biased by however much the spectrum slopes there. + int lo = j - bg_half, hi = j + bg_half + 1; + if (lo < 0) { hi = std::min(out_len, hi - lo); lo = 0; } + if (hi > out_len) { lo = std::max(0, lo - (hi - out_len)); hi = out_len; } + const double background = (pref[hi] - pref[lo]) / static_cast(hi - lo); + const double prominence = mag[j] - background; + + if (prominence > best_prom) { + best_prom = prominence; + best_len = len; + } + } + + result_fft[d] = FFTResult{ + .magnitude = static_cast(best_prom), + .direction = d, + .length = static_cast(best_len) + }; + } + }); }