replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - To point or not to point
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

To point or not to point

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Thu Mar 10, 2011 8:55 am    Post subject: To point or not to point Reply with quote

I would like to discuss the merits of passing pointers to Types and Arrays as arguments in Fortran.

Say I have an array and want to pass it to a function to calculate some property, e.g. the difference between the largest and smallest values. I always use an explicit interface these days. I would normally do something like:

Code:

MODULE MODABC

CONTAINS

   FUNCTION RANGE(A)
      REAL, INTENT (IN) ::A(:)
      REAL :: RANGE

      RANGE = MAXVAL(A) - MINVAL(A)
   END FUNCTION RANGE

END MODULE MODABC

PROGRAM ANON

   USE MODABC
   REAL :: A(100)
   REAL :: MYRANGE
   
   ! Create some numbers
   CALL RANDOM_NUMBER(A)

   ! Calculate range and print
   MYRANGE = RANGE(A)
   PRINT *,'RANGE = ',MYRANGE

END PROGRAM ANON


Is it more efficient (in argument passing) to do this using pointers as follows OR doesn't it make any difference. I am interested in a general answer as well as specific one for FTN95.

For example:

Code:

MODULE MODABC

CONTAINS

   FUNCTION RANGE(A_PTR)
      REAL, POINTER ::A_PTR(:)                      ! Fortran 95 syntax
     !REAL, POINTER, INTENT(IN) :: A_PTR(:)   ! Fortran 2003 syntax
      REAL :: RANGE

      RANGE = MAXVAL(A_PTR) - MINVAL(A_PTR)
   END FUNCTION RANGE

END MODULE MODABC

PROGRAM ANON

   USE MODABC
   REAL, TARGET :: A(100),
   REAL, POINTER :: A_PTR(:)
   REAL :: MYRANGE
   
   ! Create some numbers
   CALL RANDOM_NUMBER(A)

   ! Calculate range and print
   A_PTR => A
   MYRANGE = RANGE(A_PTR)
   PRINT *,'RANGE = ',MYRANGE

END PROGRAM ANON


And what about passing data types, is it more efficient to pass a pointer:

Code:

MODULE MOD2

   TYPE S_TYPE
      REAL :: A(100)
      REAL :: B(1000)
      REAL :: C(10000)
   END TYPE

CONTAINS

   SUBROUTINE PROCESS(S_PTR)
      TYPE(S_TYPE), POINTER :: S_PTR                     ! Fortran 95 syntax
     !TYPE(S_TYPE), POINTER, INTENT(IN) :: S_PTR   ! Fortran 2003 syntax

      ! Do something with the data pointed too by S_PTR

   END SUBROUTINE PROCESS

END MODULE MOD2

PROGRAM ANON

   USE MOD2
   TYPE (S_TYPE), TARGET :: S
   TYPE (S_TYPE), POINTER :: S_PTR

   CALL RANDOM_NUMBER(S%A)
   CALL RANDOM_NUMBER(S%B)
   CALL RANDOM_NUMBER(S%C)

   S_PTR => S
   CALL PROCESS(S_PTR)

END ANON
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group