c++openclgpgpuopencl.net

clCreateContextFromType ends up in a SEGFAULT while execution


I am trying to create an OpenCL context on the platform which is containing my graphics card. But when I call clCreateContextFromType() a SEGFAULT is thrown.

int main(int argc, char** argv)
{
    /*
    ...
    */
    cl_platform_id* someValidPlatformId;
    //creating heap space using malloc to store all platform ids
    getCLPlatforms(someValidPlatformId);
    //error handling for getCLPLatforms()

    //OCLPlatform(cl_platform_id platform)
    OCLPLatform platform = OCLPlatform(someValidPlatformId[0]);
    //OCLContext::OCL_GPU_DEVICE == CL_DEVICE_TYPE_GPU
    OCLContext context = OCLContext(platform,OCLContext::OCL_GPU_DEVICE);

    /*
    ...
    */
}

cl_platform_id* getCLPlatforms(cl_platform_id* platforms)
{
    cl_int errNum;
    cl_uint numPlatforms;

    numPlatforms = (cl_uint) getCLPlatformsCount(); //returns the platform count 
                                                    //using clGetPlatformIDs() 
                                                    //as described in the Khronos API
    if(numPlatforms == 0)
        return NULL;

    errNum = clGetPlatformIDs(numPlatforms,platforms,NULL);
    if(errNum != CL_SUCCESS)
        return NULL;

    return platforms;
}

OCLContext::OCLContext(OCLPlatform platform,unsigned int type)
{
    this->initialize(platform,type);
}

void OCLContext::initialize(OCLPlatform platform,unsigned int type)
{
    cl_int errNum;

    cl_context_properties contextProperties[] =
    {
        CL_CONTEXT_PLATFORM,
        (cl_context_properties)platform.getPlatformId(),
        0
    };

    cout << "a" << endl;std::flush(cout);

    this->context = clCreateContextFromType(contextProperties,
                                           (cl_device_type)type,
                                           &pfn_notify,
                                           NULL,&errNum);
    if(errNum != CL_SUCCESS)
        throw OCLContextException();

    cout << "b" << endl;std::flush(cout);
    /*
    ...
    */
}

The given type is CL_DEVICE_TYPE_GPU and also the platform contained by the cl_context_properties array is valid.

To debug the error I implemented the following pfn_notify() function described by the Khronos API:

static void pfn_notify(const char* errinfo, 
                       const void* private_info,
                       size_t cb, void* user_data)
{
    fprintf(stderr, "OpenCL Error (via pfn_notify): %s\n", errinfo);
    flush(cout);
}

Here is the ouput schown by the shell:

$ ./OpenCLFramework.exe
a
Segmentation fault

The machine i am working with has the following properties:

It would be great if somebody knew an answer to this problem.


Solution

  • The problem seems to be solved now.

    Injecting the a valid cl_platform_id throught gdb solved the SEGFAULT. So I digged a little bit deeper and the issue for the error was that I saved the value as a standard primitive. When I called a function with this value casted to cl_platform_id some functions failed handling that. So it looks like it is a mingling of types what lead to this failure.

    Now I save the value as cl_platform_id and cast it to an primitive when needed and not vice versa.

    I thank you for your answers and apologize for the long radio silence for my part.