PROGRAM ldacode ! ! Program to demonstrate the need for LDA argument ! in older F77 programs ! USE const IMPLICIT NONE INTEGER :: i,j,n,k,LDA ! Statically allocate A as 4x4 REAL(kind=dp) :: A(4,4) ! LDA is the number of rows of A when A is statically declared ! or dynamically allocated LDA = 4 ! Suppose we only use the 3x3 part of A n = 3 k = 0 DO i = 1,n DO j = 1,n k = k + 1 A(i,j) = k ENDDO ENDDO ! Write out A in the main program DO i = 1,n DO j = 1,n WRITE(*,*) 'i,j,A(i,j) = ',i,j,A(i,j) ENDDO ENDDO ! Send A to a subroutine the wrong way CALL writeAbad(n,n,A) ! Send A to a subroutine the right way CALL writeAgood(n,n,LDA,A) END PROGRAM ldacode !!!!!!!!!!!!!!!!!!!! SUBROUTINE writeAbad(n,m,A) USE const IMPLICIT NONE INTEGER,INTENT(IN) :: n,m REAL(Kind=dp),INTENT(IN) :: A(n,m) INTEGER :: i,j WRITE(*,*) DO i = 1,n DO j = 1,m WRITE(*,*) 'in bad: i,j,A(i,j) = ',i,j,A(i,j) ENDDO ENDDO END SUBROUTINE writeAbad !!!!!!!!!!!!!!!!!!!! SUBROUTINE writeAgood(n,m,LDA,A) ! ! To correctly pass A to a subroutine, the ! leading dimension of A (LDA) must be used ! to size A the number of rows of A in the subroutine. ! The row size of A here must exactly match the dimension ! that was used when A was sized (static or dynamic) in ! the calling routine ! USE const IMPLICIT NONE INTEGER,INTENT(IN) :: n,m,LDA REAL(Kind=dp),INTENT(IN) :: A(LDA,m) INTEGER :: i,j WRITE(*,*) DO i = 1,n DO j = 1,m WRITE(*,*) 'in good: i,j,A(i,j) = ',i,j,A(i,j) ENDDO ENDDO END SUBROUTINE writeAgood