Fortran 90 - Center 4 Fortran FORmula TRANslation 1950년대후반IBM에서개발 9가장오래된고급언어 국제표준채택(1980, ISO) FORTRAN 77 Fortran 90, Fortran 95, Fortran

Download Fortran 90 -   Center 4 Fortran FORmula TRANslation 1950년대후반IBM에서개발 9가장오래된고급언어 국제표준채택(1980, ISO) FORTRAN 77 Fortran 90, Fortran 95, Fortran

Post on 08-May-2018

232 views

Category:

Documents

16 download

TRANSCRIPT

  • Fortran 90Fortran 90

    KISTI KISTI

  • Supercomputing Center Supercomputing Center 22

    1.1. Fortran 90 Fortran 90

    2.2. Numerical RobustnessNumerical Robustness

    3.3. Data ParallelismData Parallelism

    4.4.

  • I I

    Fortran 90 Fortran 90

    Fortran 90Fortran 90

    Fortran 77, C/C++Fortran 77, C/C++

    ..

  • Supercomputing Center Supercomputing Center 44

    Fortran Fortran

    FORFORmula mula TRANTRANslationslation19501950 IBMIBM

    (1980, ISO) (1980, ISO) FORTRAN 77FORTRAN 77

    Fortran 90, Fortran 95, Fortran 2003Fortran 90, Fortran 95, Fortran 2003

  • Supercomputing Center Supercomputing Center 55

    Fortran 77Fortran 77 (1/2)(1/2)

    7272 55 66 11 , ,

    (6(6) )

  • Supercomputing Center Supercomputing Center 66

    Fortran 77Fortran 77 (2/2)(2/2)

    (()) COMMON COMMON , EQUIVQALENCE , EQUIVQALENCE

  • Supercomputing Center Supercomputing Center 77

    Type DeclarationType Declaration attributingattributing

    New control structureNew control structure

    Numeric ProcessingNumeric Processing

    Array ProcessingArray Processing

    PointersPointers

    I/OI/O

  • Supercomputing Center Supercomputing Center 88

    Fortran 90Fortran 90

    (http://www.comphys.uni(http://www.comphys.uni--duisburg.de/Fortran90/pl/pl.html)duisburg.de/Fortran90/pl/pl.html)

  • Supercomputing Center Supercomputing Center 99

    (1/12)(1/12)

    132132

    IBM XL FortranIBM XL Fortran 67006700 ;; !! && 3131 (( ))

    IBM XL FortranIBM XL Fortran 250250

    .LT., .LE., .EQ., .NE., .GE., .GT. .LT., .LE., .EQ., .NE., .GE., .GT.

  • Supercomputing Center Supercomputing Center 1010

    (2/12)(2/12)

    PRINT*, "This line is continued &on the next line"; END ! of program

  • Supercomputing Center Supercomputing Center 1111

    (3/12)(3/12)

    Fortran 90Fortran 90 -- : a: a--z, Az, A--Z, 0Z, 0--9, _ (underscore)9, _ (underscore)

    Symbol Description Symbol Description

    SpaceSpace == EqualEqual

    ++ PlusPlus -- MinusMinus

    ** AsteriskAsterisk // SlashSlash

    (( Left parenthesisLeft parenthesis )) Right parenthesisRight parenthesis

    ,, CommaComma .. PeriodPeriod

    Single quoteSingle quote Double quoteDouble quote

    :: ColonColon ;; Semi colonSemi colon

    !! ShriekShriek && Ampersand Ampersand

    %% PercentPercent > Greater thanGreater than $$ Dollar Dollar

    ?? Question markQuestion mark

  • Supercomputing Center Supercomputing Center 1212

    (4/12)(4/12)

    Type Declaration and AttributingType Declaration and Attributing

    Fortran77 Fortran90FUNCTION encode(pixels)

    INTEGER nPARAMETER(n=1000) INTEGER pixels(n, n), encode(n, n) REAL x(n), y(n) INTEGER*8 callcall = 0 ...END

    FUNCTION encode(pixels)

    IMPLICIT NONEINTEGER, PARAMETER :: n=1000 INTEGER, DIMENSION(n, n) :: pixels, encode REAL, DIMENSION(n) :: x, y INTEGER(8) :: call=0 ...END

  • Supercomputing Center Supercomputing Center 1313

    (5/12)(5/12)

    New Control Structure(1)New Control Structure(1)SELECTSELECT--CASECASE

    IF IF

    SELECT CASE (expression)CASE(value-list)CASE(value-list)

    END SELECT

  • Supercomputing Center Supercomputing Center 1414

    (6/12)(6/12)

    New Control Structure(2)New Control Structure(2)

    PROGRAM select

    INTEGER :: n, kPRINT *, 'Enter the value n = 'READ *, n

    SELECT CASE(n)CASE (:0)

    k = -nCASE (10:)

    k = n+10CASE DEFAULT

    k = nEND SELECT

    PRINT *, k

    END

  • Supercomputing Center Supercomputing Center 1515

    (7/12)(7/12)

    New Control Structure(3)New Control Structure(3) DO DO

    DO DO END DOEND DODO WHILE DO WHILE END DOEND DO

    DOIF (logical-expr) EXIT

    END DO

    DO WHILE (logical-expr)

    END DO

  • Supercomputing Center Supercomputing Center 1616

    (8/12)(8/12)

    Numeric ProcessingNumeric Processing

    kind kind

    Array ProcessingArray ProcessingPointersPointers

    REAL, TARGET :: B(100,100) ! B target .REAL, POINTER :: U(:,:),V(:),W(:,:) ! 3

    ...U => B(I:I+2,J:J+2) ! U B 3x3 pointALLOCATE ( W(M,N) ) ! MxN W V => B(:,J) ! V B J point V => W(I-1,1:N:2) ! V W I-1 point

  • Supercomputing Center Supercomputing Center 1717

    (9/12)(9/12)

    + +

    TYPE RATIONAL ! This defines the type RATIONAL.INTEGER :: NUMERATORINTEGER :: DENOMINATOR

    END TYPE RATIONAL...TYPE (RATIONAL) :: X, Y(100,100) ! X and Y are variables of type RATIONAL.X%NUMERATOR = 2; X%DENOMINATOR = 3

    INTERFACE OPERATOR(+)FUNCTION RAT_ADD(X, Y)

    TYPE (RATIONAL) :: RAT_ADDTYPE (RATIONAL), INTENT(IN) :: X, Y

    END FUNCTION RAT_ADDEND INTERFACE

  • Supercomputing Center Supercomputing Center 1818

    (10/12)(10/12)

    Recursive Recursive

    OPTIONAL, INTENE OPTIONAL, INTENE

    TYPE LISTREAL :: DATATYPE (LIST), POINTER :: PREVIOUS, NEXT !Recursive Components

    END TYPE LIST

  • Supercomputing Center Supercomputing Center 1919

    (11/12)(11/12)

    (1)(1)

    Generic Generic

  • Supercomputing Center Supercomputing Center 2020

    (12/12)(12/12)

    (2)(2)MODULE RATIONAL_ARITHMETIC

    TYPE RATIONALINTEGER :: NUMERATORINTEGER :: DENOMINATOR

    END TYPE RATIONAL

    INTERFACE OPERATOR (+)FUNCTION RAT_ADD(X,Y)

    TYPE (RATIONAL) :: RAT_ADDTYPE (RATIONAL), INTENT(IN) :: X, Y

    END FUNCTION RAT_ADDEND INTERFACE

    ... ! and other stuff for a complete rational arithmetic facility

    END MODULE RATIONAL_ARITHMETIC

  • Supercomputing Center Supercomputing Center 2121

    Fortran 90Fortran 90 Data abstractionData abstraction

    Data hidingData hiding

    EncapsulationEncapsulation

    Inheritance and ExtensibilityInheritance and Extensibility

    PolymorphismPolymorphism

    ReusabilityReusability

  • Supercomputing Center Supercomputing Center 2222

    Fortran 90Fortran 90 (1/4)(1/4)

    Data AbstractionData Abstraction

    ) )

    Fortran 90Fortran 90 , ,

    TYPE T1INTEGER(KIND=1) :: heightREAL(KIND=1) :: weight

    END TYPE T1TYPE T2

    INTEGER(KIND=2) :: heightREAL(KIND=2) :: weight

    END TYPE T2

    TYPE T1(n)INTEGER(KIND=n) :: heightREAL(KIND=n) :: weight

    END TYPE T1

  • Supercomputing Center Supercomputing Center 2323

    Fortran 90Fortran 90 (2/4)(2/4)

    Data HidingData HidingPUBLIC, PRIVATE PUBLIC, PRIVATE

    PRIVATE ! set default visibilityINTEGER :: pos, store, stack_size ! hiddenINTEGER, PUBLIC :: pop, push ! not hidden

  • Supercomputing Center Supercomputing Center 2424

    Fortran 90Fortran 90 (3/4)(3/4)

    EncapsulationEncapsulation

    data hiding, module/module procedure, usedata hiding, module/module procedure, use modulemodule renamerename

    MODULE globalsREAL, SAVE : a, b, cINTEGER, SAVE :: i, j, k

    END MODULE globals

    USE globals ! allows all variables in the module to be accessedUSE globals, ONLY: a, c ! allows only variables a and c to be accessedUSE globals, r =>a, s => b ! allows a and b to be accessed with local variables r and s

  • Supercomputing Center Supercomputing Center 2525

    Fortran 90Fortran 90 (4/4)(4/4)

    Inheritance and ExtensibilityInheritance and Extensibility

    supersuper--type type subsub--type type

    PolymorphismPolymorphismGeneric Generic polymorphism polymorphism

    ReusabilityReusabilitymodulemodule unitunit

  • Supercomputing Center Supercomputing Center 2626

    Fortran 77, C/C++Fortran 77, C/C++ Fortran 90Fortran 90

  • Supercomputing Center Supercomputing Center 2727

    ((http://www.comphys.unihttp://www.comphys.uni--duisburgduisburg. de / . de / Fortran90/pl/pl.html) Fortran90/pl/pl.html)

    Functionality Fortran 77 C C++Fortran

    90Numerical Robustness 2 4 3 1Data Parallelism 3 3 3 1Data Abstraction 4 3 2 1Object Oriented Programming 4 3 1 2Functional Programming 4 3 2 1Average 3.4 3.2 2.2 1.2

  • Supercomputing Center Supercomputing Center 2828

    Fortran 77, C/C++Fortran 77, C/C++ (1/5)(1/5)

    Numerical RobustnessNumerical Robustness11: Fortran90 : Fortran90

    numeric polymorphism, kind type, numeric polymorphism, kind type, , , numeric environmental inquiry numeric environmental inquiry

    22: Fortran 77: Fortran 77complex type complex type

    33: C++: C++CC polymorphism polymorphism

    44: C: C

  • Supercomputing Center Supercomputing Center 2929

    Fortran 77, C/C++Fortran 77, C/C++ (2/5)(2/5)

    Data parallelismData parallelism11: Fortran90 : Fortran90

    Fortran 90Fortran 90

    Fortran 77, C, C++Fortran 77, C, C++

  • Supercomputing Center Supercomputing Center 3030

    Fortran 77, C/C++Fortran 77, C/C++ (3/5)(3/5)

    Data AbstractionData Abstraction11: Fortran90 : Fortran90

    22: C++: C++

    33: C: C Fortran 77 Fortran 77

    44: Fortran 77: Fortran 77

  • Supercomputing Center Supercomputing Center 3131

    Fortran 77, C/C++Fortran 77, C/C++ (4/5)(4/5)

    11: C++ : C++

    22: Fortran90 : Fortran90 polymorphic polymorphic

    33: C: C Fortran 77 Fortran 77

    44: Fortran 77: Fortran 77

  • Supercomputing Center Supercomputing Center 3232

    Fortran 77, C/C++Fortran 77, C/C++ (5/5)(5/5)

    Functional ProgrammingFunctional Programming11: Fortran90: Fortran90

    lazy evaluation lazy evaluation : :

    22: C++ : C++ polymorphism polymorphism

    33: C: C

    44: Fortran 77: Fortran 77recursion, recursion,

  • II II

    Numerical RobustnessNumerical Robustness

    Fortran 90Fortran 90

    . .

  • Supercomputing Center Supercomputing Center 3434

    Numerical RobustnessNumerical Robustness

    , , (()) 44

    type kind mechanismtype kind mechanismnumeric approximation modelnumeric approximation modelenvironmental intrinsic functionenvironmental intrinsic function

  • Supercomputing Center Supercomputing Center 3535

    Numeric Kind Parameterization (1/2)Numeric Kind Parameterization (1/2)

    Fortran 77Fortran 77*size*size

    REAL = REAL*4REAL = REAL*4DOUBLE PRECISION = REAL*8DOUBLE PRECISION = REAL*8

    Fortran 90Fortran 90 kind kind kind kind implementationimplementation REALREAL 4, DOUBLE PRECISION4, DOUBLE PRECISION 88

    REAL = REAL(4) = REAL(kind=4)REAL = REAL(4) = REAL(kind=4)DOUBLE PRECISION = REAL(8) = REAL(kind=8)DOUBLE PRECISION = REAL(8) = REAL(kind=8)

  • Supercomputing Center Supercomputing Center 3636

    Numeric Kind Parameterization (2/2)Numeric Kind Parameterization (2/2)

    Intrinsic Function: Intrinsic Function: KINDKINDImplementationImplementation kind kind

    INTEGER, PARAMETER :: SINGLE = KIND(1.0), &DOUBLE = KIND(1D0)

    REAL(KIND=SINGLE) single precision variables REAL(KIND=DOUBLE) double precision variables

  • Supercomputing Center Supercomputing Center 3737

    Intrinsic Function: Intrinsic Function: SELECTED_REAL_KINDSELECTED_REAL_KIND kind kind : :

    INTEGER, PARAMETER :: P9 = SELECTED_REAL_KIND(9)

    REAL(KIND=P9) 9 digit (at least) precision real variables

    1.41_SINGLE and 1.41 are the same,1.41_DOUBLE and 1.41D0 are the same,1.41_P9 is the appropriate 9+ digit representation of 1.41, and typically will be equivalent to 1.41_SINGLE(Cray) or 1.41_DOUBLE(IBM)

  • Supercomputing Center Supercomputing Center 3838

    Numeric Polymorphism (1/5)Numeric Polymorphism (1/5)

    Intrinsic Function: Intrinsic Function: GGeneric eneric SINGLE x SINGLE x COS(x) is singleCOS(x) is singleDOUBLE x DOUBLE x COS(x) is doubleCOS(x) is double

    Fortran 90Fortran 90 generic generic . .

    interface block interface block generic name generic name generic namegeneric name

  • Supercomputing Center Supercomputing Center 3939

    Numeric Polymorphism (2/5)Numeric Polymorphism (2/5)

    integerinteger realreal swapswap

    SUBROUTINE swapint (a, b)INTEGER, INTENT(INOUT) :: a, bINTEGER :: temptemp = a; a = b; b = temp

    END SUBROUTINE swapint

    SUBROUTINE swapreal (a, b)REAL, INTENT(INOUT) :: a, bREAL :: temptemp = a; a = b; b = temp

    END SUBROUTINE swapreal

  • Supercomputing Center Supercomputing Center 4040

    Numeric Polymorphism (3/5)Numeric Polymorphism (3/5)

    generic name generic name swapswap INTERFACE swap ! generic name

    SUBROUTINE swapreal (a,b)REAL, INTENT(INOUT) :: a, b

    END SUBROUTINE swaprealSUBROUTINE swapint (a, b)

    INTEGER, INTENT(INOUT) :: a, bEND SUBROUTINE swapint

    END INTERFACE

    !main programINTEGER :: m,nREAL :: x,y...CALL swap(m,n)CALL swap(x,y)

  • Supercomputing Center Supercomputing Center 4141

    Numeric Polymorphism (4/5)Numeric Polymorphism (4/5)

    INTERFACE SMOOTH ! SMOOTH is the generic nameINTEGER FUNCTION SMOOTH_INT(AA) ! for procedures SMOOTH_INT

    INTEGER :: AA(:,:) ! SMOOTH_SINGLEEND FUNCTION SMOOTH_INT ! SMOOTH_DOUBLE

    ! SMOOTH_RATIONALINTEGER FUNCTION SMOOTH_SINGLE(AA)

    REAL(SINGLE) :: AA(:,:) ! AA is an assumed shape two-END FUNCTION SMOOTH_SINGLE ! dimensional array in each case.

    INTEGER FUNCTION SMOOTH_DOUBLE(AA)REAL(DOUBLE) :: AA(:,:)

    END FUNCTION SMOOTH_DOUBLE

    INTEGER FUNCTION SMOOTH_RATIONAL(AA)TYPE(RATIONAL) :: AA(:,:)

    END FUNCTION SMOOTH_RATIONALEND INTERFACE

  • Supercomputing Center Supercomputing Center 4242

    Numeric Polymorphism (5/5)Numeric Polymorphism (5/5)

    generic namegeneric name

    INTERFACE COS ! Extends the generic propertiesFUNCTION RATIONAL_COS(X) ! of COS to return results of

    TYPE(RATIONAL) :: RATIONAL_COS ! type RATIONAL, assuming theTYPE(RATIONAL) :: X ! argument is of type RATIONAL.

    END FUNCTION RATIONAL_COS END INTERFACE

  • Supercomputing Center Supercomputing Center 4343

    Numeric Approximation Model (1/3)Numeric Approximation Model (1/3)

    ImplementationImplementation numerical numerical 1616intrinsic function intrinsic function

    Environmental inquiry: 9Environmental inquiry: 9Numeric manipulation: 7Numeric manipulation: 7

  • Supercomputing Center Supercomputing Center 4444

    Numeric Approximation Model (2/3)Numeric Approximation Model (2/3)

    wherewhere

    x x is the real value is the real value ss is (the sign of the value) is (the sign of the value) b b is the radix (base) and is usually 2; is the radix (base) and is usually 2; bb is constant for a given real kind is constant for a given real kind p p is the base b precision; is the base b precision; pp is constant for a given real kind is constant for a given real kind ee is the base is the base bb exponent of the value exponent of the value

    is the digit, base is the digit, base bb, of the value; ; , of the value; ; may be 0 only if all are 0may be 0 only if all are 0

    == pibfsbx iie ,,1

    if thiif if

    bf i

  • Supercomputing Center Supercomputing Center 4545

    Numeric Approximation Model (3/3)Numeric Approximation Model (3/3)

    IEEE arithmeticIEEE arithmeticbb = 2 : binary representation= 2 : binary representationpp = 24 : single precision= 24 : single precisionpp = 56 : double precision= 56 : double precision--127< 127< e e

  • Supercomputing Center Supercomputing Center 4646

    Environmental Inquiry (1/2)Environmental Inquiry (1/2)

    Characteristic values of a real kind Intrinsic function name

    the decimal precision PRECISION

    the decimal precision range RANGE

    the largest value HUGE

    the smallest value TINY

    a small value compared to 1; b1-p EPSILON

    the base b RADIX

    the value of p DIGITS

    the minimum value of e MINEXPONENT

    the maximum value of e MAXEXPONENT

  • Supercomputing Center Supercomputing Center 4747

    Environmental Inquiry (2/2)Environmental Inquiry (2/2)

    REAL :: a

    PRINT *, 'PRECISION =', PRECISION(a)PRINT *, 'HUGE =', HUGE(a)PRINT *, 'RADIX =', RADIX(a)PRINT *, 'DIGITS', DIGITS(a)PRINT *, 'MINEXPONENT =', MINEXPONENT(a)

    $a.outPRECISION = 6HUGE = 0.3402823466E+39RADIX = 2DIGITS 24 MINEXPONENT = -125

    (KISTI IBM system)

  • Supercomputing Center Supercomputing Center 4848

    Numeric Manipulation (1/2)Numeric Manipulation (1/2)

    Values related to the argument value Intrinsic function nameexponent value of the number, e EXPONENT(X)

    fractional part of the number, FRACTION(X)

    returns the nearest representable number(secondargument specifies direction) NEAREST(X,S)

    returns the inverted value of the distance between the two nearest possible numbers RRSPACING(X)

    multiplies X by the base to the power I (change e by value of the second argument) SCALE(X,I)

    returns the number that has the fractional part of X and the exponent I (set e to value of second argument) SET_EXPONENT(X,I)

    the distance between the two nearest possible numbers SPACING(X)

    iibfs

  • Supercomputing Center Supercomputing Center 4949

    Numeric Manipulation (2/2)Numeric Manipulation (2/2)

    PRINT*, 'EXPONENT =', EXPONENT(10.2)PRINT*, 'FRACTION =', FRACTION(10.2)

    PRINT*, 'NEAREST =', NEAREST(3.0, 2.0)

    PRINT*, 'SPACING =', SPACING(3.0)

    $a.outEXPONENT = 4

    FRACTION = 0.6374999881

    NEAREST = 3.000000238

    SPACING = 0.2384185791E-06

    (KISTI IBM system)

  • III III

    Data ParallelismData Parallelism

    Fortran 90Fortran 90

    . .

  • Supercomputing Center Supercomputing Center 5151

    WHERE WHERE

    AssumedAssumed--Shape Dummy ArgumentsShape Dummy Arguments

  • Supercomputing Center Supercomputing Center 5252

    (1/2)(1/2)

    Fortran 90Fortran 90

    Fortran 77Fortran 77 Fortran 90Fortran 90REAL a(100), b(100)

    DO 10 i = 1, 100

    a(i) = 2.0

    10 b(i) = b(i)*a(i)

    REAL, DIMENSION(100) :: a, b

    a=2.0

    b=b*a

  • Supercomputing Center Supercomputing Center 5353

    (2/2)(2/2)

    A, B, C, P, Q, R : 2A, B, C, P, Q, R : 2 Z, V : 1Z, V : 1 S, X : ScalarS, X : Scalar

    C(:,:) = A(:,:) + B(:,:)PRINT* , P(:,:)*Q(:,:) - R(:,:), SCALL T3(X, Q(:,:), Z(:) - V(:))

    =C = A+BPRINT* , P*Q-R, SCALL T3(X, Q, Z-V)

  • Supercomputing Center Supercomputing Center 5454

    (1/2)(1/2)

    ..

    =

    471532

    A ,

    =

    232145

    B

    =+

    6103677

    BA

    =

    821251210

    BA

    =

    +

    =+

    454367

    222222

    232145

    2B

    ,

  • Supercomputing Center Supercomputing Center 5555

    (2/2)(2/2)

    ..

    G(P,G(P,:) : :) : GG PP G(P,K) : G(P,K) : GG PP , K, K

    ),(/:),(:),( KPGPGPG =

  • Supercomputing Center Supercomputing Center 5656

    11 : (/1,2,3,4/): (/1,2,3,4/) DO DO

    22 RESHAPE RESHAPE

  • Supercomputing Center Supercomputing Center 5757

    DO DO

    (expression(expression--list, indexlist, index--variable=firstvariable=first--value, value, lastlast--value[, increment])value[, increment])

    (/1,2,3, (/1,2,3, , n/) = (/(k, k=1,n)/) , n/) = (/(k, k=1,n)/) (/1,0,1,0, (/1,0,1,0, ,/) = (/(1,0, j=1,500000)/),/) = (/(1,0, j=1,500000)/)

    ! Implied DO-lists:(/ ((i + j, i = 1, 3), j = 1, 2) /) ! = (/ 2, 3, 4, 3, 4, 5 /)

    ! Arithmetic expressions:(/ (1.0 / REAL(i), i = 1, 6) /)

    !=(/1.0/1.0, 1.0/2.0, 1.0/3.0, 1.0/4.0, 1.0/5.0, 1.0/6.0/)!= (/1.0, 0.5, 0.33, 0.25, 0.20, 0.167 /)

  • Supercomputing Center Supercomputing Center 5858

    AA 10001000X1000X1000 22

    (/A+1.3/) (/A+1.3/) AA 1.31.3 (100(100)) 11

    (/(/ (( ( A(j,k)+1.3, j=1,1000), ( A(j,k)+1.3, j=1,1000), k=1,1000k=1,1000 )) /)/)

    (/(/ (( ( A(j,k)+1.3, k=1,1000), ( A(j,k)+1.3, k=1,1000), j=1,1000j=1,1000 )) /)/)

  • Supercomputing Center Supercomputing Center 5959

    RESHAPE RESHAPE (1/2)(1/2)

    (1(1))

    RESHAPE(arrayRESHAPE(array--constructor,shapeconstructor,shape--vector)vector)

    ShapeShape--vectorvector (/(//)/)

    REAL, DIMENSION (3,2) :: ra

    ra = RESHAPE( (/ ((i+j, i=1,3), j=1,2) /), SHAPE=(/3,2/) )

  • Supercomputing Center Supercomputing Center 6060

    RESHAPE RESHAPE (2/2)(2/2)

    1000X1000 1000X1000

    REAL, PARAMETER, DIMENSION(1000,1000) :: Ident_1000 = &

    RESHAPE( (/ (1.0,(0.0, k=1,1000), j=1,999),1.0 /), (/1000,1000/))

  • Supercomputing Center Supercomputing Center 6161

    WHERE WHERE (1/3)(1/3)

    logical logical

    WHERE(mask) arrayWHERE(mask) array--assignmentassignment--statementstatement

    WHERE WHERE maskmask .TRUE..TRUE.

  • Supercomputing Center Supercomputing Center 6262

    WHERE WHERE (2/3)(2/3)

    WHERE (C .NE. 0) A = B/CWHERE (C .NE. 0) A = B/C

    =

    0.40.30.20.1

    B ,

    =

    0.20.00.00.2

    C

    =

    0.25.0

    A

  • Supercomputing Center Supercomputing Center 6363

    WHERE WHERE (3/3)(3/3)

    WHERE (C .NE. 0)WHERE (C .NE. 0)A = B/CA = B/C

    ELSE WHEREELSE WHEREA = BA = B

    END WHEREEND WHERE

    =

    0.20.30.25.0

    A

  • Supercomputing Center Supercomputing Center 6464

    AssumedAssumed--Shape Dummy ArgumentsShape Dummy Arguments

    Fortran Fortran dummy dummy ExplicitExplicit--Shape (F77, F90)Shape (F77, F90)AssumedAssumed--Size (F77, F90)Size (F77, F90)AssumedAssumed--Shape (F90)Shape (F90)

  • Supercomputing Center Supercomputing Center 6565

    ExplicitExplicit--ShapeShape

    dummy dummy

    SUBROUTINE S1(A, B, C, k, m, n)

    REAL:: A(100, 100) !staticREAL:: B(m, n) !adjustableREAL:: C(-10:20, k:n) !adjustable

  • Supercomputing Center Supercomputing Center 6666

    AssumedAssumed--SizeSize

    dummy dummy indexindex

    indexindex

    SUBROUTINE S2(A, B, C, k, m)

    REAL:: A(100, 100) !staticREAL:: B(m, *) !assumed-sizeREAL:: C(-10:20, k:*) !assumed-size

  • Supercomputing Center Supercomputing Center 6767

    AssumedAssumed--Shape (1/3)Shape (1/3)

    dummy dummy

    dummy dummy

    SUBROUTINE S3(A, B, C, k)

    REAL:: A(100, 100) !staticREAL:: B(:,:) !assumed-shapeREAL:: C(-10:20, k:) !assumed-shape

  • Supercomputing Center Supercomputing Center 6868

    AssumedAssumed--Shape (2/3)Shape (2/3)

    assumedassumed--shape dummy shape dummy argumentsarguments

  • Supercomputing Center Supercomputing Center 6969

    AssumedAssumed--Shape (3/3)Shape (3/3)

    ... ! calling program unitINTERFACE

    SUBROUTINE sub (ra, rb, rc)REAL, DIMENSION (:, :) :: ra, rbREAL, DIMENSION (0:, 2:) :: rc

    END SUBROUTINE subEND INTERFACE

    REAL, DIMENSION (0:9,10) :: ra ! Shape (/ 10, 10 /)CALL sub(ra, ra(0:4, 2:6), ra(3:7, 5:9))...END

    SUBROUTINE sub(ra, rb, rc) ! ExternalREAL, DIMENSION (:, :) :: ra ! Shape (/10, 10/)REAL, DIMENSION (:, :) :: rb ! Shape (/ 5, 5 /) = REAL, DIMENSION (1:5, 1:5) :: rbREAL, DIMENSION (0:, 2:) :: rc

    ! Shape (/ 5, 5 /) = REAL, DIMENSION (0:4, 2:6) :: rc...

    END SUBROUTINE sub

  • Supercomputing Center Supercomputing Center 7070

    (Array Sections)(Array Sections)

    ArrayArray--Valued Valued

    AssumedAssumed--Shape Dummy ArgumentsShape Dummy Arguments

  • Supercomputing Center Supercomputing Center 7171

    Simple SubscriptSimple SubscriptSubscript TripletSubscript TripletVector SubscriptVector Subscript

  • Supercomputing Center Supercomputing Center 7272

    Simple SubscriptSimple Subscript

    arrayarray--name(subscrptname(subscrpt--1, subscript1, subscript--2, 2, ))

    B(1,2) = 2.0B(1,2) = 2.0

    =

    0.40.30.20.1

    B

  • Supercomputing Center Supercomputing Center 7373

    Triplet Subscript (1/3)Triplet Subscript (1/3)

    (start index, end (start index, end index, stride)index, stride)

    stridestride 11start indexstart index lower boundlower boundend indexend index upper boundupper bound

  • Supercomputing Center Supercomputing Center 7474

    Triplet Subscript (2/3)Triplet Subscript (2/3)

    =

    90776654563719819182072015365445162752143139912251113

    Q

    )2(:,

    562045311

    )2,5:1( QQ =

    =

    :)5,2(:275291

    )6:5,2:1( QQ =

    = ( ) :)4,5(907766)6:4,5( QQ ==

    )2,2(::564511

    )2,2:5:1( QQ =

    =

  • Supercomputing Center Supercomputing Center 7575

    Triplet Subscript (3/3)Triplet Subscript (3/3)

    Fortran 77Fortran 77 Fortran 90Fortran 90

    REAL A(10,10),B(10,10)

    DO 1 J=1,8

    DO 2 I=1,8

    A(I,J) = B(I+1,J+1)

    2 CONTINUE

    1 CONTINUE

    REAL, DIMENSION(10,10) :: A,B

    A(1:8,1:8) = B(2:9,2:9)

  • Supercomputing Center Supercomputing Center 7676

    Vector Subscript (1/4)Vector Subscript (1/4)

    subscriptsubscript (/(//)/) 11

    REAL, DIMENSION :: ra(6), rb(3)INTEGER, DIMENSION (3) :: iv

    iv = (/ 1, 3, 5 /) ! rank 1 integer expression (vector subscript)ra = (/ 1.2, 3.4, 3.0, 11.2, 1.0, 3.7 /)rb = ra(iv) ! iv is the vector subscript

    ! = (/ ra(1), ra(3), ra(5) /) ! = (/ 1.2, 3.0, 1.0 /)

  • Supercomputing Center Supercomputing Center 7777

    Vector Subscript (2/4)Vector Subscript (2/4)

    =

    90776654563719819182072015365445162752143139912251113

    Q

    Triplet SubscriptTriplet Subscript Vector SubscriptVector SubscriptQ(1:5, 2) = Q(:, 2) Q((/1,2,3,4,5/), 2)

    Q(1:2, 5:6) = Q(:2, 5:) Q((/1,2/), (/5,6/))

    Q(5, 4:6) = Q(5, 4:) Q(5, (/4,5,6/))

    Q(1:5:2, 2) = Q(::2, 2) Q((/1,3,5/), 2)

  • Supercomputing Center Supercomputing Center 7878

    Vector Subscript (3/4)Vector Subscript (3/4)

    DO DO

    Q((/1,2,3,4,5/), 2) = Q((/(k, k=1,5)/),2)

    Q((/1,2/), (/5,6/)) = Q((/(k, k=1,2)/), (/(k, k=5,6)/))

    Q(5, (/4,5,6/)) = Q(5, (/(k, k=4,6)/))

    Q((/1,3,5/), 2) = Q((/(k, k=1,5,2)/), 2)

  • Supercomputing Center Supercomputing Center 7979

    Vector Subscript (4/4)Vector Subscript (4/4)

    subscriptsubscript

    =

    53545451

    23242421

    43444441

    33343431

    23242421

    13141411

    43444441

    /))3,4,4,1(//),5,2,4,3,2,1,4((/

    QQQQQQQQQQQQQQQQQQQQQQQQQQQQ

    Q

  • Supercomputing Center Supercomputing Center 8080

    Autonomic ArrayAutonomic Array

    Allocatable ArrayAllocatable Array

    Pointer ArrayPointer Array

  • Supercomputing Center Supercomputing Center 8181

    (F77, F90)(F77, F90)

    (F90)(F90)

  • Supercomputing Center Supercomputing Center 8282

    Autonomic Array (1/2)Autonomic Array (1/2)

    dummy dummy

    SUBROUTINE sub(n, a)IMPLICIT NONEINTEGER :: nREAL, DIMENSION(n, n) :: aREAL, DIMENSION (n, n) :: work1REAL, DIMENSION (SIZE(a, 1)) :: work2...

    END SUBROUTINE sub

  • Supercomputing Center Supercomputing Center 8383

    Autonomic Array (2/2)Autonomic Array (2/2)

    SIZE (A, n)SIZE (A, n) AA nn

    FUNCTION F18(A,N)INTEGER :: N ! A scalarREAL :: A(:,:) ! An assumed shape arrayCOMPLEX :: Local_1(N,2*N+3)

    ! Local_1 is an automatic array whose size is based on N.REAL :: Local_2(SIZE(A,1),SIZE(A,2))

    ! Local_2 is an automatic array exactly the same size as A.REAL :: Local_3(4*SIZE(A,2))

    ! Local_3 is a one-dimensional array 4 times the size of ! the second dimension of A.

    ... END FUNCTION F18

  • Supercomputing Center Supercomputing Center 8484

    Allocatable Array (1/2)Allocatable Array (1/2)

    ALLOCATABLE attributeALLOCATABLE attribute , ,

    PROGRAM simulate

    IMPLICIT NONEINTEGER :: nINTEGER, DIMENSION(:,:), ALLOCATABLE :: a ! 2D

    PRINT *,'Enter n:'READ *,nIF(.NOT. ALLOCATED(a)) ALLOCATE( a(n,2*n) )DEALLOCATE(a)

  • Supercomputing Center Supercomputing Center 8585

    Allocatable Array (2/2)Allocatable Array (2/2)

    : STAT: STAT

    ALLOCATE(allocate_object_list [, STAT= status])ALLOCATE(allocate_object_list [, STAT= status])DEALLOCATE(allocate_obj_list [, STAT= status])DEALLOCATE(allocate_obj_list [, STAT= status])

    INTEGER, DIMENSION(:), ALLOCATABLE :: ages ! 1DREAL, DIMENSION(:,:), ALLOCATABLE :: speed ! 2DREAD*, isizeALLOCATE(ages(isize), STAT=ierr)IF (ierr /= 0) PRINT*, "ages : Allocation failed"ALLOCATE(speed(0:isize-1,10),STAT=ierr)IF (ierr /= 0) PRINT*, "speed : Allocation failed"

  • Supercomputing Center Supercomputing Center 8686

    Pointer Array Pointer Array

    POINTER attributePOINTER attribute AllocatableAllocatable TARGETTARGET aliasing(pointaliasing(point to)to)

    REAL, TARGET :: B(100,100) ! B target .REAL, POINTER :: U(:,:),V(:),W(:,:) ! 3 ...

    U => B(I:I+2,J:J+2) ! U B 3x3 pointALLOCATE ( W(M,N) ) ! MxN W V => B(:,J) ! V B J point V => W(I-1,1:N:2) ! V W I-1 ... ! point

  • Supercomputing Center Supercomputing Center 8787

    ArrayArray--valued valued Transformational Transformational

    Elemental Elemental

    arrayarray--valued valued

  • Supercomputing Center Supercomputing Center 8888

    Transformational Transformational (1/4)(1/4)

    transformtransform

    4242 Fortran 90 Fortran 90 array array (21) (21) environmental inquiry environmental inquiry (9) (9) (12)(12)

  • Supercomputing Center Supercomputing Center 8989

    Transformational Transformational (2/4)(2/4)

    Transformational Intrinsic Function Comment environmental inquiry function (9)array function (21)ASSOCIATED check association of pointerBIT_SIZE number of bits of integer

    DOT_PRODUCT mathematical dot product of two vectorsKINDLEN length of a character stringMATMUL mathematical matrix product

    PRESENT check presence of an optional argumentREPEAT replicate a character stringSELECTED_INT_KINDSELECTED_REAL_KINDTRIM remove trailing blanks from a stringTRANSFER transfer bit pattern to a different type

  • Supercomputing Center Supercomputing Center 9090

    Transformational Transformational (3/4)(3/4)

    Array Intrinsic Function Comment ALL true if all of the element values are trueANY true if any of the element values are trueALLOCATED check if array is allocatedCOUNT number of elements having the value trueCSHIFT circularly shift an array along a dimensionEOSHIFT end-off shift an array along a dimensionLBOUND lower bound of an arrayMAXLOC location of maximum element in an arrayMAXVAL maximum element value in an arrayMERGE merge two arrays, under a maskMINLOC location of minimum element in an arrayMINVAL minimum element value in an arrayPACK gather an array into a vector, under a maskPRODUCT product of all the elements of an arraySHAPE shape of an arraySIZE total size of an arraySPREAD spread an array by adding a dimensionSUM sum of all of the elements of an arrayTRANSPOSE matrix transpose of a 2-dimensional arrayUBOUND upper bound of an arrayUNPACK scatter a vector into an array, under a mask

  • Supercomputing Center Supercomputing Center 9191

    Transformational Transformational (4/4)(4/4)

    REAL :: a(1024), b(4,1024)

    scalar = SUM(a) ! sum of all elementsa = PRODUCT(b, DIM=1) ! product of elements in first dimscalar = COUNT(a == 0) ! gives number of zero elementsscalar = MAXVAL(a, MASK=a .LT. 0) ! largest negative element

    LOGICAL a(n)REAL, DIMENSION(n) :: b, c

    IF ( ALL(a) ) ... ! global ANDIF ( ALL(b == c) ) ... ! true if all elements equalIF ( ANY(a) ) ... ! global ORIF ( ANY(b < 0.0) ) ... ! true if any element < 0.0

  • Supercomputing Center Supercomputing Center 9292

    Elemental Elemental

    dummy dummy , ,

    Fortran Fortran

    108108 elemental elemental ex) COS(X), SIN(X), SQRT(A), ex) COS(X), SIN(X), SQRT(A),

  • Supercomputing Center Supercomputing Center 9393

    arrayarray--valued valued

    Transformational Transformational

    FUNCTION Partial_sums(P)REAL :: P(:) ! Assumed-shape dummy arrayREAL :: Partial_sums(SIZE(P)) ! The partial sums to be returnedINTEGER :: kPartial_sums = (/(SUM(P(1:k), k=1,SIZE(P))/)

    ! This is functionally equivalent to! DO k=1,SIZE(P)! Partial_sums(k) = SUM(P(1:k))! END DO! but the do loop specifies a set of sequential ! computations rather than parallel computations

    END FUNCTION Partial_sums

  • IV IV

    Fortran 90Fortran 90

    . .

  • Supercomputing Center Supercomputing Center 9595

    INTENT AttributeINTENT Attribute

  • Supercomputing Center Supercomputing Center 9696

    ((, , )) (F77, F90)(F77, F90)

    (F90)(F90)

  • Supercomputing Center Supercomputing Center 9797

    (1/5)(1/5)

    interface_bodyinterface_bodyFUNCTION/SUBROUTINE headerFUNCTION/SUBROUTINE headerDummy Dummy

    END FUNCTION/END SUBROUTINE END FUNCTION/END SUBROUTINE

    INTERFACEinterface_body

    END INTERFACE

  • Supercomputing Center Supercomputing Center 9898

    (2/5)(2/5)

    , ,

    PROGRAM stress

    INTERFACESUBROUTINE squash(a,n)

    REAL :: a(n) END SUBROUTINE

    END INTERFACE

    INTEGER, PARAMETER :: m = 100REAL :: q(m)

    q=71CALL squash(q,m)

  • Supercomputing Center Supercomputing Center 9999

    (3/5)(3/5)

    PROGRAM testINTEGER :: i=3, j=25PRINT *,'The ratio is ',ratio(i, j)

    END PROGRAM test

    REAL FUNCTION ratio(x, y)REAL:: x, yratio=x/y

    END FUNCTION ratio

    $ xlf90 nointface.f -qextchk** test === End of Compilation 1 ===** ratio === End of Compilation 2 ===1501-510 Compilation successful for file nointface.f.ld: 0711-197 ERROR: Type mismatches for symbol: .ratiold: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.

  • Supercomputing Center Supercomputing Center 100100

    (4/5)(4/5)

    PROGRAM test

    INTERFACEREAL FUNCTION ratio(x, y)

    REAL::x, yEND FUNCTION ratio

    END INTERFACE

    INTEGER :: i=3, j=25PRINT *,'The ratio is ',ratio(i,j)

    END PROGRAM test

    REAL FUNCTION ratio(x, y)REAL:: x, yratio=x/y

    END FUNCTION ratio

  • Supercomputing Center Supercomputing Center 101101

    (5/5)(5/5)

    $ xlf90 intface.f"intface.f", line 9.34: 1513-061 (S) Actual argument attributes do not match those specified by an accessible explicit interface.** test === End of Compilation 1 ===** ratio === End of Compilation 2 ===1501-511 Compilation failed for file intface.f.

  • Supercomputing Center Supercomputing Center 102102

    (1/2)(1/2)

    (()) , , CONTAINSCONTAINS ENDEND FUNCTION FUNCTION SUBROUTINESUBROUTINE

    , ,

  • Supercomputing Center Supercomputing Center 103103

    (2/2)(2/2)

    PROGRAM test

    INTEGER :: i=3, j=25PRINT *,'The ratio is ',ratio(i,j)

    CONTAINSREAL FUNCTION ratio(x, y)

    REAL:: x, yratio=x/y

    END FUNCTION ratio

    END PROGRAM test

    $ xlf90 intproc.f"intproc.f", line 3.31: 1513-061 (S) Actual argument attributes do not match those specified by an accessible explicit interface.** test === End of Compilation 1 ===1501-511 Compilation failed for file intproc.f.

  • Supercomputing Center Supercomputing Center 104104

    INTENT Attribute (1/3)INTENT Attribute (1/3)

    , , in, out, inoutin, out, inout

    INTENT(in): INTENT(in):

    INTENT(out): INTENT(out):

    INTENT(inout):INTENT(inout): : :

  • Supercomputing Center Supercomputing Center 105105

    INTENT Attribute (2/3)INTENT Attribute (2/3)

    PROGRAM intent_test

    REAL :: x, yy = 5. CALL mistaken(x, y)PRINT *, x

    CONTAINSSUBROUTINE mistaken(a, b)

    IMPLICIT NONEREAL, INTENT(in) :: aREAL, INTENT(out) :: ba = 2*b

    END SUBROUTINE mistaken

    END PROGRAM intent_test

  • Supercomputing Center Supercomputing Center 106106

    INTENT Attribute (3/3)INTENT Attribute (3/3)

    $xlf90 intent.f"intent.f", line 14.2: 1516-055 (S) The INTENT(IN) attribute specifies that a dummy argument, or a subobject of a dummy argument, must not be redefined or become undefined during the execution of the procedure.** intent_test === End of Compilation 1 ===1501-511 Compilation failed for file intent.f.

  • Supercomputing Center Supercomputing Center 107107

    (Derived Type)(Derived Type)

    SupertypesSupertypes

  • Supercomputing Center Supercomputing Center 108108

    (1/2)(1/2)

    TYPE/END TYPETYPE/END TYPE

    attributeattribute PARAMETER attributePARAMETER attribute

    TYPE TYPE type_nametype_nameDeclarationsDeclarations

    END TYPE END TYPE type_nametype_name

  • Supercomputing Center Supercomputing Center 109109

    (2/2)(2/2)

    33 (COORDS_3D)(COORDS_3D)

    TYPE COORDS_3DREAL :: x, y, z

    END TYPE COORDS_3D

    TYPE(COORDS_3D) :: pt

    TYPE(COORDS_3D), DIMENSION(10, 20), TARGET :: pt_arr

  • Supercomputing Center Supercomputing Center 110110

    SupertypesSupertypes

    recursive recursive

    (SPHERE(SPHERE COORDS_3DCOORDS_3D supertypesupertype))

    TYPE SPHERETYPE (COORDS_3D) :: centerREAL :: radius

    END TYPE SPHERE

  • Supercomputing Center Supercomputing Center 111111

    (1/2)(1/2)

    Component by componentComponent by componentAs an objectAs an object

    : : %%

    TYPE (SPHERE) :: bubble, ball

    bubble%radius = 3.0bubble%center%x = 1.0bubble%center%y = 2.0bubble%center%z = 3.0

  • Supercomputing Center Supercomputing Center 112112

    (2/2)(2/2)

    bubble%center = COORDS_3D(1.0,2.0,3.0)

    SPHERE SPHERE bubble = SPHERE(bubble%center, 3.0)bubble = SPHERE(COORDS_3D(1.0,2.0,3.0), 3.0)bubble = (1.0,2.0,3.0,3.0) (X)ball = bubble

  • Supercomputing Center Supercomputing Center 113113

    (Module)(Module)

    :Global :Global

    ::

    :Generic :Generic

    :Public, Private :Public, Private

    ::

  • Supercomputing Center Supercomputing Center 114114

    (1/2)(1/2)

    , , USEUSE

    Global Global Global Global . COMMON, INCLUDE . COMMON, INCLUDE

    Controlled Controlled , ,

    Packaged of Whole Sets of FacilitiesPackaged of Whole Sets of Facilities , , , , , ,

    Semantic ExtensionSemantic Extension Fortran 90Fortran 90

  • Supercomputing Center Supercomputing Center 115115

    (2/2)(2/2)

    UseUse--associationassociation USEUSE

    MODULE

    [CONTAINS

    ]END [MODULE []]

  • Supercomputing Center Supercomputing Center 116116

    : Global : Global

    Global Global Fortran 77Fortran 77 COMMONCOMMON USE USE

    MODULE globalsREAL :: a, b, cINTEGER :: i, j, k

    END MODULE globals

    USE globals ! allows all variables in the module ! to be accessed

    USE globals, ONLY: a, c ! allows only variables a and c ! to be accessed

    USE globals, r => a, s => b ! allows a and b to be accessed with ! local variables r and s

  • Supercomputing Center Supercomputing Center 117117

    : : (1/2)(1/2)

    CONTAINSCONTAINS ENDEND FUNCTION FUNCTION SUBROUTINESUBROUTINE

  • Supercomputing Center Supercomputing Center 118118

    : : (2/2)(2/2)

    PROGRAM point_sum

    !A program unit would contain:USE point_module

    TYPE (point) :: px, py, pz

    px = point(1., 2.)py = point(2., 5.)pz = addpoints(px, py)PRINT*, pz =, pz

    END

    MODULE point_moduleTYPE point

    REAL :: x, yEND TYPE point

    CONTAINSFUNCTION addpoints (p, q)

    TYPE (point), INTENT(IN) :: p, qTYPE (point) :: addpointsaddpoints%x = p%x + q%xaddpoints%y = p%y + q%y

    END FUNCTION addpoints

    END MODULE point_module

  • Supercomputing Center Supercomputing Center 119119

    : Generic : Generic (1/6)(1/6)

    Generic Generic

    INTERFACE generic_nameSpecific_interface_bodySpecific_interface_body

    END INTERFACE

  • Supercomputing Center Supercomputing Center 120120

    : Generic : Generic (2/6)(2/6)

    SWAPSWAP

    SUBROUTINE swapreal(a, b)REAL, INTENT(inout) :: a, bREAL :: temptemp = aa = bb = temp

    END SUBROUTINE swapreal

    SUBROUTINE swapint(a, b)INTEGER, INTENT(inout) :: a, bINTEGER :: temptemp = aa = bb = temp

    END SUBROUTINE swapint

  • Supercomputing Center Supercomputing Center 121121

    : Generic : Generic (3/6)(3/6)

    Generic SWAP Generic SWAP

    INTERFACE swap ! generic name

    SUBROUTINE swapreal(a, b)REAL, INTENT(inout) :: a, b

    END SUBROUTINE swapreal

    SUBROUTINE swapint(a, b)INTEGER, INTENT(inout) :: a, b

    END SUBROUTINE swapint

    END INTERFACE

  • Supercomputing Center Supercomputing Center 122122

    : Generic : Generic (4/6)(4/6)

    Generic Generic SWAP SWAP INTEGER :: m, nREAL :: x, yCALL swap(m, n)CALL swap(x, y)

  • Supercomputing Center Supercomputing Center 123123

    : Generic : Generic (5/6)(5/6)

    MODULE genswapTYPE point

    REAL :: x, yEND TYPE point

    INTERFACE swap ! generic interfaceMODULE PROCEDURE swapreal, swapint, swaplog, swappoint

    END INTERFACE

    CONTAINSSUBROUTINE swappoint (a, b)

    TYPE (point), INTENT(INOUT) :: a, bTYPE (point) :: temptemp = a; a=b; b=temp

    END SUBROUTINE swappoint... ! swapint, swapreal, swaplog procedures are defined here

    END MODULE genswap

  • Supercomputing Center Supercomputing Center 124124

    : Generic : Generic (6/6)(6/6)

    PROGRAM main

    USE genswap

    INTEGER :: m, nREAL :: x, yTYPE(point) :: a, bCALL swap(m, n)CALL swap(x, y)CALL swap(a, b)END PROGRAM main

  • Supercomputing Center Supercomputing Center 125125

    : Public, Private : Public, Private

    PRIVATE PRIVATE PRIVATE attribute PRIVATE attribute

    PRIVATE :: pos, store, stack_size ! hiddenPUBLIC :: pop, push ! not hidden

    MODULE blimpIMPLICIT NONEPRIVATE ! set default visibility

    END MODULE blimp

    PUBLIC ! set default visibilityINTEGER, PRIVATE :: store(stack_size), posINTEGER, PRIVATE, PARAMETER :: stack_size = 100

  • Supercomputing Center Supercomputing Center 126126

    : :

    , ,

    Program Source File

    Fortran 90Compiler

    Fortran 90Compiler

    Linker

    Module Source File

    Program Object File

    Module Object File

    Program Executable File

    Program Source File

    Fortran 90Compiler

    Fortran 90Compiler

    Linker

    Module Source File

    Program Object File

    Module Object File

    Program Executable File

  • Supercomputing Center Supercomputing Center 127127

    Fortran 90Fortran 90

    AttributingAttributing

    DisassociationDisassociation

    Valued Valued

    Linked ListLinked List

  • Supercomputing Center Supercomputing Center 128128

    Fortran 90Fortran 90

    Fortran 90Fortran 90 AliasingAliasing

    Fortran 90 Fortran 90 ((: target) : target)

    FortranFortran Linked list, tree Linked list, tree

  • Supercomputing Center Supercomputing Center 129129

    33undefined : undefined : associated : associated : disassociated : disassociated :

    Logical Logical ASSOCIATED ASSOCIATED

  • Supercomputing Center Supercomputing Center 130130

    (1/6)(1/6)

    POINTER AttributePOINTER Attribute , kind, , kind, deferreddeferred--shapeshape

    REAL, POINTER :: ptor

    REAL, DIMENSION(:, :), POINTER :: ptoa

  • Supercomputing Center Supercomputing Center 131131

    (2/6)(2/6)

    TARGET AttributeTARGET Attribute

    x, yx, y ptorptor associated associated a, b, ca, b, c ptoaptoa associated associated

    REAL, TARGET :: x, yREAL, DIMENSION(5,3), TARGET :: a, bREAL, DIMENSION(3,5), TARGET :: c

  • Supercomputing Center Supercomputing Center 132132

    (3/6)(3/6)

    : : =>=>aliasing aliasing , , , ,

    (normal) (normal) : : ==

    , , TARGET attributeTARGET attribute

  • Supercomputing Center Supercomputing Center 133133

    (4/6)(4/6)

    ptorptor yy ptoaptoa bb aliasing aliasing yy bb ptorptor ptoaptoa ,,

    ptorptor ptor2ptor2 yy aliasing aliasing

    ptorptor => y=> yptoaptoa => b=> b

    ptor2 => ptor2 => ptorptorptor2 => yptor2 => y

  • Supercomputing Center Supercomputing Center 134134

    (5/6)(5/6)

    x = 3.14159x = 3.14159xx

    ptor => yptor => yptorptor yy aliasingaliasing

    ptor = xptor = xptorptor (y)(y) xx ptorptor yy

    x = 3.14159x = 3.14159ptorptor => y=> yptorptor = x ! y = x= x ! y = x

  • Supercomputing Center Supercomputing Center 135135

    (6/6)(6/6)

    REAL, POINTER :: p1, p2REAL, TARGET :: t1 = 3.4, t2 = 4.5p1 => t1p2 => t2PRINT *, t1, p1 ! 3.4 printed out twicePRINT *, t2, p2 ! 4.5 printed out twicep2 => p1 ! Valid: p2 points to the target of p1PRINT *, t1, p1, p2 ! 3.4 printed out three times

    REAL, POINTER :: p1, p2REAL, TARGET :: t1 = 3.4, t2 = 4.5p1 => t1p2 => t2PRINT *, t1, p1 ! 3.4 printed out twicePRINT *, t2, p2 ! 4.5 printed out twicep2 = p1 ! Valid: equivalent to t2=t1PRINT *, t1,t2, p1, p2 ! 3.4 printed out four times

  • Supercomputing Center Supercomputing Center 136136

    (1/5)(1/5)

    , ,

    triplet subscripttriplet subscript

  • Supercomputing Center Supercomputing Center 137137

    (2/5)(2/5)

    SIZE(Ptoa) = 4SIZE(Ptoa) = 4SHAPE(Ptoa) = (/2,2/)SHAPE(Ptoa) = (/2,2/)

    REAL, DIMENSION(:, :), POINTER :: Ptoa

    Ptoa => A(3::2, ::2)

  • Supercomputing Center Supercomputing Center 138138

    (3/5)(3/5)

    REAL, DIMENSION(:, :), POINTER :: PtoaPtoa => A(1:1, 2:2) (O)

    REAL, DIMENSION(:, :), POINTER :: Ptoa

    Ptoa => A(1:1, 2)Ptoa => A(1, 2)Ptoa => A(1, 2:2)

    (X)

    REAL, DIMENSION(:, :), POINTER :: Ptoa

    v = (/2,3,1,2/)Ptoa => A(v,v)

    (X)

  • Supercomputing Center Supercomputing Center 139139

    (4/5)(4/5)

    REAL, DIMENSION (:), POINTER :: pv1REAL, DIMENSION (-3:5), TARGET :: tv1

    pv1 => tv1 ! pv1 aliased to tv1

    pv1 => tv1(:) ! aliased with section subscript

    pv1 => tv1(1:5:2) ! aliased with section triplet

  • Supercomputing Center Supercomputing Center 140140

    (5/5)(5/5)

    REAL, DIMENSION (:), POINTER :: pv1REAL, DIMENSION (:, :), POINTER :: pv2REAL, DIMENSION (4:8), TARGET :: tv

    pv1 => tv(4, :) ! pv1 aliased to 4th row of tv

    pv2 => tv(2:4, 4:8)

  • Supercomputing Center Supercomputing Center 141141

    (1/2)(1/2)

    (ptor)(ptor) 22 (ptoa)(ptoa) , , ptorptor ptoaptoa

    ALLOCATE(ptor, STAT=ierr)ALLOCATE(ptoa(n*n, 2*k-1), STAT=ierr)

  • Supercomputing Center Supercomputing Center 142142

    (2/2)(2/2)

    Associated Associated . .

    ALLOCATE (ptor, STAT=ierr)ptor = 2.

    REAL, TARGET :: eptor => ePtor = 2.

  • Supercomputing Center Supercomputing Center 143143

    Attributing (1/2)Attributing (1/2)

    TARGET attributeTARGET attribute associated associated ..

    ptor == A(5,1) == Ptoa(2,1)ptor == A(5,1) == Ptoa(2,1)

    Ptoa => A(3::2, 1::2)

    Ptor => Ptoa(2,1)

  • Supercomputing Center Supercomputing Center 144144

    Attributing (2/2)Attributing (2/2)

    Dangling Dangling dangling dangling

    REAL, POINTER :: p1, p2ALLOCATE (p1)

    p1 = 3.4p2 => p1DEALLOCATE (p1) ! Dynamic variable p1 and p2 both pointed to is gone.

    ! Reference to p2 now gives unpredictable results

  • Supercomputing Center Supercomputing Center 145145

    Disassociation (1/2) Disassociation (1/2)

    Association Association Nullification: Nullification: NULLIFY(ptor)NULLIFY(ptor)

    AssociationAssociation deallocate deallocate

    Deallocation: Deallocation: DEALLOCATE(ptoa, STAT=ierr)DEALLOCATE(ptoa, STAT=ierr)ALLOCATEALLOCATE associationassociation

  • Supercomputing Center Supercomputing Center 146146

    Disassociation (2/2)Disassociation (2/2)

    REAL, POINTER :: p ! p undefinedREAL, TARGET :: tPRINT *, ASSOCIATED (p) ! not validNULLIFY (p) ! point at "nothing"PRINT *, ASSOCIATED (p) ! .FALSE.p => tPRINT *, ASSOCIATED (p) ! .TRUE.

    REAL, DIMENSION(:), POINTER :: p

    ALLOCATE(p(1000))

    NULLIFY(p) ! nullify p without first deallocating it!

    ! big block of memory not released and unusable

  • Supercomputing Center Supercomputing Center 147147

    Valued Valued (1/3)(1/3)

    . .

    ..

  • Supercomputing Center Supercomputing Center 148148

    Valued Valued (2/3)(2/3)

    PROGRAM mainIMPLICIT NONEREAL :: xINTEGER, TARGET :: a, bINTEGER, POINTER :: largest

    CALL RANDOM_NUMBER(x)a = 10000.0*xCALL RANDOM_NUMBER(x)b = 10000.0*xlargest => ptr()

    CONTAINSFUNCTION ptr()

    INTEGER, POINTER :: ptrIF (a .GT. b) THEN

    ptr => aELSE

    ptr => bEND IF

    END FUNCTION ptrEND PROGRAM main

  • Supercomputing Center Supercomputing Center 149149

    Valued Valued (3/3)(3/3)

    INTEGER, DIMENSION(100) :: x

    INTEGER, DIMENSION(:), POINTER :: p

    ...

    p => gtzero(x)

    ...

    CONTAINS ! function to get all values .gt. 0 from a

    FUNCTION gtzero(a)

    INTEGER, DIMENSION(:), POINTER :: gtzero

    INTEGER, DIMENSION(:) :: a

    INTEGER :: n

    ... ! find the number of values .gt. 0 (put in n)

    ALLOCATE (gtzero(n))

    ... ! put the found values into gtzero

    END FUNCTION gtzero

    ...

    END

  • Supercomputing Center Supercomputing Center 150150

    Linked List (1/3)Linked List (1/3)

    data fielddata field fieldfield

    , , listlist

  • Supercomputing Center Supercomputing Center 151151

    Linked List (2/3)Linked List (2/3)

    TYPE nodeINTEGER :: value ! data fieldTYPE (node), POINTER :: next ! pointer field

    END TYPE node

    INTEGER :: numTYPE (node), POINTER :: list, currentNULLIFY(list) ! initially nullify list (mark its

    end)DO

    READ *, num ! read num from keyboardIF (num == 0) EXIT ! until 0 is enteredALLOCATE(current) ! create new nodecurrent%value = numcurrent%next => list ! point to previous onelist => current ! update head of list

    END DO...

  • Supercomputing Center Supercomputing Center 152152

    Linked List (3/3)Linked List (3/3)

    NULLIFY(list)NULLIFY(list)

    num num

    numnum

  • Supercomputing Center Supercomputing Center 153153

    OverloadingOverloading OverloadingOverloading

    OverloadingOverloading

  • Supercomputing Center Supercomputing Center 154154

    Overloading (1/2)Overloading (1/2)

    Generic Generic INTERFACE OPERATORINTERFACE OPERATOR

    Generic Generic overload set overload set

    INTERFACE OPERATOR (intrinsic_operator)interface_body

    END INTERFACE

  • Supercomputing Center Supercomputing Center 155155

    Overloading (2/2)Overloading (2/2)

    MODULE overINTERFACE OPERATOR (+)

    MODULE PROCEDURE concatEND INTERFACECONTAINS

    FUNCTION concat(cha, chb)CHARACTER (LEN=*), INTENT(IN) :: cha, chbCHARACTER (LEN=LEN_TRIM(cha) + LEN_TRIM(chb)) :: concatconcat = TRIM(cha) // TRIM(chb)

    END FUNCTION concatEND MODULE over

    PROGRAM testaddUSE overCHARACTER (LEN=23) :: nameCHARACTER (LEN=13) :: wordname='Balder'word='convoluted'PRINT *, name // wordPRINT *, name + word

    END PROGRAM testadd

  • Supercomputing Center Supercomputing Center 156156

    (1/2)(1/2)

    : : .... overloadingoverloading INTENT(in) INTENT(in)

  • Supercomputing Center Supercomputing Center 157157

    (2/2)(2/2)

    MODULE distance_moduleTYPE point

    REAL :: x, yEND TYPE pointINTERFACE OPERATOR (.dist.)

    MODULE PROCEDURE calcdistEND INTERFACECONTAINS

    REAL FUNCTION calcdist (px, py)TYPE (point), INTENT(IN) :: px, pycalcdist = SQRT ((px%x-py%x)**2 + (px%y-py%y)**2 )

    END FUNCTION calcdistEND MODULE distance_module

    PROGRAM mainUSE distance_moduleTYPE (point) :: p1, p2REAL :: distance...distance = p1 .dist. p2...

    END PROGRAM main

  • Supercomputing Center Supercomputing Center 158158

    Overloading (1/3)Overloading (1/3)

    OverloadingOverloading

    INTENT(out), INTENT(out), LHSLHS INTENT(in), INTENT(in), RHSRHS

  • Supercomputing Center Supercomputing Center 159159

    Overloading (2/3)Overloading (2/3)

    MODULE assignoverload_moduleTYPE point

    REAL :: x,yEND TYPE pointINTERFACE ASSIGNMENT (=)

    MODULE PROCEDURE assign_pointEND INTERFACECONTAINS

    SUBROUTINE assign_point(ax, px)REAL, INTENT(OUT)::axTYPE (point), INTENT(IN)::pxax = MAX(px%x, px%y)

    END SUBROUTINE assign_pointEND MODULE assignoverload_module

  • Supercomputing Center Supercomputing Center 160160

    Overloading (3/3)Overloading (3/3)

    PROGRAM main

    USE assignoverload_module

    REAL :: ax

    TYPE (point) :: px

    ...

    ax = px ! type point assigned to type real

    ! not valid until defined

    ...

    END PROGRAM main

  • Supercomputing Center Supercomputing Center 161161

    (Recursive) (Recursive)

  • Supercomputing Center Supercomputing Center 162162

    (1/3)(1/3)

    Fortran 90Fortran 90 RECURSIVERECURSIVE

    RESULTRESULT

    INTEGER RECURSIVE FUNCTION fact(n) RESULT(n_fact)

    RECURSIVE INTEGER FUNCTION fact(n) RESULT(n_fact)

    RECURSIVE FUNCTION fact(n) RESULT(n_fact)INTEGER n_fact

  • Supercomputing Center Supercomputing Center 163163

    (2/3)(2/3)

    PROGRAM main

    IMPLICIT NONEPRINT *, fact(5)

    CONTAINS RECURSIVE FUNCTION fact(n) RESULT(n_fact)

    INTEGER, INTENT(in) :: nINTEGER :: n_fact ! also defines type of factIF (n == 1) THEN

    n_fact = 1ELSE

    n_fact = n * fact(n - 1)END IF

    END FUNCTION fact

    END PROGRAM main

  • Supercomputing Center Supercomputing Center 164164

    (3/3)(3/3)

    PROGRAM main

    IMPLICIT NONEINTEGER :: resultCALL factorial(5, result)PRINT *, result

    CONTAINS RECURSIVE SUBROUTINE factorial(n, n_fact)

    INTEGER, INTENT(in) :: nINTEGER, INTENT(inout) :: n_factIF (n == 1) THEN

    n_fact = 1ELSE

    CALL factorial(n-1, n_fact)n_fact = n_fact * n

    END IFEND SUBROUTINE factorial

    END PROGRAM main

  • Supercomputing Center Supercomputing Center 165165

    Keyword/Optional Keyword/Optional Keyword Keyword

    Optional Optional

  • Supercomputing Center Supercomputing Center 166166

    Keyword Keyword (1/2)(1/2)

    Keyword Keyword

    Keyword Keyword (F77, F90)(F77, F90)KeywordKeyword (F90)(F90)

    Keyword = Keyword =

    READ(10,67,789) x, y, zREAD(UNIT=10,FMT=67,END=789) x, y, z

  • Supercomputing Center Supercomputing Center 167167

    Keyword Keyword (2/2)(2/2)

    CALL axis(0.0, 0.0, 100.0, 0.1, 1.0, 10)CALL axis(0.0, 0.0, 100.0, 0.1, 1.0, 10)

    Keyword Keyword CALL axis(0.0, 0.0, max=1.0, min=0.1, l=100.0, i=10)CALL axis(0.0, 0.0, max=1.0, min=0.1, l=100.0, i=10)

    SUBROUTINE axis(y0, y0, l, min, max, i)REAL, INTENT(in) :: x0, y0, l, min, maxINTEGER, INTENT(in) :: I

    END SUBROUTINE axis

  • Supercomputing Center Supercomputing Center 168168

    Optional Optional (1/2)(1/2)

    OPTIONALOPTIONAL Keyword Keyword

    PRESENT(argument_name)PRESENT(argument_name)optional optional

  • Supercomputing Center Supercomputing Center 169169

    Optional Optional (2/2)(2/2)

    SUBROUTINE SEE(a, b)IMPLICIT NONEREAL, INTENT(in), OPTIONAL :: aINTEGER, INTENT(in), OPTIONAL :: bREAL :: ay; INTEGER :: beeay = 1.0; bee = 1

    IF(PRESENT(a)) ay = aIF(PRESNET(b)) bee = b...

    CALL SEE()CALL SEE(1.0, 1); CALL SEE(b=1, a=1.0) ! sameCALL SEE(1.0); CALL SEE(a=1.0) ! sameCALL (b=1)

  • Supercomputing Center Supercomputing Center 170170

    1.1. The POWER4 Processor Introduction and Tuning Guide The POWER4 Processor Introduction and Tuning Guide ((http://http://www.ibm.com/redbooskwww.ibm.com/redboosk))

    2.2. Fortran90 and Computational Science (Fortran90 and Computational Science (http://www.comphys.unihttp://www.comphys.uni--duisburg.deduisburg.de/ Fortran90/pl/pl.html/ Fortran90/pl/pl.html))

    3.3. The Liverpool Fortran90 Courses The Liverpool Fortran90 Courses ((http://www.liv.ac.uk/HPC/F90page.htmlhttp://www.liv.ac.uk/HPC/F90page.html))

    4.4. Language Reference, XL Fortran for AIX, Version 8 Release 1 Language Reference, XL Fortran for AIX, Version 8 Release 1 5.5. The Fortran90 Essentials: Discussion The Fortran90 Essentials: Discussion

    ((http://http://www.tc.cornell.edu/Services/Eduwww.tc.cornell.edu/Services/Edu/Topics/Fortran90/more.asp/Topics/Fortran90/more.asp))

    6.6. Fortran90 for the Fortran77 Programmer Fortran90 for the Fortran77 Programmer ((http://http://www.whoi.eduwww.whoi.edu/CIS/training/ /CIS/training/ classes/f77to90/f77to90.htmlclasses/f77to90/f77to90.html))

    7.7. Larry R. Larry R. NyhoffNyhoff, Sanford C. , Sanford C. LeestmaLeestma. . Fortran90 for Engineers Fortran90 for Engineers and Scientistsand Scientists. Prentice Hall. 1997. . Prentice Hall. 1997.

    8.8. Walter S. Brainerd, Walter S. Brainerd, CharlsCharls H. Goldberg, Jeanne C. Adams. H. Goldberg, Jeanne C. Adams. Programmers Guide to Fortran90 3rd editionProgrammers Guide to Fortran90 3rd edition. Springer. 1995.. Springer. 1995.

  • Supercomputing Center Supercomputing Center 171171

    HelpdeskHelpdeskwww.hpcnet.ne.krwww.hpcnet.ne.kr

    webedu.hpcnet.ne.krwebedu.hpcnet.ne.kr

    EE--MailMailconsult@consult@supercomputing.re.krsupercomputing.re.kr

    I Fortran 90 FortranFortran 77 (1/2)Fortran 77 (2/2)Fortran 90 (1/12) (2/12) (3/12) (4/12) (5/12) (6/12) (7/12) (8/12) (9/12) (10/12) (11/12) (12/12)Fortran 90 (1/4)Fortran 90 (2/4)Fortran 90 (3/4)Fortran 90 (4/4) Fortran 77, C/C++ (1/5)Fortran 77, C/C++ (2/5)Fortran 77, C/C++ (3/5)Fortran 77, C/C++ (4/5)Fortran 77, C/C++ (5/5) II Numerical RobustnessNumerical RobustnessNumeric Kind Parameterization (1/2)Numeric Kind Parameterization (2/2) Numeric Polymorphism (1/5)Numeric Polymorphism (2/5)Numeric Polymorphism (3/5)Numeric Polymorphism (4/5)Numeric Polymorphism (5/5)Numeric Approximation Model (1/3)Numeric Approximation Model (2/3)Numeric Approximation Model (3/3)Environmental Inquiry (1/2)Environmental Inquiry (2/2)Numeric Manipulation (1/2)Numeric Manipulation (2/2) III Data Parallelism (1/2) (2/2) (1/2) (2/2) DO RESHAPE (1/2)RESHAPE (2/2) WHERE (1/3) WHERE (2/3) WHERE (3/3)Assumed-Shape Dummy ArgumentsExplicit-ShapeAssumed-SizeAssumed-Shape (1/3)Assumed-Shape (2/3)Assumed-Shape (3/3) Simple SubscriptTriplet Subscript (1/3)Triplet Subscript (2/3)Triplet Subscript (3/3)Vector Subscript (1/4)Vector Subscript (2/4)Vector Subscript (3/4)Vector Subscript (4/4) Autonomic Array (1/2)Autonomic Array (2/2)Allocatable Array (1/2)Allocatable Array (2/2)Pointer ArrayTransformational (1/4)Transformational (2/4)Transformational (3/4)Transformational (4/4)Elemental array-valued IV (1/5) (2/5) (3/5) (4/5) (5/5) (1/2) (2/2)INTENT Attribute (1/3)INTENT Attribute (2/3)INTENT Attribute (3/3) (1/2) (2/2)Supertypes (1/2) (2/2) (1/2) (2/2): Global : (1/2): (2/2): Generic (1/6): Generic (2/6): Generic (3/6): Generic (4/6): Generic (5/6): Generic (6/6): Public, Private : Fortran 90 (1/6) (2/6) (3/6) (4/6) (5/6) (6/6) (1/5) (2/5) (3/5) (4/5) (5/5) (1/2) (2/2) Attributing (1/2) Attributing (2/2) Disassociation (1/2) Disassociation (2/2) Valued (1/3) Valued (2/3) Valued (3/3)Linked List (1/3)Linked List (2/3)Linked List (3/3) Overloading (1/2) Overloading (2/2) (1/2) (2/2) Overloading (1/3) Overloading (2/3) Overloading (3/3) (1/3) (2/3) (3/3)Keyword (1/2)Keyword (2/2)Optional (1/2)Optional (2/2)

Recommended

View more >