Skip to content

Commit

Permalink
Support OpenMP for OpenBLAS and Eigen
Browse files Browse the repository at this point in the history
  • Loading branch information
sh1r0 committed Jan 1, 2016
1 parent 6c68637 commit 3ac46b0
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PATH_add ~/android-sdk/platform-tools
export NDK_ROOT=~/android-ndk-r10e
PATH_add ${NDK_ROOT}
export ANDROID_ABI="armeabi-v7a with NEON"
export ANDROID_ABI="armeabi-v7a-hard-softfp with NEON"
export USE_OPENBLAS=1
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,21 @@ cd caffe-android-lib
```

### OpenBLAS
By defalut, Eigen is used as the underlying BLAS library to build caffe in this project.
And according to my experiments, there is little performance difference between the builds based on Eigen and OpenBLAS (issue [#27](https://github.com/sh1r0/caffe-android-lib/issues/27)).
But if you really desire to use OpenBLAS instead, please check the following steps.
In general, Eigen is used as the underlying BLAS library to build caffe in this project, since OpenBLAS for Android supports arm-based processors only.
But if you hope to use OpenBLAS instead, please check the following steps.

```shell
# 1. set OpenBLAS usage explicitly
export USE_OPENBLAS=1 # if 0, eigen is used
export USE_OPENBLAS=1 # if 0, Eigen is used

# 2. specify ANDROID_ABI in either case
# "armeabi-v7a with NEON": use the prebuilt library (bad performance)
# "armeabi-v7a-hard with NEON": build from the lastest sources (see the note below)
export ANDROID_ABI="armeabi-v7a-hard with NEON" # or "armeabi-v7a with NEON"
# "armeabi-v7a with NEON": use the prebuilt library (not recommended, slower than Eigen)
# "armeabi-v7a-hard-softfp with NEON": build from the lastest sources
export ANDROID_ABI="armeabi-v7a-hard-softfp with NEON" # or "armeabi-v7a with NEON"

# 3. Build
./build.sh <path/to/ndk>
```
Note: For "armeabi-v7a-hard with NEON", you can set `NUM_THREADS` to fit your need in `scripts/build_openblas_hard.sh`.
By default, it's 1 which means single-threaded.

## Issues
- Caffe build with Eigen cannot pass some tests ([ref](https://github.com/BVLC/caffe/pull/2619#issuecomment-113224948))
Expand Down
2 changes: 1 addition & 1 deletion android-cmake
8 changes: 6 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ export ANDROID_ABI="${ANDROID_ABI:-"armeabi-v7a with NEON"}"
export USE_OPENBLAS=${USE_OPENBLAS:-0}

if [ ${USE_OPENBLAS} -eq 1 ]; then
if [ "${ANDROID_ABI}" = "armeabi-v7a-hard with NEON" ]; then
if [ "${ANDROID_ABI}" = "armeabi-v7a-hard-softfp with NEON" ]; then
./scripts/build_openblas_hard.sh
else
elif [ "${ANDROID_ABI}" = "armeabi-v7a with NEON" ]; then
./scripts/get_openblas.sh
else
echo "Warning: not support OpenBLAS for ABI: ${ANDROID_ABI}, use Eigen instead"
export USE_OPENBLAS=0
./scripts/get_eigen.sh
fi
else
./scripts/get_eigen.sh
Expand Down
8 changes: 6 additions & 2 deletions scripts/build_caffe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ BOOST_HOME=${ANDROID_LIB_ROOT}/boost_1.56.0

USE_OPENBLAS=${USE_OPENBLAS:-0}
if [ ${USE_OPENBLAS} -eq 1 ]; then
if [ "${ANDROID_ABI}" = "armeabi-v7a-hard with NEON" ]; then
if [ "${ANDROID_ABI}" = "armeabi-v7a-hard-softfp with NEON" ]; then
OpenBLAS_HOME=${ANDROID_LIB_ROOT}/openblas-hard
else
elif [ "${ANDROID_ABI}" = "armeabi-v7a with NEON" ]; then
OpenBLAS_HOME=${ANDROID_LIB_ROOT}/openblas-android
else
echo "Error: not support OpenBLAS for ABI: ${ANDROID_ABI}"
exit 1
fi

BLAS=open
Expand All @@ -47,6 +50,7 @@ cmake -DCMAKE_TOOLCHAIN_FILE="${WD}/android-cmake/android.toolchain.cmake" \
-DANDROID_ABI="${ANDROID_ABI}" \
-DANDROID_NATIVE_API_LEVEL=21 \
-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9 \
-DANDROID_USE_OPENMP=ON \
-DADDITIONAL_FIND_PATH="${ANDROID_LIB_ROOT}" \
-DBUILD_python=OFF \
-DBUILD_docs=OFF \
Expand Down
2 changes: 1 addition & 1 deletion scripts/build_openblas_hard.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ make -j${N_JOBS} \
CC="$TOOLCHAIN_DIR/arm-linux-androideabi-gcc --sysroot=$NDK_ROOT/platforms/android-21/arch-arm" \
CROSS_SUFFIX=$TOOLCHAIN_DIR/arm-linux-androideabi- \
HOSTCC=gcc NO_LAPACK=1 TARGET=ARMV7 \
USE_THREAD=1 NUM_THREADS=1
USE_THREAD=1 NUM_THREADS=8 USE_OPENMP=1

rm -rf "$INSTALL_DIR/openblas-hard"
make PREFIX="$INSTALL_DIR/openblas-hard" install
Expand Down

0 comments on commit 3ac46b0

Please sign in to comment.