Download presentation
Presentation is loading. Please wait.
Published byAvis Edwards Modified over 8 years ago
2
FORTRAN 90+ Yetmen Wang
3
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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 Introduction
4
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
5
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
6
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGEStrengths 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)
7
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGEWeaknesses 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
8
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE A good description of Fortran programing. PROGRAM name IMPLICIT NONE STOP END declarations statements Program Structure
9
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE Significant Features New Source Form Object-Oriented Programming Array Programming Dynamic Memory Allocation Pointer
10
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE New Source Form Free Format IMPLICIT NONE Statements
11
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
12
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
13
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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. =>
14
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE Object-Oriented Programming FunctionalityTYPEMODULEAttributesINTERFACEOverload
15
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE Object-Oriented Programming - Functionality Data Abstraction Data Hiding Encapsulation Inheritance Polymorphism Reusability
16
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
17
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
18
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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 addpoints END MODULE point_module Main Program. USE point_module TYPE (point) :: px, py, pz... pz = addpoints(px,py) Accesses the module.
19
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
20
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
21
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
22
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE Array Programming Whole Array Array Section Intrinsic Functions
23
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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)
24
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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. 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 2112 825 1842 36777225 96 473645 27 911 =x
25
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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] )
26
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE Array Programming – Array Section Example REAL :: A(10, 10) 123456789 1 2 3 4 5 6 7 8 9 A(2:6, 4:8)A(:, 1:3)1234567891 2 3 4 5 6 7 8 9
27
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE Array Programming – Array Section Example REAL :: A(10, 10) A(4:10, 5)A(1:10:2, 1:10:2) 123456789 1 2 3 4 5 6 7 8 91234567891 2 3 4 5 6 7 8 9 INTRODUCTION
28
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
29
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
30
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGEPointer Introduction Association Status Example
31
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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(:, :) ADDRESSDESCRIPTOR TARGET
32
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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 ? Target NULL
33
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
34
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE Dynamic Storage Allocatable Array Pointer
35
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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 RETURN or END in the program To prevent memory leak, allocatable arrays should be explicitly deallocated DEALLOCATE (A)
36
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
37
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE 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
38
Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT FEATURES FORTRAN HISTORY STRENGTHS WEAKENESSES POINTER DYNAMIC STORAGE THANK YOU!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.