什么';这个简单的cuda程序有什么问题?

什么';这个简单的cuda程序有什么问题?,cuda,Cuda,我只是想用CUDA来清空图像。但是“之前”和“之后”我得到了相同的原始图像。我想不出这个问题 sumKernel.cu: #include "sumKernel.h" __global__ void _sumKernel(char *image, int width, int height, char *kernel, int kerwidth, int kerheight) { int idx = blockIdx.x * blockDim.x + threadIdx.x;

我只是想用CUDA来清空图像。但是“之前”和“之后”我得到了相同的原始图像。我想不出这个问题

sumKernel.cu:

#include "sumKernel.h"

__global__ void _sumKernel(char *image, int width, int height, char *kernel, int kerwidth, int kerheight) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    image[idx] = 0;
}

void sumKernel(char *image, int width, int height, char *kernel, int kerwidth, int kerheight) {
    dim3 blocks(1);
    dim3 threads(width*height);
    _sumKernel<<<blocks, threads>>>(image, width, height, kernel, kerwidth, kerheight);
}
main.cpp:

findCudaDevice(argc, (const char **)argv);

Mat image = imread("a.jpg");
Mat gray; cvtColor(image, gray, CV_BGR2GRAY);
imshow("before", gray);

char *gray_g;
cudaMalloc((void **)&gray_g, gray.size().area());
cudaMemcpy(gray_g, gray.data, sizeof(char)*gray.size().area(), cudaMemcpyHostToDevice);

char kernel[9];
char *kernel_g;
cudaMalloc((void **)&kernel_g, sizeof(char)*9);

sumKernel(gray_g, gray.cols, gray.rows, kernel, 3, 3);

cudaMemcpy(gray.data, gray_g, sizeof(char)*gray.size().area(), cudaMemcpyDeviceToHost);

imshow("after", gray);
waitKey(0);

cudaFree(kernel);
cudaFree(gray_g);
(发布我的评论作为回答)


请检查所有API调用是否有错误。最可能的情况是,宽度*高度线程的块大小对于单个块来说太大。

请检查所有API调用是否有错误。很可能是宽度*高度线程的块大小对于单个块来说太大了。谢谢,这就是问题所在。@seilgu:请写一个包含解决方案/分辨率的简短答案,并将其作为答案发布(这是可以的)。以后你就可以接受了。这将从未回答的问题列表中删除该问题,并通过搜索下一个可能有类似问题的人更容易找到解决方案。
findCudaDevice(argc, (const char **)argv);

Mat image = imread("a.jpg");
Mat gray; cvtColor(image, gray, CV_BGR2GRAY);
imshow("before", gray);

char *gray_g;
cudaMalloc((void **)&gray_g, gray.size().area());
cudaMemcpy(gray_g, gray.data, sizeof(char)*gray.size().area(), cudaMemcpyHostToDevice);

char kernel[9];
char *kernel_g;
cudaMalloc((void **)&kernel_g, sizeof(char)*9);

sumKernel(gray_g, gray.cols, gray.rows, kernel, 3, 3);

cudaMemcpy(gray.data, gray_g, sizeof(char)*gray.size().area(), cudaMemcpyDeviceToHost);

imshow("after", gray);
waitKey(0);

cudaFree(kernel);
cudaFree(gray_g);