Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question about associated()

128 views
Skip to first unread message

Arjen Markus

unread,
Sep 19, 2016, 6:42:17 AM9/19/16
to
Hello,

I ran into a little riddle the other day. In Fortran 2003 it is permitted to associate a "2D" pointer to a 1D array, something like:

real, dimension(100), target :: array
real, dimension(:,:), pointer :: matrix

matrix(1:20,1:5) => array

However, I cannot check that the pointer "matrix" is associated to the array "array". A statement like:

write( *, '(a,l)' ) 'Associated? ', associated(matrix,array)

causes the compiler to complain - the number of dimensions for both arguments must match.

How can I verify this type of association?

Regards,

Arjen

Thomas Jahns

unread,
Sep 19, 2016, 7:46:05 AM9/19/16
to
In case you can use the F2008 CONTIGUOUS keyword, you could first create
1D-POINTERS to the higher dimensional one? I think this could be possible
without CONTIGUOUS, too, but might give run-time instead of compile-time errors
slightly more often when used incorrectly.

The alternative would be to take POINTERs to first and last element of each
array and check if the corresponding ranges overlap in memory via C address
computations on intptr_t? That would even work with associations to slices in
reversed directions.

Thomas



FortranFan

unread,
Sep 19, 2016, 9:06:28 AM9/19/16
to
Perhaps an oversight in the standard? That the ASSOCIATED function (which was what introduced in Fortran 90, I think) was not updated to reflect the "new feature" of remapping to rank-one arrays permitted in Fortran 2003 revision?

Arjen Markus

unread,
Sep 19, 2016, 9:48:18 AM9/19/16
to
Op maandag 19 september 2016 15:06:28 UTC+2 schreef FortranFan:
That is what I am thinking too. Thomas's suggestions may work, but they look a bit of kludge to me.

Regards,

Arjen

Steve Lionel

unread,
Sep 19, 2016, 12:50:57 PM9/19/16
to
On 9/19/2016 9:06 AM, FortranFan wrote:
> Perhaps an oversight in the standard? That the ASSOCIATED function (which was what introduced in Fortran 90, I think) was not updated to reflect the "new feature" of remapping to rank-one arrays permitted in Fortran 2003 revision?

Interesting. I will study this and write a paper for the committee about
it if it seems warranted.

--
Steve Lionel
Developer Products Division
Intel Corporation
Merrimack, NH

For email address, replace "invalid" with "com"

User communities for Intel Software Development Products
http://software.intel.com/en-us/forums/
Intel Software Development Products Support
http://software.intel.com/sites/support/
My Fortran blog
http://www.intel.com/software/drfortran

Refer to http://software.intel.com/en-us/articles/optimization-notice
for more information regarding performance and optimization choices in
Intel software products.

FortranFan

unread,
Sep 19, 2016, 1:13:35 PM9/19/16
to
Thanks much, Steve - appreciate your follow-up greatly.

Dick Hendrickson

unread,
Sep 19, 2016, 1:28:19 PM9/19/16
to
How about
matrix = 1
array = 2
> write( *, '(a,l)' ) 'Associated? ', all( matrix = 2 )

A little heavy handed, but it might work. ;)

Dick Hendrickson

FortranFan

unread,
Sep 19, 2016, 1:41:26 PM9/19/16
to
On Monday, September 19, 2016 at 1:28:19 PM UTC-4, Dick Hendrickson wrote:

> ..
> How about
> matrix = 1
> array = 2
> > write( *, '(a,l)' ) 'Associated? ', all( matrix = 2 )
>
> A little heavy handed, ..


Indeed.

It might perhaps be better then to do write's one's own ASSOCIATED function relying on the C addresses of the two objects:

---
program p

use, intrinsic :: iso_c_binding, only : c_loc

implicit none

integer, parameter :: n = 1
integer, target :: i(n)
integer, pointer :: j(:,:)
integer :: c_add_i
integer :: c_add_j

i = 42
j(1:n,1:n) => i

c_add_i = transfer( source=c_loc(i), mold=c_add_i )
c_add_j = transfer( source=c_loc(j), mold=c_add_j )
print *, "c_add_i = ", c_add_i
print *, "c_add_j = ", c_add_j

stop

end program p
---

Upon execution using gfortran 7.0 (experimental version):

c_add_i = 2686564
c_add_j = 2686564



Steve Lionel

unread,
Sep 19, 2016, 2:02:29 PM9/19/16
to
On 9/19/2016 9:06 AM, FortranFan wrote:
> On Monday, September 19, 2016 at 6:42:17 AM UTC-4, Arjen Markus wrote:
>> Hello,
>>
>> I ran into a little riddle the other day. In Fortran 2003 it is permitted to associate a "2D" pointer to a 1D array, something like:
>>
>> real, dimension(100), target :: array
>> real, dimension(:,:), pointer :: matrix
>>
>> matrix(1:20,1:5) => array
>>
>> However, I cannot check that the pointer "matrix" is associated to the array "array". A statement like:
>>
>> write( *, '(a,l)' ) 'Associated? ', associated(matrix,array)
>>
>> causes the compiler to complain - the number of dimensions for both arguments must match.
>>
>> How can I verify this type of association?
>
> Perhaps an oversight in the standard? That the ASSOCIATED function (which was what introduced in Fortran 90, I think) was not updated to reflect the "new feature" of remapping to rank-one arrays permitted in Fortran 2003 revision?

