[f2py] OpenMP - again

Steve Schmerler elcortogm at googlemail.com
Mon Sep 6 15:42:20 EEST 2010


On Sep 05 12:55 +0200, Paul Anton Letnes wrote:
> Thanks for your help there - I am closer to a workable solution now. There are still a few quirks, however. I attach my source files and the output (in output.txt) I get when running "make clean; make test" in the terminal.

Sorry for being late on the discussion. I used make+f2py in the past.
You may find this thread [1] interesting. Basically, I do

f2py -h foo.pyf foo.f90 -m _foo --overwrite-signature
f2py -c foo.pyf foo.f90 \
     --opt='-O3' \
     -DF2PY_REPORT_ON_ARRAY_COPY=1 \
     --f90exec=gfortran \
     --arch='-mmmx -msse4.2' \
     --f90flags='-x f95-cpp-input -fopenmp -D__OPENMP' \
     -lgomp

To control the number of threads, I use something like

    subroutine foo(a, b, c, nthreads, na, nb)
    #ifdef __OPENMP    
        use omp_lib
        !f2py threadsafe
    #endif    
        implicit none
        integer, intent(in) :: na, nb
        integer, intent(in), optional ::  nthreads
        double precision, intent(in) :: a(na), a(nb)
        double precision, intent(out) :: c(na)
        !f2py intent(in, out) c
    #ifdef __OPENMP
        ! With f2py, "if (present(nthreads)) then ..." doesn't work for
        ! 'optional' input args. nthreads is always present. When it is
        ! not supplied, the if clause *should* evaluate to .false. .
        ! Instead, nthreads is simply 0.
        write(*,*) "nthreads input: ", nthreads
        if (nthreads /= 0) then
            write(*,*) "setting nthreads to ", nthreads
            call omp_set_num_threads(nthreads)
        else        
            write(*,*) "number of threads controlled by OMP_NUM_THREADS or &
            number of cores"        
        end if        
        write(*,*) "num threads:", omp_get_max_threads()
    #else
        if (nthreads /= 0) then
            write(*,*) "warning: nthreads is ignored, not compiled with &
            OpenMP support"
        end if
    #endif    
    
    !$omp parallel do
        [... some do loop ...]
    !$omp end parallel do

    end subroutine foo

[1] http://thread.gmane.org/gmane.comp.python.f2py.user/1310/focus=1314

best,
Steve



More information about the f2py-users mailing list