How can I be 100% sure that my opencl kernel are actually running on the GPU and not on the CPU. I am not able to understand this because openCL kernel can also run on CPU. Any pointers here?
OpenCL™ (Open Computing Language) is a low-level API for heterogeneous computing that runs on CUDA-powered GPUs. Using the OpenCL API, developers can launch compute kernels written using a limited subset of the C programming language on a GPU.
CPU-GPU Cooperation The architecture of OpenGL is based on a client-server model. An application program written to use the OpenGL API is the "client" and runs on the CPU. The implementation of the OpenGL graphics engine (including the GLSL shader programs you will write) is the "server" and runs on the GPU.
A kernel is essentially a function written in the OpenCL language that enables it to be compiled for execution on any device that supports OpenCL. The kernel is the only way the host can call a function that will run on a device. When the host invokes a kernel, many work items start running on the device.
You have to choose between platforms and devices available on your computer when you create the OpenCL context. You can get some informations about platforms and devices with clGetPlatformInfo and clGetDeviceInfo. You can find samples codes in NVIDIA and AMD SDK to list platforms and devices.
For AMD (gDEBugger, Code XL), NVIDIA (Visual Profiler) and Intel (Intel GPA) devices, you can use profiling tools.
When creating your context, use clCreateContextFromType where you can pass the flag CL_DEVICE_TYPE_GPU, which specifies that you want GPU only. Something like this guarantees the use of a GPU (you can be 100% sure) :
cl_uint num_platforms = 0;
clGetPlatformIDs(0, NULL, &num_platforms);
cl_platform_id* platform = malloc(sizeof(cl_platform_id) * num_platforms);
cl_context_properties cprops[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform[0], 0 };
ctx = clCreateContextFromType(cprops, CL_DEVICE_TYPE_GPU, NULL, NULL, &err);
If you have already your context and devices set up, you can query the type of a device like so:
cl_device_type dev_type;
clGetDeviceInfo(my_device, CL_DEVICE_TYPE, sizeof(dev_type), &dev_type, NULL);
if (dev_type == CL_DEVICE_TYPE_GPU) {
printf("I'm 100%% sure this device is a GPU");
}
Everything you will enqueue to a command queue created on this device will run on GPU.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With