This sample code segfaults with pgfortran, but not with gfortran (for example). The problem is arising from the pack function, and it works for 10 times smaller array, but there should be no memory problem with this size of array either. I see some pack-related posts form the pasts that claim the problem was fixed.
I have pgfortran 17.10-0 and it does the same both on macOS and on Linux. This is not even using GPU, the sample code is a result of hours of debugging.
integer :: A(5000000), B(10) integer ::N A=0; B=0; A(1)=1; A(5000000)=2 N=count(A>0) B(1:N)=pack(A, mask=(A>0))