No, not an oversight in the standard, but rather a compiler that didn't
keep up with the change.

The wording in the F2008 standard, not much different from F2003, is:

"TARGET (optional) shall be allowable as the data-target or proc-target
in a pointer assignment statement (7.2.2) in which POINTER is
data-pointer-object or proc-pointer-object ."

There's no mention of rank here. Rather, you have to go look at the
description of pointer assignment to see what is "allowable". In F2003,
it said:

C720 (R735) If bounds-remapping-list is specified, data-target shall
have rank one; otherwise, the ranks of data-pointer-object and
data-target shall be the same

F2008 removes the restriction that the data-target be rank one and
simply says:

C718 (R733) If bounds-remapping-list is not specified, the ranks of
data-pointer-object and data-target shall be the same.

In any case, if the pointer assignment is "allowable", then so is
ASSOCIATED. I note that Intel Fortran gives the bogus error about the
ranks needing to match and will report that to the developers.

paul.rich...@gmail.com

unread,
Sep 19, 2016, 2:35:37 PM9/19/16
to
This is gcc PR77652.

Thanks, Steve!

Paul

Steve Lionel

unread,
Sep 19, 2016, 2:53:10 PM9/19/16
to
On 9/19/2016 2:02 PM, Steve Lionel wrote:

>
> No, not an oversight in the standard, but rather a compiler that didn't
> keep up with the change.

I had another think about this, and now I think I got it wrong with my
earlier reply.

The problem is that in the ASSOCIATED call there is no
bounds-remapping-list, just a pointer. So in the case in question,

matrix => array

would NOT be permitted since there is no bounds-remapping-list. It's
not clear to me that this is "interp fodder", as we call it, but it is
an interesting question I will raise to the committee.

FortranFan

unread,
Sep 19, 2016, 2:54:08 PM9/19/16
to
On Monday, September 19, 2016 at 2:02:29 PM UTC-4, Steve Lionel wrote:
> On 9/19/2016 9:06 AM, FortranFan wrote:
> > On Monday, September 19, 2016 at 6:42:17 AM UTC-4, Arjen Markus wrote:
> >> Hello,
> >>
> >> I ran into a little riddle the other day. In Fortran 2003 it is permitted to associate a "2D" pointer to a 1D array, something like:
> >>
> >> real, dimension(100), target :: array
> >> real, dimension(:,:), pointer :: matrix
> >>
> >> matrix(1:20,1:5) => array
> >>
> >> However, I cannot check that the pointer "matrix" is associated to the array "array". A statement like:
> >>
> >> write( *, '(a,l)' ) 'Associated? ', associated(matrix,array)
> >>
> >> causes the compiler to complain - the number of dimensions for both arguments must match.
> >>
> >> How can I verify this type of association?
> >
> > Perhaps an oversight in the standard? That the ASSOCIATED function (which was what introduced in Fortran 90, I think) was not updated to reflect the "new feature" of remapping to rank-one arrays permitted in Fortran 2003 revision?
>
> No, not an oversight in the standard, but rather a compiler that didn't
> keep up with the change.
>
> The wording in the F2008 standard, not much different from F2003, is:
>
> "TARGET (optional) shall be allowable as the data-target or proc-target
> in a pointer assignment statement (7.2.2) in which POINTER is
> data-pointer-object or proc-pointer-object ."
>
> There's no mention of rank here. ..


But, Steve, the Fortran 2008 document says in Section 13.7.16 under lines 33:36:

33 Case (vii): If TARGET is present and is an array pointer, the result is true if and only if POINTER and
34 TARGET are both associated, have the same shape, are neither of size zero nor arrays whose
35 elements are zero-sized storage sequences, and occupy the same storage units in array element
36 order.

See the stuff about the shape on line 34. This wording needs to be revised, no?

herrman...@gmail.com

unread,
Sep 19, 2016, 6:57:43 PM9/19/16
to
On Monday, September 19, 2016 at 11:54:08 AM UTC-7, FortranFan wrote:

(snip on ASSOCIATED, and pointers with different rank.)

> But, Steve, the Fortran 2008 document says in Section 13.7.16 under lines 33:36:

> 33 Case (vii): If TARGET is present and is an array pointer, the result is
> true if and only if POINTER and TARGET are both associated, have the
> same shape, are neither of size zero nor arrays whose
> elements are zero-sized storage sequences, and occupy the
> same storage units in array element order.

If you just want to make sure that the arrays start in the same place,

if(c_loc(a).eq.c_loc(b)) ...

Someone might want ASSOCIATED to distinguish pointers with
different shape.

Otherwise, generate an appropriate rank pointer for the second argument
such that you compare two same rank pointers.

> See the stuff about the shape on line 34. This wording needs to be revised, no?

The wording for ASSOCIATED indicates that both have the same shape, not
just the same sequence of storage locations.

I suppose one could have an ASSOCIATED2 that relaxed that requirement.

But generating a new pointer with appropriate shape doesn't seem so
much extra work.

-- glen

Arjen Markus

unread,
Sep 20, 2016, 5:37:03 AM9/20/16
to
I was unaware of this feature, I must say, but I came across it in the overview by John Reed of Fortran 2008.

Regards,

Arjen
0 new messages