[J3] Lower bound of an allocatable array function result
Steidel, Jon L
jon.l.steidel at intel.com
Fri Jun 14 16:51:32 EDT 2019
I searched “bound” and “result” throughout the standard, and could find no guidance as to what the lower
bounds should be. I also think lbound(f()) and lbound(z) should print the same value in both cases.
NAG and ifort do print the same values, which are 1. From the things I found and cited in the first e-mail,
I think the values should be -3 and -1. But I cannot find the words to prove it.
-jon
From: J3 [mailto:j3-bounces at mailman.j3-fortran.org] On Behalf Of Daniel C Chen via J3
Sent: Friday, June 14, 2019 4:33 PM
To: General J3 interest list <j3 at mailman.j3-fortran.org>
Cc: Daniel C Chen <cdchen at ca.ibm.com>
Subject: Re: [J3] Lower bound of an allocatable array function result
Function reference seems "is an array" as the rank is one of the characteristics of a function result that can be used in an expression (15.5.3 p1).
However, I would think the lbound(f()) should have been the same as lbound(z) (should be both 1 or -3), but neither Bill's result nor mine has that.
Moreover, Only the shape is a characteristic of a function result not bounds.
I couldn't find any wording saying if the function result should have the bounds as it is declared or allocated if it is an allocatable, or its lower bounds should default to 1.
Thanks,
Daniel
XL Fortran Development, Fortran Standard Representative
IBM Toronto Software Lab
Phone: 905-413-3056
Tie: 969-3056
Email: cdchen at ca.ibm.com<mailto:cdchen at ca.ibm.com>
http://www.ibm.com/software/awdtools/fortran/xlfortran
[Inactive hide details for Bill Long via J3 ---06/14/2019 03:39:34 PM---I tried our compiler on Jon’s test case: > ftn test1.f9]Bill Long via J3 ---06/14/2019 03:39:34 PM---I tried our compiler on Jon’s test case: > ftn test1.f90
From: Bill Long via J3 <j3 at mailman.j3-fortran.org<mailto:j3 at mailman.j3-fortran.org>>
To: General J3 interest list <j3 at mailman.j3-fortran.org<mailto:j3 at mailman.j3-fortran.org>>
Cc: Bill Long <longb at cray.com<mailto:longb at cray.com>>
Date: 06/14/2019 03:39 PM
Subject: [EXTERNAL] Re: [J3] Lower bound of an allocatable array function result
Sent by: "J3" <j3-bounces at mailman.j3-fortran.org<mailto:j3-bounces at mailman.j3-fortran.org>>
________________________________
I tried our compiler on Jon’s test case:
> ftn test1.f90
> ./a.out
lbound(y) = -1 ; y = 1, 2, 3
lbound(f()) = -3
lbound(z) = 1 ; z = 1, 2, 3
lbound(f()) = -1
lbound(z) = 1 ; z = 1, 2, 3
Not quite the same as Daniel’s output. The rule that seems to apply is that z is allocated "the shape of expr with each lower bound equal to the corresponding element of LBOUND (expr) if expr is an array”. So I guess the question is whether the function reference “is an array” for the meaning of this requirement.
Cheers,
Bill
> On Jun 14, 2019, at 12:48 PM, Daniel C Chen via J3 <j3 at mailman.j3-fortran.org<mailto:j3 at mailman.j3-fortran.org>> wrote:
>
> I got:
>
> > a.out
> lbound(y) = -1 ; y = 1 2 3
> lbound(f()) = 1
> lbound(z) = -3 ; z = 1 2 3
> lbound(f()) = 1
> lbound(z) = -1 ; z = 1 2 3
>
> I think 10.2.1.3 Interpretation of intrinsic assignment defines the low bound of y should be the same as the "expr".
>
> Thanks,
>
> Daniel
>
> XL Fortran Development, Fortran Standard Representative
> IBM Toronto Software Lab
> Phone: 905-413-3056
> Tie: 969-3056
> Email: cdchen at ca.ibm.com<mailto:cdchen at ca.ibm.com>
> http://www.ibm.com/software/awdtools/fortran/xlfortran
>
> <graycol.gif>"Steidel, Jon L via J3" ---06/14/2019 12:28:40 PM---5.4.10 Allocatable variables p3 states: "If an allocatable variable is an array, the rank is decla
>
> From: "Steidel, Jon L via J3" <j3 at mailman.j3-fortran.org<mailto:j3 at mailman.j3-fortran.org>>
> To: General J3 interest list <j3 at mailman.j3-fortran.org<mailto:j3 at mailman.j3-fortran.org>>
> Cc: "Steidel, Jon L" <jon.l.steidel at intel.com<mailto:jon.l.steidel at intel.com>>, "Shu, Michael" <michael.shu at intel.com<mailto:michael.shu at intel.com>>
> Date: 06/14/2019 12:28 PM
> Subject: [EXTERNAL] [J3] Lower bound of an allocatable array function result
> Sent by: "J3" <j3-bounces at mailman.j3-fortran.org<mailto:j3-bounces at mailman.j3-fortran.org>>
>
>
>
>
> 5.4.10 Allocatable variables p3 states:
> “If an allocatable variable is an array, the rank is declared, but the bounds are determined when it is allocated.”
>
> 9.7.1.2 Execution of an ALLOCATE statement p1 states:
> “When an allocate statement is executed for an array for which <allocate-shape-spec-list> is specified, the values
> of the lower and upper bound expressions determine the bounds of the array.”
>
> 10.2.1.3 Interpretation of intrinsic assignments p3 states:
> “If the variable is or becomes an unallocated allocatable variable, it is then allocated with
> · . . .
> · The shape of expr with each lower bound equal to the corresponding element of LBOUND (expr) if
> expr is an array.
>
> 15.5.3 Function reference p1 states:
> “When execution of the function is complete, the value of the function result is available for use in the expression
> that caused the function to be invoked. The characteristics of the function result are determined by the interface
> of the function.”
>
> 15.3.3 Characteristics of function results states p1 states:
> “If a function result is an array that is not allocatable or a pointer, its shape is a characteristic.
> If a type parameter of a function result or a bound of a function result array is not a constant expression, the
> exact dependence on the entities in the expression is a characteristic.”
>
> This all leads me to believe that if an allocatable array function result is allocated with lower bounds other than 1, and
> the result of that function is assigned to an unallocated allocatable array, the variable in the assignment statement should
> be allocated with the same bounds that the function result variable was allocated with.
>
> I have access to three compilers. When I compile and execute the test below, I get the same results from two of the
> compilers, and a segmentation fault from the third. But the results show the function result lower bound, and the lower
> bound of the allocatable array that the function result is assigned to is 1.
>
> Are the two compilers wrong, or am I missing something about the lower bounds of the result of an array valued function?
>
> PROGRAM main
> INTEGER :: x(-1:1) = [ 1, 2, 3 ]
> INTEGER, ALLOCATABLE :: y(:)
> INTEGER, ALLOCATABLE :: z(:)
>
> y = x
> PRINT *, "lbound(y) = ", LBOUND(y), "; y = ", y
> PRINT *, "lbound(f()) = ", LBOUND(f1())
> z = f1()
> PRINT *, "lbound(z) = ", LBOUND(z), "; z = ", z
> DEALLOCATE (z)
> PRINT *, "lbound(f()) = ", LBOUND(f2())
> z = f2()
> PRINT *, "lbound(z) = ", LBOUND(z), "; z = ", z
>
> CONTAINS
> FUNCTION f1() RESULT( r )
> INTEGER,DIMENSION(-3:-1) :: r
>
> r = [1, 2, 3]
> END FUNCTION
>
> FUNCTION f2() RESULT( r )
> INTEGER, ALLOCATABLE :: r(:)
>
> ALLOCATE ( r(-1:1))
> r = [1, 2, 3]
> END FUNCTION
> END PROGRAM
>
>
>
>
>
>
Bill Long longb at cray.com<mailto:longb at cray.com>
Principal Engineer, Fortran Technical Support & voice: 651-605-9024
Bioinformatics Software Development fax: 651-605-9143
Cray Inc./ 2131 Lindau Lane/ Suite 1000/ Bloomington, MN 55425
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20190614/81f5e752/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.gif
Type: image/gif
Size: 105 bytes
Desc: image001.gif
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20190614/81f5e752/attachment-0001.gif>
More information about the J3
mailing list