Pages

OpenCL with C++ Bindings in XCode

I've recently started learning OpenCL for my work at DESY CFEL and found that setting up a C++ environment in XCode for OpenCL is not an entirely straightforward task. Here are some instructions to get started. I am using OSX 10.13.4 (High Sierra) and XCode 9.3.1.

The OpenCL framework in OSX is stored at /System/Library/Frameworks/OpenCL.framework/. Unfortunately, it does not come with a header file for C++ bindings; if you want to use C alone this should not be a problem. I would like to use C++, however.

Download the appropriate cl.hpp file from the Khronos OpenCL Registry. Simply add and include this header file in XCode OpenCL projects and it should work fine. Do not forget to actually link the OpenCL framework under the project settings in XCode. Download and compile this example code, mostly copied from this tutorial, to test your system.

It is possible to add cl.hpp to the system framework so that you do not have to manually add it every time you create a new project. There is one complication: Since OSX 11.11 (El Capitan), Apple has started using something called System Integrity Protection (SIP) to prevent the naive or overly-adventurous from doing too much damage. When activated, it will prevent adding the header to the OpenCL framework even with root access.

This page provides the solution. Restart your system and hold cmd+R to launch OSX in Recovery Mode. Open a Terminal and enter csrutil disable to disable SIP. Restart normally. Move your cl.hpp file to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenCL.framework/Headers. Return to Recovery Mode and enter csrutil enable in a Terminal if you wish to reenable SIP and restart again. You should now be able to include <OpenCL/cl.hpp> in all of your XCode OpenCL projects.

Notice here that, despite the fact that OpenCL has been released up to version 2.2 as of 2018, Apple devices come with drivers only up to version 1.2. Additionally, it is impossible (as far as I know) for users to update these drivers in OSX. Clearly, Apple has no interest in providing sufficient support for OpenCL. This is due to Apple's development of its Metal framework which, in their eyes, is the obvious choice for anyone developing for the GPU in OSX. Personally, I have no interest in learning an API that is only useful for one system so I hope this is rectified in the future.

References:
Khronos Group
Apple Developer OpenCL Programming Guide for Mac