f2py and OpenMP problem

Asked by Akand W. Islam

Hello,
Following is my simple "omp_hello.f" program:

 PROGRAM HELLO

      INTEGER NTHREADS, TID, OMP_GET_NUM_THREADS,
     + OMP_GET_THREAD_NUM

C Fork a team of threads giving them their own copies of variables
!$OMP PARALLEL PRIVATE(NTHREADS, TID)

C Obtain thread number
      TID = OMP_GET_THREAD_NUM()
      PRINT *, 'Hello World from thread = ', TID

C Only master thread does this
      IF (TID .EQ. 0) THEN
        NTHREADS = OMP_GET_NUM_THREADS()
        PRINT *, 'Number of threads = ', NTHREADS
      END IF

C All threads join master thread and disband
!$OMP END PARALLEL
      END

I have made a python module "omp_hello.so" through f2py by following command:
/usr/bin/f2py2.7 -c -m omp_hello omp_hello.f --f90flags="-fopenmp -lgomp"

Now when I try to import omp_hello in python it comes up with following errors:
import omp_hello
ImportError: /home/scipylot/Desktop/practice/omp_hello.so: undefined symbol: omp_get_thread_num_

It seems for some reason "-fopenmp -lgomp" although shows no compilation error, is not connected well and thats why module "omp_hello.so" can not execute any omp command. I will be grateful if some one please help to over come this.

To be more specific, in my UBUNTU 11.04 system gcc version 4.5.2, gfortran version 4.5.2, libgomp1 version 4.5.2. My default python is 2.7. Inspecting "nm omp_hello.so" shows following:

00003a70 T main
         U malloc@@GLIBC_2.0
         U memcpy@@GLIBC_2.0
         U memset@@GLIBC_2.0
         U omp_get_num_threads_
         U omp_get_thread_num_
00006214 b omp_hello_error
00006210 b omp_hello_module
00004320 r options.2.1509
000062d8 b save_def
00001660 t set_data
         U sprintf@@GLIBC_2.0
         U stderr@@GLIBC_2.0
         U strcmp@@GLIBC_2.0
         U strlen@@GLIBC_2.0

Thanks in advance.

-- Akand

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu python-f2py Edit question
Assignee:
No assignee Edit question
Solved by:
mycae
Solved:
Last query:
Last reply:
Revision history for this message
mycae (mycae) said :
#1

Why are you declaring OMP_GET_THREAD_NUM and OMP_GET_NUM_THREADS at the top? These are not integers, but are functions declared inside openmp.

The following program compiles for me (but produces silly output). I compiled it with gfortran test.f -fopenmp -o test

       PROGRAM HELLO

      INTEGER NTHREADS, TID

C Fork a team of threads giving them their own copies of variables
!$OMP PARALLEL PRIVATE(TID)

C Obtain thread number
      TID = OMP_GET_THREAD_NUM()
      PRINT *, 'Hello World from thread = ', TID

C Only master thread does this
      IF (TID .EQ. 0) THEN
        NTHREADS = OMP_GET_NUM_THREADS()
        PRINT *, 'Number of threads = ', NTHREADS
      END IF

C All threads join master thread and disband
!$OMP END PARALLEL

Revision history for this message
mycae (mycae) said :
#2

Hmm, no, apologies, jumped the gun there. I thought it might have been some form of symbol shadowing. even with my rpogram above, I get the same problem

Revision history for this message
mycae (mycae) said :
#3

Ah, worked it out.

Add "-lgomp" to your f2py command line. The flags are not being passed at link time otherwise.

Revision history for this message
Akand W. Islam (wahid807) said :
#4

Thanks for response. As I mentioned in my question I have already added "-lgomp" in the command line:

/usr/bin/f2py2.7 -c -m omp_hello omp_hello.f --f90flags="-fopenmp -lgomp"

Is this command not correct, or I have to apply differently?

-- Akand

Revision history for this message
Best mycae (mycae) said :
#5

its different. The flags are cflags only, not link flags. Remember that compilation is in two stages , compile, then link.

Try:

/usr/bin/f2py2.7 -c -m omp_hello omp_hello.f --f90flags="-fopenmp" -lgomp

or

/usr/bin/f2py2.7 -c -m omp_hello omp_hello.f --f90flags="-fopenmp -lgomp" -lgomp

Revision history for this message
Akand W. Islam (wahid807) said :
#6

What you are saying makes sense, and this solved the issue. Thanks again.

-- Akand

Revision history for this message
Akand W. Islam (wahid807) said :
#7

Thanks mycae, that solved my question.