introduction to fortran: part 2 fortran features program organization and style: free format...

52
Introduction to Fortran: Part 2 Reuben D. Budiardja National Institute for Computational Sciences Seminar Series on High Performance Computing Reuben D. Budiardja (National Institute for Computational Sciences) Introduction to Fortran: Part 2 Seminar Series on High Performance Computi / 36

Upload: trinhkhuong

Post on 08-May-2018

229 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Introduction to Fortran: Part 2

Reuben D. Budiardja

National Institute for Computational Sciences

Seminar Series on High Performance Computing

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 1

/ 36

Page 2: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Modern Fortran Features

Program organization and style:

free format

modules, sub-module

implict none

portable precision model

Fortran 90/95 OO features:

derived type

array operations, dynamicallocation

procedure and operatoroverloading (staticpolymorphism)

Fortran pointer

Fortran 2003 adds more:

type-bound procedures withpass attribute

procedure pointers

Type-bound procedure by name

final procedure, automaticdeallocation (garbage collection)

type extension, abstract type(templating)

polymorphic entities

procedure overriding, deferredbinding (dynamicpolymorphism)

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 2

/ 36

Page 3: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Modern Fortran Features

Program organization and style:

free format

modules, sub-module

implict none

portable precision model

Fortran 90/95 OO features:

derived type

array operations, dynamicallocation

procedure and operatoroverloading (staticpolymorphism)

Fortran pointer

Fortran 2003 adds more:

type-bound procedures withpass attribute

procedure pointers

Type-bound procedure by name

final procedure, automaticdeallocation (garbage collection)

type extension, abstract type(templating)

polymorphic entities

procedure overriding, deferredbinding (dynamicpolymorphism)

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 2

/ 36

Page 4: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Arrays

Array: a collection of values (of certain data types, e.g. integer, real,etc) indexed by array index (key)

Static-ly (compile time) and Dynamic-ly(runtime) allocated arrays arepossible

The majority of intrinsic functions supports array argument

Fortran supports true multi-dimensional arrays

as opposed to C & C++, for example, where multi-D array isimplemented as a vector of pointers (array of array)true multi-D array in contiguous memory block is more efficient formany array operations

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 3

/ 36

Page 5: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Arrays

Array: a collection of values (of certain data types, e.g. integer, real,etc) indexed by array index (key)

Static-ly (compile time) and Dynamic-ly(runtime) allocated arrays arepossible

The majority of intrinsic functions supports array argument

Fortran supports true multi-dimensional arrays

as opposed to C & C++, for example, where multi-D array isimplemented as a vector of pointers (array of array)true multi-D array in contiguous memory block is more efficient formany array operations

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 3

/ 36

Page 6: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Arrays Terminologies

The rank of the array is the number of dimensions

maximum rank is 31 (F2008)previous max is 7 (F90)

The number of elements in a dimension is the extent

The shape of an array is a vector where each element of the vector isthe extent in the corresponding dimension

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 4

/ 36

Page 7: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Array Syntax

1 r e a l : : x2 r e a l , d i m e n s i o n ( 1 0 ) : : a , b3 r e a l , d i m e n s i o n ( 1 0 , 1 0 ) : : c , d4 r e a l , d i m e n s i o n ( 0 : 4 , 3 : 8 , 7 , 9 ) &5 : : e6

7 a = b8 c = d9 a ( 1 : 1 0 ) = b ( 1 : 1 0 )

10 a ( 2 : 3 ) = b ( 4 : 5 )11 a ( 1 : 1 0 ) = c ( 1 : 1 0 , 2 )12 a = x13 c = x14 a ( 1 : 5 ) = b ( 1 : 5 ) ∗ cos ( c ( 6 : 1 0 , 1 ) )15 a ( 1 : 3 ) = b ( 1 : 5 : 2 )16

17 p r i n t ∗ , shape ( e ) ! – prints 5,6,7,9

Array assignment must beconformable: size anddimension (i.e. shape)have to agree

Scalars are conformable

Strides can be used too

Default lower bound is 1,but can be explicitlyspecified

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 5

/ 36

Page 8: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Array Constructor

Example: Array Constructor

1 r e a l d i m e n s i o n ( 3 ) : : x = [ 0 . 0 , 1 . 0 , 2 . 0 ]2 r e a l , d i m e n s i o n ( 5 ) : : a , b3

