Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

How to compile Amalgamation for android? #7146

Closed
ZaccurLi opened this issue Jul 21, 2017 · 14 comments
Closed

How to compile Amalgamation for android? #7146

ZaccurLi opened this issue Jul 21, 2017 · 14 comments

Comments

@ZaccurLi
Copy link

How to compile Amalgamation for android?

Environment info

Operating System: Ubuntu 14.04 64bit Desktop
NDK vesion: android-ndk-r13b or android-ndk-r14b
Compiler: arm-linux-androideabi-clang++
MXNet version: v0.10.0 or v0.9.3
MXNet commit hash : * master 799ed45

Error Message:

1. fatal error: src/c_api/c_api_error.cc: No such file or directory #include <src/c_api/c_api_error.cc>
2. fatal error: execinfo.h: No such file or directory  #include <execinfo.h>
3. error: use of undeclared identifier 'fopen64'; did you mean 'fopen'?

Steps to Compile

Prepare:

NDK Standalone ToolChain:

cd yourpath/android-ndk-r13b/build/tools
python make_standalone_toolchain.py --arch arm --api 21 --install-dir yourpath/android-toolchain --stl=libc++  

Download and Compile OpenBLAS

export PATH=$PATH:yourpath/android-toolchain/bin 
git clone https://github.com/xianyi/OpenBLAS.git  
cd OpenBLAS  
make TARGET=ARMV7 HOSTCC=gcc CC=arm-linux-androideabi-gcc NOFORTRAN=1
make PREFIX=yourpath/openblas install

Compile:

Download Mxnet

git clone --recursive https://github.com/dmlc/mxnet.git 

Compile Amalgamation
1.move into mxnet/amalgamation and prepare compile toolchain

cd mxnet/amalgamation
export PATH=$PATH:yourpath/android-toolchain/bin
export CC=arm-linux-androideabi-clang  
export CXX=arm-linux-androideabi-clang++  

2.modify OpenBLAS path in mxnet/amalgamation/Makefile:(line 5)

export OPENBLAS_ROOT=yourpath/openblas

3.add DEFS in mxnet/nnvm/amalgamation/Makefile:(line 2)

DEFS+=-DMSHADOW_USE_CUDA=0 -DMSHADOW_USE_MKL=0 -DMSHADOW_RABIT_PS=0 -DMSHADOW_DIST_PS=0 -DMSHADOW_USE_SSE=0 -DDMLC_LOG_STACK_TRACE=0 -DMSHADOW_FORCE_STREAM -DMXNET_USE_OPENCV=0 -DMXNET_PREDICT_ONLY=1 -DDISABLE_OPENMP=1
export CFLAGS = -std=c++11 -Wall -O3 -Wno-unknown-pragmas -funroll-loops -Iinclude -fPIC $(DEFS)

4.add #define fopen64 std::fopen to Amalgamation.py(line 130) or mxnet_predict-all.cc(line 1)
4.1 In Amalgamation.py

...
print >>f, '''
#define fopen64 std::fopen      #comment<<<<<<line:130
#if defined(__MACH__)
#include <mach/clock.h>
#include <mach/mach.h>
#endif
...

or
4.2 In mxnet_predict-all.cc

#define fopen64 std::fopen      //comment<<<<<<line:1
#if defined(__MACH__)
...

5.compile amalgamation

make clean
make ANDROID=1

Library

Output: jni_libmxnet_predict.so
copy jni_libmxnet_predict.so and libc++_shared.so to android project

@piiswrong
Copy link
Contributor

Would you like to submit a pr for the fopen fix?

@edmBernard
Copy link

edmBernard commented Jul 21, 2017

@ZaccurLi when I retry to use your method I got an error on OpenBLAS compilation :

/home/dev/lib/arm7-toolchain/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lm_hard

how did you solve this problem ?

@edmBernard
Copy link

I test it different ndk version :
it work on android-ndk-12b, android-ndk-13b, android-ndk-14b
but don't work on android-ndk-15b

@edmBernard
Copy link

I made a Dockerfile to automate mxnet compilation for android and resume your process.
https://github.com/edmBernard/DockerFiles/blob/master/cpu/android_mxnet/Dockerfile

@Corea
Copy link

Corea commented Jul 24, 2017

Did you successfully run an android app with output library? I got following errors with version 0.10.0 built by android-ndk-r14b on Mac OS X 10.12.5.

  • cannot find "libopenblas.so" (Solved)
  • cannot locate symbol "__emutls_v._ZN5mxnet8autograd15AutogradRuntime9is_train_E"

@edmBernard
Copy link

not yet I'll test this week and give you feedback if it works (or not)

@ghost
Copy link

ghost commented Jul 25, 2017

hi @Corea indeed with @edmBernard we had the same log error after adding libopenblas.so with the jniLibs list
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "__emutls_v._ZN5mxnet8autograd15AutogradRuntime9is_train_E" referenced by "/data/app/com.example.admin.mod-1/lib/arm/libmxnet_predict.so"...

@edmBernard
Copy link

there is the same issue here : #7180

@Piyush3dB
Copy link
Contributor

PR #7222 should fix this issue. Please try it out.

@edmBernard
Copy link

edmBernard commented Jul 27, 2017

thanks it work fine for me

@eric-haibin-lin
Copy link
Member

eric-haibin-lin commented Sep 15, 2017 via email

@novioleo
Copy link

@ZaccurLi
i followed your tutorial step by step,i generated the predict.so successfully.but,i can't run my android application with it . because the symbol.json can't interpret rightly.
so,can you send me your complied predict.so,or help me with this problem?

@ghost
Copy link

ghost commented Jun 29, 2018

@Corea i have met the same problem 'can not find openblas.so', how did you solve it? thanks

@edmBernard
Copy link

@zhulihuacmss
Hi, You have to change the path in amalgamation Makefile: here

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants