c++parallel-processingopencl

OpenCL giving segmentation fault on creating buffer


I am trying to convert an image to greyscale using opencl. But when i am attempting to create the readbuffer. i am getting a segmentation fault even though the buffer size is well within the memory limits of the device. i tried on width = 4000 and height = 6000 This is my opencl function. The segfault appears on this line

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;
}

I tried to check whether the buffer size is greater than the memory of the host device but even if that is within the limit.

Input as defined as an unsigned char3d array

    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)
        }
    }

Solution

  • Here is your mistake

    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)
        }
    }
    

    It must be a continuous memory buffer

    uchar* array3D = new uchar[width * height * 3];