📜 ⬆️ ⬇️

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

The Intel C ++ Compiler provides many opportunities for optimizing applications for a variety of tasks, including mobile devices. In this article, we will cover two aspects of optimization: first, let's talk about using the executable Intel Cilk Plus module in Android to implement application multithreading, and second, let's touch on the use of Profile-guided Optimization (PGO) to improve the performance of Android applications. Links for a more in-depth study of these topics are given at the end of the article.

Using the 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 form a convenient environment for creating parallel applications.
When using Intel Cilk Plus, linking to the Cilk runtime library (libcilkrts.so) is required to implement multithreading in applications.

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


The NDK build system does not link the C ++ libraries for modules written in C, with the result that the compiler cannot select the correct Intel Cilk Plus library during linking, which can lead to linking 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 Application.mk file.
'APP_STL: = stlport_shared' or
'APP_STL: = gnustl_static' or
'APP_STL: = gnustl_shared'
')
3. Modify the Java code as described in the “Preparing JNI Calls” section below.

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


If your development environment contains C ++ code, you must explicitly link to the GNU_STL or stlport library.
Perform the following steps.
1. Specify the following flags for compilation and linking for the corresponding C ++ implementation in the NDK.
Compile flags:
-I $ ANDROID_GNU_LIBSTDCPP / include -I $ ANDROID_GNU_LIBSTDCPP / libs / x86 / include
Link flags (gnustl_shared):
-L $ ANDROID_GNU_LIBSTDCPP / libs / x86 -lgnustl_shared -lsupc ++
Link flags (gnustl_static):
-L $ ANDROID_GNU_LIBSTDCPP / libs / x86 -lgnustl_static -lsupc ++
Where
ANDROID_GNU_LIBSTDCPP = $ NDK / sources / cxx-stl / gnu-libstdc ++ / 4.6. 4.6 should be replaced with the version of GCC 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 replace 4.6 with 4.8.
When using Intel Cilk Plus, linking with the library libcilkrts.so is required. This library is located in the / compiler / lib / ia32 / gnustl folder.

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

JNI call preparation


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

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

Use PGO to improve application performance in Android OS


Profile Optimization (Profile-guided Optimization, PGO) improves application performance by reordering the code to eliminate inefficient caching, reduce the amount of code and identify incorrect predictions during branching. PGO provides the compiler with information on the most frequently executed areas of the application. Possessing information about these areas, the compiler can optimize application performance.
Using PGO on Android (compared to other OS) requires some additional steps.
1. Add the following parameters 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 to write 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 profiling data is recorded. Profiling data is recorded in the default mode only when the application is shut down. Android * applications do not usually shut down. Use the following options to force the application to shut down or work around this issue.

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

B. Option 2. Forced output of PGO data dump from native code.
#include <pgouser.h>
_PGOPTI_Prof_Dump_All ();

B. Option 3. Using environment variables to regularly write performance data to sdcard media while the application is running. To make the environment variables available to 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 INTEL_PROF_DUMP_INTERVAL value is measured in microseconds, it should not exceed INT_MAX.

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

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

See the Profile Optimization section of the Intel C ++ XE 15.0 Compiler User Guide and Reference Manual that came with the package for information on using PGO to optimize applications built 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
Intel C ++ 15.0 Compiler User Guide and Reference Manual
Android * NDK for Intel Architecture
Using Android * x86 NDK with Eclipse * and an example of porting an NDK application

For more information about Intel for Android developers, see the Intel Developer Zone for Android .

Source: https://habr.com/ru/post/253270/


All Articles