Can't create OpenCL context from OpenGL with specific device

Hi guys, I have a problem with creating and OpenCL context with specific OpenCL device (GPU).

My specs: Mac Mini, latest Snow Leopard

---> CL_PLATFORM_NAME:         Apple
---> CL_PLATFORM_VENDOR:       Apple
---> CL_PLATFORM_VERSION:      OpenCL 1.0 (Oct 16 2009 04:12:08)

---> CL_DEVICE_NAME:           GeForce 9400
---> CL_DRIVER_VERSION:        CLH 1.0
---> CL_DEVICE_VERSION:        OpenCL 1.0 
---> CL_DEVICE_EXTENSIONS:     cl_khr_byte_addressable_store cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_APPLE_gl_sharing cl_APPLE_SetMemObjectDestructor cl_APPLE_ContextLoggingFunctions 

err = clGetPlatformIDs(1, &pID, &num);
err = clGetDeviceIDs(pID, CL_DEVICE_TYPE_GPU, 1, &dID, &num);

// OpenCL context running atm...	
CGLContextObj kCGLContext = CGLGetCurrentContext();              
CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
cl_context_properties akProperties[] = { 
	CL_CONTEXT_PLATFORM, (cl_context_properties)pID,	

// here I pick my specific GPU
// this works only with CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE disabled (but I need interop)
// error is -31, CL_INVALID_DEVICE_TYPE
context.context = clCreateContext(akProperties, 1, &dID, clLogMessagesToStdoutAPPLE, NULL, &err);

// this works all the time, but i can't pick my GPU manually (value is zero) 
context.context = clCreateContext(akProperties, 0, 0, clLogMessagesToStdoutAPPLE, NULL, &err);

As you can see can int the code above I can’t pick my GPU “manually” to create OpenCL context (with OpenGL). When I try that, i always get error -31, CL_INVALID_DEVICE_TYPE.
I don’t know where the problem is. The platform and device query works absolutely fine. I know this is not big deal especially in my case (master thesis), but I’d like to know what I could have done wrong.

I tried also

clCreateContextFromType(akProperties, CL_DEVICE_TYPE_GPU, clLogMessagesToStdoutAPPLE, NULL, &err);

But the result is the same - it does not work with the CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE context property enabled ;(

How much memory do you have? I’ve had issues with Mac Mini’s with only 1GB of RAM (128MB of VRAM) before. If that’s the case I’d suggest trying it on a 2GB (256MB VRAM) config. If not, you should probably try some of the Apple sample demos unmodified, and if they fail, file a bug with them. Sorry I can’t be of more help.

There are relevant comments in (Apple’s) cl_gl_ext.h header file (where CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE) is defined, which seem to confirm your observations.

Since cl_APPLE_gl_sharing does not appear to be in the extension registry, you’ll need to ask Apple for more information.

Thank you guys.

You were right, it’s written there. I cannot use specific GPU. The OpenGL context device is selected by default. I don’t know why, but it’s like that.

// Additional CL devices can also be specified using the <num_devices> and <devices> arguments.
// These, however, cannot be GPU devices. On Mac OS X, you can add the CPU to the list of CL devices
// (in addition to the CL compliant devices in the CGL share group) used to create the CL context.
// Note that if a CPU device is specified, the CGL share group must also include the GL float renderer;
// Otherwise CL_INVALID_DEVICE will be returned.

It is possible to use a specific GPU device, however with this extension the choice is specified using CGL, not CL.

This extension allows a CL context to be created containing the same devices as a GL share group; the GPU devices are specified when the related CGL context is created. Other non-GPU compute devices may be included in the CL context, these are specified in the other arguments to clCreateContext, but only if share group includes the GL software renderer.

The CGL pixel format may be used to specify which devices to include in the GL context and share group, i.e. by creating a format which only matches a certain device (take a look at the CGL and CG documentation). Once a matching pixel format is found it is used to create a CGL context, from which the share group is obtained, and finally passed to clCreateContext.

You need to make sure you create a CGLsharegroup which includes all the appropriate GPU devices in the system. Then you can create a CL context using the CGLsharegroup. The CL context will now include all GPUs. You can then use clGetGLContextInfoAPPLE to query the following:

a) The cl_device_id value for the GPU associated with the virtual screen in your CGLcontext (created using the CGLsharegroup)

b) The mapping of cl_device_id values to virtual screen(s) associated with the given CGLContext.

You can then use this information to determine whether you want to use the same GPU for both GL & CL or different GPUs.

Hopefully this answers your question.

Thank you ;).