Skip to content
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

Issue building android-demo #4

Closed
nicholas-leonard opened this issue Feb 18, 2015 · 18 comments
Closed

Issue building android-demo #4

nicholas-leonard opened this issue Feb 18, 2015 · 18 comments

Comments

@nicholas-leonard
Copy link
Collaborator

Hi soumith,

So I was able to build using the first build.sh. Then I manually copied the static .a libs to the lib folder. But then when I get to compiling the android-demo using that build.sh, I get the following error:

nicholas14@hermes:~/projects/torch-android/android-demo$ bash build.sh 
Updated local.properties
Updated file ./proguard-project.txt
Android NDK: WARNING: APP_PLATFORM android-17 is larger than android:minSdkVersion 14 in ./AndroidManifest.xml    
Android NDK: WARNING:jni/Android.mk:torchdemo: non-system libraries in linker flags: -lgomp -lluaT -ltorch-lua-static -lTH -lnn -ltorch -lnnx -limage -limgraph -lluaT -ltorch-lua-static -lTH -lnn -ltorch -lnnx -limage -limgraph    
Android NDK:     This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES    
Android NDK:     or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the    
Android NDK:     current module    
[armeabi-v7a] Compile++ thumb: torchdemo <= torchandroid.cpp
[armeabi-v7a] Compile++ thumb: torchdemo <= torchdemo.cpp
[armeabi-v7a] Compile thumb  : torchdemo <= android_fopen.c
[armeabi-v7a] SharedLibrary  : libtorchdemo.so
../lib//libnn.a(init.c.o):init.c:function nn_FloatSqrt_updateGradInput: error: undefined reference to 'GOMP_parallel'
../lib//libnn.a(init.c.o):init.c:function nn_FloatSquare_updateGradInput: error: undefined reference to 'GOMP_parallel'
../lib//libnn.a(init.c.o):init.c:function nn_FloatSquare_updateOutput: error: undefined reference to 'GOMP_parallel'
../lib//libnn.a(init.c.o):init.c:function nn_FloatHardTanh_updateGradInput: error: undefined reference to 'GOMP_parallel'
collect2: error: ld returned 1 exit status
@soumith
Copy link
Owner

soumith commented Feb 19, 2015

I dont have computer access today, will look into it tomorrow. It has to just be linked against openmp with the compiler flag -fopenmp

@nicholas-leonard
Copy link
Collaborator Author

Yeah the -fopenmp is part of the build command, but still doesn't work:

/home/nicholas14/Downloads/android-ndk-r10d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ -Wl,-soname,libtorchdemo.so -shared --sysroot=/home/nicholas14/Downloads/android-ndk-r10d/platforms/android-17/arch-arm ./obj/local/armeabi-v7a/objs/torchdemo/torchandroid.o ./obj/local/armeabi-v7a/objs/torchdemo/torchdemo.o ./obj/local/armeabi-v7a/objs/torchdemo/android_fopen.o /home/nicholas14/Downloads/android-ndk-r10d/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/thumb/libgnustl_static.a -lgcc -no-canonical-prefixes -march=armv7-a -Wl,--fix-cortex-a8  -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -mthumb -fopenmp -L/home/nicholas14/Downloads/android-ndk-r10d/platforms/android-17/arch-arm/usr/lib -llog -landroid -L../lib/ -lgomp -lluaT -ltorch-lua-static -lTH -lnn -ltorch -lnnx -limage -limgraph -lluaT -ltorch-lua-static -lTH -lnn -ltorch -lnnx -limage -limgraph -lc -lm -lgomp -o ./obj/local/armeabi-v7a/libtorchdemo.so 
../lib//libnn.a(init.c.o):init.c:function nn_FloatSqrt_updateGradInput: error: undefined reference to 'GOMP_parallel'
../lib//libnn.a(init.c.o):init.c:function nn_FloatSquare_updateGradInput: error: undefined reference to 'GOMP_parallel'
../lib//libnn.a(init.c.o):init.c:function nn_FloatSquare_updateOutput: error: undefined reference to 'GOMP_parallel'
../lib//libnn.a(init.c.o):init.c:function nn_FloatHardTanh_updateGradInput: error: undefined reference to 'GOMP_parallel'
collect2: error: ld returned 1 exit status

