1.优化例子
这里才从github上找了个开源的repo,只需要修改几个地方就可以配置arm64-v8的环境了
neon优化的链接
注意这里的repo含有子模块,git clone代码以后需要执行以下命令才可以下载完整的代码
git submodule init
git submodule update
1.1 编译步骤
- 1.下载ndk最新版本
- 2.安装cmake
- 3.安装adb等工具
- 4.sh build_android.sh进行编译和单元测试
build_android的说明:
set -e
set -x
basepath=$(cd `dirname $0`/; pwd)
BUILD_DIR=${basepath}/build
BUILD_ANDROID_NDK_HOME=/home/ldpe2g/Android/android-ndk-r15c #下载好的ndk的路径
DEPLOY_DIR=/data/local/tmp/ldp #手机上的路径
CMAKE=/home/ldpe2g/Android/cmake-3.11.0-Linux-x86_64/bin/cmake #安装好的cmake的路径
rm -rf ${BUILD_DIR}
if [[ ! -d ${BUILD_DIR} ]]; then
mkdir -p ${BUILD_DIR}
fi
cd ${BUILD_DIR}
$CMAKE \
-DCMAKE_TOOLCHAIN_FILE=${BUILD_ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake \
-DANDROID_NDK=${BUILD_ANDROID_NDK_HOME} \
-DANDROID_ABI=armeabi-v7a with NEON \ # 这里修改成 arm64-v8a
-DANDROID_NATIVE_API_LEVEL=9 \
-DANDROID_TOOLCHAIN=gcc \ #这里修改成clang
-DANDROID_ARM_NEON=TRUE \ #这一行可以删掉 因为64位的arm neon都是支持的
../
make all -j4
adb shell "mkdir -p ${DEPLOY_DIR}/lib"
adb push ${basepath}/build/unit_test/runUnitTests ${DEPLOY_DIR}
adb push ${basepath}/build/src/libboxfilter.so ${DEPLOY_DIR}/lib
adb shell "cd ${DEPLOY_DIR}; export LD_LIBRARY_PATH=./lib ; ./runUnitTests"
这个工程用的是googletest,很容易添加自己的功能和测试代码。。。
1.2 优化效果
[==========] Running 6 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 6 tests from netTest
[ RUN ] netTest.org_boxfilter
[0] BoxFilfer Cost time: 1679.91ms
[1] BoxFilfer Cost time: 1669.2ms
[2] BoxFilfer Cost time: 1668.06ms
[3] BoxFilfer Cost time: 1669.61ms
[4] BoxFilfer Cost time: 1667.7ms
[5] BoxFilfer Cost time: 1667.84ms
[6] BoxFilfer Cost time: 1677.63ms
[7] BoxFilfer Cost time: 1670.87ms
[8] BoxFilfer Cost time: 1667.56ms
[9] BoxFilfer Cost time: 1671.46ms
BoxFilfer Average Cost time: 1670.99ms
[ OK ] netTest.org_boxfilter (17257 ms)
[ RUN ] netTest.fast_boxfilter
[0] Fast BoxFilfer Cost time: 341.669ms
[1] Fast BoxFilfer Cost time: 341.137ms
[2] Fast BoxFilfer Cost time: 341.387ms
[3] Fast BoxFilfer Cost time: 340.85ms
[4] Fast BoxFilfer Cost time: 341.06ms
[5] Fast BoxFilfer Cost time: 341.151ms
[6] Fast BoxFilfer Cost time: 341.605ms
[7] Fast BoxFilfer Cost time: 340.94ms
[8] Fast BoxFilfer Cost time: 341.17ms
[9] Fast BoxFilfer Cost time: 340.895ms
Fast BoxFilfer Average Cost time: 341.186ms
[ OK ] netTest.fast_boxfilter (3958 ms)
[ RUN ] netTest.fast_boxfilter_v2
[0] Fast BoxFilfer V2 Cost time: 75.034ms
[1] Fast BoxFilfer V2 Cost time: 75.52ms
[2] Fast BoxFilfer V2 Cost time: 75.206ms
[3] Fast BoxFilfer V2 Cost time: 75.028ms
[4] Fast BoxFilfer V2 Cost time: 75.324ms
[5] Fast BoxFilfer V2 Cost time: 74.995ms
[6] Fast BoxFilfer V2 Cost time: 75.311ms
[7] Fast BoxFilfer V2 Cost time: 75.237ms
[8] Fast BoxFilfer V2 Cost time: 75.218ms
[9] Fast BoxFilfer V2 Cost time: 75.44ms
Fast BoxFilfer V2 Average Cost time: 75.2313ms
[ OK ] netTest.fast_boxfilter_v2 (1308 ms)
[ RUN ] netTest.fast_boxfilter_v2_neon_intrinsics
[0] Fast BoxFilfer V2 Neon Intrinsics Cost time: 72.076ms
[1] Fast BoxFilfer V2 Neon Intrinsics Cost time: 72.52ms
[2] Fast BoxFilfer V2 Neon Intrinsics Cost time: 72.959ms
[3] Fast BoxFilfer V2 Neon Intrinsics Cost time: 72.4ms
[4] Fast BoxFilfer V2 Neon Intrinsics Cost time: 72.465ms
[5] Fast BoxFilfer V2 Neon Intrinsics Cost time: 72.367ms
[6] Fast BoxFilfer V2 Neon Intrinsics Cost time: 72.753ms
[7] Fast BoxFilfer V2 Neon Intrinsics Cost time: 72.778ms
[8] Fast BoxFilfer V2 Neon Intrinsics Cost time: 72.297ms
[9] Fast BoxFilfer V2 Neon Intrinsics Cost time: 72.758ms
Fast BoxFilfer V2 Neon Intrinsics Average Cost time: 72.5373ms
[ OK ] netTest.fast_boxfilter_v2_neon_intrinsics (1272 ms)
[ RUN ] netTest.fast_boxfilter_v2_neon_asm
[0] Fast BoxFilfer V2 Neon Asm Cost time: 73.163ms
[1] Fast BoxFilfer V2 Neon Asm Cost time: 72.803ms
[2] Fast BoxFilfer V2 Neon Asm Cost time: 72.829ms
[3] Fast BoxFilfer V2 Neon Asm Cost time: 73.206ms
[4] Fast BoxFilfer V2 Neon Asm Cost time: 72.73ms
[5] Fast BoxFilfer V2 Neon Asm Cost time: 73.08ms
[6] Fast BoxFilfer V2 Neon Asm Cost time: 72.65ms
[7] Fast BoxFilfer V2 Neon Asm Cost time: 72.883ms
[8] Fast BoxFilfer V2 Neon Asm Cost time: 72.85ms
[9] Fast BoxFilfer V2 Neon Asm Cost time: 72.598ms
Fast BoxFilfer V2 Neon Asm Average Cost time: 72.8792ms
[ OK ] netTest.fast_boxfilter_v2_neon_asm (1275 ms)
[ RUN ] netTest.fast_boxfilter_v2_neon_asm_v2
[0] Fast BoxFilfer V2 Neon AsmV2 Cost time: 62.454ms
[1] Fast BoxFilfer V2 Neon AsmV2 Cost time: 63.037ms
[2] Fast BoxFilfer V2 Neon AsmV2 Cost time: 62.932ms
[3] Fast BoxFilfer V2 Neon AsmV2 Cost time: 63.445ms
[4] Fast BoxFilfer V2 Neon AsmV2 Cost time: 62.876ms
[5] Fast BoxFilfer V2 Neon AsmV2 Cost time: 63.137ms
[6] Fast BoxFilfer V2 Neon AsmV2 Cost time: 63.33ms
[7] Fast BoxFilfer V2 Neon AsmV2 Cost time: 63.589ms
[8] Fast BoxFilfer V2 Neon AsmV2 Cost time: 63.07ms
[9] Fast BoxFilfer V2 Neon AsmV2 Cost time: 63.237ms
Fast BoxFilfer V2 Neon AsmV2 Average Cost time: 63.1107ms
[ OK ] netTest.fast_boxfilter_v2_neon_asm_v2 (1177 ms)
[----------] 6 tests from netTest (26247 ms total)
[----------] Global test environment tear-down
[==========] 6 tests from 1 test suite ran. (26248 ms total)
[ PASSED ] 6 tests.
2. 通过adb shell 进行gdb调试的方法
- 在terminal中执行下面的脚本就可以在手机端开启一个服务,port=5039
DEPLOY_DIR=/TEST_ALGO #手机上要存放的测试数据
HOST_GDBSERVER=${BUILD_ANDROID_NDK_HOME}/prebuilt/android-arm64/gdbserver/gdbserver
HOST_GDB=${BUILD_ANDROID_NDK_HOME}/prebuilt/linux-x86_64/bin/gdb
HOST_APP=./algo/sample_test #编译的本地可执行sample
TARGET_APP=${DEPLOY_DIR}/sample_test
TARGET_GDBSERVER=/data/app/gdbserver
PORT=5039
adb forward tcp:${PORT} tcp:${PORT}
adb push ${HOST_GDBSERVER} ${TARGET_GDBSERVER}
adb shell ${TARGET_GDBSERVER} :${PORT} ${TARGET_APP}
- 在电脑上另开一个terminal输入下面的命令,就可以开始调试了
${BUILD_ANDROID_NDK_HOME}/prebuilt/linux-x86_64/bin/gdb ./sample_test #进入gdb调试交互界面
target remote:5039 # 在gdb 交互界面中输入,可以调试服务端的程序
由于理解的不全面,所以上面的描述也许不准确,但是按照上面的步骤是可以正确在adb 中进行gdb调试native c/c++的