Commit b5bd2cde authored by yao's avatar yao Committed by Andrey Kamaev

A few optimizations to ocl::pyrLK::sparse, make it running on more OCL platforms

parent 02c9e0a3
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
// //
// @Authors // @Authors
// Jia Haipeng, jiahaipeng95@gmail.com // Jia Haipeng, jiahaipeng95@gmail.com
// Dachuan Zhao, dachuan@multicorewareinc.com
// //
// Redistribution and use in source and binary forms, with or without modification, // Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met: // are permitted provided that the following conditions are met:
...@@ -260,3 +261,22 @@ __kernel void arithm_mul_D6 (__global double *src1, int src1_step, int src1_offs ...@@ -260,3 +261,22 @@ __kernel void arithm_mul_D6 (__global double *src1, int src1_step, int src1_offs
} }
} }
#endif #endif
__kernel void arithm_muls_D5 (__global float *src1, int src1_step, int src1_offset,
__global float *dst, int dst_step, int dst_offset,
int rows, int cols, int dst_step1, float scalar)
{
int x = get_global_id(0);
int y = get_global_id(1);
if (x < cols && y < rows)
{
int src1_index = mad24(y, src1_step, (x << 2) + src1_offset);
int dst_index = mad24(y, dst_step, (x << 2) + dst_offset);
float data1 = *((__global float *)((__global char *)src1 + src1_index));
float tmp = data1 * scalar;
*((__global float *)((__global char *)dst + dst_index)) = tmp;
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
...@@ -48,23 +48,24 @@ using namespace cv::ocl; ...@@ -48,23 +48,24 @@ using namespace cv::ocl;
#if !defined (HAVE_OPENCL) #if !defined (HAVE_OPENCL)
void cv::ocl::PyrLKOpticalFlow::sparse(const oclMat &, const oclMat &, const oclMat &, oclMat &, oclMat &, oclMat *) { } void cv::ocl::PyrLKOpticalFlow::sparse(const oclMat &, const oclMat &, const oclMat &, oclMat &, oclMat &, oclMat &) { }
void cv::ocl::PyrLKOpticalFlow::dense(const oclMat &, const oclMat &, oclMat &, oclMat &, oclMat *) { } void cv::ocl::PyrLKOpticalFlow::dense(const oclMat &, const oclMat &, oclMat &, oclMat &, oclMat *) { }
#else /* !defined (HAVE_OPENCL) */ #else /* !defined (HAVE_OPENCL) */
namespace cv namespace cv
{ {
namespace ocl namespace ocl
{ {
///////////////////////////OpenCL kernel strings/////////////////////////// ///////////////////////////OpenCL kernel strings///////////////////////////
extern const char *pyrlk; extern const char *pyrlk;
extern const char *operator_setTo; extern const char *pyrlk_no_image;
extern const char *operator_convertTo; extern const char *operator_setTo;
extern const char *operator_copyToM; extern const char *operator_convertTo;
extern const char *arithm_mul; extern const char *operator_copyToM;
extern const char *pyr_down; extern const char *arithm_mul;
} extern const char *pyr_down;
}
} }
struct dim3 struct dim3
...@@ -84,8 +85,8 @@ struct int2 ...@@ -84,8 +85,8 @@ struct int2
namespace namespace
{ {
void calcPatchSize(cv::Size winSize, int cn, dim3 &block, dim3 &patch, bool isDeviceArch11) void calcPatchSize(cv::Size winSize, int cn, dim3 &block, dim3 &patch, bool isDeviceArch11)
{ {
winSize.width *= cn; winSize.width *= cn;
if (winSize.width > 32 && winSize.width > 2 * winSize.height) if (winSize.width > 32 && winSize.width > 2 * winSize.height)
...@@ -103,7 +104,7 @@ namespace ...@@ -103,7 +104,7 @@ namespace
patch.y = (winSize.height + block.y - 1) / block.y; patch.y = (winSize.height + block.y - 1) / block.y;
block.z = patch.z = 1; block.z = patch.z = 1;
} }
} }
inline int divUp(int total, int grain) inline int divUp(int total, int grain)
...@@ -530,7 +531,7 @@ void arithmetic_run(const oclMat &src1, oclMat &dst, string kernelName, const ch ...@@ -530,7 +531,7 @@ void arithmetic_run(const oclMat &src1, oclMat &dst, string kernelName, const ch
void multiply_cus(const oclMat &src1, oclMat &dst, float scalar) void multiply_cus(const oclMat &src1, oclMat &dst, float scalar)
{ {
arithmetic_run(src1, dst, "arithm_muls", &pyrlk, (void *)(&scalar)); arithmetic_run(src1, dst, "arithm_muls", &arithm_mul, (void *)(&scalar));
} }
void pyrdown_run_cus(const oclMat &src, const oclMat &dst) void pyrdown_run_cus(const oclMat &src, const oclMat &dst)
...@@ -735,46 +736,69 @@ void releaseTexture(cl_mem texture) ...@@ -735,46 +736,69 @@ void releaseTexture(cl_mem texture)
} }
void lkSparse_run(oclMat &I, oclMat &J, void lkSparse_run(oclMat &I, oclMat &J,
const oclMat &prevPts, oclMat &nextPts, oclMat &status, oclMat *err, bool GET_MIN_EIGENVALS, int ptcount, const oclMat &prevPts, oclMat &nextPts, oclMat &status, oclMat& err, bool /*GET_MIN_EIGENVALS*/, int ptcount,
int level, /*dim3 block, */dim3 patch, Size winSize, int iters) int level, /*dim3 block, */dim3 patch, Size winSize, int iters)
{ {
Context *clCxt = I.clCxt; Context *clCxt = I.clCxt;
char platform[256] = {0};
cl_platform_id pid;
clGetDeviceInfo(*clCxt->impl->devices, CL_DEVICE_PLATFORM, sizeof(pid), &pid, NULL);
clGetPlatformInfo(pid, CL_PLATFORM_NAME, 256, platform, NULL);
std::string namestr = platform;
bool isImageSupported = true;
if(namestr.find("NVIDIA")!=string::npos || namestr.find("Intel")!=string::npos)
isImageSupported = false;
int elemCntPerRow = I.step / I.elemSize();
string kernelName = "lkSparse"; string kernelName = "lkSparse";
size_t localThreads[3] = { 8, 32, 1 };
size_t globalThreads[3] = { 8 * ptcount, 32, 1}; size_t localThreads[3] = { 8, isImageSupported?8:32, 1 };
size_t globalThreads[3] = { 8 * ptcount, isImageSupported?8:32, 1};
int cn = I.oclchannels(); int cn = I.oclchannels();
bool calcErr; char calcErr;
if (err) if (level == 0)
{ {
calcErr = true; calcErr = 1;
} }
else else
{ {
calcErr = false; calcErr = 0;
} }
calcErr = true;
cl_mem ITex = bindTexture(I, I.depth(), cn);
cl_mem JTex = bindTexture(J, J.depth(), cn);
vector<pair<size_t , const void *> > args; vector<pair<size_t , const void *> > args;
cl_mem ITex;
cl_mem JTex;
if (isImageSupported)
{
ITex = bindTexture(I, I.depth(), cn);
JTex = bindTexture(J, J.depth(), cn);
}
else
{
ITex = (cl_mem)I.data;
JTex = (cl_mem)J.data;
}
args.push_back( make_pair( sizeof(cl_mem), (void *)&ITex )); args.push_back( make_pair( sizeof(cl_mem), (void *)&ITex ));
args.push_back( make_pair( sizeof(cl_mem), (void *)&JTex )); args.push_back( make_pair( sizeof(cl_mem), (void *)&JTex ));
//cl_mem clmD = clCreateBuffer(clCxt, CL_MEM_READ_WRITE, ptcount * sizeof(float), NULL, NULL);
args.push_back( make_pair( sizeof(cl_mem), (void *)&prevPts.data )); args.push_back( make_pair( sizeof(cl_mem), (void *)&prevPts.data ));
args.push_back( make_pair( sizeof(cl_int), (void *)&prevPts.step )); args.push_back( make_pair( sizeof(cl_int), (void *)&prevPts.step ));
args.push_back( make_pair( sizeof(cl_mem), (void *)&nextPts.data )); args.push_back( make_pair( sizeof(cl_mem), (void *)&nextPts.data ));
args.push_back( make_pair( sizeof(cl_int), (void *)&nextPts.step )); args.push_back( make_pair( sizeof(cl_int), (void *)&nextPts.step ));
args.push_back( make_pair( sizeof(cl_mem), (void *)&status.data )); args.push_back( make_pair( sizeof(cl_mem), (void *)&status.data ));
//args.push_back( make_pair( sizeof(cl_mem), (void *)&(err->data) )); args.push_back( make_pair( sizeof(cl_mem), (void *)&err.data ));
args.push_back( make_pair( sizeof(cl_int), (void *)&level )); args.push_back( make_pair( sizeof(cl_int), (void *)&level ));
args.push_back( make_pair( sizeof(cl_int), (void *)&I.rows )); args.push_back( make_pair( sizeof(cl_int), (void *)&I.rows ));
args.push_back( make_pair( sizeof(cl_int), (void *)&I.cols )); args.push_back( make_pair( sizeof(cl_int), (void *)&I.cols ));
if (!isImageSupported)
{
args.push_back( make_pair( sizeof(cl_int), (void *)&elemCntPerRow ) );
}
args.push_back( make_pair( sizeof(cl_int), (void *)&patch.x )); args.push_back( make_pair( sizeof(cl_int), (void *)&patch.x ));
args.push_back( make_pair( sizeof(cl_int), (void *)&patch.y )); args.push_back( make_pair( sizeof(cl_int), (void *)&patch.y ));
args.push_back( make_pair( sizeof(cl_int), (void *)&cn )); args.push_back( make_pair( sizeof(cl_int), (void *)&cn ));
...@@ -782,27 +806,29 @@ void lkSparse_run(oclMat &I, oclMat &J, ...@@ -782,27 +806,29 @@ void lkSparse_run(oclMat &I, oclMat &J,
args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.height )); args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.height ));
args.push_back( make_pair( sizeof(cl_int), (void *)&iters )); args.push_back( make_pair( sizeof(cl_int), (void *)&iters ));
args.push_back( make_pair( sizeof(cl_char), (void *)&calcErr )); args.push_back( make_pair( sizeof(cl_char), (void *)&calcErr ));
args.push_back( make_pair( sizeof(cl_char), (void *)&GET_MIN_EIGENVALS )); //args.push_back( make_pair( sizeof(cl_char), (void *)&GET_MIN_EIGENVALS ));
if (isImageSupported)
{
openCLExecuteKernel2(clCxt, &pyrlk, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth(), CLFLUSH); openCLExecuteKernel2(clCxt, &pyrlk, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth(), CLFLUSH);
releaseTexture(ITex); releaseTexture(ITex);
releaseTexture(JTex); releaseTexture(JTex);
}
else
{
//printf("Warning: The image2d_t is not supported by the device. Using alternative method!\n");
openCLExecuteKernel2(clCxt, &pyrlk_no_image, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth(), CLFLUSH);
}
} }
void cv::ocl::PyrLKOpticalFlow::sparse(const oclMat &prevImg, const oclMat &nextImg, const oclMat &prevPts, oclMat &nextPts, oclMat &status, oclMat *err) void cv::ocl::PyrLKOpticalFlow::sparse(const oclMat &prevImg, const oclMat &nextImg, const oclMat &prevPts, oclMat &nextPts, oclMat &status, oclMat *err)
{ {
if (prevImg.clCxt->impl->devName.find("Intel(R) HD Graphics") != string::npos)
{
cout << " Intel HD GPU device unsupported " << endl;
return;
}
if (prevPts.empty()) if (prevPts.empty())
{ {
nextPts.release(); nextPts.release();
status.release(); status.release();
if (err) err->release(); //if (err) err->release();
return; return;
} }
...@@ -836,8 +862,15 @@ void cv::ocl::PyrLKOpticalFlow::sparse(const oclMat &prevImg, const oclMat &next ...@@ -836,8 +862,15 @@ void cv::ocl::PyrLKOpticalFlow::sparse(const oclMat &prevImg, const oclMat &next
//status.setTo(Scalar::all(1)); //status.setTo(Scalar::all(1));
setTo(status, Scalar::all(1)); setTo(status, Scalar::all(1));
//if (err) bool errMat = false;
// ensureSizeIsEnough(1, prevPts.cols, CV_32FC1, *err); if (!err)
{
err = new oclMat(1, prevPts.cols, CV_32FC1);
errMat = true;
}
else
ensureSizeIsEnough(1, prevPts.cols, CV_32FC1, *err);
//ensureSizeIsEnough(1, prevPts.cols, CV_32FC1, err);
// build the image pyramids. // build the image pyramids.
...@@ -872,17 +905,22 @@ void cv::ocl::PyrLKOpticalFlow::sparse(const oclMat &prevImg, const oclMat &next ...@@ -872,17 +905,22 @@ void cv::ocl::PyrLKOpticalFlow::sparse(const oclMat &prevImg, const oclMat &next
for (int level = maxLevel; level >= 0; level--) for (int level = maxLevel; level >= 0; level--)
{ {
lkSparse_run(prevPyr_[level], nextPyr_[level], lkSparse_run(prevPyr_[level], nextPyr_[level],
prevPts, nextPts, status, level == 0 && err ? err : 0, getMinEigenVals, prevPts.cols, prevPts, nextPts, status, *err, getMinEigenVals, prevPts.cols,
level, /*block, */patch, winSize, iters); level, /*block, */patch, winSize, iters);
} }
clFinish(prevImg.clCxt->impl->clCmdQueue); clFinish(prevImg.clCxt->impl->clCmdQueue);
if(errMat)
delete err;
} }
void lkDense_run(oclMat &I, oclMat &J, oclMat &u, oclMat &v, void lkDense_run(oclMat &I, oclMat &J, oclMat &u, oclMat &v,
oclMat &prevU, oclMat &prevV, oclMat *err, Size winSize, int iters) oclMat &prevU, oclMat &prevV, oclMat *err, Size winSize, int iters)
{ {
Context *clCxt = I.clCxt; Context *clCxt = I.clCxt;
bool isImageSupported = clCxt->impl->devName.find("Intel(R) HD Graphics") == string::npos;
int elemCntPerRow = I.step / I.elemSize();
string kernelName = "lkDense"; string kernelName = "lkDense";
...@@ -901,8 +939,19 @@ void lkDense_run(oclMat &I, oclMat &J, oclMat &u, oclMat &v, ...@@ -901,8 +939,19 @@ void lkDense_run(oclMat &I, oclMat &J, oclMat &u, oclMat &v,
calcErr = false; calcErr = false;
} }
cl_mem ITex = bindTexture(I, I.depth(), cn); cl_mem ITex;
cl_mem JTex = bindTexture(J, J.depth(), cn); cl_mem JTex;
if (isImageSupported)
{
ITex = bindTexture(I, I.depth(), cn);
JTex = bindTexture(J, J.depth(), cn);
}
else
{
ITex = (cl_mem)I.data;
JTex = (cl_mem)J.data;
}
//int2 halfWin = {(winSize.width - 1) / 2, (winSize.height - 1) / 2}; //int2 halfWin = {(winSize.width - 1) / 2, (winSize.height - 1) / 2};
//const int patchWidth = 16 + 2 * halfWin.x; //const int patchWidth = 16 + 2 * halfWin.x;
...@@ -926,15 +975,27 @@ void lkDense_run(oclMat &I, oclMat &J, oclMat &u, oclMat &v, ...@@ -926,15 +975,27 @@ void lkDense_run(oclMat &I, oclMat &J, oclMat &u, oclMat &v,
args.push_back( make_pair( sizeof(cl_int), (void *)&I.cols )); args.push_back( make_pair( sizeof(cl_int), (void *)&I.cols ));
//args.push_back( make_pair( sizeof(cl_mem), (void *)&(*err).data )); //args.push_back( make_pair( sizeof(cl_mem), (void *)&(*err).data ));
//args.push_back( make_pair( sizeof(cl_int), (void *)&(*err).step )); //args.push_back( make_pair( sizeof(cl_int), (void *)&(*err).step ));
if (!isImageSupported)
{
args.push_back( make_pair( sizeof(cl_int), (void *)&elemCntPerRow ) );
}
args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.width )); args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.width ));
args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.height )); args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.height ));
args.push_back( make_pair( sizeof(cl_int), (void *)&iters )); args.push_back( make_pair( sizeof(cl_int), (void *)&iters ));
args.push_back( make_pair( sizeof(cl_char), (void *)&calcErr )); args.push_back( make_pair( sizeof(cl_char), (void *)&calcErr ));
if (isImageSupported)
{
openCLExecuteKernel2(clCxt, &pyrlk, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth(), CLFLUSH); openCLExecuteKernel2(clCxt, &pyrlk, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth(), CLFLUSH);
releaseTexture(ITex); releaseTexture(ITex);
releaseTexture(JTex); releaseTexture(JTex);
}
else
{
//printf("Warning: The image2d_t is not supported by the device. Using alternative method!\n");
openCLExecuteKernel2(clCxt, &pyrlk_no_image, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth(), CLFLUSH);
}
} }
void cv::ocl::PyrLKOpticalFlow::dense(const oclMat &prevImg, const oclMat &nextImg, oclMat &u, oclMat &v, oclMat *err) void cv::ocl::PyrLKOpticalFlow::dense(const oclMat &prevImg, const oclMat &nextImg, oclMat &u, oclMat &v, oclMat *err)
......
...@@ -118,9 +118,9 @@ TEST_P(Sparse, Mat) ...@@ -118,9 +118,9 @@ TEST_P(Sparse, Mat)
cv::Mat status_mat(1, d_status.cols, CV_8UC1, (void *)&status[0]); cv::Mat status_mat(1, d_status.cols, CV_8UC1, (void *)&status[0]);
d_status.download(status_mat); d_status.download(status_mat);
//std::vector<float> err(d_err.cols); std::vector<float> err(d_err.cols);
//cv::Mat err_mat(1, d_err.cols, CV_32FC1, (void*)&err[0]); cv::Mat err_mat(1, d_err.cols, CV_32FC1, (void*)&err[0]);
//d_err.download(err_mat); d_err.download(err_mat);
std::vector<cv::Point2f> nextPts_gold; std::vector<cv::Point2f> nextPts_gold;
std::vector<unsigned char> status_gold; std::vector<unsigned char> status_gold;
...@@ -153,9 +153,9 @@ TEST_P(Sparse, Mat) ...@@ -153,9 +153,9 @@ TEST_P(Sparse, Mat)
} }
} }
double bad_ratio = static_cast<double>(mistmatch) / (nextPts.size() * 2); double bad_ratio = static_cast<double>(mistmatch) / (nextPts.size());
ASSERT_LE(bad_ratio, 0.05f); ASSERT_LE(bad_ratio, 0.02f);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment