32

For plattform independence (desktop, cloud, mobile, ...) it would be great to use OpenCL for GPGPU development when speed does matter. I know Google pushes RenderScript as an alternative, but it seems to be only be available for Android and is unlikely to be ever included in iOS.

Therefore I seek for a solution to execute OpenCL code within Android Apps.

1
  • I'm wondering if an update might be in order here, after reading this story about Intel's adaption of Microsoft's AMP into OpenCL & llvm/Clang: phoronix.com/scan.php?page=news_item&px=MTIyOTk combined with the also very recent inclusion of llvm/Clang in the Android NDK toolchain: clang-developers.42468.n3.nabble.com/… Only just looking into this now, and not yet having tried it out, I will experiment & offer news of my results in a future post.
    – Jack
    Commented Jan 13, 2013 at 21:08

8 Answers 8

14

Although time has passed since the original question was asked, I think this is still a question for a lot of developers.

There are two aspects in the answer. First, unfortunately, Google doesn't support OpenCL officially.

Second, fortunately, many chip vendors provide their libraries to support OpenCL. As the time for now, most of the flagship and middle-tier smartphones (with Qualcomm Adreno GPU, ARM Mali GPU, or Imagination PowerVR GPU) include the OpenCL libraries.

To use OpenCL on Android, there are several steps:

  1. check if there is OpenCL library on the device. This can be done by using OpenCL-Z Android. This is a great tool to check the OpenCL availability on Android devices, and it also provides raw compute performance metrics, which could be very helpful.

The OpenCL libraries for the major chip vendors can be found in the devices: The followings are the location of the OpenCL library:

Qualcomm Adreno:

/system/vendor/lib/libOpenCL.so
or /system/lib/libOpenCL.so (older devices)

ARM Mali:

/system/vendor/lib/egl/libGLES_mali.so
or /system/lib/egl/libGLES_mali.so

PowerVR:

/system/vendor/lib/libPVROCL.so
  1. Write your OpenCL program using C or C++

  2. Create NDK project to compile your C/C++ code, and test them on the device as executable.

  3. Create JNI interface for your NDK program functions.

  4. Create Android project, using JNI functions in the JAVA code to call native functions involving with OpenCL.

The sony tutorial is a good source to refer. The techniques presented in that tutorial can be applied to any Qualcomm Adreno GPU. With very minimal modification, that code and makefiles can also run on other OpenCL-capable devices (such as Mali and PowerVR).

Hope this helps.

3
  • Just a note to say thanks for the OpenCL-Z reference... there is so little good information on OpenGLES-OpenCL interop out there! I appreciate it!
    – user755921
    Commented May 22, 2016 at 22:43
  • hmm, i think the reason is that so few people have ever used gl-cl interop features. especially we have GL compute shader in more recent OpenGL, the chance we will see gl-cl interop in an application will be even less. Commented May 24, 2016 at 2:58
  • BTW, if I remember correctly, in older Samsung Exynos chip, the Mali GPU (T-628) support OpenCL 1.1, however, some of the GL-CL interop APIs were not even implemented! I know that since I have some code containing usage of GL-CL buffer interop, the code crashed when executing those APIs. Then I checked the libGLES_mali.so and found out those APIs are not even implemented. Commented May 24, 2016 at 3:03
13

The only Android devices I know that support OpenCL are the ones based on the Mali T600 family of chips (article here). They have an OpenCL SDK. Apparently it is OpenCL 1.1 full profile as well.

