PDA

View Full Version : OpenCL device selection for interop with OpenGL.



rijpkema
11-20-2012, 09:15 AM
Dear people,

In our project we introduced OpenCL to accelerate the image analysis of OpenGL rendered images. This analysis was originally done on the CPU. Although the execution is much faster on the GPU than on the CPU, the main benefit, in our case, is the reduction in PCIe traffic. This is so because the image analysis only results in a few bytes of data (say, we need the pixel location of the brightest pixel).

To really benefit from the bandwidth reduction it is required that the OpenCL kernel runs on the same device as the OpenGL context. The issue that I have is that I don't know which OpenCL platform_id and device_id I must select that corresponds with the device that OpenGL uses to render the image.

What I was/am looking for is an identifier that uniquely identifies a device and that is consistent over both OpenCL and OpenGL, say, the serial number of the GPU chip. But to no avail; I can get a vendor string in both OpenCL and OpenGL, but that is not enough, I can get a device name in both OpenCL and OpenGL, but the return a different string. And still there is the issue in case there are multiple identical cards in the system.

The only cumbersome mechanism I can think of now is:


create OpenGL context
create 2Mx2M pixel texture in the OpenGL context

for each (candidate) OpenCL device
create OpenCL context based on OpenGL context
create OpenCL image based on OpenGL texture
glFinish()
measture start_time
acquire OpenCL image // this is fast on the right device.
clFinish()
measure stop_time
end
The right device is the device with the smallest stop_time - start_time.

Is there someone that knows how to select the OpenCL platform and device such that the OpenCL context runs on the same device as the OpenGL context from which it is created?

Best regards,

Edwin

rijpkema
11-21-2012, 04:05 AM
A small addition:

A cross platform solution would be optimal, however, a windows only solution would suffice for now.

regards,

Edwin

chippies
11-21-2012, 11:36 AM
Looking at the OpenCL 1.2 extensions section 9.6 I get the impression that one can create an OpenCL context from an existing OpenGL context. I haven't used this so I can't give you code. Please post back whether this method works for you.

rijpkema
11-22-2012, 01:31 AM
Yes, that I true. I am using OpenCL 1.1 and it is the same there. However, creating an OpenCL context from an OpenGL context is what is needed to allowing sharing of OpenGL textures (and others). However, it does not guarantee that the OpenCL context is on the same device as the OpenGL context. For example, I have been able to do OpenCL-OpgenGL interop where my OpenCL context is on the CPU and the OpenGL context is on the GPU, and still sharing works (and is slow).

For now I will take this route and will first check if the OpenCL platform is from the same vendor as OpenGL is running on. If so, I'll select the first device from the device list for that platform.

This method will work as long there is only a single GPU from the same vendor. In case there are multiple GPUs, automatic detection will fail and the user must specify the proper device.

chippies
11-23-2012, 09:57 AM
That's a real pity that OpenCL can't create a context on the right GPU given an OpenGL context. Thanks for pointing that out to me.

If you want to do a slightly more complicated auto detection, perhaps benchmarking the transfer time from CL to GL memory would help you find the right GPU?

erwin
01-29-2013, 08:58 AM
You can iterate over every OpenCL device until interop with a OpenGL context succeeds.