4 a = [ x , 3 . 0 , 4 . 0 ]5 b = [ ( s q r t ( r e a l ( i ) ) , i =1.5) ] ! – constructor with implicit do-loop

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 6

/ 36

Page 9: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Dynamically Allocated Arrays

Array can be dynamically allocated at runtime =⇒ flexible size

Dynamic array is allocated on the heap memory

vs. static array on the stackthe size of stack is often limited (default: 2 GB)remedies are somewhat problematic (e.g. compiler dependent, notportable)

Allocation/deallocation is done with allocate/deallocate statement

allocation and deallocation take timee.g. do not allocate/deallocate in a loop

Use allocatable for large arrays

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 7

/ 36

Page 10: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Dynamically Allocated Arrays

Array can be dynamically allocated at runtime =⇒ flexible size

Dynamic array is allocated on the heap memory

vs. static array on the stackthe size of stack is often limited (default: 2 GB)remedies are somewhat problematic (e.g. compiler dependent, notportable)

Allocation/deallocation is done with allocate/deallocate statement

allocation and deallocation take timee.g. do not allocate/deallocate in a loop

Use allocatable for large arrays

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 7

/ 36

Page 11: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Dynamically Allocated Arrays

Array can be dynamically allocated at runtime =⇒ flexible size

Dynamic array is allocated on the heap memory

vs. static array on the stackthe size of stack is often limited (default: 2 GB)remedies are somewhat problematic (e.g. compiler dependent, notportable)

Allocation/deallocation is done with allocate/deallocate statement

allocation and deallocation take timee.g. do not allocate/deallocate in a loop

Use allocatable for large arrays

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 7

/ 36

Page 12: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Example: Allocatable Array

1 program a l l o c a t a b l e a r r a y2 i m p l i c i t none3 i n t e g e r : : i S i z e , j S i z e , k S i z e4 r e a l , d i m e n s i o n ( : , : , : ) , a l l o c a t a b l e : : a 3d5

6 p r i n t ∗ , ” I n p u t t he s i z e o f t h e a r r a y ”7 r e a d ∗ , i S i z e , j S i z e , k S i z e8

9 a l l o c a t e ( a 3d ( i S i z e , j S i z e , k S i z e ) )10 p r i n t ∗ , shape ( a 3d )11

12 ! – Use array13

14 d e a l l o c a t e ( a 3d )15 end program a l l o c a t a b l e a r r a y

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 8

/ 36

Page 13: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Array Construct: where

The where construct is used when we want to make selective assignmentto array.

Example: where

1 r e a l , d i m e n s i o n ( 1 0 , 1 0 ) : : &2 a , r e c i p a3

4 ! – fill in a ...5

6 where ( a/= 0 . 0 )7 r e c i p a = 1 . 0 / a8 e l s e w h e r e9 r e c i p a = 1 . 0

10 endwhere

Arrays must beconformable

code block executeswhen condition is true

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 9

/ 36

Page 14: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Array Construct: forall

The forall construct provide a mechanism to specify an indexed parallelassignment to array.

Example: forall

1 ! – With do-loops:2 do j = 1 , m3 do i = 1 , n4 a ( i , j ) = i + j5 end do6 end do7

8 ! – with forall9 f o r a l l ( i = 1 : n , j = 1 :m)

10 a ( i , j ) = i + j11 end f o r a l l

The forall and whereconstructs may aid invectorization andparallelization on certainarchitecture

These constructs providemore natural mechanismto express often foundmathematical formulas

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 10

/ 36

Page 15: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

any Function

The function any returns true if any array elements are true.

Example: any

1 i n t e g e r , d i m e n s i o n ( 4 ) : : a2

3 a = [ 0 , −1, 1 , 2 ]4

5 i f ( any ( a < 0) ) then6 p r i n t ∗ , ’ found n e g a t i v e number ’7 end i f

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 11

/ 36

Page 16: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

all Function

The function all returns true if all array elements are true.

Example: all

1 i n t e g e r , d i m e n s i o n ( 4 ) : : a , b2

3 a = [ 0 , −1, 1 , 2 ]4 b = [ 5 , 3 , 6 , 4 ]5

6 i f ( a l l ( a > 0) ) then7 p r i n t ∗ , ’ A l l numbers a r e p o s i t i v e ’8 end i f9

10 i f ( a l l ( a /= b ) )11 p r i n t ∗ , ’ a and b a r e m u t u a l l y e x c l u s i v e ’12 end i f

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 12

