Skip to content

Fixed OpenBLAS builds #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions build-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Install necessary packages
echo "Installing necessary packages"
sudo apt-get update && sudo apt-get install -y --no-install-recommends \
automake \
build-essential \
ca-certificates \
curl \
cmake \
file \
libtool \
pkg-config \
unzip \
wget
echo "Package Installation completed"

echo "Getting OpenBLAS"
git clone --recursive https://github.com/solrex/caffe-mobile.git

# Setup Patch - Caffe Mobile
cp patch/caffe-mobile/build-openblas.sh caffe-mobile/third_party/

echo "Using NDK: "
echo $NDK_ROOT

echo "Building OpenBLAS"
# Build openblas lib for all ANDROID_ABIs
cd caffe-mobile/third_party
export ANDROID_ABI=arm64-v8a
./build-openblas.sh
export ANDROID_ABI=x86_64
./build-openblas.sh
export ANDROID_ABI=armeabi
./build-openblas.sh
cd ../../

# (DIR caffe-android-lib)

echo "Building Caffe"
# Build Caffe for ANDROID_ABI = arm64-v8a
mkdir -p android_lib
cp -r caffe-mobile/third_party/arm64-v8a-21-OpenBLAS/. android_lib/
export ANDROID_ABI=arm64-v8a
./build-single.sh
mv android_lib android_lib_arm64-v8a

# Build Caffe for ANDROID_ABI = x86_64
mkdir -p android_lib
cp -r caffe-mobile/third_party/x86_64-21-OpenBLAS/. android_lib/
export ANDROID_ABI=x86_64
./build-single.sh
mv android_lib android_lib_x86_64

# Build Caffe for ANDROID_ABI = armeabi
mkdir -p android_lib
cp -r caffe-mobile/third_party/armeabi-21-OpenBLAS/. android_lib/
export ANDROID_ABI=armeabi
./build-single.sh
mv android_lib android_lib_armeabi


echo "Collecting Builds"
# Copy output header files to single distribution directory
mkdir -p distribution/caffe/include
cp -r android_lib_arm64-v8a/include/. distribution/caffe/include
cp -r android_lib_arm64-v8a/boost/include/. distribution/caffe/include
cp -r android_lib_arm64-v8a/caffe/include/. distribution/caffe/include
cp -r android_lib_arm64-v8a/gflags/include/. distribution/caffe/include
cp -r android_lib_arm64-v8a/glog/include/. distribution/caffe/include
cp -r android_lib_arm64-v8a/protobuf/include/. distribution/caffe/include
cp -r opencv/include/. distribution/caffe/include
rm distribution/caffe/include/CMakeLists.txt

# Copy output .so files to single distribution directory
mkdir -p distribution/caffe/lib/arm64-v8a
cp android_lib_arm64-v8a/caffe/lib/libcaffe.so distribution/caffe/lib/arm64-v8a
cp android_lib_arm64-v8a/caffe/lib/libcaffe_jni.so distribution/caffe/lib/arm64-v8a

mkdir -p distribution/caffe/lib/x86_64
cp android_lib_x86_64/caffe/lib/libcaffe.so distribution/caffe/lib/x86_64
cp android_lib_x86_64/caffe/lib/libcaffe_jni.so distribution/caffe/lib/x86_64

mkdir -p distribution/caffe/lib/armeabi
cp android_lib_armeabi/caffe/lib/libcaffe.so distribution/caffe/lib/armeabi
cp android_lib_armeabi/caffe/lib/libcaffe_jni.so distribution/caffe/lib/armeabi

echo ""
echo "Android Caffe Build Completed. Get it from distribution directory"
echo "Copy distribution directory to your android project and give its path in CMakeList"
echo ""
23 changes: 23 additions & 0 deletions build-single.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

set -eu

# shellcheck source=/dev/null
. "$(dirname "$0")/config.sh"

pushd "${PROJECT_DIR}"

./scripts/build_boost.sh
./scripts/build_gflags.sh
./scripts/build_glog.sh
./scripts/build_lmdb.sh
./scripts/build_snappy.sh
./scripts/build_leveldb.sh
./scripts/build_opencv.sh
./scripts/build_protobuf_host.sh
./scripts/build_protobuf.sh
./scripts/build_caffe.sh

popd

echo "DONE!!"
160 changes: 160 additions & 0 deletions patch/caffe-mobile/build-openblas.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#!/bin/bash

PLATFORM=Android

# Options for All
OPENBLAS_VERSION=0.2.19
MAKE_FLAGS="$MAKE_FLAGS -j 4"
BUILD_DIR=".cbuild"

# Options for Android
# Caffe-Mobile Tested ANDROID_ABI: arm64-v8a, armeabi, armeabi-v7a with NEON
if [ "$ANDROID_ABI" = "" ]; then
ANDROID_ABI="arm64-v8a"
fi
if [ "$ANDROID_NATIVE_API_LEVEL" = "" ]; then
ANDROID_NATIVE_API_LEVEL=21
fi

if [ $ANDROID_NATIVE_API_LEVEL -lt 21 -a "$ANDROID_ABI" = "arm64-v8a" ]; then
echo "ERROR: This ANDROID_ABI($ANDROID_ABI) requires ANDROID_NATIVE_API_LEVEL($ANDROID_NATIVE_API_LEVEL) >= 21"
exit 1
fi

# Build Environment
if [ "$(uname)" = "Darwin" ]; then
OS=darwin
elif [ "$(expr substr $(uname -s) 1 5)" = "Linux" ]; then
OS=linux
elif [ "$(expr substr $(uname -s) 1 10)" = "MINGW64_NT" ] || [ "$(expr substr $(uname -s) 1 9)" = "CYGWIN_NT" ]; then
OS=windows
else
echo "Unknown OS"
exit 1
fi

