I'm writing a small hello world OpenCL program using Khronos Group's cl.hpp for OpenCL 1.2 and nVidia's openCL libraries. The drivers and ICD I have support OpenCL 1.1. Since the nVidia side doesn't support 1.2 yet, I get some errors on functions required on OpenCL 1.2.
On the other side, cl.hpp for OpenCL 1.2 has a flag, CL_VERSION_1_1
to be exact, to run the header in 1.1 mode, but it's not working. Anybody has similar experience or solution?
Note: cl.hpp for version 1.1 works but, generates many warnings during compilation. This is why I'm trying to use 1.2 version.
Unfortunately NVIDIA distributes an old version of the OpenCL ICD (the library that dispatches API calls to the appropriate driver). Your best options are to either
You can define the flag CL_USE_DEPRECATED_OPENCL_1_1_APIS which will make the 1.2 hpp file 1.1 compatible.
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
This is what I have done on NVIDIA and AMD. Works like a charm
I was fed up with downloading several GB OpenCL SDK's by Intel, Nvidia, and AMD with different issues:
- Intel requires registration and has a temporary license.
- Nvidia SDK does not support OpenCL 2.0 and you have to download cl.hpp anyway.
- AMDs
cl.hpp
file defines min and max macros which can conflict with MSVC's min and max macros (I spend too much time figuring out how to fix this with e.g. NOMINMAX
). The header is not even the same as the one defined by Khronos (which does not have the min/max problem).
Therefore, I downloaded the source code and includes from Khronos as suggested by this SO answer and compiled the OpenCL.lib
file myself. The includes and OpenCL.lib files are a couple of MB. That's a lot smaller than all the extra stuff in the Intel/Nvidia/AMD SDKs! I can include the OpenCL includes and OpenCL.lib files in my project and no longer have to tell others to download an SDK.
The includes for OpenCL 2.0 from the Khronos registry has a new C++ binding file cl2.hpp
. Looking at this file I have determined that the correct way to support the deprecated functions with the OpenCL 2.0 is something like this.
#define CL_HPP_MINIMUM_OPENCL_VERSION 110
#define CL_HPP_TARGET_OPENCL_VERSION 120
#define CL_HPP_CL_1_2_DEFAULT_BUILD
#include "CL/cl2.hpp"
This is because the cl2.hpp file has this code
#if CL_HPP_MINIMUM_OPENCL_VERSION <= 100 && !defined(CL_USE_DEPRECATED_OPENCL_1_0_APIS)
# define CL_USE_DEPRECATED_OPENCL_1_0_APIS
#endif
#if CL_HPP_MINIMUM_OPENCL_VERSION <= 110 && !defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS)
# define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#endif
#if CL_HPP_MINIMUM_OPENCL_VERSION <= 120 && !defined(CL_USE_DEPRECATED_OPENCL_1_2_APIS)
# define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#endif
#if CL_HPP_MINIMUM_OPENCL_VERSION <= 200 && !defined(CL_USE_DEPRECATED_OPENCL_2_0_APIS)
# define CL_USE_DEPRECATED_OPENCL_2_0_APIS
#endif
Notice that you no longer need to (and should not) include <CL/opencl.h>
anymore.
Lastly, after #include "CL/cl2.hpp"
in order to get my code to work with Boost/Compute I had to add
#undef CL_VERSION_2_0
My own OpenCL code works without this but Boost/Compute does not. It appears I'm not the only one having this issue. My GPU does not support OpenCL 2.0.
Looks like the only way is to use the OpenCL 1.1 headers while working with 1.1 capable devices.
You can call can set the options of clBuildProgram as follows
const char options[] = "-cl-std=CL1.1";
clBuildProgram( program, 1, &devices, options, NULL, NULL );
This forces the compiler to use OpenCL 1.1 no matter which version is supported by your device