Professional Documents
Culture Documents
Fortran Class PDF
Fortran Class PDF
Lars Koesterke
2
Modern Programming Languages: Fortran90/95/2003/2008
Outline
Outline
• Motivation
• Modern Fortran
3
Modern Programming Languages: Fortran90/95/2003/2008
Motivation
5
Modern Programming Languages: Fortran90/95/2003/2008
Motivation
7
Modern Programming Languages: Fortran90/95/2003/2008
Motivation
9
Modern Programming Languages: Fortran90/95/2003/2008
Motivation
11
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Overview
13
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Style
Free Format
• Statement may start at the first column (0−132 characters)
• Exclamation mark (!) starts a comment (not in literal strings)
• Blanks are significant: Not allowed in keywords or variables
• Continuation with an ampersand (&) as the last character
• Mulitple statements in one line separated by a semicolon (;)
Style example
program style
print *, ’This statement starts in column 1’
i = 5; j = 7 ! Two statements in one line
! Comment with an exclamation mark
i = & ! Line with continuation
j * j + j
end
15
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Style
Good Bad
program square program square
! This program calculates ... x=5.
x2=x*x
implicit none if(x.eq.13)print*,’Lucky’
real :: x, x2 end
x = 5.
x2 = x * x
if (x == 13.) print *, ’Lucky’
end
17
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Style
Good Bad
program square program square
! This program calculates ... do 100 i=1,20
x=i
implicit none x2=x*x
integer :: i if(x.eq.13)print*,...
real :: x, x2 100 continue
end
do i=1, 20
x = real(i)
x2 = x * x
if (x == 13.) print *, Lucky
enddo
end
19
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Style
Attributes
Style example
program style
integer :: i, j
real :: x
real, parameter :: pi = 3.1415
real, dimension(100) :: array
real, dimension(:,:), allocatable :: dyn_array_2d
• General form
integer :: name
real, <attributes> :: name
• attributes are:
parameter, dimension, allocatable, intent, pointer, target, optional,
private, public, value, bind, etc.
21
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Style
Implicit none
23
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Style
while-Loop repeat-Loop
i = 0 i = 0
do do
if (i > 20) exit i = i + 1
i = i + 1 if (i > 20) exit
enddo enddo
25
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Style
27
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Style
29
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Style
Case
• case takes ranges (or one
integer :: temp_c
! Temperature in Celsius! element)
select case (temp_c) • works also with characters
case (:-1) • read the fine-print
write (*,*) ’Below freezing’
case (0)
write (*,*) ’Freezing point’
case (1:20)
write (*,*) ’It is cool’
case (21:33)
write (*,*) ’It is warm’
case (34:)
write (*,*) ’This is Texas!’
end select
31
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Style
33
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Style
• real*8, real*4: works well with MPI Real8 and MPI Real4
35
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Style
37
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Fixing the Flaws
39
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Fixing the Flaws
Allocatable Arrays
• Variables live on the heap (vs. stack for scalars and static arrays)
• Declaration and allocation in 2 steps
• Declare an array as allocatable,
use colons (:) as placeholders
• allocate/deallocate in the executable part
• Allocation takes time. Do not allocate too often.
program alloc_array
real, dimension(:), allocatable :: x_1d ! Attribute
real, dimension(:,:), allocatable :: x_2d ! allocatable
...
read n, m
allocate(x_1d(n), x_2d(n,m), stat=ierror) ! Check the
if (ierror /= 0) stop ’error’ ! error status!
...
deallocate(x) ! optional
41
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Fixing the Flaws
program struct
type my_struct ! Declaration of a Derived Type
integer :: i
real :: r
real*8 :: r8
real, dimension(100,100) :: array_s ! stack
real, dimension(:), allocatable :: array_h ! heap
type(other_struct), dimension(5) :: os ! structure
end type my_struct
43
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Fixing the Flaws
Declaration of a Structure
Variables of Derived Type
program struct
type my_struct ! Declaration of a Derived Type
...
end type my_struct
45
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Fixing the Flaws
Example: Structures
47
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
49
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
51
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
module mad_science
real, parameter :: pi = 3. &
c = 3.e8 &
e = 2.7
real :: r
end module mad_science
program go_mad
! make the content of module available
use mad_science
r = 2.
print *, ’Area = ’, pi * r**2
end program
53
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
module mad_science
real, parameter :: pi = 3. &
c = 3.e8 &
e = 2.7
real :: r
type scientist
character(len=10) :: name
logical :: mad
real :: height
end type scientist
end module mad_science
55
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
57
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
59
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
61
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
63
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
65
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
67
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
69
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
Passing an array
• Traditional scheme: Shapes of the actual and the dummy array
(may) have to agree
integer, parameter :: n = 100
real, dimension(n) :: x
call sub(x, n)
subroutine sub(y, m)
integer :: m
real, dimension(m) :: y
71
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
module my_module
contains
subroutine sub(x)
real, dimension(:) :: x
print *, size(x) ! prints 100
73
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
subroutine swap(a, b)
real, dimension(:) :: a, b
real, dimension(size(a)) :: work ! Scratch array
! work is an automatic array on the Stack
work = a ! uses Array syntax
a = b ! Inquire with
b = work ! lbound, ubound
end subroutine swap ! shape, size
75
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
77
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
Optional Arguments
• Optional arguments require an explicit interface
• Optional arguments may not be changed, if they are not passed
module my_module subroutine upper_level
subroutine calc(a, b, c, d) use my_module
real :: a, b, c call calc( 1., 2., 3., 4.)
real, optional :: d call calc( 1., 2., 3.)
real :: start call calc(a=1., b=2., c=3., d=4.)
if (present(d)) then
start = d call calc(b=2., d=4., a=1., c=3.)
d = d_new call calc( 1., 2., 3., d=4.)
else call calc( 1., 2., d=4., c=3)
start = 0.
endif
79
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Module-oriented Programming
Optional Arguments
• Optional arguments require an explicit interface
• Optional arguments may not be changed, if they are not passed
module my_module subroutine upper_level
subroutine calc(a, b, c, d) use my_module
real :: a, b, c call calc( 1., 2., 3., 4.)
real, optional :: d call calc( 1., 2., 3.)
real :: start call calc(a=1., b=2., c=3., d=4.)
if (present(d)) then
start = d call calc(b=2., d=4., a=1., c=3.)
d = d_new call calc( 1., 2., 3., d=4.)
else call calc( 1., 2., d=4., c=3)
start = 0.
endif
BREAK!
79
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Complaints Department
81
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Complaints Department
83
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Complaints Department
83
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Complaints Department
No, but you have to learn (sooner or later) how to write module-oriented
code.
Writing Object-Oriented code for access control is actually pretty nice!
If you problem/algorithm requires, you may add Object-Oriented code
exploiting Polymorphism (supported in Fortran2003 & 2008).
Learn later, how to write Object-Oriented code in Fortran without
performance penalty; Access control only.
85
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Complaints Department
87
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Complaints Department
89
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Complaints Department
91
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Complaints Department
93
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
Formula Tranlation
• Array syntax
• where construct
• forall construct
• Case study: Stencil Update
• User defined Operators
• Elemental Functions
• Inquiry Functions
• Odds and Ends
95
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
97
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
Array constructor
99
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
type person
real :: age
character :: name
integer :: ssn
end type person
type(person) :: you
101
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
Arrays as Indices
103
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
where statement
105
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
where statement
107
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
any statement
109
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
real, dimension(n) :: v
real :: t1, t2
...
t2 = v(1)
do i=2, n-1
t1 = v(i)
v(i) = 0.5 * (t2 + v(i+1))
t2 = t1
enddo
111
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
real, dimension(n) :: v
real :: t1, t2
...
t2 = v(1)
do i=2, n-1
t1 = v(i)
v(i) = 0.5 * (t2 + v(i+1))
t2 = t1
enddo
111
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
real, dimension(n,n) :: a, b
do j=2, n-1
do i=2, n-1
b(i,j) = 0.25 *
(a(i-1,j) + a(i+1,j) + a(i,j-1) + a(i,j+1))
enddo
enddo
do j=2, n-1
do i=2, n-1
a(i,j) = b(i,j)
enddo
enddo
113
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
a(2:n-1,2:n-1) = 0.25 *
(a(1:n-2,2:n) + a(3:n,2:n) + a(2:n,1:n-2) + a(2:n,3:n))
115
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
a(2:n-1,2:n-1) = 0.25 *
(a(1:n-2,2:n) + a(3:n,2:n) + a(2:n,1:n-2) + a(2:n,3:n))
115
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
• Any subprogram that calls swap has to use the module my interfaces
117
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
119
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
121
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
123
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
125
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
User-defined Operators
module operator program op
public :: operator(.lpl.) use operator
private :: log plus log print *, 2. .lpl. 3.
interface operator(.lpl.) end program
module procedure log plus log
end interface
contains • prints 1.791759
function log plus log(x, y) & • .lpl. is the new operator
result(lpl result) (defined public)
real, intent(in) :: x, y • rest of the definition is private
real :: lpl result – interface
lpl_result = log(x) + log(y) – function log plus log
end function • .lpl. is defined as
end module log(x) + log(y)
• log(2.) + log(3.) = 1.791759
127
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
Elemental Functions
module e_fct program example
elemental function sqr(x) & use e_fct
result(sqr_result) real :: x = 1.5
real, intent(in) :: x real, dimension(2) :: a = &
real :: sqr_result [ 2.5, 3.5 ]
sqr_result = x * x print *, ’x = ’, sqr(x)
end function print *, ’a = ’, sqr(a)
end module end program
129
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
131
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
Inquiry Functions
• digits(x): numer of significant digits
• epsilon(x): smallest with 1 + <> 1
• huge(x): largest number
• maxexponent/minexponent: largest/smallest exponent
• tiny(x): smallest positive number (that is not 0.)
• ubound, lbound, size, shape, ...
• input unit, output unit, error unit
• file storage size (Good when you use the Intel compiler!)
• character storage size, numeric storage size
• etc.
133
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Formula Translation
Mathematical Functions
• sin, cos, tan, etc.
• New in Fortran 2008: Bessel fct., Error-fct., Gamma-fct., etc.
135
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Odds and Ends
137
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Odds and Ends
• Pointers col and row are pointing to a colum/row of the 2-dim array
data
• Memory is not contigous for row
• When you pass row to a subroutine, a copy-in/copy-out may be
necessary
• What is ’=>’ good for? Referencing and de-referencing is
automatic, so a special symbol is needed for pointing
139
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Odds and Ends
141
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Odds and Ends
143
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Odds and Ends
Environment Variables
character(len=16) :: value
call get_environment_variable(’SHELL’, value)
print value ! prints /bin/bash
145
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Odds and Ends
Fortran Prepocessor
• same as in C (#ifdef, #ifndef, #else, #endif)
• compile with -fpp
• use option -D<variable> to set variable to true
• Example: ifort -Dmacro t.f
#ifdef macro
x = y
#else
x = z
#endif
147
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Odds and Ends
149
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Odds and Ends
151
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Odds and Ends
153
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
Odds and Ends
Not Covered
• Floating-point Exception Handling
• Linked-Lists, Binary Trees
• Recursion
• I/O (Stream Data Access)
• Object-Oriented Programming, but see introduction in the next
chapter
155
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
History
History of Fortran
1986 1990 1990 1991 1991 1993 1994 1995 1996 1996 1997 1997 2000 2001 2001 2003 2003 2004
For more than half of the fifty years computer programmers have been This timeline includes fifty of the more than 2500 documented
writing code, O’Reilly has provided developers with comprehensive, programming languages. It is based on an original diagram created
in-depth technical information. We’ve kept pace with rapidly changing by Éric Lévénez (www.levenez.com), augmented with suggestions
www.oreilly.com technologies as new languages have emerged, developed, and
matured. Whether you want to learn something new or need
from O’Reilly authors, friends, and conference attendees.
For information and discussion on this poster,
answers to tough technical questions, you’ll find what you need
go to www.oreilly.com/go/languageposter.
in O’Reilly books and on the O’Reilly Network.
©2004 O’Reilly Media, Inc. O’Reilly logo is a registered trademark of O’Reilly Media, Inc. All other trademarks are property of their respective owners. part#30417
157
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
History
159
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
The Future
161
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
The Future
163
Modern Programming Languages: Fortran90/95/2003/2008
Modern Fortran
The Future
Book Recommendations
• Fortran 95/2003 for Scientists and Engineers by Chapman
Very! verbose, with many examples. Guides the programmer nicely
towards a good programming style. (International/cheaper edition
available)
• modern fortran explained by Metcalf, Reid and Cohen
Good to learn new features; more complete than the Guide (1), but
sometimes a bit confusing. Covers Fortran 2008
• Guide to Fortran 2003 Programming by Walter S. Brainerd
Good to learn the new features, clever examples
• The Fortran 2003 Handbook by Adams, Brainerd, et al.
Complete syntax and Reference
Some Guidance is definitely needed
• The same task may be accomplished in several ways
• What to use When?
165
Modern Programming Languages: Fortran90/95/2003/2008
Object-Oriented Programming: (Very) Short Version
OO Concept in 1 Slide
• Objects contain (have the properties):
Data — Instance Variables or Fields
Subr./Fct. — Instance Methods
Polymorphism/Inheritance — to allow for a lot of flexibility
• Data is only accessible through the methods
• OO-speak: Call of a Subr. (instance method) ≡ Sending a Message
• A Class is a blueprint for a Object
Similar to a Derived Type being a blueprint for a structure
type(data) :: structure containing variables
class(data plus fct) :: object containing variables and functions
167
Modern Programming Languages: Fortran90/95/2003/2008
Object-Oriented Programming: (Very) Short Version
169
Modern Programming Languages: Fortran90/95/2003/2008
Object-Oriented Programming: (Very) Short Version
allocate(x, y) Note:
x%set called with 2 arguments,
call x%set(’J. Doe’, 25) but Subroutine has 3 arguments
call x%out ! or call y%out
end
171
Modern Programming Languages: Fortran90/95/2003/2008
Object-Oriented Programming: (Very) Short Version
173