My Fortran code is below(test.f):
subroutine sub(n1,n2,wa)
implicit none
integer, intent(in) :: n1, n2
real(4), intent(inout) :: wa(1_8:1_8*n1*n2)
integer(8) :: i, j, ms
print*, 'in sub, 1_8*n1*n2=', 1_8*n1*n2
print*, 'in sub, size of wa:', size(wa,kind=8)
ms=0
!$omp parallel default(shared) private(i,j,ms)
!$omp do
do i=1, n1
do j=1, n2
ms=(i-1)*n2+j
wa(ms)=ms*1.d0
enddo; enddo;
!$omp end do nowait
!$omp end parallel
print*, 'size of wa:', size(wa,kind=8)
return
end subroutine sub
program main
implicit none
integer, parameter :: n1=2**11,n2=2**20
real(4), allocatable :: wave(:)
integer :: ierr
integer(8) :: i
allocate(wave(1_8*n1*n2), stat=ierr)
!$omp parallel default(shared) private(i)
!$omp do
do i=1_8,1_8*n1*n2
wave(i)=0.d0
enddo
!$omp end do nowait
!$omp end parallel
print*, 'in main, size of wave:', size(wave,kind=8)
call sub(n1, n2, wave)
print*, wave(1_8*n1*n2)
deallocate(wave, stat=ierr)
end program main
n1 and n2 can be larger, and make sure that n1*n2 is an long integer (>2**31-1). I just want to test how to use very large array in a subroutine.
I compile with: ifort -openmp -CB test.f
.
the array wa
in subroutine sub
will make an error if I use -CB
option for checking bound of an array.
This is the error information:
in main, size of wave: 2147483648
in sub, 1_8*n1*n2= 2147483648
in sub, size of wa: 0
forrtl: severe (408): fort: (2): Subscript #1 of the array WA has
value 108003329 which is greater than the upper bound of -2147483648.
the number in the error information is random.
When I declare wa
in subroutine as real(4), intent(inout) :: wa(1)
, the program will run well. Can someone tell me why?