Академический Документы
Профессиональный Документы
Культура Документы
History of Fortran
Variables and their assignment
Control structures
1 2
Well suited for numerical computations John W. Backus et al (1954): The IBM Mathematical
Likely over 50% of scientific applications are written in Formula Translating System
Fortran Early years development: Fortran II (1958), Fortran IV
Fast code (compilers can optimize well) (1961), Fortran 66 & Basic Fortran (1966)
Handy array data types Fortran 77 (1978)
Fortran 90 (1991) a major revision and Fortran 95 (1997)
Clarity of code
a minor revision to it
Portability of code
Optimized numerical libraries available
3 4
Short history of Fortran cont. Look and feel
PROGRAM square_root_example
Fortran 2003 (2004): a major revision, adding e.g. object- ! Comments start with an exclamation point.
oriented features, C-bindings ! You will find data type declarations, couple arithmetic operations
! and an interface that will ask a value for these computations.
Fortran 95/2003 is the current de facto standard IMPLICIT NONE
REAL :: x, y
The latest standard is Fortran 2008 (2010): a more minor INTRINSIC SQRT ! Fortran standard provides many commonly used functions
upgrade compared to F03 ! Command line interface. Ask a number and read it in
WRITE (*,*) 'Give a value (number) for x:'
Most notable addition: Fortran coarray (CAF) syntax READ (*,*) x
Compiler support nearly complete y = x**2+1 ! Power function and addition arithmetic
5 6
9 10
11 12
Control structures: DO loops Control structures: DO loops
INTEGER :: i, stepsize, NumberOfPoints REAL :: x, totalsum, eps
INTEGER, PARAMETER :: max_points=100000 totalsum = 0.0
REAL :: x_coodinate(max_points), x, totalsum
! DO loop without loop control
! DO loop with an integer counter (count controlled) DO
stepsize = 2 READ(*,*) x
DO i = 1, NumberOfPoints, stepsize IF (x < 0) THEN
x_coordinate(i) = i*stepsize*0.05
END DO EXIT ! exit the loop
! Condition controlled loop (DO WHILE) ELSE IF (x > upperlimit) THEN
totalsum = 0.0 CYCLE ! do not execute any further statements but
READ(*,*) x ! cycle back to the beginning of the loop
DO WHILE (x > 0) END IF
totalsum = totalsum + x totalsum = totalsum + x
READ(*,*) x END DO
END DO
13 14
Summary
19
Outline
Structured programming
Modules
Procedures: functions and subroutines
Interfaces
Procedure arguments
20 21
Structured programming based on program sub-units Modularity means dividing a program into minimally
(functions, subroutines and modules) enables dependent modules
Testing and debugging separately Enables division of the program into smaller self-contained
Re-use of code units
Improved readability Fortran modules enable
Re-occurring tasks Global definitions of procedures, variables and constants
Compilation-time error checking
The key to success is in well defined data structures and
scoping, which lead to clean procedure interfaces Hiding implementation details
Grouping routines and data structures
Defining generic procedures and custom operators
22 23
What are procedures? Procedure declarations
24 25
26 27
INTENT keyword Procedure types
SUBROUTINE foo(x, y, z) Declares how formal argument There are four procedure types in Fortran 90: intrinsic,
IMPLICIT NONE
is intended to be used for external, internal and module procedures
REAL, INTENT(IN) :: x
REAL, INTENT(INOUT) :: y transferring a value
REAL, INTENT(OUT) :: z IN: the value of the Procedure types differ in
x = 10 ! Compilation error
argument is read/only ie. Scoping, i.e. what data and other procedures a procedure
y = 10 ! Correct cannot be changed can access
z = y * x ! Correct OUT: the value of the
END SUBROUTINE foo
argument must be provided
Interface type, explicit or implicit
INOUT (the default) Compiler can check the argument types of the at compile
Compiler uses INTENT for error time only if the interface is explicit
checking and optimization
28 29
30 31
Visibility of module objects Internal procedures
Variables and procedures in modules can be PRIVATE or Each program unit (program/subroutine/function) may
PUBLIC contain internal procedures
PUBLIC = visible for all program units using the module
(the default) SUBROUTINE mySubroutine
...
PRIVATE will hide the objects from other program units CALL myInternalSubroutine
...
REAL :: x, y CONTAINS
PRIVATE :: x SUBROUTINE myInternalSubroutine
PUBLIC :: y ...
! Or END SUBROUTINE myInternalSubroutine
REAL, PRIVATE :: x END SUBROUTINE mySubroutine
REAL, PUBLIC :: y
32 33
34 35
External procedures Interfaces
36 37
38 39
Global data and global variables Program units
Global variables can be accessed from any program unit Main program Modules
F90 module variables provide controllable way to define USE, explicit interface
and use global variables
MODULE commons
INTEGER, PARAMETER :: r = 0.42
INTEGER, SAVE :: n, ntot External
REAL, SAVE :: abstol, reltol External, implicit interface
procedures
END MODULE commons
Summary
42
Outline
Fortran arrays
43 44
45 46
Arrays in modern Fortran Data layout in multi-dimensional arrays
INTEGER :: m = 3, n = 4, i, j Array syntax in modern Always increment the left-most index of multi-
REAL :: A(m,n), x(n), y(m)
Fortran enables a neat dimensional arrays in the innermost loop (i.e. fastest)
! Array syntax way to express
y=0.0 Column major ordering in Fortran vs. Row major
do j = 1, n mathematical operations ordering in C
y(:) = y(:) + A(:,j)*x(j)
end do A compiler (with sufficient optimization flags) may re-
= =
! Or, equivalently with explicit order loops automatically
=1
! loops 1
y=0.0 1 11 12 13 14
2 do i=1,N do j=1,M
do j = 1, n 2 = 21 22 23 24
3 do j=1,M do i=1,N
do i = 1, m 3 31 32 33 34
4 y(i) = y(i)+ a(i,j)*x(j) y(i) = y(i)+ a(i,j)*x(j)
y(i) = y(i) + A(i,j)*x(j)
end do end do
end do
end do end do
end do
47 48
49 50
Array initialization with array constructors Arrays sections
! Set elements from 3 to N+8 to 0 Fortran array syntax
INTEGER :: idx(0:10) Sub_Vector(3:N+8)=0
enables accessing a part
! using the array constructor (//) ! Access a subblock of a matrix of an array in an intuitive
idx(0:10) = (/ 0, (i, i = 1, 3), (0, j = 4, 10) /) A(2:500,3:300:3) = 4.0
way: array sections
! or equivalently (F03) ! Set every third element from 1 to
idx(0:10) = [0, (i, i = 1, 3), (0, j = 4, 10)] ! 3*N+1 to 1
Every_Third(1:3*N+1:3) = 1
! these can be used in setting variable values upon declaration ! Access subcube of a 3D array
INTEGER :: values(3) = [11, 22, 33] pixel_3D(128:150,56:80,1:256:8)=32000
51 52
53 54
Summary
55
Outline
56 57
Array intrinsic functions are built-in functions which can The most commonly used array intrinsic functions are:
apply various operations on the whole array at once, i.e., SIZE, SHAPE, COUNT, SUM
not just individual array elements ANY, ALL
As a result another array or just a scalar value is returned MINVAL, MAXVAL , MINLOC, MAXLOC
A subset selection through masking is also possible RESHAPE
Masking and use of array (intrinsic) functions is often DOT_PRODUCT, MATMUL, TRANSPOSE
accompanied with use of FORALL and WHERE array PACK, UNPACK, SPREAD
statements
Operations are performed for those elements where
corresponding elements of the mask are .TRUE.
58 59
Array intrinsic functions:
Array intrinsic functions: ANY, ALL
SIZE, SHAPE, COUNT, SUM
SIZE(array [, dim]) returns # of elements in the ANY(L_array [, dim]) returns a scalar value of
array [, along the specified dimension] .TRUE. if any value in L_array is .TRUE.
SHAPE(array) returns an INTEGER vector containing the
size of array in each dimension ALL(L_array [, dim]) returns a scalar value of
COUNT(L_array [,dim]) returns count of elements .TRUE. if all values in L_array are .TRUE.
which are .TRUE. in L_array
SUM(array[, dim][, mask]) sum of the elements of
array [, along dimension] [, under mask]
60 61
MAXVAL is the same as MINVAL, but returns the maximum print *, size(x), size(v) ! prints m * n, n
print *, shape(x) ! prints m, n
value of a given array print *, size(shape(x)) ! prints 2
print *, count(x >= 0)
MINLOC(array [, mask]) returns a vector of location(s) print *, sum(x, dim=2, mask=x < 0.5) ! the result is a vector !!
[, under mask], where the minimum value(s) is/are
v(1:n) = [ (j, j=1,n) ]
found print *, any(v > -1 .and. v < 1)
print *, all(x >= 0, dim=1)
MAXLOC similar to MINLOC, but for maximums print *, minval(v), maxval(v)
print *, minloc(v), maxloc(v)
62 63
Array intrinsic functions: RESHAPE Array intrinsic functions:
DOT_PRODUCT, MATMUL, TRANSPOSE
RESHAPE(array, shape) returns a reconstructed (=a DOT_PRODUCT(a_vec, b_vec) returns a scalar dot
copy of an) array with different shape than in the product of two vectors
input array MATMUL(a_mat, b_mat) returns a matrix containing
Can be used as a single line statement to initialize an array matrix multiply of two matrices
(sometimes at the expense of readability) TRANSPOSE(a_mat) returns a transposed matrix of the
input matrix
integer :: a(6), b(3,2), i
a = [(i, i=1,6)]
b = reshape(a, [3,2])
64 65
66 67
Array intrinsic functions: example Dynamic memory allocation
integer :: j, ix(5) integer :: j Fortran provides two different mechanisms to
ix(:) = (/ (j, j=1, size(ix)) /) real :: a(100,100), b(100), c(100)
dynamically allocate memory for arrays:
where (ix == 0) ix = -9999 ! fill in diagonal matrix 1. Array variable declaration has an ALLOCATABLE (or a
forall (j=1:100) a(j,j) = b(j) POINTER) attribute, and memory is allocated through
where (ix < 0)
ix = -ix ! fill in lower bi-diagonal matrix
ALLOCATE statement
elsewhere forall (j=2:100) a(j,j-1) = c(j) 2. Array variable, which is declared in the procedure with
ix = 0 (runtime) size information coming from the procedures
end where
argument list or from a Fortran MODULE, is called an
automatic array: no ALLOCATE is needed
68 69
deallocate(idx , mat)
70 71
Pointers to arrays Pointers to arrays
The POINTER attribute enables to create array (or scalar) A POINTER can refer to an already allocated memory
aliasing variables region
Initialized to point to nothing
Pointer variables are usually employed to refer to
integer, pointer :: p_x(:) => null()
another array or array section integer, target :: x(1000)
...
A pointer variable can also be a sole variable itself, but p_x => x Pointers provide a neat way for array
sections
requires ALLOCATE p_x => x(2 : 300)
p_x => x(1 : 1000 : 5)
Not recommended; the ALLOCATABLE attribute and ...
p_x(1) = 0
employ POINTER variables for aliasing only in stead nullify(p_x) This would also change x(1) to 0
Note for C programmers: a "pointer" has a slightly
different meaning in C and Fortran Disconnects p_x from x
72 73
74 75
Outline
76 77
78 79
Output formatting: miscellaneous The I0 and G0 format descriptors
With complex numbers provide format for both real and Dynamic sizing of REAL and INTEGER valued output
imaginary parts: I0 appeared in F03 and G0 was introduced in F08
complex :: z
write (*,'(f6.3,2x,f6.3)') z ! real & imaginary parts
Output fields are left justified with all the unnecessary
leading blanks (and precision for REAL valued variables)
Line break, whitespace, tabbing:
removed
write (*,'(f6.3,/,f6.3)') x, y ! linebreak between x,y
integer :: i = 12345
write (*,'(i3,2x,f6.3)') i, x ! 2 spaces between i & x real (kind=4) :: sp = 1.23e0
write (*,'(i5,t2,i5)') i, j ! 2 tabs between i & j real (kind=8) :: dp = 1.234567890d0
It is possible that an edit descriptor will be repeated a write(*,fmt='("<i=",i0,", reals=",2(g0,1x),">")') i,sp,dp
80 81
Fortran provides several intrinsic functions for handling Often it is necessary to filter out data from a given
character strings, such as character string
trim(string) - removes blank spaces from the end of Or to pack values into a character string
string Fortran internal I/O with READ & WRITE becomes
adjustl(string)/adjustr(string) - moves blank now handy
spaces from the beginning/end of the string to the
Actual files are not involved at all
end/beginning of it
len(string) - length of a string
index(string, substring) - returns the starting
position of a substring within a string
82 83
Internal I/O: examples Opening and closing files: basic concepts
character(len=13) :: cl1
character(len=60) :: cl2
Writing to or reading from a file is similar to writing onto
integer :: njobs, istep a terminal screen or reading from a keyboard
! extract a number from character string Differences
cl1 = 'time step# 10'
read(cl1,fmt='(10x,i3)') istep File must be opened with an OPEN statement, in which the
unit number and (optionally) the file name are given
! write data to a character string
njobs = 2014 Subsequent writes (or reads) must to refer to the given
write(cl2,'(a,i0)') 'the number of jobs completed = ', njobs unit number
File should be closed at the end
84 85
The syntax is (the brackets [ ] indicate optional keywords The first parameter is the unit number
or arguments) The keyword unit= can be omitted
open([unit=]iu, file='name' [, options]) The unit numbers 0, 5 and 6 are predefined
close([unit=]iu [, options])
0 is output for standard (system) error messages
For example
5 is for standard (user) input
open(10, file= 'output.dat', status='new')
close(unit=10, status='keep') 6 is for standard (user) output
These units are opened by default and should not be re-
opened nor closed by the user
86 87
Opening and closing a file File opening options
The default input/output unit can be referred with a star: status: existence of a file
write(*, ...) 'old', 'new', 'replace', 'scratch', 'unknown'
read(*, ...)
position: offset, where to start writing
Note that these are not necessarily the same as the stdout
and stdin unit numbers 6 and 5 'append'
If the file name is omitted in the OPEN, the file name will action: file operation mode
based on unit number being opened, e.g. for unit=12 'write', 'read', 'readwrite'
this usually means the filename fort.12 (on UNIX- form: text or binary file
systems) 'formatted', 'unformatted'
88 89
access: direct or sequential file access Use inquire statement to find out information about
'direct', 'sequential', 'stream', file existence
iostat: error indicator, (output) integer file unit open status
non-zero only upon an error various file attributes
err: the fortran label number to jump upon an error The syntax has two forms, one based on file name, the
recl: record length, (input) integer other for unit number
inquire(file='name', options ...)
for direct access files only
inquire(unit=iu, options ...)
warning (check): may be in bytes or words
90 91
File opening: file properties File opening: file properties example
! check the existence of a file
exist does file exist ? (logical)
logical :: file_exist
opened is file / unit opened ? (logical) inquire (file='foo.dat', exist=file_exist)
if (.not. file_exist) then
form 'formatted' or 'unformatted' (char) write(*,*) 'the file does not exist'
access 'sequential' or 'direct' or 'stream' (char) else
! do something with the file foo.dat
action 'read', 'write', 'readwrite' (char) endif
92 93
94 95
Unformatted I/O Stream I/O
A binary file write adds extra record delimiters (hidden
Write to a sequential binary file from programmer) to the beginning and end of records
real :: rval
character(len=60) :: string In Fortran 2003 using access method 'stream' avoids this
open(10, file='foo.dat', form='unformatted') and implements a C-like approach
write(10) rval
write(10) string It is recommended to use stream I/O
close(10)
Create a stream (binary) file
No format descriptors allowed real :: dbheader(20), dbdata(300)
Reading similarly open(10,file='my_database.dat', access='stream')
write(10) dbheader
read(10) rval write(10) dbdata
read(10) string close(10)
Reading similarly
96 97
Summary
Input/Output formatting
Internal I/O
Files: communication between a program and the
outside world
Opening and closing a file
Data reading & writing
Use unformatted (stream) I/O for all except text files
98
Outline
99 100
Standard Fortran already supports a wide variety of The variable representation method (precision) may be
fundamental data types to represent integers, floating declared using the KIND statement
point numbers ("real"), truth values ("logical") and ! SELECTED_INT_KIND(r) Integer between -10r < n < 10r
variable length character strings ! SELECTED_REAL_KIND(p)
! SELECTED_REAL_KIND(p,r) Real number accurate up to p decimals
In addition each of these built-in types can be declared INTEGER, PARAMETER :: short=SELECTED_INT_KIND(4)
as (multi-dimensional) arrays INTEGER, PARAMETER :: double=SELECTED_REAL_KIND(12,100)
INTEGER (KIND=short) :: index
Reals and integers can be declared to consume less REAL (KIND=double) :: x, y, z
COMPLEX (KIND=double) :: c
memory in expense of reduced numerical precision A floating point (real) number between
through kind parameter x=1.0_double; y=2.0_double * ACOS(x) -10100 < x < 10100, accurate up to 12
decimals
101 102
Numerical precision: example Fortran 2008 ISO_FORTRAN_ENV
PROGRAM Precision_Test MODULE prec
IMPLICIT NONE USE ISO_FORTRAN_ENV, ONLY: INT32, INT64, REAL32, REAL64
IMPLICIT NONE
INTEGER, PARAMETER :: sp = SELECTED_REAL_KIND(6,30), &
dp = SELECTED_REAL_KIND(10,200) PRIVATE
REAL(KIND=sp) :: a INTERGER, PARAMETER :: i4 = INT32 &
REAL(KIND=dp) :: b i8 = INT64 &
WRITE(*,*) sp, dp, KIND(1.0), KIND(1.0_dp) r4 = REAL32 &
WRITE(*,*) KIND(a), HUGE(a), TINY(a), RANGE(a), PRECISION(a) r8 = REAL64
WRITE(*,*) KIND(b), HUGE(b), TINY(b), RANGE(b), PRECISION(b)
PUBLIC :: i4, i8, r4, r8
END PROGRAM Precision_Test END MODULE prec
Program output:
4 8 4 8
4 3.4028235E+38 1.1754944E-38 37 6
8 1.797693134862316E+308 2.225073858507201E-308 307 15
103 104
107 108
109 110
Visibility of derived data types Summary
When declared in the same programming unit derived Derived data types enables grouping of data to form
data types are visible to that unit only logical objects
and sub-units under CONTAINS statement A Fortran program becomes more readable and modular
When declared in a module unit, a derived data type can with sensible use of derived data types
be accessed outside the module through USE statement Handling of more complicated data structures becomes
The visibility can be controlled in more detailed level more manageable with use of derived types
using PRIVATE attribute Enables the use of object oriented programming
concepts
111 112
Outline
113 114
Parameters to a program are very often given to Access separate command line arguments
programs as command line arguments get_command_argument(number[,value][,length][,status])
Input file(s), modify program behavior, etc. number is of type integer and denotes which argument to
get
Fortran 2003 has a standardized method for reading
command line arguments value is of type character string and contains the value of
the requested argument on return (optional)
get_command_argument and
length is of type integer and contains the length of the
command_argument_count
requested argument on return (optional)
To access the whole command line, use
status is of type integer. On successful return status is
get_command 0, -1 if value was too short to contain actual argument and
1 if argument could not be returned (optional)
115 116
Command line arguments Command line input
117 118
Access the whole command line Besides command line arguments, environment variables
call get_command(command[,length][,status]) are a common way to modify program behaviour
command is of type character string and contains the value Fortran 2003 has a standardized method for accessing
of the command line on return. values of environment variables
length is of type integer and contains the length of the
command line on return (optional)
status is of type integer. On successful return status is
0, -1 if value was too short to contain actual argument and
1 if argument could not be returned (optional)
119 120
Environment variables Environment variables: example
program environment
Access a value of an environment variable implicit none
call get_environment_variable(name,value[,length] character(len=256) :: enval
[,status][,trim_name]) integer:: len,stat
name is of type character string and contains the name of the ! extract hostname
requested variable call get_environment_variable('hostname',enval,len,stat)
if (stat == 0) write (*,'(a,a)') 'host=', enval(1:len)
value is of type character string and contains the value of the
requested variable ! extract user
call get_environment_variable('user',enval,len,stat)
length is of type integer and contains the length of the if (stat == 0) write (*,'(a,a)') 'user=', enval(1:len)
requested variable on return (optional) end program environment
status (optional)
trim_name is of type logical and sets if trailing blanks are
allowed in variable names or not (optional)
121 122
125 126
127 128
Function return value as ALLOCATABLE ALLOCATABLE scalars
program test Functions are permitted program test Typically used with
real :: unpacked(1000) character(:), allocatable :: ch
real, allocatable :: a(:) to have an ALLOCATABLE integer :: lench CHARACTER strings,
a = pack_data(unpacked) value lench = len('hello world!') whose length is decided at
call use_packed_data(a) ! explicit allocation
deallocate(a) Functionality similar as allocate(character(lench)::ch) run time
contains ch(:) = 'hello world!'
function pack_data(x) result (pk) with procedure arguments print *,len(ch)
real, allocatable :: pk(:) end program test
real, intent(in) :: x(:)
Particularly useful when a
integer :: npk dynamically sized array is
npk = num_distinct_values(x)
allocate(pk(npk))
returned
call get_dist_vals(x,...,pk,npk)
end function pack_data
end program test
129 130
131 132
Outline
133 134
Fortran 2003 is a major revision over Fortran 95 standard Fortran 2008 upgrades the standard even further
Interaction with the environment Fortran coarray syntax
Interoperability with C Several new intrinsic procedures (bit manipulation etc.)
I/O enhancements Several performance related enhancements
Object-oriented features
Several minor enhancements and clarifications
Support for IEEE floating point arithmetics and exceptions
Several minor enhancements
135 136
Object-oriented programming in Fortran Fortran coarrays
Fortran 2003/2008 supports object-oriented (OO) Parallel processing as part of Fortran language standard
programming Only small changes required to convert existing Fortran code
Abstract interfaces to support a robust and potentially efficient parallelism
Type extensions, polymorphism (single inheritance), A Partitioned Global Address Space (PGAS) paradigm
abstract types Parallelism implemented over distributed shared memory
Type and- object-bound procedures, type operators, type- CAF is potentially massively parallel
bound generics, finalizers
Integrated into Fortran 2008 standard
Object model designed to maintain backwards Compiler support is still incomplete (Cray: excellent, Intel:
compatibility with Fortran 95 moderate, GNU: experimental)
137 138
Adds only a few syntactic alterations and rules to the Upon startup the program gets replicated into a number
Fortran language of copies called images (i.e. processes)
Provides mechanisms to allow The number of images is usually decided at the execution
SPMD (Single Program, Multiple Data) style of explicitly time, but (in rare cases) can also be fixed at compile time
parallel programming Each replica (image) runs asynchronously in a loosely
Data distribution over partitioned memory coupled way until program controlled synchronization
Synchronization primitives Images (local) data are visible within the image only
Memory management for dynamic shared entities except for data declared as special arrays i.e. coarrays
One-sided data communication enables movement of co-
array data across different images of a coarray program
139 140
Look & feel Automatic allocations
PROGRAM DATA Each image executes PROGRAM ALLOC_EXAMPLE Fortran 2008 standard has
INTEGER :: global(3)[*], & INTEGER, ALLOCATABLE :: a(:), b(:)
& local(3), npes concurrently, INTEGER :: i many enhancements to
npes = num_images() synchronization with ALLOCATE(a(10)) the ALLOCATE
! Initialize global data a = [(i, i=1,10)]
global(:) = this_image()*(/1,2,3/) separate calls ! This would fail: b(:) = a(:) Automatic allocation,
SYNC ALL b = a
! Initialize local data by copying Division of data to a = [a, b]
reallocation and
! from image number npes global: visible to all print *, size(a), size(b) deallocation
local(:) = global(:)[npes] END PROGRAM ALLOC_EXAMPLE
END PROGRAM data images, [] operator
local: visible only locally, ! Result: 20 10
() operator
Global data is read/write
accessible to all images
141 142
Summary
143