- Using Intel® Cilk™ Plus Runtime on Android to multithread your application
- Using PGO to Improve Android* OS Application Performance
Intel® Cilk™ Plus is an extension to C and C++ that offers a quick and easy way to harness the power of both multicore and vector processing. The three Intel Cilk Plus keywords provide a simple yet surprisingly powerful model for parallel programming, while runtime and template libraries offer a well-tuned environment for building parallel applications.
Using Intel Cilk Plus to multithread your applications requires to link against the Cilk Runtime Library (libcilkrts.so).
The Intel C++ Compiler supports the latest Android* NDK R9 including R9c. The NDK build system does not link C++ libraries for modules written in C, as a result the compiler cannot choose the correct Intel Cilk Plus library during linking and this could result in linking errors.
1. Add an empty C++ file to the project to enable C++ linking in the NDK build system
2. Specify compatible C++ implementation in the Application.mk file:
3. Modify your Java code as explained in the section "Preparing JNI calls" below
If your development environment consists of C++ code you need to explicitly link against either the GNU_STL library or the stlport library.
Please following the steps below:
1. Specify the following flags for linking and compilation for the corresponding C++ implementation included in NDK like:
- Compilation flags:
- Linking flags (gnustl_shared):
-L$ANDROID_GNU_LIBSTDCPP/libs/x86 -lgnustl_shared -lsupc++
- Linking flags (gnustl_static):
-L$ANDROID_GNU_LIBSTDCPP/libs/x86 -lgnustl_static -lsupc++
ANDROID_GNU_LIBSTDCPP=$NDK/sources/cxx-stl/gnu-libstdc++/4.6. 4.6 should be replaced with GCC version pointed to by ANDROID_GNU_X86_TOOLCHAIN. For example, if ANDROID_GNU_X86_TOOLCHAIN points to $NDK/toolchains/x86-4.8/prebuilt/linux-x86, then 4.6 should be replaced with 4.8.
When using Intel Cilk Plus you need to link against the libcilkrts.so library. This library is located in the /compiler/lib/ia32/gnustl directory.
2. If stlport_shared C++ library (need at least NDK r9) is used, add following flags:
- Compilation flags:
- Linking flags:
When using Intel Cilk Plus, the corresponding libcilkrts.so library is located in the /compiler/lib/ia32/stlport directory.
Preparing JNI calls is required for using Intel Cilk Plus. ‘libcilkrts.so’ library needs to be loaded from Java Code with following API call:
If your application depends on dynamic C++ implementation, then you need to load additional corresponding library besides libcilkrts.so:
Profile-guided Optimization (PGO) improves application performance by reorganizing code layout to reduce instruction-cache problems, shrinking code size, and reducing branch mispredictions. PGO provides information to the compiler about areas of an application that are most frequently executed. By knowing these areas, the compiler is able to be more selective and specific in optimizing the application.
Using PGO on Android requires some additional steps compared to the usage on other Operating System.
1. Add the following options to the C flags in the jni/Android.mk file:
LOCAL_CFLAGS:= -prof-gen -prof-dir /sdcard
2. Add the WRITE_EXTERNAL_STORAGE permission to allow the application writing the PGO output to the sdcard folder. Add the following line to the AndroidManifest.xml file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3. Ensure that the profiling data gets written. The profiling data is written in default mode only if the application exits. Usually an application on Android* does not exit. Use the following options to enforce the application exit or to work around this issue:
a. Option 1: Call exit from JAVA code:
b. Option 2: Explicitly dump PGO data from the native code:
#include <pgouser.h> _PGOPTI_Prof_Dump_All();
c. Option 3: Use environment variables to write the performance data regularly to the sdcard while the application is running. To make the environment variables available for all applications, add them to the init.rc file of the Android Image:
export INTEL_PROF_DUMP_INTERVAL 5000 export INTEL_PROF_DUMP_CUMULATIVE 1
NOTE: The value INTEL_PROF_DUMP_INTERVAL is measured in microsecond and should not be greater than INT_MAX.
4. Copy the generated dyn files from the target to the host in the source directory of the application:
adb pull ...
5. Change the C flags in Android.mk file to use the generated dyn files. You can use the -prof-dir parameter to point to a different file location.
LOCAL_CFLAGS := -prof-use
See Profile-Guided Optimizations section in the Intel® C++ Compiler XE 14.0 User and Reference Guides supplied with the package for information on using PGO for optimizing applications built with the Intel(R) C++ Compiler on Linux*, Windows*, and OS X* operating systems.
Other Related Articles and Resources
Intel® C++ Compiler 14.0 for Android*
Intel® System Studio - Multicore Programming with Intel® Cilk™ Plus
Intel® Cilk™ Plus
User and Reference Guide for the Intel® C++ Compiler 14.0
Android* NDK for Intel® Architecture
Using the Android* x86 NDK with Eclipse* and Porting an NDK Sample App
To learn more about Intel tools for the Android developer, visit Intel® Developer Zone for Android.