@nicholas-leonard
Copy link
Collaborator Author

Which is related to all these lines in the c code of nn's prebuilt static library:

#pragma omp parallel for private(k)

@nicholas-leonard
Copy link
Collaborator Author

Error happens on both ubuntu 12.04 and 14.04.

@nicholas-leonard
Copy link
Collaborator Author

Ok so I was able to build the android-demo but only after disabling OPENMP compilation from torch and nnx. I don't want to submit a PR for my changes because I believe there should be a way to compile the torch and nnx static libraries with the same OpenMP libraries used by the new ndk versions that support it.

@jgemmeke
Copy link

EDIT: Seems I was mistaken: it is simply a linking problem. For some reason the libgomp in the toolchain is not linked during compilation. Copying it from the android toolchain subdirs to ../lib worked for me, adding -lgomp did not.

But I don't know enough about android app compilation to come up with the proper fix.

That said:

  • the camera-dependent apps still don't work for me - I get errors about preview sizes not being supported. Only the barebones torchdemo app works
  • I cannot get the neon version build - I get assembly compilation errors (which usually indicate that the neon compile flags are not set, but the cmak scripts do seem to set them)

@soumith
Copy link
Owner

soumith commented Feb 21, 2015

@jgemmeke NEON might not compile on armv8, there's some armv7 specific assembly there (wish that my work at @museami would be open-sourced, I rewrote all of it and a faster version for v8 as well).

the camera preview-sizes error is an android thing. you must be using some non-standard camera, we wrote these demos targeting most standard phones.

@rio0286
Copy link
Collaborator

rio0286 commented Feb 23, 2015

For your information,
I got same library copy problem as other issue after updating android.toolchain.cmake for ndk version r10, a month ago.
So I manually copied libraries for testing but it does not work with this demo build error.
I found that some function like __srget removed after ndk version r10.
I think these functions are used in torch code and I cannot fix it.
So I rollbacked android.toolchain.cmake to r9.

you can find this information in this page with click on Android NDK, Revision 10c (October 2014)
https://developer.android.com/tools/sdk/ndk/index.html

@rio0286
Copy link
Collaborator

rio0286 commented Feb 23, 2015

@jgemmeke you can fix that camera-dependent app error with removing some lines in demo code.

CameraClass.jave 65th line parameters.setPreviewSize(1280, 768);

After commenting this line, preview size remains as your phone's default configuration and it may work.
but the recognition result may not good.

I think that will be nice if there are some resizing step instead of using fixed preview size(1280,768)

@nicholas-leonard
Copy link
Collaborator Author

@jgemmeke Tried dropping the static libgomp.a and linking it during the compilation of the android demo, but still get the same errors. What am I missing?

My Android.mk file:

LOCAL_PATH := $(call my-dir)

LOCAL_CFLAGS += -fopenmp
LOCAL_LDFLAGS += -fopenmp

LOCAL_EXPORT_C_INCLUDES := $(TORCH_ANDROID)/include/torch
LOCAL_MODULE := gomp
LOCAL_SRC_FILES := /home/nicholas14/Downloads/android-ndk-r10d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/arm-linux-androideabi/lib/libgomp.a
include $(PREBUILT_STATIC_LIBRARY)
LOCAL_STATIC_LIBRARIES += gomp

LOCAL_MODULE := torch-lua-static
LOCAL_SRC_FILES := $(TORCH_ANDROID)/lib/libtorch-lua-static.a
include $(PREBUILT_STATIC_LIBRARY)

LOCAL_MODULE := luaT
LOCAL_SRC_FILES := $(TORCH_ANDROID)/lib/libluaT.a
LOCAL_STATIC_LIBRARIES += torch-lua-static
include $(PREBUILT_STATIC_LIBRARY)

LOCAL_MODULE := TH
LOCAL_SRC_FILES := $(TORCH_ANDROID)/lib/libTH.a
LOCAL_STATIC_LIBRARIES += luaT
include $(PREBUILT_STATIC_LIBRARY)