if [ "$(uname -m)" = "x86_64" ]; then
BIT=x86_64
else
BIT=x86
fi

echo "$(tput setaf 2)"
echo Building Openblas for $PLATFORM
echo "$(tput sgr0)"

RUN_DIR=$PWD

function fetch-OpenBLAS {
echo "$(tput setaf 2)"
echo "##########################################"
echo " Fetch Openblas $OPENBLAS_VERSION from source."
echo "##########################################"
echo "$(tput sgr0)"

if [ ! -f OpenBLAS-${OPENBLAS_VERSION}.tar.gz ]; then
curl -L https://github.com/xianyi/OpenBLAS/archive/v${OPENBLAS_VERSION}.tar.gz --output OpenBLAS-${OPENBLAS_VERSION}.tar.gz
fi
if [ -d OpenBLAS-${OPENBLAS_VERSION} ]; then
rm -rf OpenBLAS-${OPENBLAS_VERSION}
fi
tar -xzf OpenBLAS-${OPENBLAS_VERSION}.tar.gz
}


function fetch-OpenBLAS-softfp {
OPENBLAS_VERSION=arm_soft_fp_abi
echo "$(tput setaf 2)"
echo "##########################################"
echo " Fetch Openblas $OPENBLAS_VERSION from source."
echo "##########################################"
echo "$(tput sgr0)"

if [ ! -f OpenBLAS-${OPENBLAS_VERSION}.zip ]; then
curl -L https://github.com/xianyi/OpenBLAS/archive/${OPENBLAS_VERSION}.zip --output OpenBLAS-${OPENBLAS_VERSION}.zip
fi
if [ -d OpenBLAS-${OPENBLAS_VERSION} ]; then
rm -rf OpenBLAS-${OPENBLAS_VERSION}
fi
unzip -q OpenBLAS-${OPENBLAS_VERSION}.zip
}

function build-Android {
echo "$(tput setaf 2)"
echo "#####################"
echo " Building OpenBLAS for $PLATFORM"
echo "#####################"
echo "$(tput sgr0)"

# Test ENV NDK_HOME
if [ ! -d "$NDK_HOME" ]; then
echo "$(tput setaf 2)"
echo "###########################################################"
echo " ERROR: Invalid NDK_HOME=\"$NDK_HOME\" env variable, exit. "
echo "###########################################################"
echo "$(tput sgr0)"
exit 1
fi

if [ "${ANDROID_ABI}" = "armeabi-v7a with NEON" ]; then
CROSS_SUFFIX=$NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/${OS}-${BIT}/bin/arm-linux-androideabi-
SYSROOT=$NDK_HOME/platforms/android-$ANDROID_NATIVE_API_LEVEL/arch-arm
TARGET=ARMV7
BINARY=32
ARM_SOFTFP_ABI=1
elif [ "${ANDROID_ABI}" = "arm64-v8a" ]; then
CROSS_SUFFIX=$NDK_HOME/toolchains/aarch64-linux-android-4.9/prebuilt/${OS}-${BIT}/bin/aarch64-linux-android-
SYSROOT=$NDK_HOME/platforms/android-$ANDROID_NATIVE_API_LEVEL/arch-arm64
TARGET=ARMV8
BINARY=64
ARM_SOFTFP_ABI=0
elif [ "${ANDROID_ABI}" = "x86_64" ]; then
CROSS_SUFFIX=$NDK_HOME/toolchains/x86_64-4.9/prebuilt/${OS}-${BIT}/bin/x86_64-linux-android-
SYSROOT=$NDK_HOME/platforms/android-$ANDROID_NATIVE_API_LEVEL/arch-x86_64
TARGET=ATOM
BINARY=64
elif [ "${ANDROID_ABI}" = "armeabi" ]; then
CROSS_SUFFIX=$NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/${OS}-${BIT}/bin/arm-linux-androideabi-
SYSROOT=$NDK_HOME/platforms/android-$ANDROID_NATIVE_API_LEVEL/arch-arm
TARGET=ARMV5
BINARY=32
ARM_SOFTFP_ABI=1
sed -i -e 's/_MSC_VER/FORCE_OPENBLAS_COMPLEX_STRUCT/g' OpenBLAS-${OPENBLAS_VERSION}/kernel/arm/zdot.c || exit 1
else
echo "Error: not support OpenBLAS for ABI: ${ANDROID_ABI}"
exit 1
fi

PREFIX=${ANDROID_ABI%% *}-$ANDROID_NATIVE_API_LEVEL-OpenBLAS
mkdir -p $PREFIX
if [ ! -s $PREFIX/lib/libopenblas.a ]; then
cd OpenBLAS-$OPENBLAS_VERSION
make ${MAKE_FLAGS} \
NOFORTRAN=1 \
NO_NOLAPACKE=1 \
OSNAME=Android \
SMP=1 \
USE_THREAD=1 \
NUM_THREAD=4 \
CROSS_SUFFIX="$CROSS_SUFFIX" \
CC="${CROSS_SUFFIX}gcc --sysroot=$SYSROOT" \
HOSTCC=gcc \
TARGET=$TARGET \
ARM_SOFTFP_ABI=$ARM_SOFTFP_ABI \
BINARY=$BINARY
make \
SMP=1 \
PREFIX="../$PREFIX" \
install
cd ..
fi
rm -rf OpenBLAS
ln -s $PREFIX OpenBLAS
}

if [ "${ANDROID_ABI}" = "armeabi-v7a with NEON" ]; then
fetch-OpenBLAS-softfp
else
fetch-OpenBLAS
fi
build-$PLATFORM