OpenMP support in NDK

9059 views
Skip to first unread message

Keith

unread,
Feb 15, 2012, 3:33:11 PM2/15/12
to android-ndk
Hello,

Are there any plans add OpenMP support to the Android NDK?

I know this question is periodically asked on this list, but in the
past year a rather large number of Android devices on the
market now have two or more CPU cores.

With a little bit of effort I was able to enable OpenMP in the NDK
and was able to make use of the available CPU cores in a non-trivial
code.

It would be great if the official distribution had this capability.

Thanks,

Keith




David Turner

unread,
Feb 16, 2012, 11:36:51 AM2/16/12
to andro...@googlegroups.com
Hello Keith,

At the moment, we do not plan on officially supporting OpenMP with the Android NDK, which means the official binary distributions won't have this capability.

However, experimentation is welcomed. If you have a set of patches to provide to the NDK toolchain build scripts that can provide an option to enable that, I'll consider including them into the source distribution, to make rebuilding these easier from it, as far as they don't interfere with the build of the 'standard' toolchains, and not drag too many dependencies in.

The same applies to many other optional toolchain options (e.g. Fortran, Objective-C, CLoop, etc...)

- David
 
Thanks,

Keith




--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


Keith Obenschain

unread,
Feb 16, 2012, 6:57:37 PM2/16/12
to android-ndk
Hello,

I will clean up what I've done and submit a patch. It doesn't
require any additional dependancies, but does require several
modifications to gcc/libgomp.

The OpenMP directives (at least the ones that we use) for C++ and
Fortran have been tested on a Galaxy Tab and a Transformer Prime.

Thanks,

Keith

Neo Apostol

unread,
Feb 21, 2012, 4:53:35 PM2/21/12
to andro...@googlegroups.com, Keith
15.02.12 22:33, Keith пишет:
hi there
You should use boost

Keith Obenschain

unread,
Mar 23, 2012, 7:02:04 PM3/23/12
to android-ndk
Hello,

I've submitted a patch (https://android-review.googlesource.com/#/
c/34491/).

Building/Testing:

I used the following procedure to test the patch.

1: Download the r7b NDK.
2: Go to the NDK root.
3: Download the source for the NDK (./build/tools/download-
toolchain-sources.sh src)
4: Pull the patch from the link above).
5: Go to src and run the following:
mv gcc gcc.orig
6: Add a symbolic link to the gcc that was pulled from git
7: Rebuild the toolchain ./build/tools/build-gcc.sh $(pwd)/src $
(pwd) arm-linux-androideabi.
8: If all goes well you should end up with a toolchain with OpenMP
enabled. If you look under the rebuilt toolchain you should see
libgomp.a and associated files.

To use openmp add -fopenmp to the compile/link steps.

Issues:

There might be an issue where the functions in libgomp with the
__attribute__((constructor)) set are not being called. This results
in a segfault once the code that makes use of OpenMP hits a parallel
region since some structures internal to libgomp haven't been
initialized. With a console app it works fine. I've only seen this
issue so far when used with the Necessitas SDK but it might apply to
any dynamic library that uses OpenMP. A workaround is to manually call
those functions.

Below is a quick test running the STREAM (http://
www.streambench.org/) benchmark from the console of a Transformer
Prime for 1,2 and 4 cores:
It was compiled with -O3 and -fopenmp, the array size was set to
6000000.

Function 1-Core (MB/s) 2-Cores (MB/s) 4-Cores (MB/s)
Copy: 523.4000 1000.0203 1202.448
Scale: 377.4105 794.7615 1245.3782
Add: 421.4260 864.8495 980.3243
Triad: 308.8008 617.2576 965.2773

Keith


On Feb 16, 7:57 pm, Keith Obenschain <obenscha...@gmail.com> wrote:
> Hello,
>
>   I will clean up what I've done and submit a patch.  It doesn't
> require any additional dependancies, but does require several
> modifications to gcc/libgomp.
>
>   TheOpenMPdirectives (at least the ones that we use) for C++ and
> Fortran have been tested on a Galaxy Tab and a Transformer Prime.
>
> Thanks,
>
> Keith
>
> On Feb 16, 11:36 am, David Turner <di...@android.com> wrote:
>
>
>
>
>
>
>
> > Hello Keith,
>
> > On Wed, Feb 15, 2012 at 12:33 PM, Keith <obenscha...@gmail.com> wrote:
> > > Hello,
>
> > >  Are there any plans addOpenMPsupport to the Android NDK?
>
> > >  I know this question is periodically asked on this list, but in the
> > > past year a rather large number of Android devices on the
> > > market now have two or more CPU cores.
>
> > >  With a little bit of effort I was able to enableOpenMPin the NDK
> > > and was able to make use of the available CPU cores in a non-trivial
> > > code.
>
> > >  It would be great if the official distribution had this capability.
>
> > At the moment, we do not plan on officially supportingOpenMPwith the

Matek

unread,
Mar 27, 2012, 9:37:09 AM3/27/12
to android-ndk
I followed your steps to get OpenMP in NDK. But I cannot use it inside
Pthread function (I get segfault). I think it is quite common scenario
to conduct heavy computation in thread other then main. Do you think I
can solve this problem by workaround that you mentioned? How and where
should I call __attribute__((constructor)) functions?

