issue with optional argument and assumed shaped array

Hi,

I am having some seg fault with a fortran code when compiled with pgf90 (I tried with pgi12.10). I find out, that the problem comes from a place where we use assumed shaped array and explicit array at different level of subroutine calls. Here is a reproducible small example (all array are contiguous):

program test_optional
  implicit none
  integer :: nargs, n
  real :: X_1d(2),M_1d(2), M_2d(2,2)
  character*10 arg 

  nargs = command_argument_count() 
  if( nargs == 1 ) then 
     call getarg( 1, arg ) 
     read(arg,"(I8)") n 
  else 
     print*, 'usage ./test n'
     STOP
  endif 

  X_1d(:)=real(n)
  M_1d(:)=real(n)
  M_2d(:,:)=real(n)

  if (n>1) then
     call my_print_1d(X_1d,M=M_1d)
     call my_print_2d(X_1d,M=M_2d)
  else
     call my_print_1d(X_1d)
     call my_print_2d(X_1d)
  end if

contains

subroutine my_print_1d(X,M)
  real, intent(in) :: X(:)
  REAL ,       INTENT(IN),    OPTIONAL :: M(:)
  INTEGER :: id

  id=SIZE(X,1)

  call my_print(X,id,1,M=M)

end subroutine my_print_1d

subroutine my_print_2d(X,M)
  REAL, INTENT(IN) :: X(:)
  REAL, INTENT(IN),    OPTIONAL :: M(:,:)
  INTEGER :: id
  id=SIZE(X,1)

  call my_print(X,id,id,M=M)

end subroutine my_print_2d

subroutine my_print(X,id1,id2,M)
  real, intent(in) :: X(id1)
  integer, intent(in) :: id1
  integer, intent(in) :: id2 
  REAL ,  intent(IN),    OPTIONAL :: M(id1,id2)
  INTEGER :: i,j
  
  if(present(M)) then
     do j=1,id2
        do i=1,id1
           print*, 'M,i,j',i,j,M(i,j)
        end do
     end do
  end if

  print*, 'X', X(:)

end subroutine my_print


end program test_optional

The segfault only appears when the optional are not passed:

> pgf90 -o test_optional test_optional.f90
> ./test_optional 0
Segmentation fault
> ./test_optional 2
 M,i,j            1            1    2.000000    
 M,i,j            2            1    2.000000    
 X    2.000000        2.000000    
 M,i,j            1            1    2.000000    
 M,i,j            2            1    2.000000    
 M,i,j            1            2    2.000000    
 M,i,j            2            2    2.000000    
 X    2.000000        2.000000

Is what I am doing illegal ? I’ve tried with gfortran, and there it is not an issue. Do you have any suggestion on how I should proceed (basically I would like to use the same routine my_print for array M of different dimensions)


Thanks,

Xavier

Thanks Xavier. I have sent a report to our engineers(TPR#19220) who will investigate
further.

Hi Xavier,

I got word that TPR#19220 will be fixed in the 13.5 release.

  • Mat