LOCAL_MODULE := torch
LOCAL_SRC_FILES := $(TORCH_ANDROID)/lib/libtorch.a
LOCAL_STATIC_LIBRARIES += TH
include $(PREBUILT_STATIC_LIBRARY)

LOCAL_MODULE := nn
LOCAL_SRC_FILES := $(TORCH_ANDROID)/lib/libnn.a
LOCAL_STATIC_LIBRARIES += torch
include $(PREBUILT_STATIC_LIBRARY)

LOCAL_MODULE := nnx
LOCAL_SRC_FILES := $(TORCH_ANDROID)/lib/libnnx.a
LOCAL_STATIC_LIBRARIES += nn
include $(PREBUILT_STATIC_LIBRARY)

LOCAL_MODULE := image
LOCAL_SRC_FILES := $(TORCH_ANDROID)/lib/libimage.a
LOCAL_STATIC_LIBRARIES += nnx
include $(PREBUILT_STATIC_LIBRARY)

LOCAL_MODULE := imgraph
LOCAL_SRC_FILES := $(TORCH_ANDROID)/lib/libimgraph.a
LOCAL_STATIC_LIBRARIES += image
include $(PREBUILT_STATIC_LIBRARY)

LOCAL_STATIC_LIBRARIES += imgraph
LOCAL_MODULE := torchdemo
LOCAL_C_INCLUDES += $(TORCH_ANDROID)/include/torch
LOCAL_SRC_FILES := torchandroid.cpp torchdemo.cpp android_fopen.c
LOCAL_LDLIBS := -llog -landroid
include $(BUILD_SHARED_LIBRARY)

@nicholas-leonard
Copy link
Collaborator Author

I made a little test to see if I could compile a part of the android-demo with openmp support. In torchandroid.cpp, I added:

#include <omp.h>
...
lua_State* inittorch(AAssetManager* manager) {
...
  char buffer[4096]; // buffer for textview output
#pragma omp parallel for
    for(int i = 0;i < 100;i++) {
        /* Do work... */
        buffer[i] = 'd';
    }
  return L;
}

It compiled with the build command issued by ndk-build:

/home/nicholas14/Downloads/android-ndk-r10d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ -MMD -MP -MF ./obj/local/armeabi-v7a/objs/torchdemo/torchandroid.o.d -fpic -ffunction-sections -funwind-tables -fstack-protector -no-canonical-prefixes -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp -fno-exceptions -fno-rtti -mthumb -Os -g -DNDEBUG -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -I/home/nicholas14/projects/torch-android//include/torch -I/home/nicholas14/projects/torch-android//include/torch -I/home/nicholas14/projects/torch-android//include/torch -I/home/nicholas14/projects/torch-android//include/torch -I/home/nicholas14/projects/torch-android//include/torch -I/home/nicholas14/projects/torch-android//include/torch -I/home/nicholas14/projects/torch-android//include/torch -I/home/nicholas14/projects/torch-android//include/torch -I/home/nicholas14/projects/torch-android//include/torch -I/home/nicholas14/Downloads/android-ndk-r10d/sources/cxx-stl/gnu-libstdc++/4.8/include -I/home/nicholas14/Downloads/android-ndk-r10d/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I/home/nicholas14/Downloads/android-ndk-r10d/sources/cxx-stl/gnu-libstdc++/4.8/include/backward -Ijni -DANDROID -fopenmp -Wa,--noexecstack -Wformat -Werror=format-security   -fopenmp   -I/home/nicholas14/Downloads/android-ndk-r10d/platforms/android-17/arch-arm/usr/include -c  jni/torchandroid.cpp -o ./obj/local/armeabi-v7a/objs/torchdemo/torchandroid.o

So that would seem to indicate that the issue lies in how we compile torch-android, and not the android-demo...

@jgemmeke
Copy link

Literally the only thing I need to do to get the demos compiled (just tested with fresh git clone of the repo)

cp ~/android-ndk-r10d/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/arm-linux-androideabi/lib/libgomp.a lib/

I welcome understanding why this works for me, and what the proper way to do it is :)

@jgemmeke
Copy link

Forgot (to mention) I overwrote my libgomp files with the ones posted in the comments in this thread:

http://recursify.com/blog/2013/08/09/openmp-on-android-tls-workaround

