This source file includes following definitions.
- TEST
- TEST
#include "test_precomp.hpp"
#include "opencv2/hal.hpp"
using namespace cv;
enum
{
HAL_EXP = 0,
HAL_LOG = 1,
HAL_SQRT = 2
};
TEST(Core_HAL, mathfuncs)
{
for( int hcase = 0; hcase < 6; hcase++ )
{
int depth = hcase % 2 == 0 ? CV_32F : CV_64F;
double eps = depth == CV_32F ? 1e-5 : 1e-10;
int nfunc = hcase / 2;
int n = 100;
Mat src(1, n, depth), dst(1, n, depth), dst0(1, n, depth);
randu(src, 1, 10);
double min_hal_t = DBL_MAX, min_ocv_t = DBL_MAX;
for( int iter = 0; iter < 10; iter++ )
{
double t = (double)getTickCount();
switch (nfunc)
{
case HAL_EXP:
if( depth == CV_32F )
hal::exp(src.ptr<float>(), dst.ptr<float>(), n);
else
hal::exp(src.ptr<double>(), dst.ptr<double>(), n);
break;
case HAL_LOG:
if( depth == CV_32F )
hal::log(src.ptr<float>(), dst.ptr<float>(), n);
else
hal::log(src.ptr<double>(), dst.ptr<double>(), n);
break;
case HAL_SQRT:
if( depth == CV_32F )
hal::sqrt(src.ptr<float>(), dst.ptr<float>(), n);
else
hal::sqrt(src.ptr<double>(), dst.ptr<double>(), n);
break;
default:
CV_Error(Error::StsBadArg, "unknown function");
}
t = (double)getTickCount() - t;
min_hal_t = std::min(min_hal_t, t);
t = (double)getTickCount();
switch (nfunc)
{
case HAL_EXP:
exp(src, dst0);
break;
case HAL_LOG:
log(src, dst0);
break;
case HAL_SQRT:
pow(src, 0.5, dst0);
break;
default:
CV_Error(Error::StsBadArg, "unknown function");
}
t = (double)getTickCount() - t;
min_ocv_t = std::min(min_ocv_t, t);
}
EXPECT_LE(norm(dst, dst0, NORM_INF | NORM_RELATIVE), eps);
double freq = getTickFrequency();
printf("%s (N=%d, %s): hal time=%.2fusec, ocv time=%.2fusec\n",
(nfunc == HAL_EXP ? "exp" : nfunc == HAL_LOG ? "log" : nfunc == HAL_SQRT ? "sqrt" : "???"),
n, (depth == CV_32F ? "f32" : "f64"), min_hal_t*1e6/freq, min_ocv_t*1e6/freq);
}
}
enum
{
HAL_LU = 0,
HAL_CHOL = 1
};
TEST(Core_HAL, mat_decomp)
{
for( int hcase = 0; hcase < 16; hcase++ )
{
int depth = hcase % 2 == 0 ? CV_32F : CV_64F;
int size = (hcase / 2) % 4;
size = size == 0 ? 3 : size == 1 ? 4 : size == 2 ? 6 : 15;
int nfunc = (hcase / 8);
double eps = depth == CV_32F ? 1e-5 : 1e-10;
if( size == 3 )
continue;
Mat a0(size, size, depth), a(size, size, depth), b(size, 1, depth), x(size, 1, depth), x0(size, 1, depth);
randu(a0, -1, 1);
a0 = a0*a0.t();
randu(b, -1, 1);
double min_hal_t = DBL_MAX, min_ocv_t = DBL_MAX;
size_t asize = size*size*a.elemSize();
size_t bsize = size*b.elemSize();
for( int iter = 0; iter < 10; iter++ )
{
memcpy(x.ptr(), b.ptr(), bsize);
memcpy(a.ptr(), a0.ptr(), asize);
double t = (double)getTickCount();
switch (nfunc)
{
case HAL_LU:
if( depth == CV_32F )
hal::LU(a.ptr<float>(), a.step, size, x.ptr<float>(), x.step, 1);
else
hal::LU(a.ptr<double>(), a.step, size, x.ptr<double>(), x.step, 1);
break;
case HAL_CHOL:
if( depth == CV_32F )
hal::Cholesky(a.ptr<float>(), a.step, size, x.ptr<float>(), x.step, 1);
else
hal::Cholesky(a.ptr<double>(), a.step, size, x.ptr<double>(), x.step, 1);
break;
default:
CV_Error(Error::StsBadArg, "unknown function");
}
t = (double)getTickCount() - t;
min_hal_t = std::min(min_hal_t, t);
t = (double)getTickCount();
solve(a0, b, x0, (nfunc == HAL_LU ? DECOMP_LU : DECOMP_CHOLESKY));
t = (double)getTickCount() - t;
min_ocv_t = std::min(min_ocv_t, t);
}
EXPECT_LE(norm(x, x0, NORM_INF | NORM_RELATIVE), eps);
double freq = getTickFrequency();
printf("%s (%d x %d, %s): hal time=%.2fusec, ocv time=%.2fusec\n",
(nfunc == HAL_LU ? "LU" : nfunc == HAL_CHOL ? "Cholesky" : "???"),
size, size,
(depth == CV_32F ? "f32" : "f64"),
min_hal_t*1e6/freq, min_ocv_t*1e6/freq);
}
}