Hello, I am encountering a segmentation fault when assigning an array of derived types to an array of derived types. The error only occurs with optimizations on and only on the newest version of pgfortran (15.10). I’ve reduced it to the following example code:
module mymod
implicit none
type interval
real :: upper
end type interval
type :: tree_node
type(interval), pointer :: box(:) => null()
end type tree_node
contains
function init(dimen) result (res)
type (tree_node), pointer :: res
integer, intent(in) :: dimen
integer :: i
allocate (res)
allocate(res%box(dimen))
do i=1,dimen
res%box(i)%upper = real(i)**2
enddo
end function init
end module mymod
program main
use mymod
implicit none
integer :: dimen
type (tree_node), pointer :: a, b
dimen = 3 ! Size of box(:)
a => init(dimen)
b => init(dimen)
write(*,'(a,3f5.1)') 'b is: ', b%box%upper
! -- This is the line that fails
a%box%upper = b%box%upper
write(*,'(a,3f5.1)') 'a is: ', a%box%upper
write(*,*) 'complete'
end program main
With optimizations on, the code reliably seg faults:
$ pgfortran --version
pgfortran 15.10-0 64-bit target on x86-64 Linux -tp istanbul
The Portland Group - PGI Compilers and Tools
Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
$ pgfortran test_derarray.f90 && ./a.out
b is: 1.0 4.0 9.0
a is: 1.0 4.0 9.0
complete
$ pgfortran -O3 test_derarray.f90 && ./a.out
b is: 1.0 4.0 9.0
Segmentation fault
Older versions of PGI (I tested 12.9, 14.3 and 15.5) do not have this problem, and neither do other common compilers I tested (ifort 16.0 and gfortran 4.8 and 5.2).
I think the code is standard-conforming because a%box%upper and b%box%upper are conformable. Even if it’s not, though, this segmentation fault is frustrating because the error is unique to the latest PGI, and only with optimizations on.
I originally stumbled on this issue when using Matthew B. Kennel’s kdtree2 implementation, and this sample code is based on his work.