getting number of CL-GL interop devices creates sigsegv

Hi,

I’m on kUbuntu 11.10 64bit, I downloaded & installed the latest catalyst and app sdk. I tried to reproduce the functionality of the SimpleGL sample app that came with the sdk. However when I try to query the number of CL-GL interop capable devices, my app crashes with sigsegv. In the frame stack the last call from my app is line 71, clGetGLContextInfoKHR (see code), and there’s one more from OpenCL.so. I could run SimpleGL, so I assume I’m doing something wrong… I use SFML for windowing which creates a glX context. I can get the context and everything seems to work until I query for the CL-GL devices.

so here’s the code:

compute_context.h


#ifndef compute_context_h
#define compute_context_h

#include "common.h"

#include "assert.h"

struct compute_platform_description
{
    cl_platform_info info;
    std::string desc;
};

struct compute_device_description
{
    cl_device_info info;
    std::string desc;
};

typedef CL_API_ENTRY cl_int
(CL_API_CALL *clGetGLContextInfoKHR_fn)(const cl_context_properties *properties,
                                        cl_gl_context_info param_name,
                                        size_t param_value_size,
                                        void *param_value,
                                        size_t *param_value_size_ret);

#define clGetGLContextInfoKHR clGetGLContextInfoKHR_proc
static clGetGLContextInfoKHR_fn clGetGLContextInfoKHR;

class compute_context
{
private:
    cl_int error;
protected:

public:
    void init();

    cl_platform_id the_platform;
    cl_device_id the_device;
    cl_context the_context;
    cl_command_queue the_command_queue;
    compute_context() {};
};

#endif

compute_context.cpp


#include "compute_context.h"
#include "objs.h"

void compute_context::init()
{
    std::cout << "
Initializing OpenCL...
";

    GLXContext the_glx_context = glXGetCurrentContext();

    if (the_glx_context == 0)
    {
        std::cerr << "Error getting the current glX context.
";
        objs::get()->conf.the_window.Close();
        exit(1);
    }

    cl_int error = CL_SUCCESS;
    cl_context_properties the_context_properties[] =
    {
        CL_CONTEXT_PLATFORM,
        (cl_context_properties)the_platform,
        CL_GLX_DISPLAY_KHR,
        (intptr_t)glXGetCurrentDisplay(),
        CL_GL_CONTEXT_KHR,
        (intptr_t)the_glx_context,
        0
    };

    if (!clGetGLContextInfoKHR)
    {
        clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn)clGetExtensionFunctionAddress("clGetGLContextInfoKHR");
        if (!clGetGLContextInfoKHR)
        {
            std::cerr << "Error getting clGetGLContextInfoKHR function ptr.
";
            objs::get()->conf.the_window.Close();
            exit(1);
        }
    }

    cl_uint num_platfroms;

    error = clGetPlatformIDs(1, &the_platform, &num_platfroms);
    assert(error == CL_SUCCESS);

    std::cout << "Number of platforms: " << num_platfroms << "
";

    std::vector< compute_platform_description > compute_platform_info;
    compute_platform_info.resize(4);
    compute_platform_info[0] = {CL_PLATFORM_PROFILE, "Platform info: "};
    compute_platform_info[1] = {CL_PLATFORM_VERSION, "Platform version: "};
    compute_platform_info[2] = {CL_PLATFORM_NAME, "Platform name: "};
    compute_platform_info[3] = {CL_PLATFORM_VENDOR, "Platform vendor: "};
    //compute_platform_info[4] = {CL_PLATFORM_EXTENSIONS, "Platform extensions: "};

    for (int c = 0; c < compute_platform_info.size(); c++)
    {
        char buffer[1024];
        error = clGetPlatformInfo(the_platform, compute_platform_info[c].info, sizeof(buffer), &buffer, 0);
        assert(error == CL_SUCCESS);
        std::cout << compute_platform_info[c].desc << buffer << "
";
    }

    /*cl_uint num_devices;
    //could be: CPU, GPU, ACCELERATOR, DEFAULT, ALL
    error = clGetDeviceIDs(the_platform, CL_DEVICE_TYPE_GPU, 1, &the_device, &num_devices);
    assert(error == CL_SUCCESS);

    std::cout << "Number of devices: " << num_devices << "
";*/

    size_t device_size = 0;
//this line causes the app crash
    error = clGetGLContextInfoKHR(the_context_properties, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR, 0, 0, &device_size);
    assert(error == CL_SUCCESS);

    int num_of_devices = (device_size / sizeof(cl_device_id));
    std::cout << "Number of CL-GL-interop capable devices: " << num_of_devices << "
";

    error = clGetGLContextInfoKHR(the_context_properties, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR, sizeof(cl_device_id), &the_device, 0);
    assert(error == CL_SUCCESS);

    std::vector<compute_device_description> compute_device_info;
    compute_device_info.resize(2);
    compute_device_info[0] = {CL_DEVICE_VENDOR, "Device vendor: "};
    compute_device_info[1] = {CL_DEVICE_NAME, "Device name: "};
    //compute_device_info[2] = {CL_DEVICE_EXTENSIONS, "Device extensions: "};

    for (int c = 0; c < compute_device_info.size(); c++)
    {
        char buffer[1024];
        error = clGetDeviceInfo(the_device, compute_device_info[c].info, sizeof(buffer), &buffer, 0);
        assert(error == CL_SUCCESS);
        std::cout << compute_device_info[c].desc << buffer << "
";
    }

    cl_uint uint_buffer;
    size_t size_t_buffer;
    size_t size_t_array_buffer[1024];
    error = clGetDeviceInfo(the_device, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t_buffer), &size_t_buffer, 0);
    assert(error == CL_SUCCESS);
    std::cout << "Max device work group size: " << size_t_buffer << "
