所以我已经看了很多次了,似乎都想不通。发生的情况是,我试图从GPU内存复制到CPU内存的变量似乎总是空白的。
根据我的理解,我应该有一个或多个变量,并创建这些变量的副本,我将把这些副本连同一些数据一起发送给GPU进行计算,一旦计算完成,就回来把GPU中变量的内容插入CPU中。
但每次我这样做时,我的变量"d_result"总是空的。如果有人对如何解决这个问题有想法,我们将不胜感激。
我的CUDA功能:
__global__ void gpu_histogram_equalization(unsigned char * img_out, unsigned char * img_in,
int * hist_in, int img_size, int nbr_bin){
int *lut = (int *)malloc(sizeof(int)*nbr_bin);
int i, cdf, min, d;
/* Construct the LUT by calculating the CDF */
cdf = 0;
min = 0;
i = threadIdx.x;
while(min == 0){
min = hist_in[i++];
}
d = img_size - min;
if(i < nbr_bin){
cdf += hist_in[i];
//lut[i] = (cdf - min)*(nbr_bin - 1)/d;
lut[i] = (int)(((float)cdf - min)*255/d + 0.5);
if(lut[i] < 0){
lut[i] = 0;
}
}
/* Get the result image */
if(i < img_size){
if(lut[img_in[i]] > 255){
img_out[i] = 255;
}
else{
img_out[i] = (unsigned char)lut[img_in[i]];
}
}
}
然后我的函数调用它:
PGM_IMG gpu_contrast_enhancement_g(PGM_IMG img_in)
{
PGM_IMG result;
int hist[256];
unsigned char * d_result;
result.w = img_in.w;
result.h = img_in.h;
result.img = (unsigned char *)malloc(result.w * result.h * sizeof(unsigned char));
cudaMalloc(&d_result, result.w * result.h * sizeof(unsigned char));
cudaMemcpy(d_result, result.img, result.w * result.h * sizeof(unsigned char), cudaMemcpyHostToDevice);
histogram(hist, img_in.img, img_in.h * img_in.w, 256);
gpu_histogram_equalization<<<1,result.w * result.h * sizeof(unsigned char)>>>(d_result,img_in.img,hist,result.w*result.h, 256);
cudaMemcpy(result.img, d_result, result.w * result.h * sizeof(unsigned char), cudaMemcpyDeviceToHost);
cudaFree(d_result);
return result;
}
让我们看看这一行:
gpu_histogram_equalization<<<1,result.w*result.h*sizeof(unsigned char)>>>
(d_result,img_in.img,hist,result.w*result.h, 256);
以下是您遇到的一些问题:
- img_in.img-这是主机内存
- hist-这是主机内存
发生的情况是,您的内核由于无效的内存访问而崩溃。
请阅读此处有关错误检查的内容。