Which probably makes the difference.

@nicholas-leonard
Copy link
Collaborator Author

Yeah also tried that. It works for me now thanks to your comment. Actually reverted to the old style Android.mk as the one I posted above, although it fixes an ndk-build warning, sucks. Tomorrow will try to compile on my buddy's computer to confirm what steps are actually necessary. I also made some changes to the torch-android build.sh.

@nicholas-leonard
Copy link
Collaborator Author

The key was in fact changing the cmake command in the torch-android build.sh to:

cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/android.toolchain.cmake .. -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR -DANDROID_STL=none -DCMAKE_BUILD_TYPE=Release -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.8 -DANDROID_NO_UNDEFINED=ON

No need to drop the libgomp.a.
No need to do any of the recursify stuff.

@soumith
Copy link
Owner

soumith commented Feb 26, 2015

oh! nice. can you PR? let me just add you to the repo

@alfo888
Copy link

alfo888 commented Sep 3, 2015

hi @nicholas-leonard , i get the same error. After build using sh build.sh in torch-android-master i copy manually the libraries ( found using in the shell "$find . -name *.a") inside the folder /torch-android-master/lib.

I enter in android-demo folder and launching "$sh build.sh" (or using ndk-build comand after adding ndk-build at the enviroment varibles) I get:

alfonso@alfonso-Precision-T1700:~/AndroidStudioProjects/torch-android-master/android-demo$ sh build.sh
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Updated local.properties
Updated file ./proguard-project.txt
Android NDK: WARNING: APP_PLATFORM android-17 is larger than android:minSdkVersion 14 in ./AndroidManifest.xml
Android NDK: WARNING:jni/Android.mk:torchdemo: non-system libraries in linker flags: -lluaT -ltorch-lua-static -lTH -lnn -ltorch -lnnx -limage -limgraph -lluaT -ltorch-lua-static -lTH -lnn -ltorch -lnnx -limage -limgraph
Android NDK: This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES
Android NDK: or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the
Android NDK: current module
[armeabi-v7a] Compile++ thumb: torchdemo <= torchandroid.cpp
[armeabi-v7a] Compile++ thumb: torchdemo <= torchdemo.cpp
In file included from jni/torchdemo.cpp:8:0:
jni/torchdemo.cpp: In function 'jstring* Java_com_torch_torchdemo_TorchDemo_callTorch(JNIEnv, jobject, jobject)':
jni/torchandroid.h:19:78: warning: too many arguments for format [-Wformat-extra-args]

define D(x...) __android_log_print(ANDROID_LOG_INFO,"torchdemo", "%s", x)

                                                                          ^

jni/torchdemo.cpp:35:7: note: in expansion of macro 'D'
D("Error doing resource: %s\n", file);
^
[armeabi-v7a] Compile thumb : torchdemo <= android_fopen.c
[armeabi-v7a] SharedLibrary : libtorchdemo.so
../lib//libnn.a(init.c.o):init.c:function nn_FloatSqrt_updateGradInput: error: undefined reference to 'GOMP_parallel'
../lib//libnn.a(init.c.o):init.c:function nn_FloatSquare_updateGradInput: error: undefined reference to 'GOMP_parallel'
../lib//libnn.a(init.c.o):init.c:function nn_FloatSquare_updateOutput: error: undefined reference to 'GOMP_parallel'
../lib//libnn.a(init.c.o):init.c:function nn_FloatHardTanh_updateGradInput: error: undefined reference to 'GOMP_parallel'
collect2: error: ld returned 1 exit status
make: *_* [obj/local/armeabi-v7a/libtorchdemo.so] Error 1

I tried to change the cmake command in the torch-android build.sh to:

cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/android.toolchain.cmake .. -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR -DANDROID_STL=none -DCMAKE_BUILD_TYPE=Release -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.8 -DANDROID_NO_UNDEFINED=ON

I am usuing ubuntu 15.04 64bit

Thanks in advance

@nicholas-leonard
Copy link
Collaborator Author

@alfo888 Haven't touched this in a while. You might want to try a different NDK version. gcc/g++ 4.9 might also do the trick. The solution seems to be different for every OS.

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

No branches or pull requests

5 participants