- Using Intel® Cilk™ Plus Runtime on Android to multithread your application
- Developing with Intel C++ compiler integrated with NDK tools
- Developing with standalone Intel C++ compiler outside of the NDK build system
- Preparing JNI calls
- Using PGO to Improve Android* OS Application Performance
Using Intel® Cilk™ Plus Runtime on Android to multithread your application
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).
Developing with the Intel C++ compiler and the NDK build system (ndk-build)
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:
- 'APP_STL:=stlport_shared' or
- 'APP_STL:=gnustl_static' or
3. Modify your Java code as explained in the section "Preparing JNI calls" below
Developing with the standalone Intel C++ compiler without the NDK build system (ndk-build)
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:
- -I$ANDROID_GNU_LIBSTDCPP/include -I$ANDROID_GNU_LIBSTDCPP/libs/x86/include
- 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, ifANDROID_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:
- -L$ANDROID_STLPORT_LIBSTDCPP/libs/x86 -lstlport_shared
When using Intel Cilk Plus, the corresponding libcilkrts.so library is located in the /compiler/lib/ia32/stlport directory.
Preparing JNI calls
Preparing JNI calls are required for using Intel Cilk Plus for all Android versions prior Android 4.3 (Jelly Bean MR2). ‘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:
Using PGO to Improve Android* OS Application Performance
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:
1 #include <pgouser.h>
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:
1 export INTEL_PROF_DUMP_INTERVAL 5000
2 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:
1 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 15.0 User and Reference Guidessupplied 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.
For more such Android resources and tools from Intel, please visit the Intel® Developer Zone