On 24 Mar, 01:02, Keith Obenschain <obenscha...@gmail.com> wrote:
> Hello,
>
>     Below is a quick test running the STREAM (http://www.streambench.org/) benchmark from the console of a Transformer

Juned Munshi

unread,
Apr 4, 2012, 12:09:02 PM4/4/12
to andro...@googlegroups.com
I followed the steps above and successfully compiled gcc with openmp but when i tried to compile android application with ndk-build i m getting following error.

 /home/juned/Android/android-ndk-r7/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: cannot find -lpthread

My Android.mk file is as below.

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS) 

# set path to all source

MY_PREFIX := $(LOCAL_PATH)/
MY_SRC_FILES += \
$(MY_PREFIX)/*.cpp



LOCAL_C_INCLUDES += $(LOCAL_PATH)/ 


LOCAL_MODULE := test

LOCAL_LDFLAGS +=-O3 -fopenmp 

include $(BUILD_EXECUTABLE)
> > > android-ndk+unsubscribe@googlegroups.com.

Juned Munshi

unread,
Apr 4, 2012, 12:04:04 PM4/4/12
to android-ndk
I followed the steps and successfully compiled gcc with openmp but
when i tried to compile android native applicaiton with ndk-build
command i m getting following error.
/home/juned/Android/android-ndk-r7/toolchains/arm-linux-
androideabi-4.4.3/prebuilt/linux-x86/lib/gcc/arm-linux-androideabi/
4.4.3/../../../../arm-linux-androideabi/bin/ld: cannot find -lpthread


My Android.mk file is as below

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

# set path to all source


MY_PREFIX := $(LOCAL_PATH)/
MY_SRC_FILES += \
$(MY_PREFIX)/*.cpp



LOCAL_C_INCLUDES += $(LOCAL_PATH)/



LOCAL_MODULE := test

LOCAL_LDFLAGS +=-O3 -fopenmp

include $(BUILD_EXECUTABLE)



On Mar 24, 4:02 am, Keith Obenschain <obenscha...@gmail.com> wrote:
> Hello,
>
>     Below is a quick test running the STREAM (http://www.streambench.org/) benchmark from the console of a Transformer

Keith Obenschain

unread,
Apr 6, 2012, 1:13:21 AM4/6/12
to android-ndk
Can you check the log to see if it properly built? It shouldn't link
against libpthread if it is built.

Keith
Message has been deleted

nyronian

unread,
May 10, 2012, 3:36:53 PM5/10/12
to android-ndk
I have followed these instructions to a tee and received a successfull
build.  The only slight modification was the -4.4.3 on end end of the
arm-linux-androideabi-4.4.3.

Below was the output of the build.
$ ./build/tools/build-gcc.sh  $(pwd)/src $(pwd) arm-linux-
androideabi-4.4.3
To follow build in another terminal, please use: tail -F /tmp/ndk-
jbradshaw/build/toolchain/config.log
Using C compiler: gcc
Using C++ compiler: g++
Sysroot  : Copying: /cygdrive/c/1data/program-installs/android-ndk-r7b/
platforms/android-9/arch-arm --> /cygdrive/c/1data/program-installs/
android-ndk-r7b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/
windows/sysroot
Configure: arm-linux-androideabi-4.4.3 toolchain build
Building : arm-linux-androideabi-4.4.3 toolchain [this can take a long
time].
Install  : arm-linux-androideabi-4.4.3 toolchain binaries.
Done.

The problems:
You then indicate that I should see a libgomp.a which I cannot find
anywhere in the android-ndk-r7b subfolders.
I also #include <omp.h>.  This header file I cannot find anywhere in
the android-ndk-r7b subfolders

So when I use the following Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := testme
LOCAL_SRC_FILES := testme.c
LOCAL_LDFLAGS += -O3 -fopenmp
include $(BUILD_SHARED_LIBRARY)

I get the following results:
$ /cygdrive/c/1data/program-installs/android-ndk-r7b/ndk-build
Gdbserver      : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup       : libs/armeabi/gdb.setup
Compile thumb  : testme <= testme.c
C:/1data/Development/testprojects/javaprojects/testmeion/jni/testme.c:
6:17: error: omp.h: No such file or directory
/cygdrive/c/1data/program-installs/android-ndk-r7b/build/core/build-
binary.mk:240: recipe for target `/cygdrive/c/1data/Development/
testprojects/javaprojects/testmeion/obj/local/armeabi/objs-debug/
testprojectsdecompress/testme.o' failed
make: *** [/cygdrive/c/1data/Development/testprojects/javaprojects/
testmeion/obj/local/armeabi/objs-debug/testme/testme.o] Error 1

Which to me makes sense as I am unable to locate the omp.h header
anyway.

I must be missing something simple. Any Thoughts?

Any help would be appreciated.
> > > To useopenmpadd -fopenmp to the compile/link steps.
>
> > > Issues:
>
> > >     There might be an issue where the functions in libgomp with the
> > > __attribute__((constructor)) set are not being called.  This results
> > > in a segfault once the code that makes use ofOpenMPhits a parallel
> > > region since some structures internal to libgomp haven't been
> > > initialized.  With a console app it works fine.  I've only seen this
> > > issue so far when used with the Necessitas SDK but it might apply to
> > > any dynamic library that usesOpenMP. A workaround is to manually call

Jim Bradshaw

unread,
May 11, 2012, 8:42:36 AM5/11/12
to android-ndk
That was it Keith!  Thank you!

Now I get the same error as the Juned Munshi (in the thread)
/cygdrive/c/1data/program-installs/android-ndk-r7b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: cannot find -lpthread
collect2: ld returned 1 exit status

What is odd is that the first compile (with the --enable-libgomp on) failed the first time I tried.  I tried it again and it said it finished successfully but the tmp directory does not have a config.log.  

I will attempt the compile again with --verbose turned on and see if I can get an error to the screen (since the log was removed).


On Thu, May 10, 2012 at 11:12 AM, nyronian <jim8...@gmail.com> wrote:
Thank you for this patch Keith!

I have followed the instructions to a tee and it appeared everything
worked perfect.  This was my output for the compile in cygwin.  The
only change I made was the -4.4.3 which it required of me.

$ ./build/tools/build-gcc.sh  $(pwd)/src $(pwd) arm-linux-
androideabi-4.4.3
To follow build in another terminal, please use: tail -F /tmp/ndk-
jbradshaw/build/toolchain/config.log
Using C compiler: gcc
Using C++ compiler: g++
Sysroot  : Copying: /cygdrive/c/1data/program-installs/android-ndk-r7b/
platforms/android-9/arch-arm --> /cygdrive/c/1data/program-installs/
android-ndk-r7b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/
windows/sysroot
Configure: arm-linux-androideabi-4.4.3 toolchain build
Building : arm-linux-androideabi-4.4.3 toolchain [this can take a long
time].
Install  : arm-linux-androideabi-4.4.3 toolchain binaries.
Done.

You indicate in your post that I should then see a libgomp.a and
supporting files.  I see no such files.  I assume I should see them in
the toolchain but have searched the entire android-ndk-r7b directory.
The other odd thing (not sure what I'm missing) is that I use #include
<omp.h> and no where in the ndk directory do I see the omp.h header.

when I build in the [android-project]/jni folder I receive the below
output:


$ /cygdrive/c/1data/program-installs/android-ndk-r7b/ndk-build
Gdbserver      : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup       : libs/armeabi/gdb.setup
Compile thumb  : test <= test.c
C:/1data/Development/test/javaprojects/TestProject/jni/test.c:6:17:

error: omp.h: No such file or directory
/cygdrive/c/1data/program-installs/android-ndk-r7b/build/core/build-
binary.mk:240: recipe for target `/cygdrive/c/1data/Development/test/
javaprojects/TestProject/obj/local/armeabi/objs-debug/test/test.o'
failed
make: *** [/cygdrive/c/1data/Development/test/javaprojects/TestProject/
obj/local/armeabi/objs-debug/test/test.o] Error 1

So the compiler can't find the omp.h header...and frankly, nor can I.
I assumed it would be part of the compiled code.

This is the Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

# Here we give our module name and source file(s)
LOCAL_MODULE    := test
LOCAL_SRC_FILES := test.c

LOCAL_LDFLAGS += -O3 -fopenmp

include $(BUILD_SHARED_LIBRARY)

I must be missing something simple.  Any ideas? :)

Thank you in advance.

nyronian

unread,
May 11, 2012, 11:04:34 AM5/11/12
to andro...@googlegroups.com
after running in --verbose mode, the config.log is still gone and it shows a successful compile.  But I still get the error to the lpthreads.

In looking through the output I did notice this line:

libtool: finish: PATH="/tmp/ndk-jb/build/toolchain/gcc-4.4.3/arm-linux-androideabi/libgomp/.libs:/tmp/ndk-jb/build/toolchain/gcc-4.4.3/./gcc/shlib:/tmp/ndk-jb/build/toolchain/gcc-4.4.3/./prev-gcc/shlib:/usr/local/bin:/usr/bin:${MyWindowsPath}:/cygdrive/c/mingw/bin:/usr/lib/lapack:/sbin" ldconfig -n /cygdrive/c/1data/program-installs/android-ndk-r7b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/arm-linux-androideabi/lib/armv7-a
/tmp/ndk-jb/build/toolchain/gcc-4.4.3/arm-linux-androideabi/armv7-a/libgomp/libtool: line 955: ldconfig: command not found

 

nyronian

unread,
May 16, 2012, 6:12:06 PM5/16/12
to andro...@googlegroups.com
So after many days of trying to get this to compile, I can't seem too.  If anyone can point me in the right direction, that would be great.
Where is my status:
     1. the lpthreads issue appeared to be because I was compiling on cygwin.  I changed to ubuntu.
     2.  I tried ubuntu 12 and worked through installing a good number of libraries.  Untill I got stuck.  The compiler is looking for libdl.so which it says does not exist on the system.  And it doesn't in /usr/lib/ but does in /usr/lib64.  I found a forum recommending I link the lib64 files to the /usr/lib32 folder.  The problem is, I don't have a lib32 folder.  Only a lib folder.  Everything I can find on the libdl.so says it should install with the build-essential, but I have tried MANY other libraries to find it and to no avail. 
     3. I then thought that maybe ubuntu 12 has a problem with the library to I started over with Ubuntu 11.  I find the same issue.  the libdl is in the /usr/lib64 folder and there is no /usr/lib32.
     4. I tried to create a link between the files in lib64 to /usr/lib and that did not work either.  Although I have seen a few places that recommend the link be to libdl.so.2 as opposed to libdl.so but again, they recommend it in lib32. 

It takes about a 1/2 hour of compiling before I hit the issue so I am trying to speed up the testing.  I am thinking of just creating a lib32 and link the files from the lib64.  Just seems odd to me there there is no where the libdl.so can be placed into the lib32 with an apt-get install.  I must be missing something.  I just don't know what.

I have installed the following libraries on ubuntu:  build-essential, gcc, libc6-dev, texinfo, flex, libncurses5-dev, libc6-dev

Thank you in advance.

nyronian

unread,
May 16, 2012, 7:21:45 PM5/16/12
to andro...@googlegroups.com
maybe I am barking up the wrong tree....This is the actual error I am getting:
make[3]: Entering directory `/tmp/ndk-root/build/toolchain/gcc-4.4.3/gcc'
gcc  -Wno-error -DIN_GCC -DCROSS_DIRECTORY_STRUCTURE  -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wold-style-definition -Wc++-compat -Wmissing-format-attribute -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings   -DHAVE_CONFIG_H  -o cc1-dummy c-lang.o stub-objc.o attribs.o c-errors.o c-lex.o c-pragma.o c-decl.o c-typeck.o c-convert.o c-aux-info.o c-common.o c-opts.o c-format.o c-semantics.o c-ppoutput.o c-cppbuiltin.o c-objc-common.o c-dump.o c-pch.o c-parser.o arm-c.o c-gimplify.o tree-mudflap.o c-pretty-print.o c-omp.o dummy-checksum.o \
      main.o  libbackend.a ../libcpp/libcpp.a ../libdecnumber/libdecnumber.a ../libcpp/libcpp.a   ../libiberty/libiberty.a ../libdecnumber/libdecnumber.a   -L/tmp/ndk-root/build/toolchain/temp-install/lib -L/tmp/ndk-root/build/toolchain/temp-install/lib -lmpfr -lgmp -rdynamic -lm
libbackend.a(plugin.o): In function `try_init_one_plugin':
plugin.c:(.text+0xb00): undefined reference to `dlopen'
plugin.c:(.text+0xb0e): undefined reference to `dlerror'
plugin.c:(.text+0xb37): undefined reference to `dlerror'
plugin.c:(.text+0xb4b): undefined reference to `dlsym'
plugin.c:(.text+0xb59): undefined reference to `dlerror'

collect2: ld returned 1 exit status



Scott Sibley

unread,
May 16, 2012, 8:19:07 PM5/16/12
to andro...@googlegroups.com
You just need to link the right library. I believe it is '-ldl' for
dlopen and the like.
> --
> You received this message because you are subscribed to the Google Groups
> "android-ndk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/android-ndk/-/Wr9EfQndABYJ.

nyronian

unread,
May 16, 2012, 8:22:54 PM5/16/12
to andro...@googlegroups.com
Yes, you are right.  I made the following changes to the patch:

I had to add -ldl to the configure and configure.ac file within the /gcc directory.

1. line 1210 on gcc/gcc-4.4.3/configure.ac now looks like this:  gmplibs="-lmpfr -lgmp -ldl"
2. line 4522 on gcc/gcc-4.4.3/configure now looks like this:  gmplibs="-lmpfr -lgmp -ldl"

it worked! :-)

Thanks for your response Starlon.

nyronian

unread,
May 17, 2012, 12:18:50 PM5/17/12
to andro...@googlegroups.com
This change did fix the compile with no errors.  Everything looks good till you try to compile and I am now getting the same problem (again) as juned:

jb@ubuntu:/mnt/hgfs/1data/Development/myTest/javaprojects/myTest/jni$ ~/Installs/android-ndk-r7b/ndk-build V=1
rm -f /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/armeabi/lib*.so /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/armeabi-v7a/lib*.so /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/x86/lib*.so
rm -f /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/armeabi/gdbserver /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/armeabi-v7a/gdbserver /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/x86/gdbserver
rm -f /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/armeabi/gdb.setup /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/armeabi-v7a/gdb.setup /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/x86/gdb.setup
Gdbserver      : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
mkdir -p /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/armeabi
install -p /home/jb/Installs/android-ndk-r7b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/gdbserver /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/armeabi/gdbserver
Gdbsetup       : libs/armeabi/gdb.setup
mkdir -p /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/armeabi
echo "set solib-search-path /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/obj/local/armeabi" > /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/armeabi/gdb.setup
echo "directory /home/jb/Installs/android-ndk-r7b/platforms/android-14/arch-arm/usr/include /home/jb/Installs/android-ndk-r7b/sources/cxx-stl/system /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/jni" >> /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/libs/armeabi/gdb.setup
SharedLibrary  : libtestme.so
/home/jb/Installs/android-ndk-r7b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-g++ -Wl,-soname,libtestme.so -shared --sysroot=/home/jb/Installs/android-ndk-r7b/platforms/android-14/arch-arm  /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/obj/local/armeabi/objs-debug/testme/testme.o    /home/jb/Installs/android-ndk-r7b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a    -O3 -fopenmp -Wl,--no-undefined -Wl,-z,noexecstack  -lc -lm -o /mnt/hgfs/1data/Development/myTest/javaprojects/myTest/obj/local/armeabi/libtestme.so
/home/jb/Installs/android-ndk-r7b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: cannot find -lpthread

collect2: ld returned 1 exit status
make: *** [/mnt/hgfs/1data/Development/myTest/javaprojects/myTest/obj/local/armeabi/libtestme.so] Error 1

Every place I read about missing pthreads is that you do not need the -lpthread in the compilation.  Somehow the Android Make process must be adding it but I have no idea where and how to stop it.  I have run the make in debug mode and cannot find where it is putting the parameter.

Anyone have any direction on this. I am out of ideas why OpenMP will not run on the android.  I really don't want to move to libpthreads.

Thanks for you help.

Gerry Reno

unread,
May 17, 2012, 12:24:21 PM5/17/12
to andro...@googlegroups.com
I don't think you need -lpthread. pthread is already baked into Android.
> I had to add -ldl to the configure and configure.ac <http://configure.ac> file within the /gcc directory.
>
> 1. line 1210 on gcc/gcc-4.4.3/configure.ac <http://configure.ac> now looks like this: gmplibs="-lmpfr -lgmp -ldl"
> 2. line 4522 on gcc/gcc-4.4.3/configure now looks like this: gmplibs="-lmpfr -lgmp -ldl"
>
> it worked! :-)
>
> Thanks for your response Starlon.
>
> On Wednesday, May 16, 2012 6:19:07 PM UTC-6, Starlon wrote:
>
> You just need to link the right library. I believe it is '-ldl' for
> dlopen and the like.
>
>
> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/android-ndk/-/NauvbdXWndAJ.

Jim Bradshaw

unread,
May 17, 2012, 3:22:13 PM5/17/12
to andro...@googlegroups.com
Agreed.  So what is using it?  I can't find anything anywhere that tells me why the Android Make process is using it.  I am not using it anywhere.

Ray Donnelly

unread,
May 17, 2012, 3:25:30 PM5/17/12
to andro...@googlegroups.com

OpenMP itself uses threads, that's kind of the point. AFAIR libgcc is responsible for managing this stuff.

Gerry Reno

unread,
May 17, 2012, 3:38:49 PM5/17/12
to andro...@googlegroups.com
pthread symbols will be external to your libopenmp or whatever you called it. And they will get resolved when you
finally link with android.


On 05/17/2012 03:25 PM, Ray Donnelly wrote:
>
> OpenMP itself uses threads, that's kind of the point. AFAIR libgcc is responsible for managing this stuff.
>
> On May 17, 2012 8:22 PM, "Jim Bradshaw" <jim8...@gmail.com <mailto:jim8...@gmail.com>> wrote:
>
> Agreed. So what is using it? I can't find anything anywhere that tells me why the Android Make process is using
> it. I am not using it anywhere.
>
> > I had to add -ldl to the configure and configure.ac <http://configure.ac> <http://configure.ac> file
> within the /gcc directory.
> >
> > 1. line 1210 on gcc/gcc-4.4.3/configure.ac <http://configure.ac> <http://configure.ac> now looks like
> this: gmplibs="-lmpfr -lgmp -ldl"
> > 2. line 4522 on gcc/gcc-4.4.3/configure now looks like this: gmplibs="-lmpfr -lgmp -ldl"
> >
> > it worked! :-)
> >
> > Thanks for your response Starlon.
> >
> > On Wednesday, May 16, 2012 6:19:07 PM UTC-6, Starlon wrote:
> >
> > You just need to link the right library. I believe it is '-ldl' for
> > dlopen and the like.
> >
> >
> > --
> > You received this message because you are subscribed to the Google Groups "android-ndk" group.
> > To view this discussion on the web visit https://groups.google.com/d/msg/android-ndk/-/NauvbdXWndAJ.
> > To post to this group, send email to andro...@googlegroups.com <mailto:andro...@googlegroups.com>.
> > To unsubscribe from this group, send email to android-ndk...@googlegroups.com
> <mailto:android-ndk%2Bunsu...@googlegroups.com>.
> > For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com <mailto:andro...@googlegroups.com>.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com
> <mailto:android-ndk%2Bunsu...@googlegroups.com>.
> For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>
>
> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com <mailto:andro...@googlegroups.com>.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com
> <mailto:android-ndk%2Bunsu...@googlegroups.com>.

nyronian

unread,
May 17, 2012, 4:19:35 PM5/17/12
to andro...@googlegroups.com
agreed.  They just don't seem to be getting resolved and I'm not sure how to track down why. 

Well at least I'm out of ideas how to track down why ndk-build is not able to resolve pthreads.
>         > To post to this group, send email to andro...@googlegroups.com <mailto:android-ndk@googlegroups.com>.
>         > To unsubscribe from this group, send email to android-ndk+unsubscribe@googlegroups.com
>         <mailto:android-ndk%2Bunsu...@googlegroups.com>.
>         > For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>
>         --
>         You received this message because you are subscribed to the Google Groups "android-ndk" group.
>         To post to this group, send email to andro...@googlegroups.com <mailto:android-ndk@googlegroups.com>.
>         To unsubscribe from this group, send email to android-ndk+unsubscribe@googlegroups.com
>         <mailto:android-ndk%2Bunsu...@googlegroups.com>.
>         For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>
>
>     --
>     You received this message because you are subscribed to the Google Groups "android-ndk" group.
>     To post to this group, send email to andro...@googlegroups.com <mailto:android-ndk@googlegroups.com>.
>     To unsubscribe from this group, send email to android-ndk+unsubscribe@googlegroups.com
>     <mailto:android-ndk%2Bunsu...@googlegroups.com>.
>     For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-ndk+unsubscribe@googlegroups.com.

nyronian

unread,
May 17, 2012, 5:01:53 PM5/17/12
to andro...@googlegroups.com
So I think it is in the compiler itself then?  The compiler must be putting it in.  Which means there is a problem with the build of the toolchain, even though it built with no errors, there must be a switch I am missing or not putting in or not something.

Dmitry Shmunk

unread,
May 18, 2012, 11:47:11 PM5/18/12
to andro...@googlegroups.com
Have been stuck with the same problem for over a month.

Today, found solution by accident:
Do not specify -fopenmp to the linker! Apparently this is automatically turning on -lpthread

I.e. do not put -fopenmp to LOCAL_LDFLAGS

You may still need to add -lgomp

Dmitry Shmunk

unread,
May 19, 2012, 10:18:45 PM5/19/12
to andro...@googlegroups.com
Some further investigations.

As Keith wrote, invoking OpenMP from standalone console executable works fine.
When invoking from shared library though - the constructor functions are not called.
I have found a simpler way to trigger a call to the OpenMP constructor functions rather than calling them manually. Simply include an empty constructor/destructor functions in your shared library code:

// This triggers openmp constructors and destructors to be called upon library load/unload
void __attribute__((constructor)) initialize_openmp() {}
void __attribute__((destructor)) release_openmp() {}


Now, the real problem is that OpenMP seem to have problems when invoked from different threads (it will crash). And this is definitely the case if the shared library is used from JNI interface. Library constructors are called when library is loaded, but the real work is almost certainly will be done from other thread.

This seem to be similar to crash when OpenMP invoked from non-main thread:
http://stackoverflow.com/questions/5279904/handling-gui-thread-in-a-program-using-openmp

I have tracked an issue to the fact that initialize_team() (libgomp/team.c) should be called from the thread where we want to invoke OpenMP.
Within initialize_team() there is a piece of code that initializes thread-specific data:
  pthread_setspecific (gomp_tls_key, &initial_thread_tls_data);

Naturally, when OpenMP is invoked afterwards from the other thread the pthread_getspecific for gomp_tls_key returns NULL.

Here is a hack that I implemented to rectify the issue:

- In libgomp/team.c, initialize_team(): commented out
  //static struct gomp_thread initial_thread_tls_data;
and made it global:
struct gomp_thread initial_thread_tls_data;

- In libgomp/libgomp.h, changed the block of code starting with '... and here is that TLS data' to look like this:

/* ... and here is that TLS data.  */

