【发布时间】:2010-05-30 10:04:12
【问题描述】:
我正在尝试修改 CUDA SDK 中的 imageDenosing 类,我需要多次重复过滤器以捕获时间。但是我的代码不能正常工作。
//开始
__global__ void F1D(TColor *image,int imageW,int imageH, TColor *buffer)
{
const int ix = blockDim.x * blockIdx.x + threadIdx.x;
const int iy = blockDim.y * blockIdx.y + threadIdx.y;
if(iy != 0 && iy < imageH-1 && ix < imageW)
{
float4 fresult = get_color(image[imageW * iy + ix]);
float4 fresult4 = get_color(image[imageW * (iy+1) + ix]);
float4 fresult5 = get_color(image[imageW * (iy-1) + ix]);
float4 fresult7;
fresult7.x = fresult.x*0.5+fresult4.x*.25+fresult5.x*.25;
fresult7.y = fresult.y*0.5+fresult4.y*.25+fresult5.y*.25;
fresult7.z = fresult.z*0.5+fresult4.z*.25+fresult5.z*.25;
buffer[imageW * iy + ix] =
make_color(fresult7.x,fresult7.y,fresult7.z,0);
}
image[imageW * iy + ix] = buffer[imageW * iy + ix];
//should be use cudaMemcpy, But it fails
}
//外部
extern "C" void
cuda_F1D(TColor *dst, int imageW, int imageH)
{
dim3 threads(BLOCKDIM_X, BLOCKDIM_Y);
dim3 grid(iDivUp(imageW, BLOCKDIM_X), iDivUp(imageH, BLOCKDIM_Y));
Copy<<<grid, threads>>>(dst, imageW, imageH);
size_t size = imageW*imageH*sizeof(TColor);
TColor *host =(TColor*) malloc(size);
TColor *dst2;
//TColor *dst3;
//TColor *d = new TColor(imageW*imageH*sizeof(TColor));
dim3 threads2(imageW,1);
dim3 grid2(iDivUp(imageW, imageW), iDivUp(imageH, 1));
*for(int i = 0;i<1;i++)
{
cudaMalloc( (void **)&dst2, size);
cudaMemcpy(dst2, dst, imageW*imageH*sizeof(TColor),cudaMemcpyHostToDevice);
//cudaMalloc( (void **)&dst3, imageW*imageH*sizeof(TColor));
//cudaMemcpy(dst3, dst, imageW*imageH*sizeof(TColor),cudaMemcpyHostToDevice);
F1D<<<grid2, threads2>>>(dst, imageW, imageH,dst2);
//cudaMemcpy(dst, dst3, imageW*imageH*sizeof(TColor),cudaMemcpyDeviceToHost);
cudaFree(dst2);
}*
}
此代码有效,但无法同步图像数组。并导致许多同步问题
【问题讨论】:
-
大概 dst 在其他地方也是 cudaMalloc 的?一个想法供将来参考,也许您打算让“i”循环执行更多迭代 - 您应该避免在该循环内使用 cudaMalloc 和 cudaFree 并且只执行一次。将它们放在性能路径上并不是一个好主意,它们不是超快的。如果您的算法允许,您也可以考虑使用 async memcpys。
-
@kitw:点击上面的编辑按钮来修正你的问题中的错别字等
-
您丢失了该帐户?这是同一个帐户。小猫。
标签: cuda