From c7a8fabc7e7351f39f5e517200b2bcb0daa08e0e Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 26 Sep 2026 19:39:56 +0200 Subject: [PATCH] BeamCenterFFTCPU: run the capture's independent 2-D transforms at the same time PointSurfaces made three forward and four inverse transforms of the padded detector (8748 x 8400 on a 16M) one after the other on the main thread - ~3.5 s of one core in a CPU-only run. The forwards do not depend on each other, nor do the inverse products; each group now runs at the same time, one workspace per transform. A workspace is built as the single one was - its own std::vector buffers and its own FFTW_ESTIMATE plan for them - so the same plan runs on the same data and the result is bit-identical. Costs about 2 GB more transient memory on a 16M detector. CPU-only 16M rotation run: md5-identical, 216 -> 210 s. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01D1G8gJVAy6gp1K5Dz3NE5C --- .../geom_refinement/BeamCenterFFTCPU.cpp | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/image_analysis/geom_refinement/BeamCenterFFTCPU.cpp b/image_analysis/geom_refinement/BeamCenterFFTCPU.cpp index a9be3f21f..78dd33a3c 100644 --- a/image_analysis/geom_refinement/BeamCenterFFTCPU.cpp +++ b/image_analysis/geom_refinement/BeamCenterFFTCPU.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include @@ -89,19 +91,34 @@ std::vector Transpose(const std::vector &src, int64_t h, int64_t w BeamCenterConvSurfaces2D BeamCenterFFTCPU::PointSurfaces(const std::vector &a, const std::vector &m, int64_t h, int64_t w) { - Rfft2 fft(BeamCenterFFTPadSize(2 * h), BeamCenterFFTPadSize(2 * w)); - const auto A = fft.Forward(a, h, w); - const auto M = fft.Forward(m, h, w); + // The three forward transforms are independent of each other, and so are the four inverse ones, + // so each group runs at the same time, one workspace per transform. A workspace is built exactly + // as a single one was - its own buffers and its own plan for them - so every transform is the one + // it was, bit for bit; the four inverse ones reuse the three forward workspaces and one more. + const int64_t ny = BeamCenterFFTPadSize(2 * h), nx = BeamCenterFFTPadSize(2 * w); + std::vector> fft; + for (int i = 0; i < 4; i++) + fft.push_back(std::make_unique(ny, nx)); + std::vector a2(a.size()); for (size_t i = 0; i < a2.size(); i++) a2[i] = a[i] * a[i]; - const auto A2 = fft.Forward(a2, h, w); + auto fA = std::async(std::launch::async, [&] { return fft[0]->Forward(a, h, w); }); + auto fM = std::async(std::launch::async, [&] { return fft[1]->Forward(m, h, w); }); + auto fA2 = std::async(std::launch::async, [&] { return fft[2]->Forward(a2, h, w); }); + const auto A = fA.get(); + const auto M = fM.get(); + const auto A2 = fA2.get(); BeamCenterConvSurfaces2D out; - out.C = fft.InverseProduct(A, A, 2 * h, 2 * w); - out.S = fft.InverseProduct(A, M, 2 * h, 2 * w); - out.Q = fft.InverseProduct(A2, M, 2 * h, 2 * w); - out.D = fft.InverseProduct(M, M, 2 * h, 2 * w); + auto fC = std::async(std::launch::async, [&] { return fft[0]->InverseProduct(A, A, 2 * h, 2 * w); }); + auto fS = std::async(std::launch::async, [&] { return fft[1]->InverseProduct(A, M, 2 * h, 2 * w); }); + auto fQ = std::async(std::launch::async, [&] { return fft[2]->InverseProduct(A2, M, 2 * h, 2 * w); }); + auto fD = std::async(std::launch::async, [&] { return fft[3]->InverseProduct(M, M, 2 * h, 2 * w); }); + out.C = fC.get(); + out.S = fS.get(); + out.Q = fQ.get(); + out.D = fD.get(); return out; }