#ifdef HAVE_TLS
extern __thread struct gomp_thread gomp_tls_data;
static inline struct gomp_thread *gomp_thread (void)
{
  return &gomp_tls_data;
}
#else
extern pthread_key_t gomp_tls_key;
extern struct gomp_thread initial_thread_tls_data;

static inline struct gomp_thread *gomp_thread (void)
{
  if (pthread_getspecific (gomp_tls_key)==NULL)
  {
    pthread_setspecific (gomp_tls_key, &initial_thread_tls_data);
    gomp_sem_init (&(&initial_thread_tls_data)->release, 0);
  }
  return pthread_getspecific (gomp_tls_key);
}
#endif

So, whenever it is detected that OpenMP is being called from a new thread for which
gomp_tls_key has no value yet - it will assign it to initial_thread_tls_data

Not being an OpenMP expert, I believe this hack may have side-effects. It might affect:
- invoking OpenMP concurrently from multiple threads;
- using OpenMP parallel sections in nested fashion.
I have not tested these scenarios, otherwise it works fine for my tasks.

If there is somebody more knowledgeable in the subject, please comment.
> > > android-ndk+unsubscribe@googlegroups.com.

Keith Obenschain

unread,
May 22, 2012, 1:51:58 AM5/22/12
to android-ndk
One test I would try is to make the stand alone toolchain. Then
compile a very small openmp program and see if it links properly with
the compile/link (arm-linux-androideable-gcc -fopenmp program.c) on
one line.