The Nexus 10 is a device that uses such a chip. The Samsung Exynos 5 dual SoC uses a Mali T604, so anything using this supposedly could be used with the Mali T600 OpenCL SDK (havne't tried any of this myself).

The author of the OpenCL blog is trying to have a go with this, so it might be worth following his series of articles.

But, OpenCL support on Android is brand new (as of 16/2/2013) so, while great for experimentation, it might be worth being cautious until there is more support (who says how buggy the intitial support of OpenCL 1.1 is)

4
  • 3
    Adreno 320 and later have OpenCL, e.g. Nexus 4 supports it Commented Mar 11, 2013 at 21:43
  • 2
    None of the Google devices since I believe 4.3 will expose it however.
    – chrisvarnz
    Commented Jan 10, 2014 at 11:36
  • Correct me if I am wrong, but AFAIK, it is absolutely impossible to use OpenCL on recent versions of Android and it is not going to change. Google is pushing Rendescript on Android for this kind of tasks.
    – Teovald
    Commented Sep 3, 2014 at 10:18
  • "The only Android devices I know that support OpenCL are the ones based on the Mali T600": many other vendors seem to support it in 2014: arrayfire.com/opencl-on-mobile-devices Commented Nov 13, 2016 at 1:36
6

Checkout an Android OpenCL demo over at Sony developer world, complete project with source, where a bilateral filtering of an image is done in OpenCL and compared to a single threaded C implementation. Some information on what kind of support is expected in Sony devices etc can be found in the article as well.

Article:

Boost the performance of your Android app with OpenCL

Source for article:

Source to OpenCl Android project

Disclaimer: I'm a consultant at Sony Mobile

2
  • OpenCL is not available on all Android devices and misleading developers into believing that it is a reasonable API for them to use for development/distribution is wrong. Feel free to reach out to me if you need further clarification. Commented Nov 12, 2013 at 21:59
  • 1
    I have never stated the opposite (that it's available on all devices) on the contrary it's clearly stated in the article which (in that case) Sony device supports it, several other answers here provided insights what support is to be expected on other devices.
    – Magnus
    Commented Nov 12, 2013 at 22:30
6

In 2018, you can use openCL to develop Android app with Android Studio.

In order to use openCL with Android Studio, you will need to do several things.

  1. Check to see if your device supports openCL and what version using the OpenCL-Z Android and copy the prebuilt library into your computer like Robert Wang said.
  2. Download Android Studio.
  3. Create a project C/C++ support.
  4. Copy your libOpenCL.so to the folder /<your_project>/app/src/main/jniLibs/<architecture>/(You will have to create the folder yourself).
  5. Create your native C/C++ file if it is not created yet and link it with the prebuilt library in Cmake. Also, add your native C/C++ file as a library for the android project. https://developer.android.com/studio/projects/configure-cmake.
  6. Config your module(app) build.gradle file.

    android{
       ...
       default_config{
           externalNativeBuild{
              cmake {
                 // Filter based on your device architecture
                 abiFilters 'armeabi-v7a'
              }
            }
            ...
       }
       sourceSets {
          main {
             jniLibs.srcDirs = ['src/main/jniLibs']
          }
       }
       ...
    }
    
2
  • Great. But what about the device support? Renderscript support is mandated in Android Compatibility Definition Document. Is OpenCL widely supported on android devices?
    – rpattabi
    Commented May 28, 2018 at 8:29
  • 1
    From my research, the recent years Android devices mostly support OpenCL. Some of the newer devices, like Samsung Tab S3, even support OpenCL 2.0 Full Profile. So, I think OpenCL support in Android is pretty good.
    – Khoa Tran
    Commented May 29, 2018 at 12:15
3

You should use RenderScript Compute instead: http://developer.android.com/guide/topics/renderscript/compute.html

Using OpenCL is not very safe because the library (or capability) may not be available on the target device. To be honest, I don't even know if any Android device out there actually supports it. RenderScript will fall back to CPU computation if the GPU on the device is not capable of executing whatever program you want to run.

However, if you still want to use OpenCL, heres something that may help you http://www.pgroup.com/lit/articles/insider/v4n2a3.htm

You might want/need the device-specific SDK (like the nVidia Tegra SDK) in order to have proper control.

4
  • 3
    This doesn't answer the question asked. If the question was how do I access the GPU ...
    – Tim Child
    Commented Mar 6, 2013 at 4:27
  • 2
    @TimChild The question was how to use OpenCL for GPGPU, which I answered.
    – silverclaw
    Commented Mar 13, 2013 at 17:37
  • 5
    You didn't - you said not to use it when the OP clearly mentioned his lack of interest in RenderScript. I believe @prunge 's answer is more accurate. Also, PGCL is a commercial product.
    – Ani
    Commented Jun 28, 2013 at 14:59
  • 3
    You didn't answer the question. YOu can use OpenCL on android devices if you are using a Nexus 4 and a Nexus 10 on android 4.2. After that Google has prevent developers from using OpenCL on 4.3.
    – Jim V
    Commented Aug 12, 2013 at 21:46
2

All Qualcomm Adreno 300 serials support OpenCL 1.1 embedded profile. To use OpenCL, you need to develop NDK code as OpenCL is not supported by google at Java layer. It is pretty straighforward to write OpenCL code if you know how to develop NDK code. You need to have the libOpenCL.so available, which can be fetched from the OpenCL capable device, such as HTC one, Moto X, and Samsung Note/Galaxy versions that use Snapdragon.

1

Khronos released OpenCL 2.0 including official support for Android: https://www.khronos.org/news/press/khronos-releases-opencl-2.0

0
1

Just look at the open source arm compute library (ACL), which contains OpenCL kernels: https://developer.arm.com/technologies/compute-library

It has a documentation site: https://arm-software.github.io/ComputeLibrary/latest/

and a github site: https://github.com/ARM-software/ComputeLibrary

Look also for Qualcomm SNPE SDK (it uses OpenCL: https://developer.qualcomm.com/forum/qdn-forums/software/snapdragon-neural-processing-engine-sdk/34526): https://developer.qualcomm.com/docs/snpe/overview.html

https://www.youtube.com/watch?v=h3T1ekJ_iXM

You can also see Tensorflow Lite for mobile apps: https://www.tensorflow.org/lite/

Maybe in future it will support OpenCL in the way of ACL (now it is Android 8.1 NNAPI solution - https://www.slideshare.net/kstan2/open-source-nn-frameworks-on-cellphones):

https://github.com/tensorflow/tensorflow/issues/18324

Tensorflow Lite GPU acceleration - does it work for all OS versions or only for 8.1?

http://jevois.org/qa/index.php?qa=2057&qa_1=can-tensorflow-lite-use-the-gpu

A fine example programmed with Kotlin is here: https://github.com/eddywm/KTFLITE

For caffe2 using also NNAPI or OpenGL, there is some hope for OpenCL in future: https://github.com/laMia482/caffe2_android

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.