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

233 views

Category:

Documents

16 download

Embed Size (px)

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 representation...

Recommended

View more >