【发布时间】:2015-11-23 23:38:49
【问题描述】:
我正在尝试在 OpenCL 中实现圆的霍夫变换,但我遇到了非常奇怪的问题。每次我运行 Hough 内核时,我都会得到稍微不同的累加器,即使参数相同并且累加器总是一个新的归零表(例如http://imgur.com/a/VcIw1)。我的内核代码如下:
#define BLOCK_LEN 256
__kernel void HoughCirclesKernel(
__global int* A,
__global int* imgData,
__global int* _width,
__global int* _height,
__global int* r
)
{
__local int imgBuff[BLOCK_LEN];
int localThreadIndex = get_local_id(0); //threadIdx.x
int globalThreadIndex = get_local_id(0) + get_group_id(0) * BLOCK_LEN; //threadIdx.x + blockIdx.x * Block_Len
int width = *_width; int height = *_height;
int radius = *r;
A[globalThreadIndex] = 0;
barrier(CLK_GLOBAL_MEM_FENCE);
if(globalThreadIndex < width*height)
{
imgBuff[localThreadIndex] = imgData[globalThreadIndex];
barrier(CLK_LOCAL_MEM_FENCE);
if(imgBuff[localThreadIndex] > 0)
{
float s1, c1;
for(int i = 0; i<180; i++)
{
s1 = sincos(i, &c1);
int centerX = globalThreadIndex % width + radius * c1;
int centerY = ((globalThreadIndex - centerX) / height) + radius * s1;
if(centerX < width && centerY < height)
atomic_inc(A + centerX + centerY * width);
}
}
}
barrier(CLK_GLOBAL_MEM_FENCE);
}
这可能是我如何增加累加器的错吗?
【问题讨论】:
-
请发布一个可运行的示例来重现您的错误。我写一个的尝试正常运行并且每次都产生相同的结果,但是,当然,在你没有向我们展示的部分中可能会发生一些事情。顺便说一句,您可以将
height、width和r作为标量传递,无需使用 1 元素数组。 -
P.S.如果可能,请在另一台设备上测试您的程序。此外,here's an attempt at reproduction 使用 Python 与
numpy、matplotlib和pyopencl。 -
我已经在这里上传了整个解决方案speedy.sh/m5ZXn/Hough.7z 这似乎很奇怪,因为我的另一个项目是不规则霍夫变换似乎工作得很好,圆圈是基于它的。
-
很遗憾,我没有VS来编译它,但是程序看起来不错。也许有 Windows 的人可以提供更多帮助。尝试的事情:在不同的设备上运行;尝试运行我的代码(上图)。检查编译器/驱动程序问题:将标量作为标量而不是数组传递;预先用零填充
A,而不是在内核中;删除本地内存使用(这里不需要);并行化累加器单元,而不是图像像素(这种方式不需要atomic_inc)。
标签: opencl gpgpu hough-transform