If that works then there might be a rogue -lpthread lurking somewhere
that is triggered with a stock android makefile. The patch modified
gcc so it should look in -lc instead of -lpthread for the pthread
symbols.

Keith

On May 18, 11:47 pm, Dmitry Shmunk <dmitry.shm...@gmail.com> wrote:
> Have been stuck with the same problem for over a month.
>
> Today, found solution by accident:
> Do not specify -fopenmp to the linker! Apparently this is automatically
> turning on -lpthread
>
> I.e. do not put -fopenmp to LOCAL_LDFLAGS
>
> You may still need to add -lgomp
>
>
>
>
>
>
>
> On Friday, May 18, 2012 4:01:53 AM UTC+7, nyronian wrote:
>
> > So I think it is in the compiler itself then?  The compiler must be
> > putting it in.  Which means there is a problem with the build of the
> > toolchain, even though it built with no errors, there must be a switch I am
> > missing or not putting in or not something.
>
> > On Thursday, May 17, 2012 2:19:35 PM UTC-6, nyronian wrote:
>
> >> agreed.  They just don't seem to be getting resolved and I'm not sure how
> >> to track down why.
>
> >> Well at least I'm out of ideas how to track down why ndk-build is not
> >> able to resolve pthreads.
>
> >> On Thursday, May 17, 2012 1:38:49 PM UTC-6, greno wrote:
>
> >>> pthread symbols will be external to your libopenmp or whatever you
> >>> called it.  And they will get resolved when you
> >>> finally link with android.
>
> >>> On 05/17/2012 03:25 PM, Ray Donnelly wrote:
>
> >>> > OpenMP itself uses threads, that's kind of the point. AFAIR libgcc is
> >>> responsible for managing this stuff.
>
> >>> > On May 17, 2012 8:22 PM, "Jim Bradshaw" <jim85...@gmail.com <mailto:
> >>> andro...@googlegroups.com <mailto:andro...@googlegroups.com>.
> >>> >         > To unsubscribe from this group, send email to
> >>> android-ndk...@googlegroups.com
> >>> >         <mailto:android-ndk%2Bunsu...@googlegroups.com>.
> >>> >         > For more options, visit this group at
> >>>http://groups.google.com/group/android-ndk?hl=en.
>
> >>> >         --
> >>> >         You received this message because you are subscribed to the
> >>> Google Groups "android-ndk" group.
> >>> >         To post to this group, send email to
> >>> andro...@googlegroups.com <mailto:andro...@googlegroups.com>.
> >>> >         To unsubscribe from this group, send email to
> >>> android-ndk...@googlegroups.com
> >>> >         <mailto:android-ndk%2Bunsu...@googlegroups.com>.
> >>> >         For more options, visit this group at
> >>>http://groups.google.com/group/android-ndk?hl=en.
>
> >>> >     --
> >>> >     You received this message because you are subscribed to the Google
> >>> Groups "android-ndk" group.
> >>> >     To post to this group, send email to andro...@googlegroups.com<mailto:
> >>> andro...@googlegroups.com>.
> >>> >     To unsubscribe from this group, send email to
> >>> android-ndk...@googlegroups.com
> >>> >     <mailto:android-ndk%2Bunsu...@googlegroups.com>.
> >>> >     For more options, visit this group at
> >>>http://groups.google.com/group/android-ndk?hl=en.
>
> >>> > --
> >>> > You received this message because you are subscribed to the Google
> >>> Groups "android-ndk" group.
> >>> > To post to this group, send email to andro...@googlegroups.com.
> >>> > To unsubscribe from this group, send email to
> >>> android-ndk...@googlegroups.com.

