MODULE Attributes INTERFACE Overload... 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 th
Trang 1Yetmen Wang
Trang 2Mainly intended for mathematical computations Areas of Application
Trang 3CVF 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
Trang 5Easy-to-learn compared to C/C++
The majority of individuals in the numerical computing field still use Fortran to develop program(s)
Trang 6Difficult to convert the codes into applications Platform porting
Interfacing to other language
Trang 7of Fortran programing.
Trang 8Dynamic Memory Allocation Pointer
Trang 10New 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
Trang 11New 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
Trang 12New 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
Trang 13MODULE Attributes INTERFACE Overload
Trang 14Encapsulation Inheritance Polymorphism Reusability
Trang 15 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
Trang 16TYPE :: 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
Trang 17Object-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
Main Program
USE point_module TYPE (point) :: px, py, pz
.
pz = addpoints(px,py)
Accesses the module.
Trang 18Object-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
Trang 19 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
Trang 20 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
Trang 22 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)
Trang 23i LOOP
j LOOP C(j, i) = A(j, i) * B(j, i) END i LOOP
FORTRAN 90+
REAL, DIMENSION (5, 5) :: A, B, C
Trang 252 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
Trang 262 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
INTRODUCTION
Trang 27CSHIFT 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
Trang 28FORTRAN 90+
REAL :: A(100), B(100)
B = ( CSHIFT(A, +1) + CSHIFT(A, -1) ) / 2
Trang 30 Alternative to allocatable arrays
A tool to create and manipulate dynamic data structures
Declarations
REAL, POINTER :: Ptr(:, :) REAL, TARGET :: TA(:, :)
TARGET
Trang 31 Undefined – initially specified in a type declaration statement
Associated – points to a target
Null – nullified by a NULLIFY or a DEALLOCATE statement
Trang 32A = 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
Trang 34Dynamic 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)
Trang 35 Can be passed to a procedure in an unallocated state
An explicit INTERFACE is required when passing a pointer to a procedure
Trang 36ALLOCATE (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