并行CUDA编程
CUDA programming in parallel
所以我已经看了很多次了,似乎都想不通。发生的情况是,我试图从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-这是主机内存
发生的情况是,您的内核由于无效的内存访问而崩溃。
请阅读此处有关错误检查的内容。
相关文章:
- 编译时未启用intel oneApi CUDA支持
- 在cuda线程之间共享大量常量数据
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- CUDA内核和数学函数的显式命名空间
- 模板元编程:如何将参数包组合成新的参数包
- CUDA:统一内存和指针地址的更改
- Qt Q串行端口未编程设备未关闭
- 使用推力的 ODE 求解器的 CUDA 编程
- CUDA 编程未处理的异常和堆栈溢出
- 与CPU程序相比,CUDA编程的计算精度不能具有相同的计算精度
- CUDA中的顺序编程
- 使用CUDA从2D图像编程3D模型
- CUDA 共享内存编程不起作用
- 并行CUDA编程
- 如何在Cuda编程中为矩阵做元素明智的指数
- CUDA:使用两倍的块(平铺?)进行编程
- 为CUDA编程计算设备和主机的执行时间