/ 36

Page 17: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Passing Arrays to Subroutines

Example: Array Arguments

1 s u b r o u t i n e ex2 ( a , b , x )2 r e a l , d i m e n s i o n ( : ) , i n t e n t ( i n ) : : a3 r e a l , d i m e n s i o n ( 0 : ) , i n t e n t ( i n ) : : b4 r e a l , d i m e n s i o n ( 2 : , : ) , i n t e n t ( i n o u t ) : : x5 . . .6 end s u b r o u t i n e ex27

8 c a l l ex2 ( u , v , w( 4 : 9 , 2 : 6 ) )

An assumed-shape array is a dummy argument that takes the shapeof the actual argument

The default lower bound is 1, but can be redefined

The extent of the array is determined from the actual argument

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 13

/ 36

Page 18: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Automatic Array

An automatic array can be created upon entry to a procedure

The size can vary and determined at runtime

The array is allocated on the stack

The object disappear when the procedure exit

Requires explicit interface of the procedure (see module section)

Example: Automatic Array

1 s u b r o u t i n e swap ( a , b )2 r e a l , d i m e n s i o n ( : ) , i n t e n t ( i n ) : : a , b3 r e a l , d i m e n s i o n ( s i z e ( a ) ) : : work4

5 work = a6 a = b7 b = work8 end s u b r o u t i n e swap

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 14

/ 36

Page 19: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Elemental Procedures

A procedure (function or subroutine) can be declared as elemental

An elemental procedure can be passed an array of any dimension

Each element of the array is acted upon one element at a time

The argument and result must be scalar

The procedure must have an explicit interface visible to the caller(more on this later in module)

Elemental procedure must be pure:

A pure procedure has no side effect: it does not change the state of theprogram except,For function: it returns a valueFor subroutines: it modifies intent(out) and intent(inout) argumentsAll intrinsic functions are pure

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 15

/ 36

Page 20: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Example: Elemental Procedures

1 module geometry2 i m p l i c i t none3

4 c o n t a i n s5

6 e l e m e n t a l f u n c t i o n c i r c u m f e r e n c e ( r ) r e s u l t ( c )7 r e a l , i n t e n t ( i n ) : : r ! – intent(in) required8 r e a l : : c9 c = 2 . 0 ∗ acos (−1.0) ∗ r

10 end f u n c t i o n c i r c u m f e r e n c e11

12 e l e m e n t a l s u b r o u t i n e a r e a ( r , a )13 r e a l , i n t e n t ( i n ) : : r ! – intent(in) required14 r e a l , i n t e n t ( out ) : : a15 a = acos (−1.0) ∗ r ∗∗216 end s u b r o u t i n e a r e a17

18 end module geometry

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 16

/ 36

Page 21: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Example: Invoking Elemental Procedures

1 program c i r c l e s2 use geometry3

4 r e a l , d i m e n s i o n ( 5 ) : : r , a5

6 r = [ 2 . 0 , 4 . 0 , 4 . 3 , 5 . 0 , 7 . 0 ]7

8 p r i n t ∗ , c i r c u m f e r e n c e ( r )9

10 c a l l a r e a ( r , a )11 p r i n t ∗ , a12

13 end program c i r c l e

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 17

/ 36

Page 22: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Derived (Structures) Data Types

A collection of intrinsic data types (integer, real, logical, etc)

Can hold static and allocatable arrays

Can hold other derived types

A derived type must first be declared before it can be used

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 18

/ 36

Page 23: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Example: Derived Type

1 program Der ivedTypeExample2

3 i m p l i c i t none4

5 t y p e : : c o l o r6 i n t e g e r : : shade7 r e a l , d i m e n s i o n ( 3 ) : : rgb8 c h a r a c t e r ( l e n =30) : : name9 end t y p e c o l o r

10

11 t y p e ( c o l o r ) : : background12

13 background % shade = 1014 background % rgb = [ 1 2 8 . 0 , 2 5 5 . 0 , 1 3 2 . 0 ]15 background % name = ” g r e y i s h ”16

17 p r i n t ∗ , background18 end program Der ivedTypeExample

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 19

/ 36

Page 24: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Procedures and Derived Type

Procedures (functions and subroutines) can have derived-types asarguments and results (for functions)

Use derived-types just like intrinsic type

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 20

/ 36

Page 25: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Program Organization

