Using the Advanced Intel® C ++ Compiler for Android Applications

Original author: Alexander Weggerle
  • Transfer
The Intel C ++ compiler provides many possibilities for optimizing applications for a wide variety of tasks, including for mobile devices. In this article, we will touch upon two aspects of optimization: firstly, we will talk about using the Intel Cilk Plus executable module in Android to implement multithreading applications, and secondly, we will discuss the topic of using Profile-guided Optimization (PGO) to improve application performance in Android OS. Links for a deeper study of these topics are given at the end of the article.

Using Intel Cilk Plus executable in Android to implement multi-threading applications

Intel Cilk Plus is an extension of C and C ++ that makes it easy and convenient to use multi-core architecture and vector processing. The three Intel Cilk Plus keywords provide a simple but surprisingly powerful model for parallel programming, and the runtime module and template libraries provide a convenient environment for creating parallel applications.
When using Intel Cilk Plus to implement multithreading in applications, a link to the Cilk runtime library ( is required.

Development using Intel C ++ compiler and NDK build system (ndk-build)

The NDK build system does not link C ++ libraries for modules written in C, as a result of which the compiler cannot select the necessary Intel Cilk Plus library during linking, which can lead to link errors.
1. Add an empty C ++ file to the project to enable C ++ linking in the NDK build system.

2. Specify a compatible C ++ implementation in the file.
'APP_STL: = stlport_shared' or
'APP_STL: = gnustl_static' or
'APP_STL: = gnustl_shared'

3. Change the Java code according to the instructions in the section “Preparing JNI Calls” below.

Development using Intel C ++ compiler without NDK build system (ndk-build)

If your development environment contains C ++ code, you must explicitly link to the GNU_STL or stlport library.
Follow these steps.
1. Indicate the following flags for compilation and linking for the corresponding implementation of C ++ in the NDK.
Compilation flags:
-I $ ANDROID_GNU_LIBSTDCPP / the include -I $ ANDROID_GNU_LIBSTDCPP / libs / the x86 / the include
linking Flags (gnustl_shared):
-L $ ANDROID_GNU_LIBSTDCPP / libs / the 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 by the GCC version that ANDROID_GNU_X86_TOOLCHAIN ​​points to. For example, if ANDROID_GNU_X86_TOOLCHAIN ​​points to $ NDK / toolchains / x86-4.8 / prebuilt / linux-x86, then replace 4.6 with 4.8.
When using Intel Cilk Plus, a link to the library is required. This library is located in the / compiler / lib / ia32 / gnustl folder.

2. If you are using the C ++ stlport_shared library (requires NDK r9 or later), add the following flags.
Compilation flags:
-L $ ANDROID_STLPORT_LIBSTDCPP / libs / x86 -lstlport_shared
ANDROID_STLPORT_LIBSTDCPP = $ NDK / sources / cxx-stl.
When using Intel Cilk Plus, the corresponding library is located in the / compiler / lib / ia32 / stlport folder.

Preparing JNI Calls

JNI call preparation is required to use Intel Cilk Plus for all versions of Android up to Android 4.3 (Jelly Bean MR2). The library must be loaded from Java code using the following API call:
System.loadLibrary ("cilkrts");
If the application requires a dynamic C ++ implementation, you need to load the appropriate library in addition to

System.loadLibrary ("gnustl_shared");
System.loadLibrary ("cilkrts");

System.loadLibrary ("stlport_shared");
System.loadLibrary ("cilkrts");

Using PGO to improve application performance on Android

Profile-guided Optimization (PGO) improves application performance by reordering code to eliminate inefficient caching, reduce code size, and detect incorrect branch prediction. PGO provides the compiler with information about the most frequently executed areas of the application. With information about these areas, the compiler can optimize the application.
To use PGO in Android (compared to other OSs), certain additional steps are required.
1. Add the following parameters to the C flags in the jni / file:
LOCAL_CFLAGS: = -prof-gen -prof-dir / sdcard .

2. Add permission WRITE_EXTERNAL_STORAGE to allow the application to write PGO output to the sdcard folder. Add the following line to the AndroidManifest.xml file:

3. Verify that profiling data is being recorded. Profiling data is recorded in default mode only when the application terminates. Android * apps usually don't shut down. Use the following options to force the application to quit or work around this problem.

A. Option 1. Call exit from Java code.
System.exit (0);

B. Option 2. Force dump of PGO data from native code.
_PGOPTI_Prof_Dump_All ();

B. Option 3. Using environment variables to regularly write performance data to sdcard media when the application is running. To make environment variables accessible to all applications, add them to the init.rc file of the Android image:

Note. The value of INTEL_PROF_DUMP_INTERVAL is measured in microseconds, it should not exceed INT_MAX.

4. Copying the created dyn files to the host in the application source folder:
adb pull ...

5. Changing the C flags in the file to use the created dyn files. You can use the -prof-dir option to specify a different file location.
LOCAL_CFLAGS: = -prof-use

See sectionProfile optimization in the user guide and reference guide for the Intel C ++ XE 15.0 compiler that ships with the package to learn how to use PGO to optimize applications created with the Intel C ++ compiler on Linux *, Windows *, and OS X *.

Other Related Articles and Resources

Intel System Studio - Multi-Core Programming with Intel Cilk Plus
Intel Cilk Plus
User Guide and Intel C ++ 15.0
Android * NDK Compiler Reference for Intel Architecture
Using Android * x86 NDK with Eclipse * and an Example of Porting an NDK Application

More Information on Intel Tools for Android Developers see the Intel Developer Zone for Android .

Also popular now: