Boost the performance of your Android app with OpenCL [tutorial]

Although our Xperia™ devices have become much more powerful the last couple of years, there might still be cases when your application needs more processing power. With OpenCL™, you can use the power of the GPU to handle resource intensive tasks in your app. This article is a short introduction to OpenCL, and how to get it up and running on your Sony Xperia device.

By Anonymous

By Anonymous

If your device is based on one of Qualcomm’s later chipsets with Adreno 3xx GPUs, there is a chance, but no guarantees 🙂

If it does not work, you might try with the specific libOpenCL.so file residing on your device (probably in /vendor/lib/). Use abd pull to copy it from your device to your pc and use it to replace the libOpenCL.so file in your project (e.g., in my code example, the libOpenCL.so in placed in the “extra_libs” folder)

By Jimmy Ren

Thanks a lot Jim! I tried it in an alternative Android device running Adreno 330 GPU and Android 4.2.2 and it works! Since that device doesn’t support Android 4.3 and Android 4.4, I’m wondering if there’ s a chance to enable Open CL in the newer Android version? Thanks a lot!!!

By Jim Rasmusson

I am sorry, not sure I understand your question fully. OpenCL is added to Android as a vendor extension. It is (unfortunately) not built into Android from the start, but one of the beauties of Android is that it is extensible and flexible, in so much it lets us add interesting new functionalty like OpenCL.

If your phone vendor have the OpenCL libraries present on 4.2.2, lets hope they are still present in upcoming releases..

By Jim Rasmusson

I can only speak on behalf of Sony and it is clearly our ambition to keep OpenCL on our Xperia phones and tablets going forward (we recently updated many of our Xperia devices to Android 4.3, and OpenCL is of course still there, using the same vendor extension mechanisms as before).

I certainly hope the other mobile device vendors will do the same (for OpenCL on Android to be “succesful”, we need more than Sony on this train..)

I agree it is pity that Google is not allowing OpenCL on the Nexus devices, at least for experimental purposes, especially since Nexus devices are often used by developers for trying out new things.

The multi-platform, multi-vendor OpenCL eco system has evolved and matured dramatically the last 1-2 years, on desktop and (especially) on mobile. Today I would say OpenCL is quite strong, not at all in the “experimental state” it use to be, say 3-4 years ago. And the roadmap ahead with many new useful features in OpenCL 2.0 is also promising (implementations coming in a not too distant future 🙂

Googles own Renderscript-Compute is nice but OpenCL certainly has a lot to offer. Actually, when I think of it, it is indeed strange that Google do not embrace this (these days) mature and powerful open standard and add it to Android, in similar spirit to OpenGL ES and the other open standards present and succesfully utilized in Android.

/Jim Rasmusson

By Jim Vaughn

The issue in question is that on as of 4.3 and 4.4 Google devices using ARM, Adreno GPUs such as the nexus 4 and 10 Google has rebuilt the drivers from said vendors to not compile and return an error on the clbuildprogram() call. Google can only block this call as they cannot stop the OpenCL runtime from working as Renderscript GPU for these devices runs on top of the OpenCL runtime. This was done specifically to promote Renderscript and you can see Tim Murray’s (a google Renderscript dev) comments indicating this and bashing OpenCL publicly on stack overflow, often with misleading and false info about OpenCL. Google is certainly within their right to do this as they license and can modify the drivers for ARM and Adreno GPUs but it is certainly low to attack OpenCL when both of those devices run Renderscript as a layer on top of OpenCL using a separate compiler that they have blocked users from accessing. It is very unfortunate but it is great news that Sony is releasing theirs in this manor and that it has not been blocked (I assume because google can not unless it is on their own devices such as the nexus 4 and 10.

By Jim Vaughn

Jim (and Sony) THANK YOU for supporting OpenCL on android! It was looking like we were going to get stuck with Renderscript compute only which is a non-starter for my work. I hope to pick up two of these devices for testing one with the 330 and one with the snapdragon 800.

One question is this 1.1 full or ES profile? I would certainly prefer the full as it makes porting our code so much easier and we don’t have to worry about precision errors. Again that you so much. Is there a dev support forum or a contact at sony to touch base with on this.

I would also like to stress the commitment to OpenCL for the long term. We need this in every mobile device and hopefully Sony will not block it like google did on their devices.

By Jim Rasmusson

Thank you -Yes we are excited as well! I agree that porting desktop OpenCL projects would be much easier with full profile support but unfortunately it is the embedded profile of OpenCL 1.1. You can investigate the capabilities with the standard OpenCL query mechanisms.

Please note that “full precision” comes with a cost; lower execution speeds and higher energy consumption, and the difference can be substantial on a mobile phone. So unless you really need the precision, try to use the “native_” and “fast_” instructions (and “relaxed-math” compiler options) as much as you can.

By Super Saiyan

This is great news and congratulations to Sony on shipping OpenCL. Great to hear that finally an important OEM like Sony has stepped up and shipped OpenCL.

I do have a question. Let us say I write an app that is written for and tested on one of the Xperia devices mentioned. Can I rely that Sony will not remove OpenCL from these devices in future firmware updates? Google did remove OpenCL drivers from the Nexus 4 and 10 for example because they are not too interested in CL. I am hoping something similar doesn’t happen with the Xperia devices.

By Jim Rasmusson

Yes, we hope to get OpenCL going also among 3rd party Android developers. There is so much potential. We have the libOpenCL.so in /vendor/lib/ on the Xperia devices (on some older models they are in /system/lib/) where the dynamic linker should find it. Hopefully the other vendors will do the same. There are discussions in Khronos to use the ICD (installable client driver) mechanism to help out. We will see how it evolves.

In the OpenCL code example project, I have put the lib file libOpenCL.so in a separate folder “extra_libs”, in order to have a “self contained” project that you can build directly without fuzz (hopefully) after downloading it. Same thing with the CL header files (they are in a separate folder “include”). If you want, you can put the library and header files in, e.g., $ANDROID_NDK/platforms/android-14/arch-arm/usr/ (or android-18 if you use ndk revision 9) for OpenCL to be available in general for your NDK projects.

Appreciate your efforts with Jogamp JOCL! Having Java bindings for OpenCL also in Android will definitely make it easier for many developers.

Jim, thanks for the heads up on the driver location. A small footnote. I’m new to the JogAmp / JOCL community and indeed it is Sven and Xerxes (core JogAmp maintainers) that are leading the way for JOCL on mobile / Android. I’ll definitely relay this information. I’d like to think there will be a solution in place soon, so for anyone interested keep an eye out on the JogAmp JOCL forums for updates.

It’s great to see Sony embrace OpenCL. Please continue supporting this important technology and provide a spectrum of computation possibilities for Android developers; OpenCL & Renderscript on Android both widely available. I’m working on assisting the Jogamp JOCL community in extending their existing Java binding to support Android / mobile and will likely pick up a Z1 for testing.

One of the larger issues of course is driver location since it varies between vendor and there is no standard way to use LOCAL_LDLIBS with an NDK build presently to associate OpenCL drivers. I haven’t had time to look at the example code provided in this article, but I assume things are hard coded for the driver location. This definitely is the next major hurdle to provide a standard way to access the OpenCL driver on Android. IMHO a “uses-feature” tag for the AndroidManifest which filters devices that don’t support OpenCL is sufficient to protect a standard LOCAL_LDLIBS driver lookup.