Why should we use Subprogram (Function / Subroutines)?

Decompose complex tasks into simpler, manageable code blocks

no “super main” program

Enable reuse of code, reduce code duplication

Hide implementation details, limit scope of variables

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 21

/ 36

Page 26: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Module

Modules are another, more flexible tool to organize code base.

A module contains definition that can be made accessible to otherprogram units.

Modules may contain:

Derived Type definitions

Variables

Subroutines, Functions

A module collects related type definition, subroutines, and functions into apackage that can be re-used (by other program, modules, etc).

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 22

/ 36

Page 27: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Example: Module

1 module geometry2 i m p l i c i t none3 p r i v a t e4

5 type , p u b l i c : : c i r c l e T y p e6 r e a l : : r a d i u s7 r e a l , d i m e n s i o n ( 2 ) : : c e n t e r8 end t y p e c i r c l e T y p e9

10 r e a l , p a ra m e t e r : : PI = acos (−1.0)11

12 c o n t a i n s13

14 e l e m e n t a l f u n c t i o n c i r c u m f e r e n c e ( g ) r e s u l t ( c )15 t y p e ( c i r c l e T y p e ) , i n t e n t ( i n ) : : g16 r e a l : : c17 c = 2 . 0 ∗ PI ∗ g % r a d i u s18 end f u n c t i o n c i r c u m f e r e n c e19

20 end module geometryReuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2

Seminar Series on High Performance Computing 23/ 36

Page 28: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

How To / Why Use Module ?

The public entities (default) of a module is accessible to otherprogram unit via use statement

Modules provide realible mechanism for specifying global data,including variables, type definitions, and procedure interface

Modules facilitate object-oriented concepts and modular programming

Modules provide information hiding:

private entities not visible outside the module,protected entities are visible as read-only outside the moduleInternal design can be altered without the need to alter the programPrevent accidental alteration of internal data

Modules provide explicit interfaces (see next slide)

module is arguably the most important feature for programorganization in modern Fortran. Use it!

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 24

/ 36

Page 29: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

How To / Why Use Module ?

The public entities (default) of a module is accessible to otherprogram unit via use statement

Modules provide realible mechanism for specifying global data,including variables, type definitions, and procedure interface

Modules facilitate object-oriented concepts and modular programming

Modules provide information hiding:

private entities not visible outside the module,protected entities are visible as read-only outside the moduleInternal design can be altered without the need to alter the programPrevent accidental alteration of internal data

Modules provide explicit interfaces (see next slide)

module is arguably the most important feature for programorganization in modern Fortran. Use it!

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 24

/ 36

Page 30: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

How To / Why Use Module ?

The public entities (default) of a module is accessible to otherprogram unit via use statement

Modules provide realible mechanism for specifying global data,including variables, type definitions, and procedure interface

Modules facilitate object-oriented concepts and modular programming

Modules provide information hiding:

private entities not visible outside the module,protected entities are visible as read-only outside the moduleInternal design can be altered without the need to alter the programPrevent accidental alteration of internal data

Modules provide explicit interfaces (see next slide)

module is arguably the most important feature for programorganization in modern Fortran. Use it!

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 24

/ 36

Page 31: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

How To / Why Use Module ?

The public entities (default) of a module is accessible to otherprogram unit via use statement

Modules provide realible mechanism for specifying global data,including variables, type definitions, and procedure interface

Modules facilitate object-oriented concepts and modular programming

Modules provide information hiding:

private entities not visible outside the module,protected entities are visible as read-only outside the moduleInternal design can be altered without the need to alter the programPrevent accidental alteration of internal data

Modules provide explicit interfaces (see next slide)

module is arguably the most important feature for programorganization in modern Fortran. Use it!

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 24

/ 36

Page 32: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Implicit and Explicit Interface

Implicit interface:

Position of arguments to procedures are the only available information

The call to procedure is matched and resolved during program linking

This is all that is available in Fortran 77

Drawbacks:

No compile-time type checkingProgrammer is responsible for checking the interfacesIf there is a mismatch of type, precision, etc, may result in strangeprogram behavior, segmentation fault, and other runtime error

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 25

/ 36

Page 33: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Implicit and Explicit Interface

Explicit interface:

More information is available to the compilers: types, precision,argument names, etc.

Provided automatically when procedures are in a module

Module has to be compiled first, then use-d by the other program unit(other module or program). (Usually this is in the form of .mod file.)

