Skip to content

Instantly share code, notes, and snippets.

@pwolfram
Created August 4, 2017 23:13
Show Gist options
  • Save pwolfram/5b77a54c95a2650d833ba288e15aaeb4 to your computer and use it in GitHub Desktop.
Save pwolfram/5b77a54c95a2650d833ba288e15aaeb4 to your computer and use it in GitHub Desktop.
example of f2py using openmp (hello world)

This works (build with this command: f2py -c -m omp_hello omp_hello.f90 --f90flags="-fopenmp" -lgomp):

program hello

  call hellofunc

end

subroutine HELLOfunc

  INTEGER :: NTHREADS, TID, OMP_GET_NUM_THREADS, OMP_GET_THREAD_NUM

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

  ! Obtain thread number
  TID = OMP_GET_THREAD_NUM()
  write(*,*) 'Hello World from thread = ', TID

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

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

Use like follows:

┌─[pwolfram][shapiro][~/Downloads/f2yp_openmp][17:07]
└─▪ ipython 
import Python 2.7.13 | packaged by conda-forge | (default, Mar 20 2017, 14:26:36) 
Type "copyright", "credits" or "license" for more information.

IPython 5.4.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import omp_hello

In [2]: omp_hello.hellofunc()
 Hello World from thread =            1
 Hello World from thread =            5
 Hello World from thread =            3
 Hello World from thread =            2
 Hello World from thread =            0
 Hello World from thread =            6
 Hello World from thread =            7
 Hello World from thread =            4
 Number of threads =            8

or build in pure fortran:

┌─[pwolfram][shapiro][~/Downloads/f2yp_openmp][17:09]
└─▪ gfortran omp_hello.f90 -fopenmp -lgomp
┌─[pwolfram][shapiro][~/Downloads/f2yp_openmp][17:09]
└─▪ ./a.out 
 Hello World from thread =            2
 Hello World from thread =            5
 Hello World from thread =            3
 Hello World from thread =            4
 Hello World from thread =            0
 Hello World from thread =            1
 Hello World from thread =            6
 Hello World from thread =            7
 Number of threads =            8
all: build run
build:
gfortran omp_hello.f90 -fopenmp -lgomp
f2py -c -m omp_hello omp_hello.f90 --f90flags="-fopenmp" -lgomp
run:
@echo '********************'
@echo 'running pure fortran'
@echo '********************'
./a.out
@echo '********************'
@echo 'running from python'
@echo '********************'
./run.py
clean:
rm -rf a.out omp_hello.so.dSYM omp_hello.so
program hello
call hellofunc
end
subroutine HELLOfunc
INTEGER :: NTHREADS, TID, OMP_GET_NUM_THREADS, OMP_GET_THREAD_NUM
! Fork a team of threads giving them their own copies of variables
!$OMP PARALLEL PRIVATE(NTHREADS, TID)
! Obtain thread number
TID = OMP_GET_THREAD_NUM()
write(*,*) 'Hello World from thread = ', TID
! Only master thread does this
IF (TID .EQ. 0) THEN
NTHREADS = OMP_GET_NUM_THREADS()
write(*,*) 'Number of threads = ', NTHREADS
END IF
! All threads join master thread and disband
!$OMP END PARALLEL
end
#!/usr/bin/env python
import omp_hello
omp_hello.hellofunc()
@pwolfram
Copy link
Author

Fyi,
It compiles with openMPi, using
f2py -c -m --fcompiler=gnu95 self_consistent_fortran self_consistent.f90 --f90flags="-march=native -fPIC -fopenmp -shared -ffixed-line-length-132 -O3 -g0 -lm" -lm --verbose

~kane

From: "Bennett, Kane" kaneb@lanl.gov
Date: Thursday, August 17, 2017 at 1:36 PM
To: "Wolfram Jr., Phillip" pwolfram@lanl.gov
Subject: openMP on a cluster

Hi Phil,

Log shot, but I don’t suppose you recognize by any chance an error like this when compiling fortran with openMP on a cluster?

/usr/bin/ld: /home/kaneb/miniconda2/lib/libgomp.a(critical.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
/home/kaneb/miniconda2/lib/libgomp.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status

Tried recompiling with fPIC, obviously, among various google searched answers, but to no avail. It’s on the new T-3 (dry side?) cluster that I’m trying to compile what works fine on my own machine.

~Kane

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment