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) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D1G8gJVAy6gp1K5Dz3NE5C
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-09-26 20:48:08 +02:00
1 parent 87cfc879b6
commit ef4090c830
2 files changed
+79 -50

No files matched your search

@@ -150,22 +150,43 @@ std::vector<BeamCenterFFTCandidate> BeamCenterShortlist2D(std::vector<float> &su
int64_t w, float nms_pxl, int budget) {
std::vector<BeamCenterFFTCandidate> 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<float> row_v(h);
std::vector<int64_t> row_i(h);
const auto scan_row = [&](int64_t y) {
float v = -std::numeric_limits<float>::infinity();
int64_t at = -1;
for (int64_t x = 0; x < w; x++)
if (surface[static_cast<size_t>(y * w + x)] > v) {
v = surface[static_cast<size_t>(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<float>::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<float>(ix) / 2.0f, static_cast<float>(iy) / 2.0f, best_v});
for (int64_t y = std::max<int64_t>(0, iy - d); y <= std::min(h - 1, iy + d); y++)
for (int64_t y = std::max<int64_t>(0, iy - d); y <= std::min(h - 1, iy + d); y++) {
for (int64_t x = std::max<int64_t>(0, ix - d); x <= std::min(w - 1, ix + d); x++)
surface[static_cast<size_t>(y * w + x)] =
-std::numeric_limits<float>::infinity();
scan_row(y);
}
}
return out;
}
+53 -45
View File
@@ -11,6 +11,7 @@
#include <vector>
#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> &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<size_t>(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<size_t>(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<long long>(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<long long>(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> &coord, size_t nspots) {
constexpr double BG_HALF_WIDTH_A = 15.0;
const int bg_half = std::max(1, static_cast<int>(std::lround(BG_HALF_WIDTH_A / len_coeff)));
std::vector<double> mag(out_len), pref(out_len + 1);
ParallelChunks(D, nthreads, [&](int d0, int d1) {
std::vector<double> mag(out_len), pref(out_len + 1);
for (int d = 0; d < D; ++d) {
const auto* spec = h_output_fft.data() + static_cast<size_t>(d) * out_len;
for (int d = d0; d < d1; ++d) {
const auto* spec = h_output_fft.data() + static_cast<size_t>(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<double>(j);
if (len <= static_cast<double>(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<double>(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<float>(best_prom),
.direction = d,
.length = static_cast<float>(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<double>(j);
if (len <= static_cast<double>(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<double>(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<float>(best_prom),
.direction = d,
.length = static_cast<float>(best_len)
};
}
});
}