";
    error = clGetDeviceInfo(the_device, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(uint_buffer), &uint_buffer, 0);
    assert(error == CL_SUCCESS);
    std::cout << "Max device compute units: " << uint_buffer << "
";
    error = clGetDeviceInfo(the_device, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(uint_buffer), &uint_buffer, 0);
    assert(error == CL_SUCCESS);
    std::cout << "Max device work item dimensions: " << uint_buffer << "
";
    error = clGetDeviceInfo(the_device, CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(size_t_array_buffer), &size_t_array_buffer, &size_t_buffer);
    assert(error == CL_SUCCESS);
    for (int c = 0; c < size_t_buffer / 8; c++)
    {
        std::cout << "Max device work item size [" << c << "] : " << size_t_array_buffer[c] << "
";
    }

    the_context = clCreateContext(the_context_properties, 1, &the_device, 0, 0, &error);
    assert(error == CL_SUCCESS);

    the_command_queue = clCreateCommandQueue(the_context, the_device, 0, &error);
    assert(error == CL_SUCCESS);
}

any ideas what I’m doing wrong?

Best regards,
Yours3lf

Does the SDK sample query the return size of clGetGLContextInfoKHR? If it just assumes the result is always sizeof(cl_device_id) then it may be avoiding a bug in the driver.

hi,

when I start the SimpleGL sample it outputs this:
$ ./SimpleGL
Platform 0 : Advanced Micro Devices, Inc.
Platform found : Advanced Micro Devices, Inc.

Selected Platform Vendor : Advanced Micro Devices, Inc.
Device 0 : Redwood Device ID is 0x27e2400
Number of displays 1
glXCreateContextAttribsARB 0x7f20c5a2be50
Number of interoperable devices 1 //sizeof(cl_device_id) is 8, so in my app the device size should be 8 too, but since the function never returns but crashes I don’t know what would it return…
Interop Device ID is 0x27e2400

and yes the sdk does query the return size. If you download the sdk, the relevant bit is in SimpleGL.cpp on lines 561-585

Well the only other input to that function is the_context_properties.

Have you used the exact same context properties as the sample? Perhaps you have given the function an incorrect property that is causing it to crash.

From memory the GLX handles are pointers so it is possible that these pointers are invalid for some reason.

EDIT: I got it solved… the problem was that I didn’t initialize the_platform before setting up the_context_properties, now I reorganized the code below so that it works :slight_smile:
Thank you for the help!!!

I guess I do the same as the sample, well here are the relevant lines:

my app:


std::cout << "
Initializing OpenCL...
";

    GLXContext the_glx_context = glXGetCurrentContext();

    if (the_glx_context == 0)
    {
        std::cerr << "Error getting the current glX context.
";
        objs::get()->conf.the_window.Close();
        exit(1);
    }

    cl_int error = CL_SUCCESS;

    if (!clGetGLContextInfoKHR)
    {
        clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn)clGetExtensionFunctionAddress("clGetGLContextInfoKHR");
        if (!clGetGLContextInfoKHR)
        {
            std::cerr << "Error getting clGetGLContextInfoKHR function ptr.
";
            objs::get()->conf.the_window.Close();
            exit(1);
        }
    }

    cl_uint num_platfroms;

    error = clGetPlatformIDs(1, &the_platform, &num_platfroms);
    assert(error == CL_SUCCESS);

    std::cout << "Number of platforms: " << num_platfroms << "
";
    
    cl_context_properties the_context_properties[] =
    {
        CL_CONTEXT_PLATFORM,
        (cl_context_properties)the_platform,
        CL_GLX_DISPLAY_KHR,
        (intptr_t)glXGetCurrentDisplay(),
        CL_GL_CONTEXT_KHR,
        (intptr_t)the_glx_context,
        0
    };

[...]

sample:


glXMakeCurrent (displayName, win, ctx);	
        gGlCtx = glXGetCurrentContext();
        cl_context_properties cpsGL[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform,
                                          CL_GLX_DISPLAY_KHR, (intptr_t) glXGetCurrentDisplay(),
                                          CL_GL_CONTEXT_KHR, (intptr_t) gGlCtx, 0
                                        };
        if (!clGetGLContextInfoKHR)
        {
            clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn) clGetExtensionFunctionAddress("clGetGLContextInfoKHR");
            if (!clGetGLContextInfoKHR)
                {
                    std::cout << "Failed to query proc address for clGetGLContextInfoKHR";
                }
        }

        size_t deviceSize = 0;
        status = clGetGLContextInfoKHR(cpsGL,
                                   CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,
                                   0,
                                   NULL,
                                   &deviceSize);
        CHECK_OPENCL_ERROR(status, "clGetGLContextInfoKHR failed!!");

        int numDevices = (deviceSize / sizeof(cl_device_id));
        std::cout<<"Number of interoperable devices "<<numDevices<<std::endl;

the difference may be that my app uses SMFL (which uses glX) for windowing, and the sample uses glX directly for windowing.