0

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:

  • Intel Core i5 2500 CPU
  • NVIDIA Geforce 210 GPU
  • OS: Windows 7
  • AMD APP SDK 3.0 Beta
  • IDE: Eclipse with gdb

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

Tobias Off
  • 73
  • 1
  • 8
  • A small but unrelated note: The [`std::endl`](http://en.cppreference.com/w/cpp/io/manip/endl) [stream manipulator](http://en.cppreference.com/w/cpp/io/manip) not only writes a newline, it *also* flushes the stream. So no need for an explicit flush. – Some programmer dude Apr 09 '15 at 12:03
  • More related to your problem, how do you use/call this `initialize` function? Can you please show us a little more context? – Some programmer dude Apr 09 '15 at 12:06
  • I added the calling functions for the `initialize()` function. If you need some more information I must check which parts of the code I can publicate, because it is a closed source project. – Tobias Off Apr 09 '15 at 14:20
  • I know that `std::endl` causes an flush. The code survived from another code where I had no `std::endl`. – Tobias Off Apr 09 '15 at 14:23
  • Can you also show the code for getting `someValidPlartformId`? – jprice Apr 09 '15 at 15:17
  • I added the function which queries me the platform ids. It is almost as it is described in the Khronos API. The `cl_platform_id`'s are stored in the single `OCLPlatform` Objects. I wonder why the function does not throw me an error code through errNum but dies in a SEGFAULT. – Tobias Off Apr 13 '15 at 12:25

1 Answers1

0

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.

Tobias Off
  • 73
  • 1
  • 8