Files
opencv-MIRROR/modules/core/perf/perf_stat.cpp
pranayr710 1780d20b80 core: vectorize cv::sum for CV_64F (#30582)
* core: vectorize cv::sum for CV_64F

Sum_SIMD has specializations for the 8-, 16- and 32-bit depths, but not for
CV_64F, so cv::sum and cv::mean on a double matrix fall through to the scalar
unrolled loop in sum_. CV_64FC1 is not in TYPICAL_MAT_TYPES, so no perf test
covered it.

The source type is already the accumulator type, so the specialization needs
neither expansion nor conversion: it loads three vectors at the native width and
adds them. The channel limit matches DEFINE_SUM_SIMD_32 because init_maskbuf can
only build the cn == 4 masks once the vector holds four lanes, which rules the
case out on an SSE2 baseline.

Extend sum_wide with the CV_64F rows so the change is measurable.

* core: reuse DEFINE_SUM_SIMD_16 for CV_64F

Drop the near-duplicate DEFINE_SUM_SIMD_64 and instantiate DEFINE_SUM_SIMD_16
with v_float64 instead, as suggested in review.

The one thing that kept the two apart was the channel guard. DEFINE_SUM_SIMD_16
rejects cn > 4, which is correct only while the vector holds at least four lanes:
init_maskbuf builds the cn == 4 masks under that condition, so with a narrower
vector the reduction runs against all-zero masks and drops the vectorized
elements without reporting anything. Every 32-bit register type has four lanes or
more, so the existing instantiations never saw it; v_float64 has two on an SSE2
baseline.

Widening the guard to the DEFINE_SUM_SIMD_32 form leaves the existing
instantiations unchanged, since 3+(simd_width>=4) is 4 for all of them.
2026-11-08 08:31:23 +05:30

163 lines
3.8 KiB
C++

#include "perf_precomp.hpp"
namespace opencv_test
{
using namespace perf;
// The wide depths are not in TYPICAL_MAT_TYPES, so they were not measured; they are the ones
// whose scalar accumulation step this touches.
PERF_TEST_P(Size_MatType, sum_wide,
testing::Combine(testing::Values(TYPICAL_MAT_SIZES),
testing::Values(CV_32SC1, CV_32UC1, CV_32SC3,
CV_64FC1, CV_64FC3)))
{
Size sz = get<0>(GetParam());
int type = get<1>(GetParam());
Mat arr(sz, type);
Scalar s;
declare.in(arr, WARMUP_RNG).out(s);
TEST_CYCLE() s = sum(arr);
SANITY_CHECK_NOTHING();
}
PERF_TEST_P(Size_MatType, sum, TYPICAL_MATS)
{
Size sz = get<0>(GetParam());
int type = get<1>(GetParam());
Mat arr(sz, type);
Scalar s;
declare.in(arr, WARMUP_RNG).out(s);
TEST_CYCLE() s = sum(arr);
SANITY_CHECK(s, 1e-6, ERROR_RELATIVE);
}
PERF_TEST_P(Size_MatType, mean, TYPICAL_MATS)
{
Size sz = get<0>(GetParam());
int type = get<1>(GetParam());
Mat src(sz, type);
Scalar s;
declare.in(src, WARMUP_RNG).out(s);
TEST_CYCLE() s = cv::mean(src);
SANITY_CHECK(s, 1e-5);
}
PERF_TEST_P(Size_MatType, mean_mask, TYPICAL_MATS)
{
Size sz = get<0>(GetParam());
int type = get<1>(GetParam());
Mat src(sz, type);
Mat mask = Mat::ones(src.size(), CV_8U);
Scalar s;
declare.in(src, WARMUP_RNG).in(mask).out(s);
TEST_CYCLE() s = cv::mean(src, mask);
SANITY_CHECK(s, 5e-5);
}
PERF_TEST_P(Size_MatType, meanStdDev, TYPICAL_MATS)
{
Size sz = get<0>(GetParam());
int matType = get<1>(GetParam());
Mat src(sz, matType);
Scalar mean;
Scalar dev;
declare.in(src, WARMUP_RNG).out(mean, dev);
TEST_CYCLE() meanStdDev(src, mean, dev);
SANITY_CHECK(mean, 1e-5, ERROR_RELATIVE);
SANITY_CHECK(dev, 1e-5, ERROR_RELATIVE);
}
PERF_TEST_P(Size_MatType, meanStdDev_mask, TYPICAL_MATS)
{
Size sz = get<0>(GetParam());
int matType = get<1>(GetParam());
Mat src(sz, matType);
Mat mask = Mat::ones(sz, CV_8U);
Scalar mean;
Scalar dev;
declare.in(src, WARMUP_RNG).in(mask).out(mean, dev);
TEST_CYCLE() meanStdDev(src, mean, dev, mask);
SANITY_CHECK(mean, 1e-5);
SANITY_CHECK(dev, 1e-5);
}
PERF_TEST_P(Size_MatType, countNonZero, testing::Combine( testing::Values( TYPICAL_MAT_SIZES ), testing::Values( CV_8UC1, CV_8SC1, CV_16UC1, CV_16SC1, CV_32SC1, CV_32FC1, CV_64FC1 ) ))
{
Size sz = get<0>(GetParam());
int matType = get<1>(GetParam());
Mat src(sz, matType);
int cnt = 0;
declare.in(src, WARMUP_RNG);
int runs = (sz.width <= 640) ? 8 : 1;
TEST_CYCLE_MULTIRUN(runs) cnt = countNonZero(src);
SANITY_CHECK(cnt);
}
PERF_TEST_P(Size_MatType, hasNonZero, testing::Combine( testing::Values( TYPICAL_MAT_SIZES ), testing::Values( CV_8UC1, CV_8SC1, CV_16UC1, CV_16SC1, CV_32SC1, CV_32FC1, CV_64FC1 ) ))
{
Size sz = get<0>(GetParam());
int matType = get<1>(GetParam());
Mat src(sz, matType);
/*bool hnz = false;*/
declare.in(src, WARMUP_RNG);
int runs = (sz.width <= 640) ? 8 : 1;
TEST_CYCLE_MULTIRUN(runs) /*hnz =*/ hasNonZero(src);
SANITY_CHECK_NOTHING();
}
typedef tuple<Size, double> Size_Density_t;
typedef perf::TestBaseWithParam<Size_Density_t> Size_Density;
PERF_TEST_P(Size_Density, findNonZero, testing::Combine(
testing::Values(TYPICAL_MAT_SIZES),
testing::Values(0.0, 0.001, 0.01, 0.1, 0.5)))
{
Size sz = get<0>(GetParam());
double density = get<1>(GetParam());
Mat r(sz, CV_32FC1);
randu(r, 0, 1);
Mat src = r < density; // CV_8UC1: 255 where r < density, 0 elsewhere
std::vector<Point> idx;
declare.in(src);
TEST_CYCLE() findNonZero(src, idx);
SANITY_CHECK_NOTHING();
}
} // namespace