For Programmers: Free Programming Magazines  


Home > Archive > Fortran > August 2007 > Q: Association rules ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Q: Association rules ?
Oskar Enoksson

2007-08-18, 8:06 am

I feel I don't understand the rules for pointer- and dummy argument
association and aliasing problems - could someone help me?

If I understand right, the following program may not do what I want (?)

TYPE A_T
INTEGER, POINTER :: X
END TYPE A_T

SUBROUTINE FOO(A,X)
TYPE(A_T), INTENT(INOUT) :: A
INTEGER, INTENT(INOUT) :: X

A%X = A%X+1
X = X-1

END SUBROUTINE FOO

PROGRAM MYPROG

TYPE(A_T) A
ALLOCATE(A%X)
A%X = 0

CALL FOO(A,A%X)

WRITE(*,*) A%X ! Will it print "0" ?

END PROGRAM MYPROG


Will I be safe if I declare dummy argument X with attribute TARGET in
FOO ? If not, what about declaring X with attribute POINTER?

What about subprograms that doesn't modify X and declare X with INTENT(IN)

SUBROUTINE FOO(A,X)
TYPE(A_T), INTENT(INOUT) :: A
INTEGER, INTENT(IN) :: X

A%X = A%X+1

END SUBROUTINE FOO

PROGRAM MYPROG

TYPE(A_T) A
ALLOCATE(A%X)
A%X = 0

CALL FOO(A,A%X)

WRITE(*,*) A%X ! Will it print "1" ?

END PROGRAM MYPROG


Thanks
/Oskar
robert.corbett@sun.com

2007-08-20, 4:39 am

On Aug 18, 3:50 am, Oskar Enoksson <nob...@nowhere.org> wrote:
> I feel I don't understand the rules for pointer- and dummy argument
> association and aliasing problems - could someone help me?
>
> If I understand right, the following program may not do what I want (?)
>
> TYPE A_T
> INTEGER, POINTER :: X
> END TYPE A_T
>
> SUBROUTINE FOO(A,X)
> TYPE(A_T), INTENT(INOUT) :: A
> INTEGER, INTENT(INOUT) :: X
>
> A%X = A%X+1
> X = X-1
>
> END SUBROUTINE FOO
>
> PROGRAM MYPROG
>
> TYPE(A_T) A
> ALLOCATE(A%X)
> A%X = 0
>
> CALL FOO(A,A%X)
>
> WRITE(*,*) A%X ! Will it print "0" ?
>
> END PROGRAM MYPROG


The derived type A_T must be declared within a scoping unit.
In this case the scoping unit should be a module that is used
in FOO and MYPROG.

The program will not be standard-conforming even then.

> Will I be safe if I declare dummy argument X with attribute TARGET in
> FOO?


If both A and X in the subroutine FOO are given the attribute
TARGET, if FOO is declared in an interface block in MYPROG,
and if the declaration of the derived type A_T is fixed, the
program will be standard-conforming.

> If not, what about declaring X with attribute POINTER?
>
> What about subprograms that doesn't modify X and declare X with INTENT(IN)
>
> SUBROUTINE FOO(A,X)
> TYPE(A_T), INTENT(INOUT) :: A
> INTEGER, INTENT(IN) :: X
>
> A%X = A%X+1
>
> END SUBROUTINE FOO
>
> PROGRAM MYPROG
>
> TYPE(A_T) A
> ALLOCATE(A%X)
> A%X = 0
>
> CALL FOO(A,A%X)
>
> WRITE(*,*) A%X ! Will it print "1" ?
>
> END PROGRAM MYPROG


If you fix the declaration of the derived type A_T, the program
will be standard-conforming and it should print 1. Declaring
X to have the attribute INTENT(IN) in FOO has no effect. Removing
the second assignment is what does the trick.

Bob Corbett

Oskar Enoksson

2007-08-20, 7:10 pm

PROGRAM MYPROG
IMPLICIT NONE
TYPE A_T
INTEGER, POINTER :: X
END TYPE A_T

TYPE(A_T) A
ALLOCATE(A%X)
A%X = 0

CALL FOO(A,A%X)

WRITE(*,*) A%X ! Will it print "0" ?
CONTAINS

SUBROUTINE FOO(A,X)
TYPE(A_T), INTENT(INOUT) :: A
INTEGER, INTENT(INOUT) :: X

A%X = A%X+1
X = X-1

END SUBROUTINE FOO
END PROGRAM MYPROG

>
> If both A and X in the subroutine FOO are given the attribute
> TARGET, if FOO is declared in an interface block in MYPROG,
> and if the declaration of the derived type A_T is fixed, the
> program will be standard-conforming.


Thanks for responding. So adding TARGET to both dummy arguments A and X
will ensure that X and A%X are associated with the same target in FOO?
I'm surprised that A needs to have the TARGET attribute - why isn't it
enough that the component A%X has the POINTER attribute?

/Oskar
robert.corbett@sun.com

2007-08-21, 4:33 am

On Aug 20, 7:33 am, Oskar Enoksson <enok_tabort...@lysator.liu.se>
wrote:
> PROGRAM MYPROG
> IMPLICIT NONE
> TYPE A_T
> INTEGER, POINTER :: X
> END TYPE A_T
>
> TYPE(A_T) A
> ALLOCATE(A%X)
> A%X = 0
>
> CALL FOO(A,A%X)
>
> WRITE(*,*) A%X ! Will it print "0" ?
> CONTAINS
>
> SUBROUTINE FOO(A,X)
> TYPE(A_T), INTENT(INOUT) :: A
> INTEGER, INTENT(INOUT) :: X
>
> A%X = A%X+1
> X = X-1
>
> END SUBROUTINE FOO
> END PROGRAM MYPROG
>
>
>
> Thanks for responding. So adding TARGET to both dummy arguments A and X
> will ensure that X and A%X are associated with the same target in FOO?
> I'm surprised that A needs to have the TARGET attribute - why isn't it
> enough that the component A%X has the POINTER attribute?


You're right. In this case, A % X is a pointer subobject,
and so the A that appears in FOO does not need to have the
attribute TARGET.

Bob Corbett

Jan Vorbrüggen

2007-08-21, 4:33 am

Bob:

> If both A and X in the subroutine FOO are given the attribute
> TARGET, if FOO is declared in an interface block in MYPROG,
> and if the declaration of the derived type A_T is fixed, the
> program will be standard-conforming.


You're confusing me. In this case, I see A%X and X as being aliases, and both
are modified in the subroutine. Surely that's prohibited by the standard?

> If you fix the declaration of the derived type A_T, the program
> will be standard-conforming and it should print 1. [...] Removing
> the second assignment is what does the trick.


Agreed.

Jan
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com