1 / 26

Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, 2007 dsavas@clemson

Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, 2007 dsavas@clemson.edu. Part II: Contents. Data Declarations and Specifications ARRAYS and Array Handling Where & Forall statements. Data Specifications & Declarations. Type declarations ( built in and user-defined)

fordon
Download Presentation

Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, 2007 dsavas@clemson

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, 2007 dsavas@clemson.edu

  2. Part II: Contents • Data Declarations and Specifications • ARRAYS and Array Handling • Where & Forall statements

  3. Data Specifications & Declarations • Type declarations ( built in and user-defined) • IMPLICIT statement • PARAMETER statement • DIMENSION statement • DATA statements • SAVE statement • USE statement

  4. Type Declarations Syntax: type [,attribute] :: list TYPE can be one of ; INTEGER ( KIND= n) - REAL ( KIND=n) COMPLEX( KIND= n ) - LOGICAL(KIND=n) CHARACTER ( LEN=m,KIND=n) • where (KIND=n) is OPTIONAL TYPE ( type-name) ATTRIBUTE can be a combination of; PARAMETER - PUBLIC - PRIVATE POINTER - TARGET - ALLOCATABLE DIMENSION - INTENT(inout) - OPTIONAL SAVE - EXTERNAL - INTRINSIC

  5. Example Type Declarations • Old Style (FTN77) INTEGER N , M PARAMETER ( N = 20 , M=30) REAL X , Y , Z, PI DIMENSION X(N) , Y(N) , Z(N) DATA PI / 3.1416 / • New Style (FTN90 onwards) INTEGER, PARAMETER :: N =20 , M = 30 REAL, DIMENSION(N) :: X , Y , Z REAL :: PI = 3.146 Note: The above two code segments are identical in effect.

  6. Example use of KIND valuesin variable declarations INTEGER , PARAMETER :: SMLINT=SELECTED_INT_KIND(2) INTEGER , PARAMETER :: BIGINT=SELECTED_INT_KIND(7) INTEGER,PARAMETER :: BIG=SELECTED_REAL_KIND(7,40) INTEGER(KIND=SMLINT) :: I , J , K INTEGER(KIND=BIGINT) :: ISUM , JSUM REAL ( KIND=BIG) :: A , B ,C The Intrinsic function KIND( )returns the kind value of its argument. EXAMPLE: IKIND = KIND(1.0E60)

  7. User Defined Types Also referred as Derived-Data-Types or Structures EXAMPLE: TYPE VERTEX REAL X , Y, Z END TYPE VERTEX TYPE ( VERTEX ) :: CORNER1 , CORNER2 CORNER2%Y = 2.5 ; CORNER1=( 1.0,1.0,0.2)

  8. Derived Types ( Examples) TYPE VERTEX REAL :: X , Y, Z END TYPE VERTEX TYPE PATCH TYPE ( VERTEX ) :: CORNER(4) END PATCH TYPE (VERTEX) :: P1 , P2, P3 , P4 TYPE (PATCH) :: MYPATCH , YOURPATCH P1 = ( 0.0 , 0.0 , 0.0 ) ; P3 = ( 1.0 , 1.0 , 0.0 ) P2 = ( 1.0 , 0.0 , 0.0 ) ; P4 = ( 0.0 , 1.0 , 0.0 ) MYPATCH = ( P1 , P2, P3,P4 ) YOURPATCH = MYPATCH YOURPATCH%CORNER(1) = MYPATCH%CORNER(4) - & (1.0,1.0,1.0)

  9. Derived Types ( More Examples) TYPE USERNAME CHARACTER*2 :: DEPARTMENT INTEGER :: STATUS CHARACTER*4 :: INITIALS TYPE ( USERNAME ) , POINTER :: NEIGHBOUR END TYPE USERNAME TYPE ( USERNAME ) , DIMENSION(1000) :: USERS USERS(1) = ( ‘CS’ , 1 , ‘DS’ ) USERS(12) = (‘CS’ , 1 , ‘PF’ ) USERS(1)%NEIGHBOUR => USERS(12) NULLIFY( USERS(12)%NEIGHBOUR )

  10. Array Declarations • STATIC INTEGER, PARAMETER :: N = 36 REAL :: A( 20) , B(-1:5) , C(4,N,N) , D(0:N,0:4) INTEGER , DIMENSION(10,10) :: MX ,NX,OX Note that up to 7 dimensional arrays are allowed. • ALLOCATABLE ( Dynamic global memory allocation) REAL, ALLOCATABLE :: A(:) , B(:,:,:) • AUTOMATIC( Dynamic local memory allocation) REAL, DIMENSION SIZE(A) :: work • ASSUMED SHAPE ( Used for declaring arrays passed to procedures ) REAL A(* )! Fortran77 syntax REAL :: A(:)! or A(:,:) so on Fortran90 syntax

  11. Allocatable Array Examples Used for declaring arrays whose size will be determined during run-time. REAL, ALLOCATABLE :: X(:) , Y(:,:) , Z(:) : READ(*,*) N ALLOCATE( X(N) ) ALLOCATE ( Y(-N:N,0:N ) , STATUS=status) ALLOCATE ( Z(SIZE(X) ) : DEALLOCATE ( X,Y,Z) END

  12. Assumed Shape Array Examples Used for declaring the dummy array arguments’ dimensions to procedures when these vary from call to call. PROGRAM TEST REAL :: AX1(20) , AX2(30) , AY(10,10) , BX1(80) , BX2(90),BY(20,30) : Call spline( AX1, AX2, AY) Call spline (BX1, BX2, BY) : END SUBROUTINE SPLINE( X1 , X2 , Y ) REAL , DIMENSION(:) :: X1 , X2 REAL , DIMENSION(:,:) :: Y : RETURN END

  13. Automatic Array Examples Use this method when you need short term storage for intermediate results. WORK arrays in NAG library are good examples ! SUBROUTINE INTERMIT( X1 , X2 , Y ,M) REAL , DIMENSION(:) :: X1 , X2 , Y INTEGER :: M REAL , DIMENSION(SIZE(Y) ) :: WORK COMPLEX , DIMENSION(M) :: CWORK : RETURN END

  14. Array Terminology • RANK , EXTENT, SHAPE and SIZE Determines the conformance • CONFORMANCE REAL A( 4,10) , B( 0:3,10) , C( 4,5,2) , D(0:2 , -1:4 , 6 ) A & B are: rank=2, extents=4 and 10 shape=4,10, size=40 C is rank=3, extents=4,5and 2 - shape=4,5,2 - size=40 D is rank=3 - extents=3 , 6 and 6 - shape=3,6,6 - size= 108 For two arrays to be conformable their rank, shape and size must match up. Above only A and B are conformable with each other.

  15. Whole Array Operations • Whole array operations can be performed on conformable arrays. This avoids using DO loops. REAL :: A(10,3,4) , B( 0:9,3,2:5) , C(0:9,0:2,0:4) : C = A + B ; C = A*B ; B= C/A ; C =sqrt(A) All the above array expressions are valid.

  16. WHERE Statement • This feature is a way of implementing vector operation masks. It can be seen as the vector equivalent of the IF statement WHERE (logical_array_expr ) array_var=array_expr WHERE (logical_array_expr ) array_assignments ELSE WHERE array_assignments END WHERE Note: ELSE WHERE clause is optional.

  17. Examples of where REAL :: A(300) , B(300) WHERE ( A > 1.0 ) A = 1.0/A WHERE ( A > B) A = B END WHERE WHERE ( A > 0.0 .AND. A>B ) A = LOG10( A ) ELSEWHERE A = 0.0 END WHERE

  18. FORALL statement This is a new Fortran95 feature. Extending the ability of Fortran Array Processing that was introduced by the WHERE statement. Syntax: FORALL (index=subscript_triplet, scalar_mask_expression) block of executable statements END FORALL

  19. FORALL statement example FORALL ( II=1:100,A(II)>0 ) X(II)=X(II)+Y(II) Y(II)= Y(II) * X(II) END FORALL Mask is elements of A between 1 and 100 whose values are positive

  20. Referencing Array Elements REAL A (10) , X(10,20,30) , value INTEGER I , K(10) value = A( 8)OK VALUE = A(12) WRONG !!!! I = 2; value = A( I )OK J = 3 ; I = 4 ; VALUE = X ( J, 10 , I )OK Value = X (1.5 ) WRONG !!! X(:,1,1) = A(K) !OK assuming K has values within the range 1 to 10

  21. Referencing Array Sections REAL :: A(10) , B(4,8) , C(4,5,6) I = 2 ; J=3 ; K=4 Referencing the array elements: A(2) B(3,4) C( 3,1,1) A(K) B(I,4) C(I,J,K) Referencing the array section: A(2:5) B(1,1:6) C( 1:1,3:4, 1:6 ) A(I:K) B( I:J,K) C(1:I,1:J,5 )

  22. Referencing Array Sections (cont.) It is also possible to use a stride index when referring to array sections. The rule is : array(start-index:stop-index:stride , ….) Stride can be a negative integer. If start-index is omitted it is assumed to be the lower_bound If stop-index is omitted it is assumed to be the upper_bound For example if array A is declared with DIMENSION(10,20) we can reference a subsections that contains only the even colums as; A(2:10:2,: ) The following are some other examples: A(1:9:3,1:20:2) this is the same as A(1:9:3,::2) A(10:1:-1,: ) this reverses the ordering of first dimension

  23. Array Intrinsics • Many of the elemental numeric functions such as those listed below can be called with array arguments to return array results ( of same shape and size). abs , sin, acos , exp , log , int , sqrt ... • There are also additional Vector/Matrix algebra functions designed to perform vector/matrix operations: dot_product , matmul , transpose , minval , sum , size , lbound , ubound , merge , maxloc , pack

  24. Array Assignments REAL A(10,10) , B(10) , C(0:9) , D(0:30) A = 1.0 ; B=0.55 B = (/ 3. , 5. ,6. ,1., 22. ,8. ,16. , 4. ,9. , 12.0 /) I = 3 C = A( I,1:10) D(11:20) = A(I+1,1:10) D(1:10) = D(11:20) D(3:10) = D(5:12) A(2,:) = SIN( B )

  25. Acknowledgement &References: • Thanks to Manchester and North High Performance Computing, Training & Education Centre for the Student Notes. • See APPENDIX A of the above notes for a list of useful reference books • Fortran 90 for Scientists and Engineers, Brian D Hahn, ISBN 0-340-60034-9 • Fortran 90 Explained by Metcalf & Reid is available from Blackwells ‘ St Georges Lib.’ Oxford Science Publications, ISBN 0-19-853772-7

  26. THE END

More Related