FORTRAN 90+ Yetmen Wang Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT.

Download FORTRAN 90+ Yetmen Wang Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT.

Post on 19-Jan-2016

214 views

Category:

Documents

1 download

TRANSCRIPT

  • FORTRAN 90+Yetmen Wang

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    FORmula TRANslationDeveloped by the IBM team led by John BackusThe first high-level programming languageMainly intended for mathematical computationsAreas of ApplicationNumerical AnalysisSystem SimulationScientific ComputationsEngineering ProceduresIntroduction

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Fortran HistoryPublished by IBM in 1957MS PowerStation 4.0Sold to Digital with DEC, it developed into Digital Visual Fotran 5.xDigital was later merged with Compaq; CVF 6.x emergedCVF development team was purchased by IntelHP merged with Compaq, introducing HP CVF 6.6aIntel Fortran, combining CVF, developed Intel Visual Fortran 8.x

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Fortran Versions1962 FORTRAN IV1966 FORTRAN 661978 FORTRAN 771992 FORTRAN 90ArraySemi-OOPResembles MATLAB1997 FORTRAN 95HPF extension more OOP2003 FORTRAN 2000Fully OOP

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    StrengthsArray language and object-oriented programmingHigher computational speed compared to C/C++ and MATLABMaintains plenty of legacy codesEasy-to-learn compared to C/C++The majority of individuals in the numerical computing field still use Fortran to develop program(s)

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    WeaknessesFile I/O is difficult to modify and comprehendLow reusability and high cost of code maintenanceLack of numerical and graphical librariesDifficult to convert the codes into applicationsPlatform portingInterfacing to other language

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    A good descriptionof Fortran programing.Program Structure

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Significant FeaturesNew Source FormObject-Oriented ProgrammingArray ProgrammingDynamic Memory AllocationPointer

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    New Source FormFree FormatIMPLICIT NONEStatements

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    New Source Form Free Formatnames of variables may consist of up to 31 characters132 characters per lineup to 39 continuation linesblanks are significant& as line continuation character; as statement separator for multiple statements per line! as comment symbol

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    New Source Form - IMPLICIT NONEThe first line after any USE statementsUsed to inhibit the old Fortran feature that treats, by default, all variable beginning with the letters I, j, k, l, m, and n as integers and others as real argumentsIMPLICIT NONE should always be used to prevent potential confusion in variable typesUpper and lowercase letters are equivalent

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    New Source Form - StatementsINCLUDE can be used to include source text from external filesEND DO statements are used to complete DO loopsRelational Operator Alternatives.LT.

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Object-Oriented ProgrammingFunctionalityTYPEMODULEAttributesINTERFACEOverload

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Object-Oriented Programming - FunctionalityData AbstractionData HidingEncapsulationInheritancePolymorphismReusability

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Object-Oriented Programming TYPEuser-defined TYPEA new type can be defined in a derived-type statement, which can later be used to describe an objectExample ICreate a type COORDS_3D with three REAL components X, Y, and Z.TYPE :: COORDS_3DREAL :: X, Y, ZEND TYPE COORDS_3DCreate a variable of type COORDS_3D with values 0.0, 1.0, and 5.0.TYPE(COORDS_3D) :: PtPt%X = 0.0Pt%Y = 1.0Pt%Z = 5.0

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Object-Oriented Programming TYPEuser-defined TYPEExample IICreate a type NONZERO in which nonzero matrix elements are described.TYPE :: NONZEROREAL :: VALUE INTEGER :: ROW, COLUMNEND TYPE NONZEROCreate a sparse matrix A with 100 nonzero elements.TYPE(NONZERO) :: A(100)Obtain the value of A(10).X = A(10)%Value

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Object-Oriented Programming MODULEMODULE / MODULE PROCEDUREA collection of data, type definitions, and procedure definitions which can be exploited by any other program unit attaching it (via the USE statement).ExampleMODULE point_module TYPE point REAL :: x, yEND TYPE point CONTAINS FUNCTION addpoints(p, q) TYPE (point), INTENT(IN) :: p, q TYPE (point) :: addpoints addpoints%x = p%x + q%x addpoints%y = p%y + q%yEND FUNCTION addpoints END MODULE point_module Main Program...USE point_module TYPE (point) :: px, py, pz . . . pz = addpoints(px,py) Accesses the module.

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Object-Oriented Programming ATTRIBUTESPUBLIC and PRIVATE attributesPRIVATE variables/subroutines/functions defined can only be used in the specified modulePUBLIC variables/subroutines/functions defined can be used publiclyExampleMODULE bank PRIVATE money PUBLIC SaveMoney integer :: money = 1000000 CONTAINS SUBROUTINE SaveMoney(num) integer :: num money = money+num returnEND SUBROUTINEEND MODULE

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Object-Oriented Programming INTERFACEINTERFACEA way to specify information for an external procedure Name of the procedure Types of passed and returned parameters Whether an argument may be changedINTERFAXE detects incorrect calls at compile time ExampleINTERFACEREAL FUNCTION DISTANCE( A, B) REAL, INTENT(IN) :: A, BEND FUNCTION DISTANCEEND INTERFACE

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Object-Oriented Programming OverloadOverload OperatorsOperators can be overloaded to clarify unambiguous definitionsIntrinsic operators can be overloaded to apply to all types in a programOverloading is encapsulated in a module generic operator symbol in an INTERFACE OPERATOR statement overload set in a generic interfaceExampleINTERFACE OPERATOR(-) FUNCTION DIFF(A,B) TYPE(POINT) :: DIFF, A, B END FUNCTION END INTERFACE

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Array ProgrammingWhole ArrayArray SectionIntrinsic Functions

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Array Programming Whole ArrayAssignmentAll arrays must conformThe operation is applied to each element of the arrayScalars broadcastDeclarationsREAL, DIMENSION(5, 5) :: A, BORREAL :: A(5,5), B(5,5)

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Array Programming Whole ArrayOperation

    ExampleAssume A and B to be two 2D arrays of the same shape. Multiply them and assign the result to array C. FORTRAN 77REAL A(5, 5), B(5, 5), C(5, 5)...i LOOPj LOOP C(j, i) = A(j, i) * B(j, i)END i LOOP END j LOOPFORTRAN 90+REAL, DIMENSION (5, 5) :: A, B, C...C = A * B =x

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Array Programming Array SectionDeclarationREAL, DIMENSION(10, 10) :: ASubscript Notation( [row lower bound] : [row upper bound] : [row stride], [column lower bound] : [column upper bound] : [column stride] )

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Array Programming Array SectionExampleREAL :: A(10, 10)A(2:6, 4:8)A(:, 1:3)

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Array Programming Array SectionExampleREAL :: A(10, 10)A(4:10, 5)A(1:10:2, 1:10:2)INTRODUCTION

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Array Programming Intrinsic FunctionsFunctionsarray manipulationsCSHIFT and EOSHIFT for shifts along array axisTRANSPOSE for the transpose of a matrixreduction functionsSUM , PRODUCT , MAXVAL , MINVAL , COUNT , ALL , and ANYinquiry functions SHAPE , SIZE, ALLOCATED, LBOUND, and UBOUNDarray constructor functions MERGE, SPREAD, RESHAPE, PACK and UNPACK

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Array Programming Intrinsic FunctionsExample - CSHIFTFORTRAN 77REAL :: A(0:99), B(0:99)DO i = 0, 99B(i) = ( A( mod(i+99, 100) ) + A( mod(i+1, 100) ) ) / 2ENDDOFORTRAN 90+REAL :: A(100), B(100)B = ( CSHIFT(A, +1) + CSHIFT(A, -1) ) / 2

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    PointerIntroductionAssociation StatusExample

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Pointer - IntroductionA pointer has the POINTER attribute and may point to another data object of the same type, which has the TARGET attribute or an area of dynamically allocated memory.

    UsesAlternative to allocatable arraysA tool to create and manipulate dynamic data structuresDeclarationsREAL, POINTER :: Ptr(:, :)REAL, TARGET :: TA(:, :)

    TARGET

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Pointer Association StatusStatusUndefined initially specified in a type declaration statementAssociated points to a targetNull nullified by a NULLIFY or a DEALLOCATE statement

    NULLIFY(Ptr) DEALLOCATE(Ptr, STAT = ierr)PointerPointerPointer?TargetNULL

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Pointer - ExampleExampleREAL, TARGET :: A REAL, POINTER :: P, Q A = 3.14P => A Q => P A = 2.718 WRITE(*,*) Q

    Q outputs 2.718

    Q => P and P => ATherefore, Q => A, whose value has changed from 3.14 to 2.718

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Dynamic StorageAllocatable ArrayPointer

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Dynamic Storage Allocatable ArrayAcquire and return a storage area in HEAP MEMORY for an array with attributesExampleREAL, DIMENSION(:), ALLOCATABLE :: A ALLOCATE( A(5:5) )A(j) = q! assignment of the arrayCALL sub(A)! Use of the array in a subroutineDeallocation occurs automatically reaching RETURN or END in the programTo prevent memory leak, allocatable arrays should be explicitly deallocatedDEALLOCATE (A)

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Dynamic Storage - PointerUse a pointerCan be passed to a procedure in an unallocated stateAn explicit INTERFACE is required when passing a pointer to a procedure

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    Dynamic Storage - PointerUse a pointerExampleSubroutine Procedure:SUBROUTINE SUB(B) REAL, DIMENSION (:,:), POINTER :: BINTEGER M, N! Assign M and NALLOCATE (B(M,N))! Allocate B as a matrixEND SUBROUTINE SUBMain Program:INTERFACE SUBROUTINE SUB(B) REAL, DIMENSION (:,:), POINTER :: B END SUBROUTINE SUB END INTERFACE REAL, DIMENSION (:,:), POINTER :: A CALL SUB(A) ! matrix A is called and allocated in the subroutine

    Fortran 90/95/2000 INTRODUCTIONFORTRAN VERSIONSPROGRAM STRUCTURENEW SOURCE FORMOO PROGRAMMINGARRAY PROGRAMMINGSIGNIFICANT FEATURESFORTRAN HISTORYSTRENGTHSWEAKENESSESPOINTERDYNAMIC STORAGE

    THANK YOU!

Recommended

View more >