[f2py] openmp ifort and write(*,*) problems

Ethan Gutmann <[email protected]> Mon, 28 Jan 2013 13:32:55 -0700
Newsgroups gmane.comp.python.f2py.user
Message-ID <[email protected]>
Hi all, 
Does anybody know if there are any known issues with f2py concerning write(*,*) statements inside open mp parallel blocks with the intel compiler?  

I have a simple fortran subroutine which runs a loop in parallel and contains a write(*,*) for the loop index inside each loop.  When compiled and called from a fortran program this works fine with both the gfortran and fort compilers.  When compiled with f2py and called from python, it works fine with gfortran, but breaks when compiled with the ifort compiler.  If I comment out the write(*,*) statement, it works fine with either compiler.  

"Breaking" means python crashes typically with an error such as: 
forrtl: severe (40): recursive I/O operation, unit -1, file unknown
It usually manages to print out a few of the loop indicies before crashing, sometimes there is a segfault and core dump, sometimes not, but the python process always dies (I end up back in the shell). 

If I set the environment variable OMP_NUM_THREADS=1, or if I comment out the write(*,*) line that is inside the parallel region, then there is no problem.  

This happens with 
	f2py v2, numpy 1.6.2, python 2.7.3 and ifort 12.1.5 
as well as with 
	f2py v1, numpy 1.5.1, python 2.6.5, and ifort 10.0.  
For both setups gfortran 4.7 works fine. Both cases are linux machines with 16 cores. 

Below is a simple test case, save as test_f2py.f90

!! To compile with intel:
! $ f2py -h test.pyf -m test test_f2py.f90
! $ f2py -c test.pyf -m test --fcompiler=intelem --f90flags='-openmp' -liomp5 --opt='-fast' test_f2py.f90 

!! In python at the command line
! >>> import numpy as np
! >>> from test import test_case
! >>> i=np.zeros(10) 
! >>> test_case.test(i)

!! This works with gfortran
! $ f2py -h test.pyf -m test test_f2py.f90 --overwrite-signature
! $ f2py -c test.pyf -m test --fcompiler=gnu95 --f90flags='-fopenmp' -lgomp --opt="-O3" test_f2py.f90

module test_case
contains
        subroutine test(i)
                real*8, intent(inout),dimension(10) :: i
                integer :: j
                write(*,*) "Entered Fortran Code"
                !$omp parallel shared(i) private(j)
                j=0
                !$omp do
                do j=1,10
                        write(*,*) j
                        i(j)=j
                enddo
                !$omp end do
                !$omp end parallel
        end subroutine test
end module test_case

!! uncomment main program and compile directly to test without f2py... this works fine
! $ ifort -openmp -liomp5 -fast test_f2py.f90
! $ ./a.out
! program main
!       use test_case
!       real*8 :: i(10)
!       integer :: j
!       
!       do j=1,10
!               i(j)=0.0
!       enddo
!       
!       write(*,*) i
!       call test(i)
!       write(*,*) i
! end program main