【发布时间】:2016-07-20 12:34:23
【问题描述】:
我正在从事一个小型图像处理项目。我想运行一个进行图像减法的 CUDA 程序。所以你有图像背景和具有相同背景但上面有其他东西的图像。一旦你减去图像,你就会得到剩下的东西。这两个图像的大小都是 480*360,我的 gpu 是 GTX780。我的程序抛出错误./main': free(): invalid next size (normal): 0x000000000126bd70 ***
Aborted (core dumped) 并且输出图像错误。我一直在努力解决这个问题。代码如下:
内核:
__global__ void add(unsigned char* a, unsigned char* b, unsigned char* c, int numCols, int numWidth) {
int i = blockIdx.x * blockDim.x + threadIdx.x; //Column
int j = blockIdx.y * blockDim.y + threadIdx.y; //Row
if(i < numWidth && j < numCols)
{
int idx = j * numCols + i;
c[idx] = b[idx] - a[idx];
}
}
及主要功能:
int main() {
CImg<unsigned char> img1("1.bmp");
CImg<unsigned char> img2("2.bmp");
//both images have the same size
int width = img1.width();
int height = img1.height();
int size = width * height * 3; //both images of same size
dim3 blockSize(16, 16, 1);
dim3 gridSize((width + blockSize.x - 1) / blockSize.x, (height + blockSize.y - 1) / blockSize.y, 1);
unsigned char *dev_a, *dev_b, *dev_c;
cudaMalloc((void**)&dev_a, size * (sizeof(unsigned char)));
cudaMalloc((void**)&dev_b, size * (sizeof(unsigned char)));
cudaMalloc((void**)&dev_c, size * (sizeof(unsigned char)));
cudaMemcpy(dev_a, img1, size * (sizeof(unsigned char)), cudaMemcpyHostToDevice);
cudaMemcpy(dev_b, img2, size * (sizeof(unsigned char)), cudaMemcpyHostToDevice);
add<<<gridSize, blockSize>>>(dev_a, dev_b, dev_c, height, width);
cudaMemcpy(img2, dev_c, size * (sizeof(unsigned char)), cudaMemcpyDeviceToHost);
img2.save("out.bmp");
cudaFree(dev_a);
cudaFree(dev_b);
cudaFree(dev_c);
return 0;
}
图片使用CImg 库加载。
【问题讨论】:
-
int idx = j * numCols + i;前面带有j<numCols的行看起来很可疑。如果j是您的列索引,您应该使用int idx = i * numCols + j;。 -
@mman 没有它的设备。如果它在主机上,它将是普通的 malloc 或 new。问题出在字节上,我认为以某种方式分配的字节更少
-
这不会使您的程序崩溃,但是您定位的内存是所需内存的 3 倍,或者您只计算了三分之一(您正在使用
size = width * height * 3,然后将宽度和高度作为 numWidth 和 numHeight .) -
img1 的大小是多少?我的意思是 sizeof ?
-
您不应该在
cudaMemcpy操作中使用img1.data()而不是仅使用img1和img2.data()而不是仅使用img2吗?那appears to be是如何获取指向底层数据的指针的:“这个内存缓冲区的地址可以通过函数CImg::data()来获取。”
标签: c++ image-processing cuda cimg