0

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?

darrel
  • 1
  • 2
  • Are you aware that a compiler may interpret `real(4), intent(inout) :: wa(1)` not as meaning that `wa` is an array of size 1 (but as assumed size)? That may be significant depending on the "mistake" the compiler sees. If you have a message from compiler (compiling or running) then please show it. – francescalus Jun 01 '18 at 13:18
  • this is the error information: 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. – darrel Jun 01 '18 at 13:22
  • Please [edit] the question, do not use comments. No that using kinds numbers as `4` and `8` directly is not recommended and not portable. Those numbers are not numbers of bytes https://stackoverflow.com/questions/838310/fortran-90-kind-parameter – Vladimir F Героям слава Jun 01 '18 at 13:24
  • I do not get any error message when I run your code. Also, the number `-2147483648` is suspicious. Please double-check that the code you show is exact and exactly corresponds to the error message. – Vladimir F Героям слава Jun 01 '18 at 13:30
  • when i set n1 and n2 smaller (n1*n2<2**31), the program will be ok using -CB option. – darrel Jun 01 '18 at 13:47
  • OK, there is clearly some integer overflow involved. It does NOT happen with my Intel Fortran. Please report your exact compiler version and configuration like 32bit/64bit. – Vladimir F Героям слава Jun 01 '18 at 14:04
  • I don't get any error with ifort either (besides that the value shown by print*, wave(1_8*n1*n2) at the end is not zero) – Rodrigo Rodrigues Jun 01 '18 at 15:52
  • if i compile with `gfortran -fopenmp --fbounds-check test.f`, there will be no problem. maybe this difference comes from the intel fortran. – darrel Jun 02 '18 at 12:15
  • I define a new `integer(8)` number `n` and `n=1_8*n1*n2`, then declare the array as `wa(n)`. The program will run well. So i think the problem come from that how the compiler deals with those declared parameters – darrel Jun 05 '18 at 09:06

1 Answers1

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 index on the error may be random, but the upper bound is not. -2147483648 is the result of the unchecked expression 2**11 * 2**20, for the default integer (kind=int32), since the maximum representable number is 2**31-1 = 2147483647.

You are declaring the upper bound of the dummy array inside the subroutine like this:

wa(1_8:1_8*n1*n2)

As both operation have the same precedence, maybe the compiler is multiplying n1 and n2 first, getting the negative result, and then multiplying by 1_8 to convert it to a larger kind. I can't test right now.

You could try using parentheses on the expression, and if you have access to a debugger, you can check the type and bounds of the variable at runtime.

Also, don't rely on "magic kind numbers" like 4 or 8. Thosr numbers are compiler dependent. You should use selected_int_kind or the constants on the intrinsic module iso_fortran_env.

Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36