Files
opencv-MIRROR/modules/core/test
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
..
2026-02-20 22:05:35 +03:00
2018-02-03 19:39:47 +00:00