I think I have found another compiler bug - it appears to generate wrong code.
The following code gives a run-time segfault, although I believe it to be correct. (It runs to completion when compiled with Intel, GNU, Sun/Oracle, NAG, Pathscale and open64 Fortran). The segfault appears internal to “pack”, and turning on -Mchkptr appears to diagnose an error with a compiler-generated array.
This occurs with pgf90 v 12.9 on linux x86_64 at any opti mization level.
program packtest
integer, parameter :: n=36
logical, dimension(n,2) :: mask_0
logical, dimension(:), allocatable :: mask
integer, dimension(:),allocatable :: rank, tmp
integer :: nzero, k
allocate(rank(n), tmp(n), mask(n))
mask_0 = reshape(&
[.TRUE.,.TRUE.,.TRUE., .FALSE.,.TRUE.,.TRUE.,.TRUE.,.TRUE.,.TRUE.,&
.TRUE., .FALSE.,.TRUE.,.TRUE.,.TRUE.,.TRUE.,.TRUE.,.TRUE., .FALSE.,&
.FALSE.,.TRUE.,.TRUE.,.TRUE.,.TRUE.,.TRUE.,.TRUE., .FALSE.,.TRUE.,&
.TRUE.,.TRUE.,.TRUE.,.TRUE.,.TRUE., .FALSE.,.TRUE.,.TRUE.,.TRUE.,&
.TRUE.,.TRUE.,.TRUE., .FALSE.,.TRUE.,.TRUE.,.TRUE.,.TRUE.,.TRUE.,&
.TRUE., .FALSE.,.TRUE.,.TRUE.,.TRUE.,.TRUE.,.TRUE.,.TRUE., .FALSE.,&
.FALSE.,.TRUE.,.TRUE.,.TRUE.,.TRUE.,.TRUE.,.TRUE., .FALSE.,.TRUE.,&
.TRUE.,.TRUE.,.TRUE.,.TRUE.,.TRUE., .FALSE.,.TRUE.,.TRUE.,.TRUE.],[n,2])
do k = 0,1
write(*,*) k
rank = 0
mask = mask_0(:,k+1)
nzero = count(mask)
tmp(1:nzero) = pack(rank, mask)
tmp(nzero+1:n) = pack(rank, .not. mask)
rank = tmp
end do
write(*,*) rank
end program packtest