Cool_Coder
Cool_Coder

Reputation: 5083

OpenCL crashing while dynamic linking?

I am trying to load OpenCL library at run time so that the same exe can run on platforms which do not have OpenCL drivers without finding unresolved symbols. I am using Qt to do this but I dont think I am facing my problem due to Qt. Here is my function which checks if OpenCL 1.1 is installed or not:

QLibrary *MyOpenCL::openCLLibrary = NULL;

bool MyOpenCL::loadOpenCL()
{
    if(openCLLibrary)
        return  true;

    QLibrary *lib = new QLibrary("OpenCL");
    if(!lib->load())
        return false;

    bool result = false;
    typedef cl_int (*MyPlatorms)(cl_uint, cl_platform_id *, cl_uint *);
    MyPlatorms pobj = (MyPlatorms) lib->resolve("clGetPlatformIDs");
    if(pobj)
    {
        cl_uint nplatforms = 0;
        cl_uint myerr = pobj(0, NULL, &nplatforms);
        if((myerr == CL_SUCCESS) && (nplatforms > 0))
        {
            cl_platform_id *mplatforms = new cl_platform_id[nplatforms];
            myerr = pobj(nplatforms, mplatforms, NULL);

            typedef cl_int (*MyPlatformInfo)(cl_platform_id, cl_platform_info, size_t, void *, size_t *);
            MyPlatformInfo pinfoobj = (MyPlatformInfo) lib->resolve("clGetPlatformInfo");
            if(pinfoobj)
            {
                size_t size;
                for(unsigned int i = 0; i < nplatforms; i++)
                {
                    size = 0;
                    myerr = pinfoobj(mplatforms[i], CL_PLATFORM_VERSION, 0, NULL, &size);//size = 27
                    if(size < 1)
                        continue;

                    char *ver = new char[size];
                    myerr = pinfoobj(mplatforms[i], CL_PLATFORM_VERSION, size, ver, NULL);
                    qDebug() << endl << ver;//segmentation fault at this line
...
}

As can be seen Qt successfully resolved clGetPlatformIDs(). It even showed that there is 1 platform available. But when I pass the array to store the cl_platform_id, it crashes. Why is this happening?

EDIT: I am using Qt 4.8.1 with MinGW compiler using OpenCL APP SDK 2.9. I am using the OpenCL 1.1 header from Khronos website. My laptop which has Windows7 64 bit also has ATI Radeon 7670m GPU which has OpenCL 1.1 drivers.

Upvotes: 1

Views: 289

Answers (1)

Dithermaster
Dithermaster

Reputation: 6343

The first parameter to clGetPlatformIDs is the number of elements the driver is allowed to write to the array pointed to by the second element.

If the first call, you are passing INT_MAX and NULL for these. I'd expect a crash here because you are telling the driver to go ahead and write through your NULL pointer.

You should pass 0 for the first parameter since all you are interested in is the returned third parameter value.

In the second call you at least pass valid memory for the second parameter, but you again pass INT_MAX. Here you should pass nplatforms since that is how much memory you allocated. For the third parameter, pass NULL since you don't need the return value (again).

Upvotes: 2

Related Questions