how to let clBLAS run on Android - tingxingdong/clBLAS-private GitHub Wiki

Read this wiki first to get an idea building C/C++ on Android. I am using the Method 2 as in this page.

Also read NDK build first. I am using the CMake build here, since clBLAS only deal with CMake. I do not use Cradle in CMake build.

Step 1. Preparing.

1.1 Install Android Studio, SDK, NDK

I skipped the installation details here, as they are easy to find on Android website and easy to follow.

1.2 Be familiar with your ndk-bundle.

On my win10. It is C:\Users\tim.dong\AppData\Local\Android\Sdk\ndk On my Linux. It is /home/tim/Android/Sdk/ndk-bundle

cd C:\Users\tim.dong\AppData\Local\Android\Sdk\ndk\21.0.6113669\platforms\android-29\arch-arm64\usr\lib. It has tons of useful *.so you will need to link. Notice here, the android's bionic libc.so (mine is 264K) is much smaller than Linux standard c library (which is glibc usually as /usr/lib/x86_64-linux-gnu/libc.so.6).

A standalone compiler are in C:\Users\tim.dong\AppData\Local\Android\Sdk\ndk\21.0.6113669\toolchains\aarch64-linux-android-4.9\prebuilt\windows-x86_64

Be Familiar with your NDK tool-chain file. You must have it for cross-compilation in CMake. In normal builds, CMake automatically determines the toolchain for host builds based on system introspection and defaults. In cross-compiling scenarios, a toolchain file may be specified with information about compiler and utility paths.

My tool-chain file is C:\Users\tim.dong\AppData\Local\Android\Sdk\ndk\21.0.6113669\build\cmake\android.toolchain.cmake

Andorid SDK comes with a CMake. It is C:\Users\tim.dong\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin. You have to use Android CMake instead of system's CMake.

Notice, although I am using Windows. The Linux is the same.

Step 2.

If on Windows. I am using cmd.

cmake ../src -DCMAKE_ANDROID_STL_TYPE=c++_shared -DNETLIB=OFF -DNETLIB_PERSISTENT_OPENCL=ON -G "Ninja"

According to my version Cmake 3.10. It cannot generate on VS studio build files on Windows, but only Ninja. So you have use Ninja as your cmake generator.

However, clBLAS.so depends on other executable, like tplgen during the building. I do not know how to handle this in cmd. So I switch to Ubuntu.

On Linux, Open a terminal.

mkdir clBLAS_BUILD && cd clBLAS_BUILD

cmake ../src -DCMAKE_ANDROID_API=26

cd library

make VERBOSE=1

During the last link step. It is important to see If using NDK 16. I get /home/tim/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android --gcc-toolchain=/home/tim/Android/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64 --sysroot=/home/tim/Android/Sdk/ndk-bundle/sysroot -fPIC -isystem /home/tim/Android/Sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 (Do not know why not 26?) -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -Wno-narrowing -O2 -DNDEBUG

-Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a (excluding gcc library)

--sysroot /home/tim/Android/Sdk/ndk-bundle/platforms/android-21/arch-arm64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libclBLAS.so -o libclBLAS.so CMakeFiles/clBLAS.dir/common/list.c.o `` ``... `` ``CMakeFiles/clBLAS.dir/blas/AutoGemm/UserGemmKernelSources/UserGemmClKernels.cc.o /home/tim/Mali-OpenCL/lib/libOpenCL.so -lm -latomic -lm "/home/tim/Android/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/arm64-v8a/libgnustl_static.a"

If using NDK 18. I get

home/tim/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/home/tim/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/tim/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -Wno-narrowing -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libclBLAS.so -o libclBLAS.so CMakeFiles/clBLAS.dir/common/list.c.o CMakeFiles/clBLAS.dir/common/clkern.c.o ... CMakeFiles/clBLAS.dir/blas/AutoGemm/UserGemmKernelSources/UserGemmClKernels.cc.o /home/tim/Mali-OpenCL/lib/libOpenCL.so -Wl, -rpath /home/tim/Mali-OpenCL/lib -lm -latomic -lm

Step 3.

You might have successfully build clBLAS.so for (not on) Android or might not. You have verify it. open clBLAS/src/CMakelist.txt and turn BUILD_SAMPLE = ON. a sample build dir will appear. cd sample && make ./example_sgemm VERBOSE=1

Step 2. Push your build exe to the phone and run it on the phone.

On the bottom of your AS.There is a terminal. Use the "adb" tool like mine.

C:\Users\tim.dong\AppData\Local\Android\Sdk\platform-tools>adb devices -l will list all the devices. Be familiar with how to use adb.

You must have rooted the phone, enter the phone by

C:\Users\tim.dong\AppData\Local\Android\Sdk\platform-tools>adb shell

Take a look the *.so by cd /system/lib64. Many basic libraries are here, like libc.so libblas.so

type su to get root privilege.

the libOpenCL.so however is in /vendor/lib64 on my Galaxy phone. chmod 755 your_application

adb push hello /dev/sample/

这里要上传的目录必须是root用户所有的。

然后就是运行程序,可以在adb shell里测试

adb shell

cd /dev/sample/

chmod 777 hello

./hello

或者在手机上安装超级终端,在终端里运行

./hello

adb shell ls -laR | grep file_name_you_want to locate your want file.

Notice, if you use Window Command Line. It is '', for others like cmake. It should be changed to Unix style '/'

. If you see

ANDROID_PLATFORM not set. Defaulting to minimum supported version

Q&A: You may have some failures in building clBLAS_tune. clBLAS_tune is not part of clBLAS.so but a nice ultility tool to have. You can ignore the failures by "cd clBLAS_BUILD/library && make".

Notice clBLAS.so does dependent on executable tplgen. tplgen is build during you build clBLAS.so, it generate clBLAS required kernels. So it is safe to use host system's like gcc to build. Then its generated source kernels will be compiled into clBLAS.so.

Download OpenCL headers https://github.com/KhronosGroup/OpenCL-Headers find cl_version.h and add an line #define CL_TARGET_OPENCL_VERSION 200. As Mali OpenCL only support to 2.0