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; }