nyronian

unread,
May 22, 2012, 2:42:53 AM5/22/12
to andro...@googlegroups.com
I changed my make file to look like this and now the -fopenmp works fine now with no -lpthread problem.  I am able to run streambench and get solid results.

LOCAL_PATH := $(call my-dir)
 
include $(CLEAR_VARS)
 
# Here we give our module name and source file(s)
LOCAL_MODULE    := stream
LOCAL_SRC_FILES := stream.c
LOCAL_LDLIBS := -lgomp 
LOCAL_CFLAGS := -O3 -fopenmp
 
#include $(BUILD_SHARED_LIBRARY)
include $(BUILD_EXECUTABLE)
> >>> andro...@googlegroups.com <mailto:android-ndk@googlegroups.com>.
> >>> >         > To unsubscribe from this group, send email to
> >>> android-ndk+unsubscribe@googlegroups.com
> >>> >         <mailto:android-ndk%2Bunsu...@googlegroups.com>.
> >>> >         > For more options, visit this group at
> >>>http://groups.google.com/group/android-ndk?hl=en.
>
> >>> >         --
> >>> >         You received this message because you are subscribed to the
> >>> Google Groups "android-ndk" group.
> >>> >         To post to this group, send email to
> >>> andro...@googlegroups.com <mailto:android-ndk@googlegroups.com>.
> >>> >         To unsubscribe from this group, send email to
> >>> android-ndk+unsubscribe@googlegroups.com
> >>> >         <mailto:android-ndk%2Bunsu...@googlegroups.com>.
> >>> >         For more options, visit this group at
> >>>http://groups.google.com/group/android-ndk?hl=en.
>
> >>> >     --
> >>> >     You received this message because you are subscribed to the Google
> >>> Groups "android-ndk" group.
> >>> >     To post to this group, send email to andro...@googlegroups.com<mailto:
> >>> andro...@googlegroups.com>.
> >>> >     To unsubscribe from this group, send email to
> >>> android-ndk+unsubscribe@googlegroups.com
> >>> >     <mailto:android-ndk%2Bunsu...@googlegroups.com>.
> >>> >     For more options, visit this group at
> >>>http://groups.google.com/group/android-ndk?hl=en.
>
> >>> > --
> >>> > You received this message because you are subscribed to the Google
> >>> Groups "android-ndk" group.
> >>> > To post to this group, send email to andro...@googlegroups.com.
> >>> > To unsubscribe from this group, send email to
> >>> android-ndk+unsubscribe@googlegroups.com.

