2

I'm learning OpenCL and trying to apply a black and white on a picture but enqueueNDRangeKernel return CL_OUT_OF_RESOURCES and I don't understand why. OpenCL is running on a GTX 980M, and OpenCL 1.2 .

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <CL/cl.hpp>


#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_write.h"

int main() {

    unsigned int vectorSize = 1000;
    const std::string progCode = "__kernel                                   \n"
                            "void img_kernel( __read_only image2d_t inputImage, __write_only image2d_t outputImage) \n"
                            "{                                                                                             \n"
                            "const sampler_t sampler=CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST; \n"
                            "    int width = get_image_width(inputImage);                                                      \n"
                            "    int height = get_image_height(inputImage);                                                    \n"
                            "    int2 pixelcoord = (int2) (get_global_id(0), get_global_id(1));                            \n"
                            "    float4 pixel = read_imagef(inputImage, sampler, pixelcoord);                         \n"
                            "    float color = (pixel.x + pixel.y + pixel.z)/3;                                            \n"
                            "    float4 outColor = (float4)(pixel.x,pixel.y,pixel.z, 1.0);                                    \n" 
                            "    write_imagef(outputImage, pixelcoord, outColor);          }                                     \n";
    int imageX, imageY, imageN;
    unsigned char *dataImage = stbi_load("test.jpg", &imageX, &imageY, &imageN, 3);
    if (dataImage == nullptr)
    {
        std::cout << "Unable to load picture" << std::endl;
        getchar();
        return 1;
    }

    cl_int  error;

    std::vector<cl::Platform> platformsList;
    error = cl::Platform::get(&platformsList);
    if (error != CL_SUCCESS) 
    {
        std::cout << "Unable to find any OpenCL platforms" << std::endl;
        getchar();
        return 1;
    }


    std::vector<cl::Device> devicesList;
    error = platformsList[0].getDevices(CL_DEVICE_TYPE_DEFAULT, &devicesList);
    if (error != CL_SUCCESS) 
    {
        std::cout << "Unable to find any OpenCL device" << std::endl;
        getchar();
        return 1;
    }

    cl::Device currentDevice = devicesList[0];
    std::string nameDevice, driverDevice;
    error = currentDevice.getInfo(CL_DEVICE_NAME, &nameDevice);
    error = currentDevice.getInfo(CL_DRIVER_VERSION, &driverDevice);
    std::cout << "Device : " << nameDevice << "   " << driverDevice << std::endl;;

    cl::Context context(currentDevice);

    cl::Program::Sources source;
    source.push_back({ progCode.c_str(), progCode.size() });
    cl::Program program(context, source);
    if (program.build({ currentDevice }) != CL_SUCCESS)
    {
        std::cout << " Error building: " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(currentDevice) << "\n";
        getchar();
        exit(1);
    }

    cl::ImageFormat globalImgFormat; 
    globalImgFormat.image_channel_data_type = CL_UNSIGNED_INT8;
    globalImgFormat.image_channel_order = CL_RGB;

    cl::size_t<3> origin;
    origin[0] = 0; origin[1] = 0, origin[2] = 0;
    cl::size_t<3> region;
    region[0] = imageX; region[1] = imageY; region[2] = 1;



    cl::Image2D inputImage(context, CL_MEM_READ_ONLY, globalImgFormat, imageX, imageY, 0, dataImage, &error);
    if (error != CL_SUCCESS) 
    {
        std::cout << "Unable to create cl Image for input." << std::endl;
        getchar();
        return 1;
    }

     cl::Image2D outputImage(context, CL_MEM_WRITE_ONLY, globalImgFormat, imageX, imageY, 0, nullptr, &error);
     if (error != CL_SUCCESS)
    {
        std::cout << "Unable to create cl Image for output." << std::endl;
        getchar();
        return 1;
    }

    cl::CommandQueue queue(context, currentDevice);


    cl::Kernel image_kernel(program, "img_kernel", &error);
    if (error != CL_SUCCESS)
    {
        std::cout << "Unable to create kernel." << std::endl;
        getchar();
        return 1;
    }

    error = image_kernel.setArg(0, inputImage);
    if (error != CL_SUCCESS)
    {
        std::cout << "Unable to set param." << std::endl;
        getchar();
        return 1;
    }
    error = image_kernel.setArg(1, outputImage);
    if (error != CL_SUCCESS)
    {
        std::cout << "Unable to set param." << std::endl;
        getchar();
        return 1;
    } 

    cl::NDRange globalSize(imageX, imageY);


    error = queue.enqueueNDRangeKernel(image_kernel, cl::NullRange, globalSize, cl::NullRange);
    if (error != CL_SUCCESS)
    {
        std::cout << "Unable to compute Image data." << std::endl;
        getchar();
        return 1;
    }
    queue.finish(); 



    unsigned char *resultPros = new unsigned char[imageX * imageY * imageN];
    error = queue.enqueueReadImage(outputImage, CL_TRUE, origin, region, 0, 0, resultPros);
    stbi_write_bmp("testresul.jpg", imageX, imageY, imageN, resultPros);


    stbi_image_free(dataImage);
    stbi_image_free(resultPros);
    getchar();
    return 0;
}
Soro
  • 21
  • 2
  • Are you sure that values for `imageX` and `imageY` are initialized correctly? Btw, nVidia OpenCL implementation is full of bugs which they do not fix on purpose because they want people to use CUDA. This is why nothing should surprise you ... – Marko Popovic Jan 22 '16 at 13:25

2 Answers2

1

On NVIDIA hardware if you write outside of a buffer or image (which is an undefined operation) then CL_OUT_OF_RESOURCES is a common error to get. It's better than the early hardware or drivers that simply crashed! Double-check your writes.

Dithermaster
  • 6,223
  • 1
  • 12
  • 20
0

It seems that the combination of image_channel_data_type and image_channel_order is not correct. Could be your problem related with this?

Please, take a look here: https://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/cl_image_format.html

Regards

Moises Viñas
  • 1,073
  • 2
  • 7
  • 9