【发布时间】:2015-10-05 20:28:37
【问题描述】:
我正在尝试完成 udacity“并行编程入门”课程的作业,但我被困在第二个作业中,该作业基本上是使用 CUDA 对图像应用高斯模糊蒙版。 我想通过利用共享内存来有效地做到这一点。 我解决“边界像素问题”的想法是启动比块中实际像素数更多的线程:例如,如果我将输入图像划分为 16x16 大小的活动像素块,并且我有一个 9x9 大小的掩码,那么我的实际块尺寸将是(对于 x 和 y):16+2*(9/2) = 24。这样我在一个块中启动 24 个线程,以便“外部”线程将仅用于将像素从输入 img 加载到共享内存中,而“内部”线程对应于实际执行计算的活动像素(此外还缓存在共享内存中)。
由于某种原因它不起作用。正如您从附加代码中看到的那样,我能够将像素缓存到共享内存中,但是在计算过程中出现了严重错误,并且我附上了一张我得到的糟糕结果的图像。
__global__ void gaussian_blur(const unsigned char* const inputChannel,
unsigned char* const outputChannel,
int numRows, int numCols,
const float* const filter, const int filterWidth)
{
int filter_radius = (int)(filterWidth / 2); //getting the filter "radius"
int x = blockDim.x*blockIdx.x+threadIdx.x;
int y = blockDim.y*blockIdx.y+threadIdx.y;
if(x>=(numCols+filter_radius) || y>=(numRows+filter_radius))
return;
int px = x-filter_radius;
int py = y-filter_radius;
//clamping
if(px<0) px = 0;
if(py<0) py = 0;
//if(px>=numCols) px = numCols-1;
// if(py>=numRows) py = numRows-1;
__shared__ unsigned char tile[(16+8)*(16+8)]; //16 active pixels + 2*filter_radius
tile[threadIdx.y*24+threadIdx.x] = inputChannel[py*numCols+px];
__syncthreads();
//Here everything is working fine: if I do
// outputChannel[py*numCols+px] = tile[threadIdx.y*24+threadIdx.x];
//then I am able to see the perfect reconstruction of the input image.
//caching the filter
__shared__ float t_filter[81]; //9x9 conv mask
if(threadIdx.x==0 && threadIdx.y==0)
{
for(int i=0; i<81; i++)
t_filter[i] = filter[i];
}
__syncthreads();
//I am checking the threadIdx of the threads and I am performing the mask computation
//only to those threads that are pointing to active pixels:
//i.e. all the threads whose id is greater or equal to the filter radius,
//but smaller than the whole block of active pixels will perform the computation.
//filter_radius = filterWidth/2 = 9/2 = 4
//blockDim.x or y = 16 + filterWidth*2 = 16+8 = 24
//active pixel index limit = filter_radius+16 = 4+16 = 20
//is that correct?
if(
threadIdx.y>=filter_radius && threadIdx.x>=filter_radius &&
threadIdx.x < 20 && threadIdx.y < 20
)
{
float value = 0.0;
for(int i=-filter_radius; i<=filter_radius; i++)
for(int j=-filter_radius; j<=filter_radius; j++)
{
int fx = i+filter_radius;
int fy = j+filter_radius;
int ty = threadIdx.y+i;
int tx = threadIdx.x+j;
value += ((float)tile[ty*24+tx])*t_filter[fy*filterWidth+fx];
}
outputChannel[py*numCols+px] = (unsigned char) value;
}
输出图片:http://i.stack.imgur.com/EMu5M.png
编辑:添加内核调用:
int filter_radius = (int) (filterWidth / 2);
blockSize.x = 16 + 2*filter_radius;
blockSize.y = 16 + 2*filter_radius;
gridSize.x = numCols/16+1;
gridSize.y = numRows/16+1;
printf("\n grx %d gry %d \n", blockSize.x, blockSize.y );
gaussian_blur<<<gridSize, blockSize>>>(d_red, d_redBlurred, numRows,numCols, d_filter, filterWidth);
gaussian_blur<<<gridSize, blockSize>>>(d_green, d_greenBlurred, numRows,numCols, d_filter, filterWidth);
gaussian_blur<<<gridSize, blockSize>>>(d_blue, d_blueBlurred, numRows,numCols, d_filter, filterWidth);
cudaDeviceSynchronize(); checkCudaErrors(cudaGetLastError());
blockSize.x = 32; gridSize.x = numCols/32+1;
blockSize.y = 32; gridSize.y = numRows/32+1;
// Now we recombine your results. We take care of launching this kernel for you.
//
// NOTE: This kernel launch depends on the gridSize and blockSize variables,
// which you must set yourself.
recombineChannels<<<gridSize, blockSize>>>(d_redBlurred,
d_greenBlurred,
d_blueBlurred,
d_outputImageRGBA,
numRows,
numCols);
cudaDeviceSynchronize(); checkCudaErrors(cudaGetLastError());
编辑之二:
为了编译和运行所有其他必要的代码可以在这里找到: https://github.com/udacity/cs344/tree/master/Problem%20Sets/Problem%20Set%202 并且上面的内核应该编码在student_func.cu文件中。
【问题讨论】:
-
来自here:"寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及重现它所需的最短代码问题本身。没有明确问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable example(MCVE)。 CUDA 内核本身不是 MCVE。最好,您的 MCVE 应该是独立的,并且不应该需要 OpenCV 或其他框架,或单独的数据文件。
-
那么很抱歉,因为我在这里浏览了一些 CUDA 问题,但没有一个问题显示全部内容。其中一些可能会显示内核调用本身,但我很确定在处理图像时它们都没有提供自己的读取和输出图像文件的函数,因此要避免使用 OpenCV 或其他框架。我正在添加内核调用并发布指向编译所需的其他文件的链接。我认为应该足够了。至于这段代码应该做什么,我认为解释得很好。
标签: parallel-processing cuda convolution