Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Allocate with SOURCE= and bounds

Harald1
Novice
352 Views

The following code seems to partially disagree with other Fortran compilers:

program p
  implicit none
  integer, allocatable :: a(:), b(:), c(:)
  allocate(a, source=f(3))
  allocate(b, source=g(3))
  allocate(c, source=h(3))
  write(*,*) lbound(a,1), ubound(a,1) ! prints 1 3
  write(*,*) lbound(b,1), ubound(b,1) ! prints 1 3
  write(*,*) lbound(c,1), ubound(c,1) ! prints 1 3
contains
  pure function f(i)
    integer, intent(in) :: i
    integer :: f(i)
    f = 2*i
  end function f

  pure function g(i) result(r)
    integer, value, intent(in) :: i
    integer, allocatable :: r(:)
    r = [1,2,3]
  end function g

  pure function h(i) result(r)
    integer, value, intent(in) :: i
    integer, allocatable :: r(:)
    allocate(r(3:5))
    r = [1,2,3]
  end function h
end program p

Intel prints "1 3" for all three cases, whereas NAG, Nvidia and AMD flang print "3 5" for the third case.

My reading of F2018,

9.7.1.2 Execution of an ALLOCATE statement

(6) When an ALLOCATE statement is executed for an array with no
allocate-shape-spec-list, the bounds of source-expr determine the
bounds of the array. Subsequent changes to the bounds of source-expr
do not affect the array bounds.

suggests that the Intel compiler is likely wrong here.

 

0 Kudos
1 Reply
FortranFan
Honored Contributor II
296 Views

For whatever it's worth: Intel Fortran conforms to the standard whereas the other processors do not.  A function result once it returns only has a lower bound of 1 and the upper bound is then determined by the shape of the array.  Allocation with a shape-spec-list and its effect on the bounds only apply within the function itself and not to the returned function result.  source-expr in the ALLOCATE statement then has the bounds of the returned function result and it thus has a lower bound of 1.

Reply