Benefits:

At compile time, compiler does consistency checking of argumentpassed by the caller to procedures.Required for many features of modern Fortran: optional parameters,elemental procedures, assume-shape arrays, procedure overloading, etc.

Always use explicit interface. The easiest way to do this is to put all theprocedures inside module(s).

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 26

/ 36

Page 34: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Putting It All Together: Module Example

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 27

/ 36

Page 35: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Generic Interface: Procedures Overloading

Function/suboutine overloading allows several procedures to be calledby the same generic name

Useful for implementing different variations of the same basicconcepts, while hiding implementation details

In Object-oriented principle, this is known as polymorphism.

Examples:

The area of any object has the same basic concept, but computeddifferently for circle, rectangle, triangle, etc.

A procedure can be overloaded if at least one of its arguments isdistinguishable

Anything distinguishable works: different precision, array shape, types

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 28

/ 36

Page 36: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Generic Interface: Procedures Overloading

Function/suboutine overloading allows several procedures to be calledby the same generic name

Useful for implementing different variations of the same basicconcepts, while hiding implementation details

In Object-oriented principle, this is known as polymorphism.

Examples:

The area of any object has the same basic concept, but computeddifferently for circle, rectangle, triangle, etc.

A procedure can be overloaded if at least one of its arguments isdistinguishable

Anything distinguishable works: different precision, array shape, types

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 28

/ 36

Page 37: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Generic Interface: Procedures Overloading

Function/suboutine overloading allows several procedures to be calledby the same generic name

Useful for implementing different variations of the same basicconcepts, while hiding implementation details

In Object-oriented principle, this is known as polymorphism.

Examples:

The area of any object has the same basic concept, but computeddifferently for circle, rectangle, triangle, etc.

A procedure can be overloaded if at least one of its arguments isdistinguishable

Anything distinguishable works: different precision, array shape, types

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 28

/ 36

Page 38: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Generic Interface: Procedures Overloading

Function/suboutine overloading allows several procedures to be calledby the same generic name

Useful for implementing different variations of the same basicconcepts, while hiding implementation details

In Object-oriented principle, this is known as polymorphism.

Examples:

The area of any object has the same basic concept, but computeddifferently for circle, rectangle, triangle, etc.

A procedure can be overloaded if at least one of its arguments isdistinguishable

Anything distinguishable works: different precision, array shape, types

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 28

/ 36

Page 39: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Example: Generic Interface

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 29

/ 36

Page 40: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Generic Interface (2)

Generic interface works across modules in different files

A better way for the previous example:

Create one module for Circle, another for RectangleScalable programming: different people can work on different object

Intrinsic subroutines / functions can also be overloaded

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 30

/ 36

Page 41: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Operator Overloading

Intrinsic operator (+, -, /, =) can be overloaded for user-defined andderived-type entities

Using interface block, new meaning can be given to intrinsic operator

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 31

/ 36

Page 42: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Example: Operator Overloading

1 module geometry2 i m p l i c i t none3 type , p u b l i c : : c i r c l e T y p e4 r e a l : : r a d i u s5 r e a l , d i m e n s i o n ( 2 ) : : c e n t e r6 end t y p e c i r c l e T y p e7

8 i n t e r f a c e o p e r a t o r (+)9 module p r o c e d u r e c i r c l e s u m

10 end i n t e r f a c e11

12 c o n t a i n s13

14 f u n c t i o n c i r c l e s u m ( c1 , c2 ) r e s u l t ( c s )15 t y p e ( c i r c l e T y p e ) , i n t e n t ( i n ) : : c1 , c216 t y p e ( c i r c l e T y p e ) : : c s17 c s%r a d i u s = c1%r a d i u s + c2%r a d i u s18 end f u n c t i o n c i r c l e s u m19

20 end module geometryReuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2

Seminar Series on High Performance Computing 32/ 36

Page 43: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Example: Operator Overloading

1 program c i r c l e s u m2 use geometry3 i m p l i c i t none4

5 t y p e ( c i r c l e T y p e ) : : c1 , c2 , c36

7 c1%r a d i u s = 3 . 08 c2%r a d i u s = 5 . 09

10 c3 = c1 + c211

12 p r i n t %, c3%r a d i u s ! – prints 8.013

14 end module geometry

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 33

/ 36

Page 44: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Best Practices in Fortran Programming