manu shrivastava

unread,
Feb 11, 2013, 2:17:42 AM2/11/13
to andro...@googlegroups.com


I have followed these instructions to a tee and received a successfull
build.

The problems:
You then indicate that I should see a libgomp.a which I cannot find
anywhere in the android-ndk-r7b subfolders.
I also #include <omp.h>.  This header file I cannot find anywhere in
the android-ndk-r7b subfolders

So when I use the following Android.mk:
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c


LOCAL_LDFLAGS += -O3 -fopenmp

include $(BUILD_SHARED_LIBRARY)

I get the following results:

Gdbserver      : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup       : libs/armeabi/gdb.setup
Compile thumb  : hello-jni <= hello-jni.c
jni/hello-jni.c:19:16: error: omp.h: No such file or directory
make: *** [obj/local/armeabi/objs-debug/hello-jni/hello-jni.o] Error 1

manu shrivastava

unread,
Feb 11, 2013, 2:31:31 AM2/11/13
to andro...@googlegroups.com
  I have followed the method as you describe . but after adding --enable-lobgomp under build-gcc.sh i am getting following error

make[3]: Entering directory `/tmp/ndk-root/build/toolchain/gcc-4.4.3/arm-linux-androideabi/libgomp'
make[3]: *** No rule to make target `install'.  Stop.
make[3]: Leaving directory `/tmp/ndk-root/build/toolchain/gcc-4.4.3/arm-linux-androideabi/libgomp'
make[2]: *** [install-target-libgomp] Error 2
make[2]: Leaving directory `/tmp/ndk-root/build/toolchain/gcc-4.4.3'
make[1]: *** [install] Error 2
make[1]: Leaving directory `/tmp/ndk-root/build/toolchain/gcc-4.4.3'
make: *** [install-target-gcc-gcc] Error 2
Error while installing toolchain. See /tmp/ndk-root/build/toolchain/config.log

any help would be appreciated
 

Ray Donnelly

unread,
Feb 11, 2013, 8:17:38 AM2/11/13
to andro...@googlegroups.com
See /tmp/ndk-root/build/toolchain/config.log
> --
> You received this message because you are subscribed to the Google Groups
> "android-ndk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to android-ndk...@googlegroups.com.
> To post to this group, send email to andro...@googlegroups.com.
> Visit this group at http://groups.google.com/group/android-ndk?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

manu shrivastava

unread,
Feb 11, 2013, 9:40:31 AM2/11/13
to andro...@googlegroups.com
Thanks for the reply but i have copy the above error from the config.log file only . I think there is some file missing in libgomp package .  I tried to run makefile from libgomp dir and it give me following error

makefile480: .deps/affinity.plo no such file or directory found 

some dependency file are missing . Can anyone guide me to fix the problem
Message has been deleted

Md Mahbubul Islam

unread,
Jun 13, 2013, 10:32:09 AM6/13/13
to andro...@googlegroups.com
I will be glad to see any working  example code with OpenMp and NDK and OpenCV.

Thanks



David Erceg

unread,
Aug 9, 2013, 1:51:32 AM8/9/13
to andro...@googlegroups.com
As of revision 9, the NDK comes with support for OpenMP. The code I currently have works from a shared library, and uses more than one thread.

Based on the very helpful replies in this topic, I've written two blog posts about it:
The first gives a very basic overview of the setup needed for OpenMP. The second gives a more thorough explanation of the TLS bug that Dmitry raises above. I've included two patches in the post that work for me. Ultimately, it would be best to fix the behavior in GCC directly. There's an open bug at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42616, but it hasn't been updated in two years.

I've also made a post detailing the exact steps I had to go through to get NDK r9 to compile on Ubuntu 12.04 64-bit (including Windows builds, if anyone else uses it). See http://recursify.com/blog/2013/08/08/building-an-android-ndk-toolchain.

zhu poirot

unread,
Feb 19, 2014, 1:21:56 AM2/19/14
to andro...@googlegroups.com
Hi everyone: 

I followed the steps in 
But when I tried to rebuild the toolchain, there was an error "Patching toolchain sources
ERROR: Could not patch sources." in the source downloading step. I stopped applying patches by adding --no-patches.
Finally it worked. I copied the libgomp.a into my ndk folder and enable omp in my jni. But the program did not seem to run faster than my previous one without omp.

My os is ubuntu 12.04 64bit. NDK is r9c. Cell phone is HTC one.


在 2013年8月9日星期五UTC+8下午1时51分32秒,David Erceg写道:

thành phan

unread,
Apr 30, 2015, 5:14:59 AM4/30/15
to andro...@googlegroups.com
Hi Smth Chntla,
I am trying to do these thing with ndk-r8b, but i don't know what should i do in steps 4 and 5. Can you explain it clearly, please.

Vào 00:50:27 UTC+7 Thứ Ba, ngày 06 tháng 11 năm 2012, Smth Chntla đã viết:
To get the toolchain on ndk-r8b compiled on OSX, for gcc-4.6 I had to make a few trivial changes in the procedure. This should help anyone who were in my position.

1: Download the r8b NDK. 
    2: Go to the NDK root. 
    3: Download the source for the NDK (./build/tools/download- 
toolchain-sources.sh src) 
    5: Manually apply the patch, because the code seems to have changed and the patch no longer applies correctly.
    6: Make sure you set your host compiler to apple's shipped compiler with XCode. I tried compiling with my version of gcc 4.8 and it gave some errors. The compiler that worked for me is gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00). Check the compiler version with "gcc -v" .
    7: Open build/tools/build-host-gcc.sh and change "--disable-libgomp" to "--enable-libgomp" 
    8: Open src/build/Makefile.in and at line 440,  change "--disable-libgomp" to "--enable-libgomp"
    6: Rebuild the toolchain with the following command (have to build using -j1 as multi-core build looks like it has a race condition or some sort of a bug) 
bash build/tools/build-gcc.sh --platform=android-14 $(pwd)/src $(pwd) arm-linux-androideabi-4.6 -j1
    7: If all goes well you should end up with a toolchain with OpenMP and see the file
    toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/arm-linux-androideabi/lib/armv7-a/libgomp.a
Reply all
Reply to author
Forward
0 new messages