How to Compile Torch-Android with OpenBLAS Support

This post aims to provide a practical guidance to how to compile Torch-Android deep learning tool with OpenBLAS support. All the procedure is target for UNIX-like environment.
I presume that you know what BLAS or OpenBLAS is, if you don’t click the blue word to find out.

Prepare Source Code

You can find OpenBLAS on Github OpenBLAS’s repo..
Get the source code by Git.

1
git clone https://github.com/xianyi/OpenBLAS.git

If this link doesn’t work for some reason, (I believe this project will keep going on, people are still working on it, I cannot guarantee today’s compiling method will work for tomorrow.) You can find a forked version by:

1
git clone https://github.com/yeephycho/OpenBLAS.git

Set Up Environment for OpenBLAS

Because we are going to compile the source code under Android environment, so I suppose you have already downloaded Android NDK which will be used as the compiling tool for the OpenBLAS.
First, tell the NDK compiler where to find the system environment, basic C header files and libraries will be found under this environment, you can find this environment under NDK folder.

1
2
3
export CFLAGS=--sysroot=/path/to/your/android/ndk/platforms/android-21/arch-arm
# For me the example is:
export CFLAGS=--sysroot=/hom/yeephycho/Android/android-ndk-r11/platforms/android-21/arch-arm

Next, set OpenMP multi-thread environment variables if you like.

1
2
3
4
# Enable OpenMP.
export USE_OPENMP=1
# Set OpenMP thread number according to your OS and hardware platform.
export OMP_NUM_THREADS=8

Compile OpenBLAS without Fortran.

BLAS was originally writen by Fortran. But NDK doesn’t provide Fortran support, it means that there’s no Fortran compiler under Android environment. So, what we are going to do is to compile the C version OpenBLAS.
According to the OpenBLAS-Android main page.

1
2
3
4
cd OpenBLAS
make TARGET=ARMV7 HOSTCC=gcc CC=/path/to/your/android/ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc NOFORTRAN=1
# For me, the example is:
make TARGET=ARMV7 HOSTCC=gcc CC=/home/yeephycho/Android/android-ndk-r11/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc NOFORTRAN=1

Change the above path according to your environment, NDK path and Android NDK version.

Compiling should begin at this time. Wait for a moment, it will take a few minutes.
After compiling finished, install OpenBLAS to a specific folder.

1
2
3
4
make PREFIX=/path/to/your/installation/ install
# For me, the example is:
make PREFIX=/home/yeephycho/Desktop/OpenBLAS_Installation
# Make sure the folder exists.

Now, we have a OpenBLAS library and associated include files under the installation folder.

Compile Torch-Android

This part is relatively difficult to write because just a few days ago, I tried to git clone Torch-Android project from github, the project cannot be compiled any more. Because of some links has already out of date. I never tested it, but there are some saying claims that the missing folder can be download manually or just copy from the linux version’s Torch project.
So, if you are lucky enough to have an older version without OpenBLAS support. You can try to do the followings to add OpenBLAS support:
Try to find out which android environment is used by your NDK. Under my environment it is (I don’t realy know how to make sure of it, try to find it out from the messages of build.sh during execution? Or maybe you can try to set the NDK platform version by add an Application.mk file under the project?):
Anyway, the thing that you need to do is just to copy the compiled OpenBLAS include file and libraries from your installation folder to the environment folder. Paste and copy will work.

1
2
cp /home/yeephycho/Desktop/OpenBLAS_Installation/include/* /home/yeephycho/Android/android-ndk-r11/platforms/android-8/arch-arm/usr/include/
cp /home/yeephycho/Desktop/OpenBLAS_Installation/lib/* /home/yeephycho/Android/android-ndk-r11/platforms/android-8/arch-arm/usr/lib/

Then, try to run build.sh from your torch-android folder.
If it says that “No rule to make target ‘install’. Stop.”, just run it again.

Follow ups

I hope this post can be of help, but, acctual, there’s some details missing because of the torch-android’s problem.
I’d glad to provide necessary help if anyone need any. My e-mail is: huyixuanhyx@gmail.com.

Cheers!


License


The content of this blog itself is licensed under the Creative Commons Attribution 4.0 International License.
CC-BY-SA LICENCES

The containing source code (if applicable) and the source code used to format and display that content is licensed under the Apache License 2.0.
Copyright [2016] [yeephycho]
Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Apache License 2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language
governing permissions and limitations under the License.
APACHE LICENCES