Take advantage of free format

Use spaces, indentations, line breaks as necessary for better readabilityUse comments as necessary

Use implicit none to avoid implicit declaration

Put all subroutines in modules to get explicit interface checking

Write module-oriented (modular) code:

Put related subroutines, data types, into a moduleUse module for access control, information hidingCreate one file per moduleBreak up modules as needed, to manage complexity and to get scalableprogramming

Use module variables and common blocks judiciously

module variables are globalnot thread-safe by default

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 34

/ 36

Page 45: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Best Practices in Fortran Programming

Take advantage of free format

Use spaces, indentations, line breaks as necessary for better readabilityUse comments as necessary

Use implicit none to avoid implicit declaration

Put all subroutines in modules to get explicit interface checking

Write module-oriented (modular) code:

Put related subroutines, data types, into a moduleUse module for access control, information hidingCreate one file per moduleBreak up modules as needed, to manage complexity and to get scalableprogramming

Use module variables and common blocks judiciously

module variables are globalnot thread-safe by default

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 34

/ 36

Page 46: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Best Practices in Fortran Programming

Take advantage of free format

Use spaces, indentations, line breaks as necessary for better readabilityUse comments as necessary

Use implicit none to avoid implicit declaration

Put all subroutines in modules to get explicit interface checking

Write module-oriented (modular) code:

Put related subroutines, data types, into a moduleUse module for access control, information hidingCreate one file per moduleBreak up modules as needed, to manage complexity and to get scalableprogramming

Use module variables and common blocks judiciously

module variables are globalnot thread-safe by default

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 34

/ 36

Page 47: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Best Practices in Fortran Programming

Take advantage of free format

Use spaces, indentations, line breaks as necessary for better readabilityUse comments as necessary

Use implicit none to avoid implicit declaration

Put all subroutines in modules to get explicit interface checking

Write module-oriented (modular) code:

Put related subroutines, data types, into a moduleUse module for access control, information hidingCreate one file per moduleBreak up modules as needed, to manage complexity and to get scalableprogramming

Use module variables and common blocks judiciously

module variables are globalnot thread-safe by default

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 34

/ 36

Page 48: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Best Practices in Fortran Programming

Take advantage of free format

Use spaces, indentations, line breaks as necessary for better readabilityUse comments as necessary

Use implicit none to avoid implicit declaration

Put all subroutines in modules to get explicit interface checking

Write module-oriented (modular) code:

Put related subroutines, data types, into a moduleUse module for access control, information hidingCreate one file per moduleBreak up modules as needed, to manage complexity and to get scalableprogramming

Use module variables and common blocks judiciously

module variables are globalnot thread-safe by default

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 34

/ 36

Page 49: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Best Practices in Fortran Programming

Use derived-type to organize your data

One derived-type (with its type-bound procedures) and relatedmethods per module: a class (object and methods)

Use intent for subroutine arguments

Function should not have side-effects: pure function

Use portable precision model with kind

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 35

/ 36

Page 50: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Best Practices in Fortran Programming

Use derived-type to organize your data

One derived-type (with its type-bound procedures) and relatedmethods per module: a class (object and methods)

Use intent for subroutine arguments

Function should not have side-effects: pure function

Use portable precision model with kind

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 35

/ 36

Page 51: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Best Practices in Fortran Programming

Use derived-type to organize your data

One derived-type (with its type-bound procedures) and relatedmethods per module: a class (object and methods)

Use intent for subroutine arguments

Function should not have side-effects: pure function

Use portable precision model with kind

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 35

/ 36

Page 52: Introduction to Fortran: Part 2 Fortran Features Program organization and style: free format modules, sub-module implict none portable precision model Fortran 90/95 OO features:

Modern Fortran Features (Recap)

Program organization and style:

free format

modules, sub-module

implict none

portable precision model

Fortran 90/95 OO features:

derived type

array operations, dynamicallocation

procedure and operatoroverloading (staticpolymorphism)

Fortran pointer

Fortran 2003 adds more:

type-bound procedures withpass attribute

procedure pointers

Type-bound procedure by name

final procedure, automaticdeallocation (garbage collection)

type extension, abstract type(templating)

polymorphic entities

procedure overriding, deferredbinding (dynamicpolymorphism)

Reuben D. Budiardja (National Institute for Computational Sciences)Introduction to Fortran: Part 2Seminar Series on High Performance Computing 36

/ 36