OpenCL 在创建缓冲区时出现分段错误

问题描述 投票:0回答:1

我正在尝试使用 opencl 将图像转换为灰度。但是当我尝试创建读缓冲区时。即使缓冲区大小完全在设备的内存限制之内,我也会遇到分段错误。我试过宽度 = 4000 和高度 = 6000 这是我的 opencl 函数。 段错误出现在这一行

cl::Buffer input_buffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(uchar) * width * height * 3, input, &err);
void executeOpenCL(uchar*** input, uchar** output, const int height, const int width)
{
    cl_int err;
    std::vector<cl::Platform> platforms;
    err = cl::Platform::get(&platforms);
 
    cl::Platform platform = platforms[0]; // Choose the first platform
    std::vector<cl::Device> devices;
    err = platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);

    cl::Device device = devices[0]; // Choose the first GPU device
    cl::Context context(device);
    cl::CommandQueue queue(context, device);

    // Create OpenCL buffers for input and output image data
    cl::Buffer input_buffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(uchar) * width * height * 3, input, &err);

    return;
}

我尝试检查缓冲区大小是否大于主机设备的内存,但即使在限制范围内。

输入定义为无符号 char3d 数组

    uchar*** array3D = new uchar** [height];
    for (int i = 0; i < height; ++i) {
        array3D[i] = new uchar* [width];
        for (int j = 0; j < width; ++j) {
            array3D[i][j] = new uchar[3]; // Assuming 3 channels (RGB)
        }
    }
c++ parallel-processing opencl
1个回答
0
投票

这是你的错误

uchar*** array3D = new uchar** [height];
for (int i = 0; i < height; ++i) {
    array3D[i] = new uchar* [width];
    for (int j = 0; j < width; ++j) {
        array3D[i][j] = new uchar[3]; // Assuming 3 channels (RGB)
    }
}

必须是连续的内存缓冲区

uchar* array3D = new uchar[width * height * 3];
© www.soinside.com 2019 - 2024. All rights reserved.