11 Sep 2017
Running OpenCL Sample Application on Open-Q™ 820 System on Module, powered by Qualcomm® Snapdragon™ 820 SOC
Running OpenCL Sample Application on Open-Q™ 820 powered by Qualcomm® Snapdragon™ 820 SOC
OpenCL (Open Computing Language) is an industry standard that allows software developers to take advantage of multiple processors in order to increase the performance of computational tasks. While originally designed for desktop computers, OpenCL has in recent years slowly been becoming more available on embedded systems as more Graphics Processing Units (GPUs) become OpenCL compatible. With the help of OpenCL, GPUs are able to perform intensive parallel programming tasks in graphics-related as well as non-graphics related computational workloads, allowing for significantly improved processing times. One of the GPUs used in smartphones and tablets that now supports OpenCL is Qualcomm’s Adreno 530, which is used in Intrinsyc’s Open-Q 820 Development Kit.
In order to demonstrate the decreased processing time enabled by OpenCL on the Adreno 530, a sample application taken from the Sony Mobile website  will be used. Since the source code was originally targeted toward Sony Xperia devices , it has been slightly modified for use by the different hardware of the Open-Q™ 820. The sample application applies OpenCL to a math intensive bilateral filter operation on a noisy photo. In one case, the bilateral filter is applied using OpenCL, while in the second case it is applied using Native C. While the execution time improvements may vary across different OpenCL-supported devices due to differences in architecture, executing the bilateral filter using OpenCL on the Open-Q 820 has demonstrated to be over 300 times faster than using Native C.
The following sections describe our modifications to Sony’s sample application that we performed for the Open-Q 820. Section 1 illustrates the expected behavior of the application after modifying it for use on the Open-Q™ 820. Section 2 describes the steps that were taken to modify the original sample application from Sony Mobile such that the source code could be built for Intrinsyc’s Open-Q 820. Section 3 provides some suggestions for when troubleshooting is required.
Note: the instructions in this blog were tested for Open-Q™ 820 BSP Version 2.2. These instructions may still work for other versions if OpenCL is supported on them. Please see Section 3 step 2 to check if OpenCL is supported.
Section 1: Using the OpenCL Sample Application
Upon opening OpenCLexample1 on the device, a noisy image is shown along with three buttons.
The application will run a bilateral filter on the image to reduce the noise using either OpenCL or Native C, depending on which button is pressed. Press Reset between running OpenCL and NativeC to ensure the bilateral filter is applied to the noisy image. Pressing the OpenCL button should yield a much faster processing time than pressing the NativeC button (for example, 5ms versus 1638ms). The processing time will be displayed at the top of the screen.
Section 2: Modifying the Original OpenCL Sample Application to be built for the Open-Q™ 820
The following tutorial outlines the steps required to modify and build Sony’s OpenCL code example, taken from the Sony Mobile website, using Android Studio for Intrinsyc’s Open-Q™ 820 Development Kit.
Before initiating the steps below, ensure the latest NDK is set up by following steps at https://developer.android.com/ndk/guides/index.html
1.Download Sony’s OpenCL code example from:
2.To ensure that OpenCL is supported on the device, install openclexample1.APK provided in the download and run the application, testing both OpenCL and NativeC buttons. Using the nativeC button should yield a much higher processing time (for example, 5ms versus 1638ms), which will be indicated at the top of the screen.
3. Ensure NDK is set up:
4. Unzip both OpenCL_code_example.zip and OpenCL_code_example/openclexample1.zip
5. Import openclexample1 into Android Studio as a new project:
A. In Android Studio, select File a New a Import Project
B. Select the location of OpenCL_code_example/openclexample1
C. Create an import destination directory.
D. Click Finish with the default options selected
6. Manually copy over any files from listed under "Ignored Files" in the generated import-summary.txt file.
A. For example, it may be necessary to manually copy over the following from the OpenCL_code_example/openclexample1 folder to the destination directory created in step 3c:
i./OpenCL_code_example/openclexample1/extra_libs to /OpenCLSample/app/src/main/extra_libs
ii./OpenCL_code_example/openclexample1/include/ to /OpenCLSample/app/src/main/include
7. Link C++ project with gradle:
a.Right-click the project in Android Studio ("app" in the project view) and select "Link C++ Project with Gradle"
b.Select ndk-build and the location of Android.mk for the Build System and Project Path, respectively.
8. Replace libOpenCL.so with the one used by the device:
a.Use adb pull to get the file directly from the device.
b.Replace /OpenCLSample/app/src/main/extra_libs/libOpenCL.so with this new libOpenCL.so file.
9. The ABI used by the Open-Q 820 dev kit is arm64-v8a, which is supported as of SDK version 21. Therefore:
a.Modify the app level build.gradle file:
i.Change values of compileSdkVersion, minSdkVersion, and targetSdkVersion to 21.
ii.Add abiFilters ‘arm64-v8a’ to ndk (which should have been generated under defaultConfig).
i.Change the value of APP_ABI to arm64-v8a
ii.Change the value of APP_PLATFORM to Android-21
10.Sync Project with Gradle Files.
11.Make Project using Build -> Make Project
12.Either Run on the connected device (Run ->Run ‘app’ ->Select a connected device), or Build APK (Build -> Build APK), which can be installed using the method in step 2. The application will appear on the device as OpenCLexample1.
Section 3: Troubleshooting
If you are experiencing errors during the sync or build process, please try the following
1.Ensure you are using the latest version of Android Studio and NDK.
2.If you are not using Open-Q BSP Version 2.2, or if you are not using the Intrinsyc Open-Q™ 820 Development Kit you may need to:
a.Ensure that your device supports OpenCL by installing the provided APK. Run the application, testing both OpenCL and NativeC buttons. Using the NativeC button should yield a much longer processing time (for example, 1638ms versus 5ms), which will be indicated at the top of the screen. If this fails, your device may not support OpenCL. Check to see if the OpenCL libraries exist on the device at the below locations before proceeding to step b. If these files do not exist, it is very likely that OpenCL is not supported for the current software version.
If these files do exist, but Android Studio still cannot seem to recognize them, proceed to step b to use the OpenCL libraries taken directly from the device rather than the ones provided in the source code.
b.Use libOpenCL.so taken directly from your device as in Section 2 step 8.
c.Ensure the correct ABI filter is listed under abiFilters as in Section 2 step 9. You can check which ABI filter your device is using ADB in a terminal.
Note: only list the ABI filter you need, otherwise, you may need to modify the extra_libs folder to include different versions of libOpenCL.so for different ABIs.
Section 4: References
 Rasmusson, J. (2013, October 29). OpenCL code example. Retrieved May 2, 2017, from https://developer.sonymobile.com/downloads/code-example-module/opencl-code-example/
 Cuhrajs, S. (2013, November 14). Boost the performance of your Android app with OpenCL. Retrieved May 2, 2017, from https://developer.sonymobile.com/knowledge-base/tutorials/android_tutorial/boost-the-performance-of-your-android-app-with-opencl/
Author: Angela Rosa, Embedded Software Developer