OpenCL on Android

I spend a fair bit of my time doing OpenCL development rather than CUDA or anything else because of its great cross platform capabilities. I love cross platform libraries 🙂 If I write code in C++, do some graphics with OpenGL, fancy compute with OpenCL and use a cross platform GUI library, such as Qt, I can build, run, distribute and – perhaps most importantly – sell on all three of the major desktop, workstation and server platforms aka Windows, Linux and OS X. Even if a product isn’t eventually sold on a particular platform perhaps due to support or marketing reasons, it is reassuring to know that the code could be fairly swiftly ported across if needed.

Now things are changing. Many consumers aren’t using a desktop or a laptop and are using phones, tablets, hybrid laptop/tablets to run programs so, as a developer, it is becoming increasing important that code, or a large chunk of it, can be reused across all platforms including mobile. OpenCL, it would seem, is the perfect choice to maintain cross platform support.

Enter Android

So in steps Android with claims of open source-ness, an easy to set up development environment and no OpenCL 🙁 And no plans to add OpenCL :'( Darn. This rather surprised me as all the major GPU vendors in the mobile space, such as Imagination, Qualcomm and ARM, list OpenCL support very clearly as supported on most of their designs available in mobile devices over the last year or so. So what’s the problem?

For Android to support OpenCL there must be a driver installed on the device. On a desktop system adding new drivers is quite an easy and familiar task whereas adding drivers to Android is not easy or familiar and is definitely not something an end user will be doing. Even so, as a developer I don’t mind jumping through some hoops to start experimenting with OpenCL on a mobile device with an assumption that OpenCL will come to these devices at some point soon.

Now comes the crux of the matter: there are very few drivers for OpenCL (publicly) available from any vendor. In a post here Imagination quite nicely say why they, and others, don’t provide drivers directly. Chip designers provide drivers to whoever integrates their chips into devices and then it is those end manufacturers, e.g. Samsung, Apple, Sony, etc., who roll those drivers into the OS for a mobile device.

The other side of the problem is that Google does not support OpenCL on Android, probably because they are pushing their RenderScript alternative. This wouldn’t usually be too much of an issue as, it may be thought, as vendors could simply ship phones with Android loaded with OpenCL drivers. However, Android is the most closed open-source project that I’ve seen which means a phone OEM wishing to use Android has to tow the Google line to be able to say their device runs “Android”.

The chinks of light

Despite all this, there are a few ways to get OpenCL running on Android. If you have a Google Nexus platform then Qualcomm provide a set of developer drivers that include OpenCL support. If you have a Sony Xperia Z series phone (follow link for complete list) then OpenCL 1.1 drivers are already installed. If you are a little bit more adventurous there is the ODROID-XU with OpenCL 1.1 already installed or the Onda V989 tablet with the latest firmware (click middle tab then select the version of firmware) that includes OpenCL 1.2 if you don’t mind everything being in Chinese.

Assuming you have got a device with an OpenCL driver installed, it is then a case of linking against that library in your build after carefully copying the library from the device onto your development machine. Here there is another slight wrinkle: the various OpenCL libraries seem to have slightly different names on different platforms, for example on the Onda V989 tablet. As a developer trying out OpenCL on a single device this isn’t a great problem as it is just a case of making sure to link against the appropriate library name.

For distribution of an OpenCL program, this means your program must dynamically try to load various likely OpenCL library names and if all of the loads fail you must fallback to a non-OpenCL path. This isn’t particularly nice but if you want to push the envelope…. 😉

The future

With OpenCL already having a strong foothold in the desktop, workstation and server space I find it unlikely that it will not eventually become ubiquitous in the mobile space. With OpenCL 2.0, Khronos introduced the Android installable Client Driver (ICD) Extension which enables OEMs to swap different vendors’ OpenCL drivers in and out while presenting developers with a consistent library name. It also feels like a statement of intent from Khronos that they want to see OpenCL on Android.

The GPU hardware companies all have OpenCL support in their chips. Phone and tablet OEMs, such as Sony, Asus and Onda, are already shipping OpenCL drivers with their phones with more OEMs sure to follow suite. All developers can write their programs to support OpenCL and allow their customers to benefit from the performance increases it enables. (Of course, this is where you as a developer must step up to the plate :))

The only missing part is consumers seeing those applications and insisting on OpenCL being in their phone so they can benefit from the cool features and performance increases. Then finally perhaps Google will get with the programme and make OpenCL a standard part of Android!

Leave a Reply

Your email address will not be published. Required fields are marked *