1 / 37

FORTRAN 90+

FORTRAN 90+. Yetmen Wang. Introduction. FORmula TRANslation Developed by the IBM team led by John Backus The first high-level programming language Mainly intended for mathematical computations Areas of Application Numerical Analysis System Simulation Scientific Computations

mandy
Download Presentation

FORTRAN 90+

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. FORTRAN 90+ Yetmen Wang

  2. Introduction • FORmula TRANslation • Developed by the IBM team led by John Backus • The first high-level programming language • Mainly intended for mathematical computations • Areas of Application • Numerical Analysis • System Simulation • Scientific Computations • Engineering Procedures

  3. Fortran History • Published by IBM in 1957 • MS PowerStation 4.0 • Sold to Digital with DEC, it developed into Digital Visual Fotran 5.x • Digital was later merged with Compaq; CVF 6.x emerged • CVF development team was purchased by Intel • HP merged with Compaq, introducing HP CVF 6.6a • Intel Fortran, combining CVF, developed Intel Visual Fortran 8.x

  4. Fortran Versions • 1962  FORTRAN IV • 1966  FORTRAN 66 • 1978  FORTRAN 77 • 1992  FORTRAN 90 • Array • Semi-OOP • Resembles MATLAB • 1997  FORTRAN 95 • HPF extension • more OOP • 2003  FORTRAN 2000 • Fully OOP

  5. Strengths • Array language and object-oriented programming • Higher computational speed compared to C/C++ and MATLAB • Maintains plenty of legacy codes • Easy-to-learn compared to C/C++ • The majority of individuals in the numerical computing field still use Fortran to develop program(s)

  6. Weaknesses • File I/O is difficult to modify and comprehend • Low reusability and high cost of code maintenance • Lack of numerical and graphical libraries • Difficult to convert the codes into applications • Platform porting • Interfacing to other language

  7. Program Structure A good description of Fortran programing.

  8. Significant Features • New Source Form • Object-Oriented Programming • Array Programming • Dynamic Memory Allocation • Pointer

  9. New Source Form • Free Format • IMPLICIT NONE • Statements

  10. New Source Form – Free Format • names of variables may consist of up to 31 characters • 132 characters per line • up to 39 continuation lines • blanks are significant • & as line continuation character • ; as statement separator for multiple statements per line • ! as comment symbol

  11. New Source Form - IMPLICIT NONE • The first line after any USE statements • Used 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 arguments • IMPLICIT NONE should always be used to prevent potential confusion in variable types • Upper and lowercase letters are equivalent

  12. New Source Form - Statements • INCLUDE can be used to include source text from external files • END DO statements are used to complete DO loops • Relational Operator Alternatives • .LT.  < • .LE.  <= • .EQ.  == • .NE.  /= • .GE.  > • .GT.  =>

  13. Object-Oriented Programming • Functionality • TYPE • MODULE • Attributes • INTERFACE • Overload

  14. Object-Oriented Programming - Functionality • Data Abstraction • Data Hiding • Encapsulation • Inheritance • Polymorphism • Reusability

  15. Object-Oriented Programming – TYPE • user-defined TYPE • A new type can be defined in a derived-type statement, which can later be used to describe an object • Example I Create a type COORDS_3D with three REAL components X, Y, and Z. TYPE :: COORDS_3D REAL :: X, Y, Z END TYPE COORDS_3D Create a variable of type COORDS_3D with values 0.0, 1.0, and 5.0. TYPE(COORDS_3D) :: Pt Pt%X = 0.0 Pt%Y = 1.0 Pt%Z = 5.0

  16. Object-Oriented Programming – TYPE • user-defined TYPE • Example II Create a type NONZERO in which nonzero matrix elements are described. TYPE :: NONZERO REAL :: VALUE INTEGER :: ROW, COLUMN END TYPE NONZERO Create a sparse matrix A with 100 nonzero elements. TYPE(NONZERO) :: A(100) Obtain the value of A(10). X = A(10)%Value

  17. Object-Oriented Programming – MODULE • MODULE / MODULE PROCEDURE • A collection of data, type definitions, and procedure definitions which can be exploited by any other program unit attaching it (via the USE statement). • Example MODULE point_module TYPE point REAL :: x, y END 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%y END FUNCTION addpointsEND MODULE point_module Main Program . . . USE point_moduleTYPE (point) :: px, py, pz...pz = addpoints(px,py) Accesses the module.

  18. Object-Oriented Programming – ATTRIBUTES • PUBLIC and PRIVATE attributes • PRIVATE – variables/subroutines/functions defined can only be used in the specified module • PUBLIC – variables/subroutines/functions defined can be used publicly • Example MODULE bank PRIVATE money PUBLIC SaveMoney integer :: money = 1000000 CONTAINS     SUBROUTINE SaveMoney(num)         integer :: num         money = money+num         return END SUBROUTINE END MODULE

  19. Object-Oriented Programming – INTERFACE • INTERFACE • A way to specify information for an external procedure • Name of the procedure • Types of passed and returned parameters • Whether an argument may be changed • INTERFAXE detects incorrect calls at compile time • Example INTERFACE REAL FUNCTION DISTANCE( A, B) REAL, INTENT(IN) :: A, B END FUNCTION DISTANCE END INTERFACE

  20. Object-Oriented Programming – Overload • Overload Operators • Operators can be overloaded to clarify unambiguous definitions • Intrinsic operators can be overloaded to apply to all types in a program • Overloading is encapsulated in a module • generic operator symbol in an INTERFACE OPERATOR statement • overload set in a generic interface • Example INTERFACE OPERATOR(-) FUNCTION DIFF(A,B) TYPE(POINT) :: DIFF, A, B END FUNCTION END INTERFACE

  21. Array Programming • Whole Array • Array Section • Intrinsic Functions

  22. Array Programming – Whole Array • Assignment • All arrays must conform • The operation is applied to each element of the array • Scalars broadcast • Declarations REAL, DIMENSION(5, 5) :: A, B OR REAL :: A(5,5), B(5,5)

  23. Array Programming – Whole Array • Operation • Example Assume A and B to be two 2D arrays of the same shape. Multiply them and assign the result to array C. = x FORTRAN 77 REAL A(5, 5), B(5, 5), C(5, 5) ... i LOOP j LOOP C(j, i) = A(j, i) * B(j, i) END i LOOP END j LOOP FORTRAN 90+ REAL, DIMENSION (5, 5) :: A, B, C ... C = A * B

  24. Array Programming – Array Section • Declaration REAL, DIMENSION(10, 10) :: A • Subscript Notation • ( [row lower bound] : [row upper bound] : [row stride], [column lower bound] : [column upper bound] : [column stride] )

  25. Array Programming – Array Section • Example REAL :: A(10, 10) A(2:6, 4:8) A(:, 1:3)

  26. Array Programming – Array Section INTRODUCTION • Example REAL :: A(10, 10) A(4:10, 5) A(1:10:2, 1:10:2)

  27. Array Programming – Intrinsic Functions • Functions • array manipulations CSHIFT and EOSHIFT for shifts along array axis TRANSPOSE for the transpose of a matrix • reduction functions SUM , PRODUCT , MAXVAL , MINVAL , COUNT , ALL , and ANY • inquiry functions SHAPE , SIZE, ALLOCATED, LBOUND, and UBOUND • array constructor functions MERGE, SPREAD, RESHAPE, PACK and UNPACK

  28. Array Programming – Intrinsic Functions • Example - CSHIFT FORTRAN 77 REAL :: A(0:99), B(0:99) DO i = 0, 99 B(i) = ( A( mod(i+99, 100) ) + A( mod(i+1, 100) ) ) / 2 ENDDO FORTRAN 90+ REAL :: A(100), B(100) B = ( CSHIFT(A, +1) + CSHIFT(A, -1) ) / 2

  29. Pointer • Introduction • Association Status • Example

  30. Pointer - Introduction • A 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. • Uses • Alternative to allocatable arrays • A tool to create and manipulate dynamic data structures • Declarations REAL, POINTER :: Ptr(:, :) REAL, TARGET :: TA(:, :)

  31. Pointer – Association Status • Status • Undefined – initially specified in a type declaration statement • Associated – points to a target • Null – nullified by a NULLIFY or a DEALLOCATE statement • NULLIFY(Ptr) • DEALLOCATE(Ptr, STAT = ierr) ? Pointer Pointer Target Pointer NULL

  32. Pointer - Example • Example REAL, TARGET :: A REAL, POINTER :: P, Q A = 3.14 P => A Q => P A = 2.718 WRITE(*,*) Q Q outputs 2.718 Q => P and P => A Therefore, Q => A, whose value has changed from 3.14 to 2.718

  33. Dynamic Storage • Allocatable Array • Pointer

  34. Dynamic Storage – Allocatable Array • Acquire and return a storage area in HEAP MEMORY for an array with attributes • Example REAL, DIMENSION(:), ALLOCATABLE :: A ALLOCATE( A(5:5) ) A(j) = q ! assignment of the array CALL sub(A) ! Use of the array in a subroutine • Deallocation occurs automatically reaching RETURNor END in the program • To prevent memory leak, allocatable arrays should be explicitly deallocated DEALLOCATE (A)

  35. Dynamic Storage - Pointer • Use a pointer • Can be passed to a procedure in an unallocated state • An explicit INTERFACE is required when passing a pointer to a procedure

  36. Dynamic Storage - Pointer • Use a pointer • Example Subroutine Procedure: SUBROUTINE SUB(B) REAL, DIMENSION (:,:), POINTER :: B INTEGER M, N ! Assign M and N ALLOCATE (B(M,N)) ! Allocate B as a matrix END SUBROUTINE SUB Main 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

  37. THANK YOU!

More Related