Files
opencv-MIRROR/modules/core/perf/perf_dft.cpp
Vadim Pisarevsky 784be814b2 core(dft): follow-up to the new 1D DFT engine: memory, plan overhead, vectorization
Fixes the 64F 1D regressions measured on x86 after #30061 and reduces the
memory of the generic odd radix. Measured without IPP (AVX2 kernels).

Root cause of the 1D regressions: cv::dft() builds a fresh plan per call, and
at n = 65536 the engine allocated three MB-sized blocks per call (twiddles,
permutation, workspace). glibc then trims the heap after every free (dynamic
M_TRIM_THRESHOLD = 2 x the largest block), so every call page-faulted the
buffers again: 65536 64FC2 ran at 620 us, of which ~215 us were outside the
transform itself. The old code made one ~1.5 MB allocation and escaped.

Changes:
- The plan's tables, the permutation and the owner's workspace are one
  allocation (DftPlan::build(..., with_workspace)): one malloc per cv::dft()
  call instead of three, and glibc's trimming is not triggered by a single
  freed block (its threshold is twice the largest block seen). No memory is
  retained between calls, no plan cache.
- Static, lazily built tables for power-of-two lengths nc <= 1024: radix-4
  stage twiddles (shared by all plans: they depend on (radix, span) only),
  digit-reversal tables and the real-FFT twiddles. Plan building for 256..2048
  points is now table lookups only.
- Generic odd radix: cs/sn tables of r entries with a cyclic index p*k mod r
  instead of h*h entries each (h = (r-1)/2). n = 4093 in double: peak RSS
  72 MB -> 7.6 MB, max error 5e-13 -> 7e-15.
- DFT_STAGE_SIMD128: stages whose span is shorter than the full vector but at
  least 128 bits run on v_float32x4 / v_float64x2 instead of scalar code
  (span 4 on AVX2 floats, i.e. every 4^k length; span 2 on AVX-512 doubles).
- Fused twiddle-free first stage of radix 3 and 5 for odd nc (vectorized
  gather + DFT3/DFT5), replacing the scalar table gather and two scalar
  stages for lengths like 4725.
- Fused last radix-4 stage writing the interleaved, scaled output (C2C and
  even C2R): one pass over the data less.
- Leading stages run chunk by chunk (depth-first, 32 KB per array) for long
  transforms so that their data stays in L2.
- perf: 1D dft1d/dct1d cases and 256x256 / 512x512 2D sizes; test: prime and
  2*prime lengths (257, 1009, 2018, ...) in the sweep.

Single thread, Core Ultra 7 255H (AVX2 kernels, no IPP), min of two perf runs,
geomean over flags and row/column; speedup of the optimized build vs 5.x head
and vs the pre-#30061 code:
             32FC1       64FC1       32FC2       64FC2
  dft1d   256  2.0 / 2.8   1.8 / 2.2   2.1 / 3.5   1.7 / 2.4
  dft1d  1000  1.1 / 2.7   1.0 / 1.9   1.0 / 4.2   1.0 / 2.3
  dft1d  1024  2.4 / 5.8   1.9 / 3.4   2.3 / 6.0   1.6 / 3.3
  dft1d  4096  1.0 / 2.8   1.0 / 1.8   1.2 / 3.2   1.1 / 1.7
  dft1d  4725  1.7 / 3.2   1.7 / 1.8   1.7 / 3.0   1.7 / 1.7
  dft1d 65536  1.0 / 2.7   1.15/ 1.75  1.2 / 2.8   1.0 / 1.65
  dct1d 256..65536: 1.03-1.22 / 1.8-2.6
  2D dft 256x256..2048x2048: 1.05-1.15 / 2.2-3.8; 2D dct 1.04 / 2.6;
  matchTemplate 1.01-1.06 / 2.1-3.4; filter2D unchanged.
In a plain cv::dft() loop (outside the perf harness) 65536 64FC2 goes from
620 us (5.x head) to 287 us, as the page-fault storm is gone.
No case is slower than 5.x head beyond run-to-run noise (checked by
alternating runs of the suspicious cases).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-01 12:43:06 +03:00

120 lines
4.1 KiB
C++

