I have a very strange error when I enable OpenMP in my compilation options. I have pinned it down to a call to a module subroutine using a dynamic sized array from my main program subroutine. Here is a simplified example:
module arr_mod
contains
subroutine add2_mod(arr)
integer, dimension(:) :: arr
integer i, n
n = size(arr)
do i=1,n
arr(i) = arr(i)+2
enddo
end subroutine
end module
PROGRAM TEST_OMP
use arr_mod
integer, dimension(2000000) :: array
array = 0
write(*,*) array(1)
contains
subroutine add2()
! Note that this subroutine is not even called in the main program...
! When the next line is commented, the program runs.
call add2_mod(array)
end subroutine
END PROGRAM TEST_OMP
When I compile and run this program without OpenMP, it runs fine:
$ gfortran -o test_omp test_omp.f90
$ ./test_omp
0
But when I use OpenMP, the program immediately segfaults:
$ gfortran -o test_omp test_omp.f90 -fopenmp
$ ./test_omp
[1] 10291 segmentation fault ./test_omp
If I remove the program subroutine (or simply comment the add2_mod
call), it works fine even with OpenMP. It still works fine even if I call the add2_mod
subroutine from the main program directly.
It also works when compiling with optimizations (tested with -O3), and when setting unlimited stack with ulimit -s unlimited
.
As far as I can tell, it works fine with Intel Fortran (tested on version 17, with no specific flags other than -qopenmp).