zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

[ARM] Accelerate computing on Android

AndroidOnARM Computing
2023-09-27 14:23:24 时间

一系列学习笔记总结,对当前发展趋势做一次调研。

 

一、优化策略

From: 代码Android端加速

1. OpenCL优化(GPU)

编译成功后,在android端优化却不是非常明显,这个问题我们还在找。

Ref: 在Android Studio上使用OpenCL

    1. 判断手机是否支持OpenCL
    2. Google可能不太喜欢OpenCL,需要自己配置。

More details: Android OpenCL测试程序,使用dlopen动态加载libOpenCL.so库

 

2. NEON指令集优化(并行)

NEON指令集加速,是目前android端推荐最多的方式,基本的介绍可以百度,有很多文章。
这个优化有两种:
(1)直接使用NEON指令集封装好的C指令,网上有很多介绍,操作方法也是类C风格,比较容易上手。
(2)对寄存器写汇编进行优化。

第一种方法上手快,效果也比较明显;

第二种写起来会比较费劲,但加速效果会非常恐怖(前提是要对汇编指令足够熟悉)。 而这两种方法对于使用opencv的函数却没法优化。

 

二、NDK引入NEON

Ref: ARM和NEON指令

Ref: Optimizing C Code with Neon Intrinsics

Ref: A Software Library for Computer Vision and Machine Learning

 

  • 配置为NEON模式编译

Build for Android: https://arm-software.github.io/ComputeLibrary/v19.11.1/index.xhtml#S3_3_android

 

  • 集成进工程

相关问题:how to build for android #300

Did you manage to use ACL in your project?
There shouldn't be anything different from other libraries (Except that you might need to link against OpenCL if you're using it, in which case it will not work on Android O and after).

It means that because of some added security now applications can only link against system libraries that have been explicitly whitelisted by the manufacturer.
Therefore, even if OpenCL is available on your platform, you're unlikely to be able to use it in an App

 

相关问题:How to use the Binaries release (android version) ? #434

直接下载Binary版本。

.
├── android-arm64-v8a-cl
├── android-arm64-v8a-cl-asserts
├── android-arm64-v8a-cl-debug
├── android-arm64-v8a-neon
│ ├── libarm_compute_core.so
│ ├── libarm_compute_core-static.a
│ ├── libarm_compute_graph.so
│ ├── libarm_compute_graph-static.a
│ ├── libarm_compute.so
│ └── libarm_compute-static.a
├── android-arm64-v8a-neon-asserts
├── android-arm64-v8a-neon-cl
├── android-arm64-v8a-neon-cl-asserts
├── android-arm64-v8a-neon-cl-debug
├── android-arm64-v8a-neon-debug
├── android-armv7a-cl
├── android-armv7a-cl-asserts
├── android-armv7a-cl-debug
├── android-armv7a-neon
├── android-armv7a-neon-asserts
├── android-armv7a-neon-cl
├── android-armv7a-neon-cl-asserts
├── android-armv7a-neon-cl-debug
└── android-armv7a-neon-debug

 

Ref: Arm NN 软件开发套件

libarm_compute.so, libarm_compute_core.so, libarm_compute_graph.so

 

集成到 Android 项目中。

In order to use the compute library on Android there are few steps to follow to make everything working. Before starting to present the necessary steps, I would suggest to use the static libs of arm compute library (i.e. libarm_compute-static.a) rather than the dynamic ones and to use the arm compute library only with NEON acceleration as there are extra steps to follow for OpenCL.
Following I reported few necessary steps for using the arm compute library on Android. Hope this can help you.

Step
1: Add pre-built library for arm compute library in CMakeLists.txt along with the include directory You could have something like this: // Add includes for arm_compute include_directories(${PATH_ARM_COMPUTE}/include) // Add prebuilt library for arm_compute add_library( lib_arm_compute STATIC IMPORTED ) set_target_properties(lib_arm_compute PROPERTIES IMPORTED_LOCATION ${PATH_ARM_COMPUTE}/libs/${ANDROID_ABI}/libarm_compute-static.a) ... target_link_libraries( native-lib lib_arm_compute ${log-lib} ) where PATH_ARM_COMPUTE is the path to your arm_compute folder
Step2: Add necessary includes
in your .cpp file located in the JNI folder #include "arm_compute/runtime/NEON/NEFunctions.h" #include "arm_compute/runtime/CPP/CPPScheduler.h"
Step3
: Write your example
in Java_com_chenleiblogs_mlopencl_ml_1opencl_MainActivity_stringFromJNI( function In the arm compute library we have few examples which could be used (examples/ folder). Have a look at for instance to neon_convolution example. This should be a good example to integrate in your app.

 

 

/* implement */