#include "perf_precomp.hpp"
namespace opencv_test
{
using namespace perf;
///////////////////////////////////////////////////////dft//////////////////////////////////////////////////////////////
#define MAT_TYPES_DFT CV_32FC1, CV_32FC2, CV_64FC1
#define MAT_SIZES_DFT cv::Size(256, 256), cv::Size(512, 512), cv::Size(320, 480), cv::Size(800, 600), cv::Size(1280, 1024), sz1080p, sz2K
CV_ENUM(FlagsType, 0, DFT_INVERSE, DFT_SCALE, DFT_COMPLEX_OUTPUT, DFT_ROWS, DFT_INVERSE|DFT_COMPLEX_OUTPUT)
#define TEST_MATS_DFT testing::Combine(testing::Values(MAT_SIZES_DFT), testing::Values(MAT_TYPES_DFT), FlagsType::all(), testing::Values(true, false))
typedef tuple<Size, MatType, FlagsType, bool> Size_MatType_FlagsType_NzeroRows_t;
typedef perf::TestBaseWithParam<Size_MatType_FlagsType_NzeroRows_t> Size_MatType_FlagsType_NzeroRows;
PERF_TEST_P(Size_MatType_FlagsType_NzeroRows, dft, TEST_MATS_DFT)
{
Size sz = get<0>(GetParam());
int type = get<1>(GetParam());
int flags = get<2>(GetParam());
bool isNzeroRows = get<3>(GetParam());
int nonzero_rows = 0;
Mat src(sz, type);
Mat dst(sz, type);
declare.in(src, WARMUP_RNG).time(60);
if (isNzeroRows)
nonzero_rows = sz.height/2;
TEST_CYCLE() dft(src, dst, flags, nonzero_rows);
SANITY_CHECK(dst, 1e-5, ERROR_RELATIVE);
}
///////////////////////////////////////////////////////dct//////////////////////////////////////////////////////
CV_ENUM(DCT_FlagsType, 0, DCT_INVERSE , DCT_ROWS, DCT_INVERSE|DCT_ROWS)
typedef tuple<Size, MatType, DCT_FlagsType> Size_MatType_Flag_t;
typedef perf::TestBaseWithParam<Size_MatType_Flag_t> Size_MatType_Flag;
PERF_TEST_P(Size_MatType_Flag, dct, testing::Combine(
testing::Values(cv::Size(320, 240),cv::Size(800, 600),
cv::Size(1024, 768), cv::Size(1280, 1024),
sz1080p, sz2K),
testing::Values(CV_32FC1, CV_64FC1), DCT_FlagsType::all()))
{
Size sz = get<0>(GetParam());
int type = get<1>(GetParam());
int flags = get<2>(GetParam());
Mat src(sz, type);
Mat dst(sz, type);
declare
.in(src, WARMUP_RNG)
.out(dst)
.time(60);
TEST_CYCLE() dct(src, dst, flags);
SANITY_CHECK(dst, 1e-5, ERROR_RELATIVE);
}
///////////////////////////////////////////// 1D transforms /////////////////////////////////////////////
CV_ENUM(DFT1D_FlagsType, 0, DFT_INVERSE, DFT_SCALE, DFT_COMPLEX_OUTPUT)
typedef tuple<int, MatType, DFT1D_FlagsType, bool> Len_MatType_Flags_Col_t;
typedef perf::TestBaseWithParam<Len_MatType_Flags_Col_t> Len_MatType_Flags_Col;
// single row (1 x N) or single column (N x 1) vectors, N covers the 2^k, 5-smooth and odd plans
PERF_TEST_P(Len_MatType_Flags_Col, dft1d, testing::Combine(
testing::Values(256, 1000, 1024, 4096, 4725, 65536),
testing::Values(CV_32FC1, CV_32FC2, CV_64FC1, CV_64FC2),
DFT1D_FlagsType::all(), testing::Bool()))
{
int len = get<0>(GetParam());
int type = get<1>(GetParam());
int flags = get<2>(GetParam());
bool column = get<3>(GetParam());
Mat src(column ? len : 1, column ? 1 : len, type);
Mat dst;
declare.in(src, WARMUP_RNG).time(20);
TEST_CYCLE() dft(src, dst, flags);
SANITY_CHECK_NOTHING();
}
typedef tuple<int, MatType, DCT_FlagsType> Len_MatType_Flag_t;
typedef perf::TestBaseWithParam<Len_MatType_Flag_t> Len_MatType_Flag;
PERF_TEST_P(Len_MatType_Flag, dct1d, testing::Combine(
testing::Values(256, 1000, 1024, 4096, 65536),
testing::Values(CV_32FC1, CV_64FC1),
testing::Values(0, DCT_INVERSE)))
{
int len = get<0>(GetParam());
int type = get<1>(GetParam());
int flags = get<2>(GetParam());
Mat src(1, len, type);
Mat dst;
declare.in(src, WARMUP_RNG).time(20);
TEST_CYCLE() dct(src, dst, flags);
SANITY_CHECK_NOTHING();
}
} // namespace