FORTRAN 90+ Yetmen Wang ENTER Fortran 90/95/2000 INTRODUCTION Introduction FORTRAN HISTORY FORTRAN VERSIONS FORmula TRANslation STRENGTHS Developed by the IBM team led by John Backus WEAKENESSES The first high-level programming language PROGRAM STRUCTURE SIGNIFICANT FEATURES Mainly intended for mathematical computations NEW SOURCE FORM Areas of Application OO PROGRAMMING Numerical Analysis System Simulation Scientific Computations Engineering Procedures ARRAY PROGRAMMING POINTER DYNAMIC STORAGE Fortran 90/95/2000 INTRODUCTION Fortran History FORTRAN HISTORY FORTRAN VERSIONS Published by IBM in 1957 STRENGTHS MS PowerStation 4.0 WEAKENESSES Sold to Digital with DEC, it developed into Digital Visual Fotran 5.x PROGRAM STRUCTURE SIGNIFICANT FEATURES Digital was later merged with Compaq; CVF 6.x emerged NEW SOURCE FORM CVF development team was purchased by Intel OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE HP merged with Compaq, introducing HP CVF 6.6a Intel Fortran, combining CVF, developed Intel Visual Fortran 8.x Fortran 90/95/2000 INTRODUCTION Fortran Versions FORTRAN HISTORY FORTRAN VERSIONS 1962 FORTRAN IV STRENGTHS 1966 FORTRAN 66 WEAKENESSES 1978 FORTRAN 77 PROGRAM STRUCTURE SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING 1992 FORTRAN 90 Array Semi-OOP Resembles MATLAB ARRAY PROGRAMMING POINTER DYNAMIC STORAGE 1997 FORTRAN 95 HPF extension more OOP 2003 FORTRAN 2000 Fully OOP Fortran 90/95/2000 INTRODUCTION Strengths FORTRAN HISTORY FORTRAN VERSIONS Array language and object-oriented programming STRENGTHS Higher computational speed compared to C/C++ and MATLAB WEAKENESSES Maintains plenty of legacy codes PROGRAM STRUCTURE SIGNIFICANT FEATURES Easy-to-learn compared to C/C++ NEW SOURCE FORM The majority of individuals in the numerical computing field still use OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE Fortran to develop program(s) Fortran 90/95/2000 INTRODUCTION Weaknesses FORTRAN HISTORY FORTRAN VERSIONS File I/O is difficult to modify and comprehend STRENGTHS Low reusability and high cost of code maintenance WEAKENESSES Lack of numerical and graphical libraries PROGRAM STRUCTURE SIGNIFICANT FEATURES Difficult to convert the codes into applications NEW SOURCE FORM Platform porting OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE Interfacing to other language Fortran 90/95/2000 INTRODUCTION Program Structure FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS WEAKENESSES PROGRAM name IMPLICIT NONE PROGRAM STRUCTURE SIGNIFICANT FEATURES declarations NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING A good description of Fortran programing. POINTER DYNAMIC STORAGE statements STOP END Fortran 90/95/2000 INTRODUCTION Significant Features FORTRAN HISTORY FORTRAN VERSIONS New Source Form STRENGTHS WEAKENESSES PROGRAM STRUCTURE Object-Oriented Programming Array Programming SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE Dynamic Memory Allocation Pointer Fortran 90/95/2000 INTRODUCTION New Source Form FORTRAN HISTORY FORTRAN VERSIONS Free Format STRENGTHS WEAKENESSES PROGRAM STRUCTURE SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE IMPLICIT NONE Statements Fortran 90/95/2000 INTRODUCTION New Source Form – Free Format FORTRAN HISTORY FORTRAN VERSIONS names of variables may consist of up to 31 characters STRENGTHS 132 characters per line WEAKENESSES up to 39 continuation lines PROGRAM STRUCTURE SIGNIFICANT FEATURES blanks are significant NEW SOURCE FORM & as line continuation character OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE ; as statement separator for multiple statements per line ! as comment symbol Fortran 90/95/2000 INTRODUCTION New Source Form - IMPLICIT NONE FORTRAN HISTORY FORTRAN VERSIONS The first line after any USE statements STRENGTHS Used to inhibit the old Fortran feature that treats, by default, all WEAKENESSES variable beginning with the letters I, j, k, l, m, and n as integers and PROGRAM STRUCTURE others as real arguments SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE IMPLICIT NONE should always be used to prevent potential confusion in variable types Upper and lowercase letters are equivalent Fortran 90/95/2000 INTRODUCTION New Source Form - Statements FORTRAN HISTORY FORTRAN VERSIONS INCLUDE can be used to include source text from external files STRENGTHS END DO statements are used to complete DO loops WEAKENESSES Relational Operator Alternatives PROGRAM STRUCTURE .LT. < .LE. <= OO PROGRAMMING .EQ. == ARRAY PROGRAMMING .NE. /= POINTER .GE. > .GT. => SIGNIFICANT FEATURES NEW SOURCE FORM DYNAMIC STORAGE Fortran 90/95/2000 INTRODUCTION Object-Oriented Programming FORTRAN HISTORY FORTRAN VERSIONS Functionality STRENGTHS WEAKENESSES PROGRAM STRUCTURE TYPE MODULE SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING Attributes INTERFACE ARRAY PROGRAMMING POINTER DYNAMIC STORAGE Overload Fortran 90/95/2000 INTRODUCTION Object-Oriented Programming - Functionality FORTRAN HISTORY FORTRAN VERSIONS Data Abstraction STRENGTHS Data Hiding WEAKENESSES Encapsulation PROGRAM STRUCTURE SIGNIFICANT FEATURES Inheritance NEW SOURCE FORM Polymorphism OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE Reusability Fortran 90/95/2000 INTRODUCTION Object-Oriented Programming – TYPE FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS user-defined TYPE be used to describe an object WEAKENESSES PROGRAM STRUCTURE A new type can be defined in a derived-type statement, which can later Example I SIGNIFICANT FEATURES Create a type COORDS_3D with three REAL components X, Y, and Z. NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER TYPE :: COORDS_3D REAL :: X, Y, Z END TYPE COORDS_3D DYNAMIC STORAGE 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 Fortran 90/95/2000 INTRODUCTION Object-Oriented Programming – TYPE FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS WEAKENESSES PROGRAM STRUCTURE user-defined TYPE Example II Create a type NONZERO in which nonzero matrix elements are described. TYPE :: NONZERO SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER REAL :: VALUE INTEGER :: ROW, COLUMN END TYPE NONZERO Create a sparse matrix A with 100 nonzero elements. DYNAMIC STORAGE TYPE(NONZERO) :: A(100) Obtain the value of A(10). X = A(10)%Value Fortran 90/95/2000 INTRODUCTION Object-Oriented Programming – MODULE FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS MODULE / MODULE PROCEDURE A collection of data, type definitions, and procedure definitions which WEAKENESSES can be exploited by any other program unit attaching it (via the USE PROGRAM STRUCTURE statement). SIGNIFICANT FEATURES Example Main Program MODULE point_module . NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE 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 addpoints END MODULE point_module Accesses the module. . . USE point_module TYPE (point) :: px, py, pz . . . pz = addpoints(px,py) Fortran 90/95/2000 INTRODUCTION Object-Oriented Programming – ATTRIBUTES FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS PUBLIC and PRIVATE attributes the specified module WEAKENESSES PROGRAM STRUCTURE PRIVATE – variables/subroutines/functions defined can only be used in PUBLIC – variables/subroutines/functions defined can be used publicly Example SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING MODULE bank ARRAY PROGRAMMING PRIVATE money POINTER PUBLIC SaveMoney DYNAMIC STORAGE integer :: money = 1000000 CONTAINS SUBROUTINE SaveMoney(num) integer :: num money = money+num return END SUBROUTINE END MODULE Fortran 90/95/2000 INTRODUCTION Object-Oriented Programming – INTERFACE FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS INTERFACE A way to specify information for an external procedure WEAKENESSES Name of the procedure PROGRAM STRUCTURE Types of passed and returned parameters SIGNIFICANT FEATURES Whether an argument may be changed NEW SOURCE FORM INTERFAXE detects incorrect calls at compile time Example OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE INTERFACE REAL FUNCTION DISTANCE( A, B) REAL, INTENT(IN) :: A, B END FUNCTION DISTANCE END INTERFACE Fortran 90/95/2000 INTRODUCTION Object-Oriented Programming – Overload FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS WEAKENESSES 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 PROGRAM STRUCTURE SIGNIFICANT FEATURES generic operator symbol in an INTERFACE OPERATOR statement NEW SOURCE FORM overload set in a generic interface OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE Example INTERFACE OPERATOR(-) FUNCTION DIFF(A,B) TYPE(POINT) :: DIFF, A, B END FUNCTION END INTERFACE Fortran 90/95/2000 INTRODUCTION Array Programming FORTRAN HISTORY FORTRAN VERSIONS Whole Array STRENGTHS WEAKENESSES PROGRAM STRUCTURE SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE Array Section Intrinsic Functions Fortran 90/95/2000 INTRODUCTION Array Programming – Whole Array FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS Assignment All arrays must conform The operation is applied to each element of the array Scalars broadcast WEAKENESSES PROGRAM STRUCTURE SIGNIFICANT FEATURES NEW SOURCE FORM Declarations OO PROGRAMMING REAL, DIMENSION(5, 5) :: A, B ARRAY PROGRAMMING OR REAL :: A(5,5), B(5,5) POINTER DYNAMIC STORAGE Fortran 90/95/2000 INTRODUCTION Array Programming – Whole Array FORTRAN HISTORY FORTRAN VERSIONS Operation 21 12 7 2 3 6 8 25 2 5 4 5 PROGRAM STRUCTURE 18 42 9 6 2 7 SIGNIFICANT FEATURES 36 77 4 7 9 11 STRENGTHS WEAKENESSES = x NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER Example Assume A and B to be two 2D arrays of the same shape. Multiply them and assign the result to array C. DYNAMIC STORAGE FORTRAN 77 FORTRAN 90+ REAL A(5, 5), B(5, 5), C(5, 5) REAL, DIMENSION (5, 5) :: A, B, C ... i LOOP j LOOP C(j, i) = A(j, i) * B(j, i) END i LOOP END j LOOP ... C=A*B Fortran 90/95/2000 INTRODUCTION Array Programming – Array Section FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS WEAKENESSES PROGRAM STRUCTURE Declaration REAL, DIMENSION(10, 10) :: A Subscript Notation ( [row lower bound] : [row upper bound] : [row stride], SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE [column lower bound] : [column upper bound] : [column stride] ) Fortran 90/95/2000 INTRODUCTION Array Programming – Array Section FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS Example REAL :: A(10, 10) WEAKENESSES PROGRAM STRUCTURE SIGNIFICANT FEATURES A(2:6, 4:8) A(:, 1:3) NEW SOURCE FORM 1 2 3 4 5 6 7 8 9 OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 Fortran 90/95/2000 INTRODUCTION Array Programming – Array Section FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS Example REAL :: A(10, 10) WEAKENESSES PROGRAM STRUCTURE SIGNIFICANT FEATURES A(4:10, 5) A(1:10:2, 1:10:2) NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 Fortran 90/95/2000 INTRODUCTION Array Programming – Intrinsic Functions FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS Functions WEAKENESSES array manipulations CSHIFT and EOSHIFT for shifts along array axis PROGRAM STRUCTURE TRANSPOSE for the transpose of a matrix SIGNIFICANT FEATURES NEW SOURCE FORM SUM , PRODUCT , MAXVAL , MINVAL , COUNT , ALL , and ANY OO PROGRAMMING ARRAY PROGRAMMING reduction functions POINTER inquiry functions SHAPE , SIZE, ALLOCATED, LBOUND, and UBOUND DYNAMIC STORAGE array constructor functions MERGE, SPREAD, RESHAPE, PACK and UNPACK Fortran 90/95/2000 INTRODUCTION Array Programming – Intrinsic Functions FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS Example - CSHIFT FORTRAN 77 FORTRAN 90+ PROGRAM STRUCTURE REAL :: A(0:99), B(0:99) REAL :: A(100), B(100) SIGNIFICANT FEATURES DO i = 0, 99 B = ( CSHIFT(A, +1) + CSHIFT(A, -1) ) / 2 WEAKENESSES NEW SOURCE FORM OO PROGRAMMING B(i) = ( A( mod(i+99, 100) ) + A( mod(i+1, 100) ) ) / 2 ARRAY PROGRAMMING POINTER DYNAMIC STORAGE ENDDO Fortran 90/95/2000 INTRODUCTION Pointer FORTRAN HISTORY FORTRAN VERSIONS Introduction STRENGTHS WEAKENESSES PROGRAM STRUCTURE SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE Association Status Example Fortran 90/95/2000 INTRODUCTION Pointer - Introduction FORTRAN HISTORY FORTRAN VERSIONS A pointer has the POINTER attribute and may point to another data STRENGTHS object of the same type, which has the TARGET attribute or an area of WEAKENESSES dynamically allocated memory. PROGRAM STRUCTURE ADDRESS DESCRIPTOR SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING TARGET Uses POINTER Alternative to allocatable arrays A tool to create and manipulate dynamic data structures DYNAMIC STORAGE Declarations REAL, POINTER :: Ptr(:, :) REAL, TARGET :: TA(:, :) Fortran 90/95/2000 INTRODUCTION Pointer – Association Status FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS Status WEAKENESSES Undefined – initially specified in a type declaration statement Pointer ? PROGRAM STRUCTURE SIGNIFICANT FEATURES Associated – points to a target Pointer NEW SOURCE FORM Target OO PROGRAMMING ARRAY PROGRAMMING POINTER Null – nullified by a NULLIFY or a DEALLOCATE statement Pointer NULL DYNAMIC STORAGE NULLIFY(Ptr) DEALLOCATE(Ptr, STAT = ierr) Fortran 90/95/2000 INTRODUCTION Pointer - Example FORTRAN HISTORY FORTRAN VERSIONS Example STRENGTHS REAL, TARGET :: A WEAKENESSES REAL, POINTER :: P, Q PROGRAM STRUCTURE A = 3.14 SIGNIFICANT FEATURES P => A NEW SOURCE FORM Q => P OO PROGRAMMING A = 2.718 ARRAY PROGRAMMING WRITE(*,*) Q POINTER DYNAMIC STORAGE Q outputs 2.718 Q => P and P => A Therefore, Q => A, whose value has changed from 3.14 to 2.718 Fortran 90/95/2000 INTRODUCTION Dynamic Storage FORTRAN HISTORY FORTRAN VERSIONS Allocatable Array STRENGTHS WEAKENESSES PROGRAM STRUCTURE SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE Pointer Fortran 90/95/2000 INTRODUCTION Dynamic Storage – Allocatable Array FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS WEAKENESSES Acquire and return a storage area in HEAP MEMORY for an array with attributes Example PROGRAM STRUCTURE REAL, DIMENSION(:), ALLOCATABLE :: A SIGNIFICANT FEATURES ALLOCATE( A(5:5) ) NEW SOURCE FORM A(j) = q ! assignment of the array OO PROGRAMMING CALL sub(A) ! Use of the array in a subroutine ARRAY PROGRAMMING POINTER Deallocation occurs automatically reaching RETURN or END in the program DYNAMIC STORAGE To prevent memory leak, allocatable arrays should be explicitly deallocated DEALLOCATE (A) Fortran 90/95/2000 INTRODUCTION Dynamic Storage - Pointer FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS WEAKENESSES PROGRAM STRUCTURE SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE 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 Fortran 90/95/2000 INTRODUCTION Dynamic Storage - Pointer FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS Use a pointer Example WEAKENESSES PROGRAM STRUCTURE Subroutine Procedure: SIGNIFICANT FEATURES SUBROUTINE SUB(B) NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING REAL, DIMENSION (:,:), POINTER :: B INTEGER M, N ! Assign M and N ALLOCATE (B(M,N)) ! Allocate B as a matrix END SUBROUTINE SUB POINTER DYNAMIC STORAGE 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 Fortran 90/95/2000 INTRODUCTION FORTRAN HISTORY FORTRAN VERSIONS STRENGTHS WEAKENESSES PROGRAM STRUCTURE SIGNIFICANT FEATURES NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING POINTER DYNAMIC STORAGE THANK YOU!