OpenCL 图像读取不适用于动态分配的数组

OpenCL Image reading not working on dynamically allocated array

我正在编写一个在图像上应用卷积矩阵的 OpenCL 程序。如果我将所有像素存储在一个数组 image[height*width][4] 上,一切都会正常(第 65 行,注释)(抱歉,我说西班牙语,而且我主要用西班牙语编写代码)。但是,由于我正在处理的图像非常大,我需要动态分配内存。我执行代码,但出现 Segmentation fault 错误。

经过一些可怜人的调试,我发现问题出现在执行内核并将输出图像读回主机,将数据存储到动态分配的数组中之后。我只是无法在不收到错误的情况下访问数组的数据。

我认为问题在于 clEnqueueReadImage 函数(第 316 行)将图像数据写入 image 数组的方式。这个数组是动态分配的,所以它没有预定义 "structure".

但我需要一个解决方案,但我找不到,无论是在我自己还是在 Internet 上。

C程序和OpenCL内核在这里: https://gist.github.com/MigAGH/6dd0fddfa09f5aabe7eb0c2934e58cbe

不要使用指向指针的指针 (unsigned char**)。改为使用常规指针:

unsigned char* image = (unsigned char*)malloc(sizeof(unsigned char)*ancho*alto*4);

然后在for循环中:

for(i=0; i<ancho*alto; i++){
    unsigned char* pixel = (unsigned char*)malloc(sizeof(unsigned char)*4);
    fread (pixel, 4, 1, bmp_entrada);
    image[i*4] = pixel[0];
    image[i*4+1] = pixel[1];
    image[i*4+2] = pixel[2];
    image[i*4+3] = pixel[3];
    free(pixel);
}