class11_fortran.pdf
TRANSCRIPT
-
7/29/2019 class11_Fortran.pdf
1/128
Scientific Programming With Modern Fortran
M. D. Jones, Ph.D.
Center for Computational ResearchUniversity at Buffalo
State University of New York
High Performance Computing I, 2012
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 1 / 140
-
7/29/2019 class11_Fortran.pdf
2/128
Part I
Fortran Basic Operations
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 2 / 140
-
7/29/2019 class11_Fortran.pdf
3/128
Introduction History
History of FORTRAN
One of the earliest of the high-level programming languages, FORTRAN(short for Formula Translation) was developed by John Backus at IBMin the 1950s (circa 1953 for the 704, first released in 1957):
FORTRAN 66, ANSI standard (X3.9-1966) based on FORTRAN IV
FORTRAN 77, X3.9-1978, improved I/OFortran 90, ISO/IEC 1539:1991(E), FORTRAN becomes Fortran
Fortran 95, ISO/IEC 1539-1:1997, minor revisions
Fortran 2003, ISO/IEC 1539:2004, command line arguments,
more intrinsics, IEEE exception handling, C interoperabilityFortran 2008, revision to Fortran 2003, to include BIT type andCo-array parallel processing (not yet widely supported)
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 4 / 140
-
7/29/2019 class11_Fortran.pdf
4/128
Introduction So What?
Why Is This Language Still Used?
So why is Fortran still in use?
Efficiency has always been a priority, in the sense that compilersshould produce efficient code. Fortran data are generally not
allowed to be aliased (e.g. pointers) making life easier on thecompiler
Designed from the outset as the tool for numerically intensiveprogramming in science and engineering
Backward compatibility - most modern compilers can still compile
old (as in decades!) code
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 5 / 140
-
7/29/2019 class11_Fortran.pdf
5/128
Introduction So What?
Come On, Fortran is 50 Years Old!
Indeed, Fortran celebrated its 50th birthday in 2007. How many other
high-level languages can you say that about? Think of it this way - it is
arguably the most efficient, compact, portable high-level languageavailable. And you will likely still be able to use programs written todayuntil you retire (unmodified, at that) ...
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 6 / 140
I d i S Wh ?
-
7/29/2019 class11_Fortran.pdf
6/128
Introduction So What?
Some Fortran Compilers
Fortran compilers are easy to find, some are freely available (including
compiler source code):
gfortran, part of the GNU toolchain (gcc/g++), supplanted g77
as of version 4.0
g95, another freebie
open64, open sourced by SGI (derived from their MIPSProcompiler suite), further developed by Intel and the Chinese
Academy of Sciences (Gnu Public License), intended mainly forcompiler research (includes C/C++) on the IA64 platform
ifort, Intels commercial compilerpgf95, PGIs commercial compiler
Other commercial compilers: IBM, Qlogic/Pathscale,Lahey/Fujitsu, Sun, NAGWare, ...
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 7 / 140
I t d ti S Wh t?
-
7/29/2019 class11_Fortran.pdf
7/128
Introduction So What?
The Rest of This Talk
In this presentation I will focus almost entirely on Fortran 90/95features, with some side notes on FORTRAN 77 (mostly for contrast).
This talk is not intended to be exhaustive in its coverage of Fortransyntax, but should be enough to yield a rough and ready knowledgefor HPC.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 8 / 140
Introduction Source Format
-
7/29/2019 class11_Fortran.pdf
8/128
Introduction Source Format
Source Code Format
While FORTRAN 77 used by default a fixed format for its source code,
the default with Fortran 95 is now a free format, in which lines can beup to 132 columns long, with a maximum of 39 continuation lines,indicated by the & character.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 9 / 140
Introduction Source Format
-
7/29/2019 class11_Fortran.pdf
9/128
Introduction Source Format
Source Format Summary
132 c ha ra ct er s per l i n e! i n i t i a t e s comment& c o n t i n u a t i o n character; s ta te me n t s ep ar at or ( m u lt i pl e p er l i n e )
1 p r i n t , You need t wo c o n t i n u a t i o n &2 &c h a r a c t e r s when s p l i t t i n g a toke n &3 & o r s t r i n g across l i n e s
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 10 / 140
Introduction Source Format
-
7/29/2019 class11_Fortran.pdf
10/128
Introduction Source Format
Statement Labels
Statement labels are still in fashion, and consist of 1-5 digits (leadingzeros are neglected), e.g.
1 101 WRITE( 6 , ) E nt er r u n i d : ( < =9 c h ar s ) 2 READ( , 2 ) r u ni d3 2 format ( a 9 )
Most commonly used for FORMAT statements.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 11 / 140
Introduction Source Format
-
7/29/2019 class11_Fortran.pdf
11/128
Introduction Source Format
Names
In Fortran, names must:
Start with a letter (a-z)
Contain only letters, digits, and underscore
Must not be longer than 31 characters
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 12 / 140
Introduction Source Format
-
7/29/2019 class11_Fortran.pdf
12/128
Introduction Source Format
More Resources
Good reference material on all things Fortran:M. Metcalf and J. Reid, Fortran 90/95 Explained, 2nd Ed., (Oxford,
Great Britain, 2000)Fortran 95/2003 Explained, 3rd Ed., (Oxford, Great Britain, 2004),with M. Cohen
Modern Fortran Explained, (Oxford, Great Britain, 2011), with M.Cohen
Metcalfs online tutorial:
http://wwwasdoc.web.cern.ch/wwwasdoc/WWW/f90/f90.html
Alan Millers Fortran Resources:
http://users.bigpond.net.au/amiller/
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 13 / 140
Language Elements Implicit Types
http://wwwasdoc.web.cern.ch/wwwasdoc/WWW/f90/f90.htmlhttp://users.bigpond.net.au/amiller/http://users.bigpond.net.au/amiller/http://wwwasdoc.web.cern.ch/wwwasdoc/WWW/f90/f90.html -
7/29/2019 class11_Fortran.pdf
13/128
g g p yp
Implicit Types
Unfortunately FORTRAN historically allowed implicit types, which bydefault were:
FORTRAN 77
implicit real (a-h,o-z)implicit integer (i-n)
Thanks to backward compatibility, this feature is still present:
Fortran
implicit type (letter-list) [,type (letter-list) ...]
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 15 / 140
Language Elements Implicit Types
-
7/29/2019 class11_Fortran.pdf
14/128
g g p yp
Implicit None
I would recommend starting every declaration block with the following:
Fortran
implicit none
which will turn all implicit typing off, and save a lot of potential errors
(misspelled variable names just being one of them). Use implicit typesat your own risk!
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 16 / 140
Language Elements Intrinsic Types
-
7/29/2019 class11_Fortran.pdf
15/128
Basic Declarations
Declarative Syntax
[,] :: & [ = ]
The attribute is usually one of PARAMETER, DIMENSION, or POINTER.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 17 / 140
Language Elements Intrinsic Types
-
7/29/2019 class11_Fortran.pdf
16/128
Fortran Intrinsic Types
We have five different intrinsic types, integer, real, logical,complex, and character. The syntax looks like:
i n t e g e r : : ir e a l : : xcomplex : : zl o g i c a l : : b ea ut ycharacter : : c
These are all of default kind ... (Note that integers can also be
represented in binary (base 2), octal (base 8), and hex (base 16))
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 18 / 140
Language Elements Intrinsic Types
-
7/29/2019 class11_Fortran.pdf
17/128
Kind
There is an intrinsic function KIND that returns an integer and can be
used to query (or set) the kind type of a variable
Fortran
KIND(x) returns default integer whose value is the kind type parameter
value of x
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 19 / 140
Language Elements Intrinsic Types
-
7/29/2019 class11_Fortran.pdf
18/128
Example: Declarative Use of KIND
These are two of the very handy transformational functions:
SELECTED_REAL_KIND/SELECTED_INT_KIND
SELECTED_REAL_KIND([p][,r])
p :: integer, decimal precision (intrinsic PRECISION)
r :: integer, decimal exponent range (RANGE)
(at least one of p or r must be present)
SELECTED_INT_KIND(r)
r :: integer, decimal exponent range
the return values of both functions are default integers whose valuescan be used in the KIND intrinsic function. -1 is returned if the desiredprecision is unavailable (-2 for the range in SELECTED_REAL_KIND, -3
if both).
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 20 / 140
Language Elements Intrinsic Types
-
7/29/2019 class11_Fortran.pdf
19/128
Best illustrated by example - this is a chunk of code that use in all ofmy Fortran codes:
i n te g e r , parameter : : s i =KIND ( 1 ) , s p=KIND ( 1 . 0 ) , s c =KIND ( ( 1 . 0 , 1 . 0 ) ) , &d i =SELECTED_INT_KIND(2RANGE(1 _ s i ) ) , &dp=SELECTED_REAL_KIND(2PRECISION ( 1 . 0 _sp ) ) , &dc=SELECTED_REAL_KIND(2PRECISION ( 1 . 0 _ sc ) )
Note the use of underscores in literal constants to indicate kind type,which is quite generally applicable:
1 r e a l ( kind=dp ) : : a , b , c2 complex ( kind=dc ) : : z i = ( 0. 0 _dp , 1 . 0 _dp )
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 21 / 140
Language Elements Intrinsic Types
PARAMETER A ib
-
7/29/2019 class11_Fortran.pdf
20/128
PARAMETER Attribute
Instead of the old FORTRAN 77 PARAMETER statement, one can use
the parameter attribute:
REAL, PARAMETER : : E = 2 .7 18 28 , P I = 3. 141 59 2
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 22 / 140
Language Elements Intrinsic Types
I t i i T A
-
7/29/2019 class11_Fortran.pdf
21/128
Intrinsic Type Arrays
Arrays can be indicated by the old-type DIMENSION attribute, or simplyin the declaration itself:
1 re a l , dimension ( 3 ) : : a2 r e a l : : b ( 3 )3 r e a l : : c (1:1)
are all arrays of rank 1, shape 3.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 23 / 140
Language Elements Intrinsic Types
-
7/29/2019 class11_Fortran.pdf
22/128
Note that arrays can be sectioned:
1 a ( i : j ) ! rank 1 a rr ay , s iz e ji +12 b (k , 1 : n ) ! rank 1 a rr ay , s iz e n3 c ( 1 :m, 1 : n , k ) ! r an k 2 a rr ay , e x te n t m by n
and vector subscripts can be used:1 x ( i v ec t or ) ! i v e ct o r i s an i n t e g e r a rr ay
which makes for a very flexible (essentially arbitrary) indexing scheme.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 24 / 140
Language Elements Intrinsic Types
I iti li i A
-
7/29/2019 class11_Fortran.pdf
23/128
Initializing Arrays
There is some special syntax for initialization arrays:
1 a (1 : 4 ) = 0 .02 a ( 1 : 4 ) = ( / 1 . 1 , 1 . 2 , 1 . 3 , 1 . 4 / )
3 a ( 1 :4 ) = ( / i , i =1 ,7 ,2 / ) ! i m pl ie d do l oop w it h s t r i d e 2
Note that the RESHAPE function can also be used to initialize an array
of rank greater than 1.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 25 / 140
Language Elements Intrinsic Types
Character Arrays
-
7/29/2019 class11_Fortran.pdf
24/128
Character Arrays
Better known as strings, Fortran is not the best for string handling (it isintended for number crunching, after all), but modern Fortran isconsiderably improved in this regard:
1 character , dimension ( 12 0) : : l i ne 12 character ( len =120) : : l i n e23 character ( len = 1 2 0 ) ,dimension ( 8 0) : : page
Substrings can be handled in several ways:
1 character ( len =1200) : : l i n e2 character ( len =1 2) : : words , te rm8 ! te rm has l e ng t h 8 , a l t e r n a t e l e ng t h s pec3 l i n e ( : i ) ! same as l i n e ( 1 : i )4 l i n e ( i : ) ! same as l i n e ( i : 1 2 0 )5 l i n e ( : ) ! same as l i n e ( 1 : 1 2 0 )
and we will come back later to some of the intrinsic string handlingfunctions and subroutines.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 26 / 140
Language Elements Derived Types
Derived Types
-
7/29/2019 class11_Fortran.pdf
25/128
Derived Types
It is often advantageous to define your own data types:1 TYPE Coords_3D2 r e a l : : r , t h e t a , p h i ! s p h e r ic a l c o or di n at e s3 END TYPE Coords_3D45 TYPE ( C oords_3D ) : : p o i nt 1 , p o i n t 2
You can reference the internal components of a structure:1 x1 = p oi nt 1%r SIN ( poin t1%th et a ) COS( point1%phi )2 y1 = p oi nt 1%r SIN ( poin t1%th et a ) SIN ( poi nt 1%ph i )3 z1 = p oi nt 1%r COS( point1%theta )4 x2 = p oi nt 2%r SIN ( poin t2%th et a ) COS( point2%phi )5 y2 = p oi nt 2%r SIN ( poin t2%th et a ) SIN ( poi nt 2%ph i )6 z2 = p oi nt 2%r COS( point2%theta )
N.B. Derived type components can not be ALLOCATABLE (but theycan be POINTERS).
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 27 / 140
Language Elements Derived Types
Assigning Derived Types
-
7/29/2019 class11_Fortran.pdf
26/128
Assigning Derived Types
Derived types can be assigned either by component or using aconstructor:
1 p oi nt 1%r = 1 .02 p oi nt 1%t he ta = 0 .0
3 p oi nt 1%p hi = 0 .04 p o i nt 1 = C oords_3D ( 1 . 0 , 0 . 0 , 0 . 0 )5 p o in t 2 = p o in t 1
Note that two variables of the same type can be handled very simply.
Derived types should always be placed in a MODULE.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 28 / 140
Language Elements Pointers
Pointers
-
7/29/2019 class11_Fortran.pdf
27/128
Pointers
Fortran finally has the capability (often best left unused) of usingpointers. Along with ALLOCATABLE and automatic arrays, pointers
make up the 3 types of dynamic data in Fortran 90/95. Pointers can bemembers of derived types, most useful for implementation of linkedlists:
1 re a l , pointer , dimension ( : , : ) : : a2 re a l , p o i n te r : : x , y3 !4 !5 NULLIFY ( x , y , a ) ! P oi nt t o n ot hi ng6 x => n u l l ( ) ! F o rt r an 95 o nl y7 ALLOCATE( x , y , a ( 1 0 0 : 1 0 0 ) ) ! Fres h s t o ra ge a l l o c a t i o n
We will talk a bit more about pointers later when dealing with other
aspects of dynamic memory.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 29 / 140
Language Elements Pointers
DATA Statement
-
7/29/2019 class11_Fortran.pdf
28/128
DATA Statement
The DATA statement can be used to initialize values:
DATA o b j e c tl i s t / va l u el i s t / [ [ , ] o b je c tl i s t / va l u el i s t / ] . . .
Values initialized in DATA statements automatically have the SAVE
attribute.
1 r e a l : : a , b , c2 i n t e g e r : : i , j , k3 DATA a , b , c / 1 . 0 , 2 . 0 , 3 . 0 / , i , j , k / 30 /4 r e a l : : d ia g ( 1 00 , 1 0 0)5 ! s e ts d i ag o na l e le me nt s b y i m p l i e d d o6 DATA ( d i a g ( i , i ) , i = 1 , 1 0 0) / 1 005. 0 / 7 ! s et s t he upper and l ow er t r i a n g l e s t o 0 , d ia go na l t o 18 DATA ( ( matA( i , j ) ,matA( j , i ) , j = i +1 ,1 0 0 ) , i =1 ,1 0 0 )/ 9 90 00.0 /
9 DATA (matA( i , i ) , i =1 ,1 0 0 )/ 1 001.0 /
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 30 / 140
Expressions & Operators Scalar Expressions
Scalar Expressions
-
7/29/2019 class11_Fortran.pdf
29/128
Scalar Expressions
Scalar expressions use operators , , /,+,. The following tablesummarizes the result KIND for all but exponentiation ()
a b used(a) used(b) result
I I a b II R real(a,kind(b)) b R
I C cmplx(a,0,kind(b)) b CR I a real(b,kind(a)) RR R a b RR C cmplx(a,0,kind(b)) b CC I a cmplx(b,0,kind(a)) CC R a cmplx(b,0,kind(a)) CC C a b C
(I = integer, R = real, C = complex)
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 32 / 140
Expressions & Operators Scalar Expressions
-
7/29/2019 class11_Fortran.pdf
30/128
and for exponentiation ():
a b used(a) used(b) result
I I a b II R real(a,kind(b)) b RI C cmplx(a,0,kind(b)) b CR I a b R
R R a b RR C cmplx(a,0,kind(b)) b CC I a b CC R a cmplx(b,0,kind(a)) CC C a b C
(I = integer, R = real, C = complex)
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 33 / 140
Expressions & Operators Scalar Expressions
Relational Operators
-
7/29/2019 class11_Fortran.pdf
31/128
Relational Operators
Fortran supports the old-style FORTRAN 77 relational operators aswell as more modern syntax (which most compilers were supporting byextension anyway):
77 Modern
.lt. =
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 34 / 140
Expressions & Operators Scalar Expressions
Logical Operators
-
7/29/2019 class11_Fortran.pdf
32/128
Logical Operators
The logical operators are simply:
.not. logical negation
.and. logical intersection.or. logical union
.eqv. logical equivalence
.neqv. logical non-equivalence
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 35 / 140
Expressions & Operators Scalar Expressions
Character Operations
-
7/29/2019 class11_Fortran.pdf
33/128
Character Operations
Substrings are easy using the same syntax as for arrays:
1 character ( len =) , parameter : : a l ph a be t 1= a b c d e f g h i jk l m &2 a l p h ab e t2 = nopqrstuvwxyz 3 character ( len =) , parameter : : numerals= 0123456789
45 p r i n t , F i r s t f o u r l e t t e r s : , a lp ha be t1 ( 1 : 4 )6 p r i n t , a l p h a n ume ri c : , a l p h a b e t1 / / a l p h a b e t2 / / n u mera l s
where we also made use of the // concatenation operator.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 36 / 140
Expressions & Operators Scalar Expressions
Array Assignment
-
7/29/2019 class11_Fortran.pdf
34/128
Array Assignment
Arrays can be assigned using expressions that involve arrays of thesame shape, or by scalar (in which case the scalar is applied to allelements of the array), e.g.
1 r e a l : : a ( 2 0 , 2 0 ) , b ( 1 0 )23 a (1 , 11 : 20 ) = b4 a = a + 1. 0
This flexible syntax makes for much more compact code.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 37 / 140
Control Constructs IF Conditional
IF Construct
-
7/29/2019 class11_Fortran.pdf
35/128
Co st uct
1 [ name : ] i f ( expr ) then2 block3 [ el se i f ( expr ) then [ name ]4 block ] . . .5 [ else [ name ]6 block ]7 end i f [ name ]
Note that the optional name is only for program clarity (especially fornested loops), and needs to be unique. Also note the single statementvariation:
i f ( e x p r ) s i n g l esta te me n t
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 39 / 140
Control Constructs DO Loops
DO Loops
-
7/29/2019 class11_Fortran.pdf
36/128
p
Old style loop syntax:
1 [ name : ] do var =expr1 , expr2 [ , expr3 ]2 block3 end do [ name ]
where the loop is executed MAX(0,(expr2-expr1+expr3)/expr3) times,and var is a named scalar integer variable, and expr1, expr2, and
expr3 are scalar integer expressions (expr3 must be nonzero if used).
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 40 / 140
Control Constructs DO Loops
Infinite DO Loop
-
7/29/2019 class11_Fortran.pdf
37/128
p
One important variation on the DO construct is the endless do loop,which has a simple means to exit:
1 do2 i = i +1
3 i f ( i >= e no ug h ) e x i t4 end do
Note that the exit statment is also handy for early termination inbounded loops. The cycle statement is similar, but just dropsexecution to the next iteration.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 41 / 140
Control Constructs DO Loops
DO-WHILE Loop?
-
7/29/2019 class11_Fortran.pdf
38/128
p
There is a DO-WHILE construct, but with the exit option for early looptermination, it is not really worth talking about ...
1 DO WHILE ( a / = b )2 . . .3 END DO
is exactly the same as
1 DO2 i f ( a = = b ) e x i t3 . . .4 END DO
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 42 / 140
Control Constructs DO Loops
The GOTO Statement
-
7/29/2019 class11_Fortran.pdf
39/128
Ah, the bane of FORTRAN 77 programmers everywhere - the infamousGOTO statement. Still supported in Fortran 90/95, be wary of using theGOTO. If you have to use it, make it as clear as you possibly can.
1 101 WRITE( 6 ,
) E nt er r u n i d : ( < =9 c h ar s ) 2 READ( , ( a 9 ) ) r u n i d3 i =INDEX ( r u n i d , )14 INQUIRE ( f i l e =r un id (1 : i ) / / " . in " , e x i s t = i f e x )5 i f ( . n ot . i f e x ) goto 1016 OPEN( u n i t = i u n i t , f i l e =ru n i d (1 : i ) / / . i n , s ta tu s= o l d , &7 & form= fo rma tte d )
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 43 / 140
Control Constructs DO Loops
Using Named and Nested Loops
-
7/29/2019 class11_Fortran.pdf
40/128
Here is an example of when using named loops comes in handy:
1 outa : DO2 i n n a : DO3 . . .
4 I F ( a . G T . b ) EXIT outa ! jump t o l i n e 105 I F ( a . E Q . b ) CYCLE outa ! jump t o l i n e 16 I F ( c . G T . d ) EXIT i n n a ! jump t o l i n e 97 I F ( c . EQ . a ) CYCLE i n n a ! jump t o l i n e 28 END DO i n n a9 END DO outa
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 44 / 140
Control Constructs CASE Construct
The CASE Construct
-
7/29/2019 class11_Fortran.pdf
41/128
Somewhat like the C switch statement ...
1 [ name : ] s e l e c t c as e ( expr )2 [ case s e l ec t o r [ name ]3 block ] . . .4 end s e l e c t [ name ]
The CASE construct can be used as an efficient substitute for a moreelaborate IF ... THEN ... ELSEIF ... ELSE ... ENDIF construct.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 45 / 140
Control Constructs CASE Construct
-
7/29/2019 class11_Fortran.pdf
42/128
An example of using CASE when parsing an input file:
1 s e l e c t c as e ( c t er m s ( 1 ) )2 case ( "MODEL" )3 s e l e c t c as e ( c te rm s ( 2 ) )4 case ( "XBMC" , "xbmc" )5 MCflg = . t r u e .6 case ( "NRL" , " n r l " )
7 case ( "NN2" , "nn2 " )8 c as e d e f a u l t9 end s e l e c t
10 . . .
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 46 / 140
-
7/29/2019 class11_Fortran.pdf
43/128
Part II
Fortran Essentials
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 47 / 140
Modules, Functions & Subroutines Modules
Modules
-
7/29/2019 class11_Fortran.pdf
44/128
Modules provide a way to package together commonly used code(similar to the old common blocks in FORTRAN 77) and have distinct
advantages:
Can be used to hide internal data and routines through PRIVATE
and PUBLIC declarationsCan contain common subroutines and functions with explicit
interfaces which can be changed without affecting calling code
Modules can (and often should) be compiled separately, before
the program units that use them
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 49 / 140
Modules, Functions & Subroutines Modules
Module Syntax
-
7/29/2019 class11_Fortran.pdf
45/128
module modulename[ s p e c i f i c a t i o n s ta te m en ts ]
[ c o n t a i n s
modulesubprograms ]end [ module [ modulename ] ]
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 50 / 140
Modules, Functions & Subroutines Modules
-
7/29/2019 class11_Fortran.pdf
46/128
and a very simple example:
1 MODULE TBconst2 i m p l i c i t none3 i n te g e r , parameter : : sp=KIND ( 1 . 0 ) , &4 & dp=SELECTED_REAL_KIND (2PRECISION ( 1 . 0 _sp ) ) , &5 & sc=KIND ( ( 1 . 0 , 1 . 0 ) ) , &
6 & d c=SELECTED_REAL_KIND (2PRECISION ( 1 . 0 _ sc ) )7 r e a l ( kind=dp ) , parameter : : pi =3.141592653589793238462643_dp8 END MODULE TBconst
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 51 / 140
Modules, Functions & Subroutines Modules
Using Modules
-
7/29/2019 class11_Fortran.pdf
47/128
Modules encapsulate code that can be made accessible to otherprogram units through the USE statement:
1 MODULE TBatoms2 USE TBconst3 . . .
Modules are free to load other modules, but not themselves.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 52 / 140
Modules, Functions & Subroutines Modules
Module Data Visibility
-
7/29/2019 class11_Fortran.pdf
48/128
You can allow or prevent access to the internal workings of a moduleusing the PRIVATE and PUBLIC attributes:
1 PRIVATE : : pos , s t or e , s t a c k_ s i z e ! h id de n2 PUBLIC : : pop , push ! n ot h id de n
or
1 PUBLIC ! s e t d e f a u l t v i s i b i l i t y2 INTEGER, PRIVATE, SAVE : : s t o r e ( s t a c k _ s i z e ) , p os3 INTEGER, PRIVATE, PARAMETER : : s t ac k _s i ze = 100
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 53 / 140
Modules, Functions & Subroutines Modules
Renaming/USE ONLY
-
7/29/2019 class11_Fortran.pdf
49/128
You can rename a module entity in a local context:
1 USE T Bc on st , l o c a l _ d p => dp ! dp becomes l o c a l _ d p i n c u r r e n t s co pe
or you can restrict access by
1 USE TBconst , ONLY: d p , d c ! o n l y l o a d dp a nd d c f r o m m od ul e T B co ns t
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 54 / 140
Modules, Functions & Subroutines Modules
Module Summary
-
7/29/2019 class11_Fortran.pdf
50/128
1 MODULE modname
2 . . . type d e fs3 . . . Gl o b a l data4 . . .5 CONTAINS6 SUBROUTINE sub_17 . . .8 CONTAINS9 SUBROUTINE i n t e r n a l _ 1
10 . . .
11 END SUBROUTINE i n t e r n a l _ 112 SUBROUTINE i n t e r n a l _ 213 . . .14 END SUBROUTINE i n t e r n a l _ 215 END SUBROUTINE sub_116 . . .17 FUNCTION fun_118 . . .19 CONTAINS
20 . . .21 END FUNCTION fun_122 END MODULE modname
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 55 / 140
Modules, Functions & Subroutines Subroutines
Subroutine Syntax
-
7/29/2019 class11_Fortran.pdf
51/128
The syntax for a subroutine call is given by
SUBROUTINE[recursive] subroutine subroutine-name[([dummy arguments])]
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 56 / 140
Modules, Functions & Subroutines Subroutines
Argument Intent
-
7/29/2019 class11_Fortran.pdf
52/128
You can provide information to the compiler (always a good idea!)about the dummy arguments to a routine by:
1 REAL, INTENT ( IN ) : : arg1 ! p as si ng i n v al ue2 REAL, INTENT (OUT) : : arg2 ! r e t u r n i n g v a lu e o n ly3 REAL, INTENT ( INOUT) : : arg3 ! b ot h a pp ly
You should always make use of the INTENT attribute - it allows the
compiler to do extensive error checking and optimization (remember,the more that your compilers knows about your code, the better it will
be able to perform).
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 57 / 140
Modules, Functions & Subroutines Subroutines
SAVE Attribute
-
7/29/2019 class11_Fortran.pdf
53/128
The SAVE attribute can be applied to a specified entity, or all of the
local entities in a procedure:
1 SUBROUTINE sub_1 ( arg1 , arg2 )2 i n te g e r , save : : n um be r_ ca ll s = 03 . . .4 n um ber _c al ls = nu mbe r_c al ls +1
1 SUBROUTINE sub_1 ( arg1 , arg2 )2 . . .3 SAVE4 . . .
SAVE acts to preserve values between calls to the subroutine/function.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 58 / 140
Modules, Functions & Subroutines Functions
Function Call Syntax
-
7/29/2019 class11_Fortran.pdf
54/128
The syntax for a function call is given by
FUNCTION
type [recursive] function function-name[([dummy arguments])] &[result(result-name)]
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 59 / 140
Modules, Functions & Subroutines Explicit Interfaces
Explicit Interfaces
-
7/29/2019 class11_Fortran.pdf
55/128
External subprograms have an implicit interface by default (even if one
uses the external statement to indicate that a subunit is outside thecurrent code, the arguments and their types remain unspecified), and
an INTERFACE block is necessary to specify an explicit interface of anexternal subprogram; as mentioned above, this allows type-checking ofactual and formal arguments in a reference to a subprogram
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 60 / 140
Modules, Functions & Subroutines Explicit Interfaces
Explicit Interface Example
-
7/29/2019 class11_Fortran.pdf
56/128
1 INTERFACE
2 SUBROUTINE r e s i d (m, n , x , f ve c , i f l a g )3 USE TBatoms4 USE TBconst5 USE T B f i t d a t a6 USE TBfl a g s7 USE TBmat8 USE TBopt9 USE TBparams
10
11 i m p l i c i t none12 i n te g e r , i n t e n t ( i n ) : : m, n13 i n te g e r , i n t e n t ( i n o ut ) : : i f l a g14 r e a l ( kind=dp ) , i n t e n t ( i n ) : : x ( n)15 r e a l ( kind=dp ) , i n t e n t ( i n o ut ) : : f v ec (m)16 END SUBROUTINE r e s i d17 END INTERFACE
Note that the syntax of the interface-body is just an exact copy of thesubprograms header, argument specifications, function result, andEND statement.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 61 / 140
Modules, Functions & Subroutines Explicit Interfaces
INTERFACE Properties
-
7/29/2019 class11_Fortran.pdf
57/128
Can not use both EXTERNAL and INTERFACE
Explicit interfaces required for POINTER or TARGET dummy
arguments in a procedure, or pointer-valued function result
Explicit interfaces also required for OPTIONAL, KEYWORD, andprocedural arguments
Even when not required, explicit interfaces are a good idea
(usually placed inside a MODULE)
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 62 / 140
Modules, Functions & Subroutines Explicit Interfaces
Optional & Keyword Arguments
-
7/29/2019 class11_Fortran.pdf
58/128
Dummy arguments can be optional - using OPTIONAL:
1 SUBROUTINE o p t ar g s ( a , b )2 REAL, INTENT ( IN ) , OPTIONAL : : a3 INTEGER, INTENT ( IN ) , OPTIONAL : : b4 REAL : : ay ; INTEGER : : by56 ay = 1 . 0 ; bee = 1 ! d e fa u lt s7 I F (PRESENT(a ) ) ay = a8 I F (PRESENT(b ) ) bee = b
9 . . .
1 CALL o p t a r g s ( )2 CALL o p t ar g s ( 1 . 0 , 1 ) ; CALL o p t a r g s ( b = 1 , a = 1 . 0 ) ! same , u si n g ke yword s3 CALL o p t ar g s ( 1 . 0 ) ; CALL o p t a r g s ( a = 1 . 0 ) ! keywords h an di er s t i l l f o r l on g l i s t s4 CALL o p ta rg s (b =1 )
Note that optional and keyword arguments need explicit interfaces, andshould come after positional arguments.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 63 / 140
Modules, Functions & Subroutines Procedures as Arguments
Procedures as Arguments
-
7/29/2019 class11_Fortran.pdf
59/128
When using a procedure as an argument, an explicit interface isrequired (as it is for POINTER, optional, and keyword arguments):
1 REAL FUNCTION minimum ( a , b , f u n c )2 ! r e t u r n s t he minimum v al ue o f t he f u n c t i o n f u nc ( x )3 ! i n t he i n t e r v a l ( a , b)4 REAL, INTENT ( i n ) : : a , b5 INTERFACE
6 REAL FUNCTION f u n c ( x )7 REAL, INTENT ( IN ) : : x8 END FUNCTION fu n c9 END INTERFACE
10 REAL f , x11 :12 f = fu n c ( x ) ! i n v oc a t i on o f t he u se r f u n c t i on .13 :14 END FUNCTION minimum
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 64 / 140
Modules, Functions & Subroutines Recursion
Recursion
-
7/29/2019 class11_Fortran.pdf
60/128
Recursion is supported in Fortran 90/95; we can illustrate it best by
example:
1 RECURSIVE FUNCTION f a c t o r i a l ( n ) RESULT( res )2 INTEGER r es , n3 I F (n .EQ.1 ) THEN
4 r e s = 15 ELSE6 r e s = n f a c t o r i a l ( n1)7 END IF8 END FUNCTION f a c t o r i a l
this would be an example of direct recursion ...
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 65 / 140
Modules, Functions & Subroutines Recursion
and an example of indirect recursion:
-
7/29/2019 class11_Fortran.pdf
61/128
p
1 volume = i n t e g r a t e ( f un c , bounds )2 :3 RECURSIVE FUNCTION i n t e g r a t e ( f , b oun ds )
4 ! I n t e g r a t e f ( x ) f ro m b ounds ( 1 ) t o bounds ( 2 )5 REAL i n t e g r a t e6 INTERFACE7 FUNCTION f ( x )8 REAL f , x9 END FUNCTION f
10 END INTERFACE11 REAL, DIMENSION( 2 ) , INTENT ( IN ) : : bounds12 :
13 END FUNCTION i n t e g r a t e14 :15 FUNCTION f u n c ( x )16 USE MODfunc ! m od ul e MODfunc c o n t a i n s f u n c t i o n f17 REAL f un c , x18 x v a l = x19 fu n c = i n t eg ra te ( f , bounds )20 END FUNCTION fu n c21 :
Generally indirect recursion is of the form A calls B calls A ... - in thiscase integrate calls func which calls integrate ...
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 66 / 140
Input/Output
Fortran I/O
-
7/29/2019 class11_Fortran.pdf
62/128
Too large a topic to cover in its entirety here - we will focus on the
basics required to familiarize you with basic Fortran I/O functionality.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 68 / 140
Input/Output File Handling
OPEN
-
7/29/2019 class11_Fortran.pdf
63/128
OPEN ( [ UNIT= i n te g e r , ] FILE= f i le n am e , [ERR= l ab e l , ] &[STATUS=status , ] [ACCESS=me th od , ] [ ACTION=mode , ] &[RECL= i n texpr )
filename is a string
status is OLD, NEW, REPLACE, SCRATCH or UNKNOWNmethod is DIRECT or SEQUENTIAL
mode is READ, WRITE or READWRITE
RECL (record length) needs to be specified for DIRECT access
files
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 69 / 140
Input/Output File Handling
OPEN Examples
-
7/29/2019 class11_Fortran.pdf
64/128
Note that UNIT 1-7 are typically reserved (6, or , is almost always thestandard output, for example), and each file stream needs a uniquenumber.
1 OPEN(17 , FILE= out put . dat ,ERR=10, STATUS= REPLACE , &
2 ACCESS= SEQUENTIAL ,ACTION= WRITE )3 :4 OPEN(14 , FILE= in pu t . dat ,ERR=1 0 , STATUS= OLD , RECL= i ex p , &5 ACCESS= DIRECT ,ACTION= READ )6 :7 w r i t e ( u n i t = i o u n i t , fm t=" (a ) " , i o s t a t =b a d u n i t ) t i t l e8 i f ( b a d u ni t > 0 ) c a l l er ro r_ han dl er (ERR_IO)
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 70 / 140
Input/Output File Handling
INQUIRE
-
7/29/2019 class11_Fortran.pdf
65/128
A very handy statement for querying the status of a file by unit numberor filename:
INQUIRE ( [ UNIT = ] u n i t | FILE=fi l e n a me , i l i s t )
and there are many possible entries in ilist, of which the most
handy are:
IOSTAT=ios as in the OPEN syntax
EXIST=log_exist returns a logical on the existence of the file
OPENED=log_opened returns a logical on whether the file is
open
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 71 / 140
Input/Output File Handling
INQUIRE Example
-
7/29/2019 class11_Fortran.pdf
66/128
1 !2 ! Open i n p u t f i l e f a i l g r a c e f u l l y i f n ot f ou nd .3 !4 INQUIRE ( f i l e =ru n i d (1 : l e n _ ru n i d ) / / . i n , e x i s t = e x i s t _ i n )5 i f ( . n o t . e x i s t _ i n ) then6 w r i t e ( ,) < r ea di n > Unable t o open i n p u t f i l e : , &7 r u n i d ( 1 : l e n _ r u n i d ) / / . i n 8 stop9 e n d i f
10 OPEN( f i l e =ru n i d (1 : l e n _ ru n i d ) / / . i n , s ta tu s= ol d , u n i t = i n u n i t )
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 72 / 140
Input/Output File Handling
Other I/O Statements
-
7/29/2019 class11_Fortran.pdf
67/128
CLOSE unattach specified unit number
REWIND place file pointer back to start of file
BACKSPACE place file pointer back one record
ENDFILE force writing end-of-file
1 REWIND( 1 1 )2 BACKSPACE( UNIT=27)3 ENDFILE( 1 9 )4 CLOSE(13 , IOSTAT= i o _ v a l u e )
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 73 / 140
Input/Output Read & Write
READ
-
7/29/2019 class11_Fortran.pdf
68/128
READ ( [ UNIT= unit , ] [FMT=format , ] [ IOSTAT= i n t
v a r ia b le , ] &[ERR= la be l , ] [END= l a b e l , ] [ EOR= l a b e l , ] &[ADVANCE=advmode , ] [REC= i n texpr , ] &[ SIZE=numc h a rs ] ) < o u t p utl i s t >
where the non-obvious entries are:
unit is an integer (some lower values are reserved) or forstandard input
format is a string of FORMAT statement label number
label is a statement label
adv-mode is YES or NO
IOSTAT returns zero for no error
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 74 / 140
Input/Output Read & Write
READ Example
-
7/29/2019 class11_Fortran.pdf
69/128
1 READ(1 4 ,FMT= (3 (F10 .7 ,1 x )) ,REC= ie xp ) a , b , c2 READ(1 4 , (3 (F10 .7 ,1 x )) ,REC= i e x p ) a , b , c ! same as above3 READ( , (A) ,ADVANCE= NO ,EOR=12 ,SIZE=nch ) s t r
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 75 / 140
Input/Output Read & Write
WRITE
-
7/29/2019 class11_Fortran.pdf
70/128
WRITE ( [ UNIT= u n i t , ] [FMT=format , ] [ IOSTAT= i n tv a r ia b le , ] &[ERR= la be l , ] [ADVANCE=advmode , ] &[REC= i n te x p r ) < o u t p u tl i s t >
where the entries are as in the READ case.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 76 / 140
Input/Output Read & Write
FORMAT
-
7/29/2019 class11_Fortran.pdf
71/128
Fortran does have quite an elaborate formatting system. Here are the
highlights.
Iw w chars of integer dataFw.d w chars of real data, d decimal placesEw.d w chars of real data, d decimal placesLw w chars of logical dataAw w chars of CHARACTER data
nX skip n spaces
Note that:
The E descriptor is just the F with scientific notation
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 77 / 140
Input/Output Read & Write
FORMAT Example
-
7/29/2019 class11_Fortran.pdf
72/128
1 WRITE( ,FMT= (2X, 2( I4 ,1X) , name ,A4, F13. 5 ,1X , E13 .5 ) ) &2 77778 ,3 , a b c d e fg h i ,14.45 ,14.566 6666
1 3 name abcd 14.45000 0.14567E+02
Note that are quite a few other format descriptors, much less
commonly used.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 78 / 140
Input/Output Read & Write
Unformatted I/O
-
7/29/2019 class11_Fortran.pdf
73/128
Unformatted I/O is simpler (no FORMAT statements) and involves less
overhead, less chance of roundoff error), but is inherently non-portable,since it relies on the detailed numerical representation. A file must beeither entirely formatted or unformatted, not a mix of the two.
READ( 1 4 ) AWRITE(15 , IOSTAT=i o s ,ERR=2001) B
Note that unformatted i/o is generally quite a lot faster than formatted -
so unless you are concerned with moving your files from one platformto another, you will be much better off using unformatted i/o.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 79 / 140
Arrays and Array Syntax Assumed-shape & Automatic Arrays
Assumed-shape Arrays
-
7/29/2019 class11_Fortran.pdf
74/128
Arrays passed as dummy arguments should generally be what arecalled assumed-shape arrays, meaning that the dimensions are left tothe actual (calling arguments):
1 SUBROUTINE s t u bb y ( a , b )2 i m p l i c i t none3 re a l , i n t e n t ( i n ) : : a ( : ) , b ( : , : )
4 :
Note that the default bounds (1) apply
Actual arguments can not be vector subscripted or themselves
assumed-shape
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 81 / 140
Arrays and Array Syntax Assumed-shape & Automatic Arrays
Automatic Arrays
-
7/29/2019 class11_Fortran.pdf
75/128
Local arrays whose extent is determined by dummy arguments arecalled automatic objects. Example:
1 SUBROUTINE s t u b b y 1 ( b , m , n )2 i n te g e r , i n t e n t ( i n ) : : m, n3 re a l , i n t e n t ( inout ) : : b ( : , : ) ! assumed
4 REAL : : b1 ( m, n ) ! a u t om a t ic5 REAL : : b2 ( SIZE ( b , 1 ) , SIZE ( b , 2 ) ) ! a u t om a t ic
Note that both assumed-shape arrays and automatic objects are likelyto be placed on the stack in terms of memory storage.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 82 / 140
Arrays and Array Syntax Dynamic(Allocatable) Arrays
Allocatable Arrays
-
7/29/2019 class11_Fortran.pdf
76/128
Finally, dynamic data storage elements for Fortran! An array that is nota dummy argument or function can be given the ALLOCATABLEattribute:
re a l , a l l o c a t a b l e : : a ( : , : )::
ALLOCATE( a ( n t yp e s , 0 : n t y pe s + 2 ) ) ! n ty pes i s an i n te g er:! l o t s o f work:
DEALLOCATE( a )
Note that ALLOCATABLE arrays can not be part of a derived type (have
to use a POINTER to get the same functionality) - that oversight shouldbe fixed in Fortran 2003.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 83 / 140
Arrays and Array Syntax Dynamic(Allocatable) Arrays
ALLOCATE & DEALLOCATE
-
7/29/2019 class11_Fortran.pdf
77/128
The syntax for ALLOCATE and DEALLOCATE are given by:
ALLOCATE( l i s t [ , s t a t = i s t a t ] )DEALLOCATE( l i s t [ , s t a t = i s t a t ] )
The optional stat= specifier can be used to test the success of the(de)allocation through the scalar integer istat. As usual, zero forsuccess. Leaving out stat= should result in a termination if the
(de)allocation was unsuccessful.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 84 / 140
Arrays and Array Syntax More Pointers
Pointer Flexibility
-
7/29/2019 class11_Fortran.pdf
78/128
Note that you can not associate pointers with just any variable (as in
C), instead the variables must be declared using the target attribute:
re a l , t a r g e t : : x , y ( 1 0 0) , z ( 4 , 4 )i n te g e r , t a r g e t : : m, n ( 1 0 ) , k ( 1 0 , 1 0)
re a l , p o i n te r : : p tr 1 , p tr 2 , p t r_ y ( : ) , p tr _ z1 ( : ) , p tr _ z2 ( : , : )
p t r 1 => x ! s i m pl e p o i n t e r a ss ig nm en t
a l p ha = e xp ( p t r 1 ) ! p o i n t e r s ha re s memory l o c a t i o n w i t h x ,! b ut used l i k e any v al ue
n u l l i f y ( p tr1 ) ! f r ee s u p p o i n te r
i f ( a sso ci a te d ( p tr1 ) ) thenp r i n t , p t r 1 i s a ss oc ia te d i f ( a sso ci a te d ( p tr1 , t a r g e t =x ) ) then
p r i n t , p t r1 i s a ss oc ia te d w it h " x " e n d i f
e n d i f
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 85 / 140
Arrays and Array Syntax More Pointers
More Fun With Pointers
-
7/29/2019 class11_Fortran.pdf
79/128
array-associated pointers have considerable flexibility:
p t r_ y => y ! can use p t r _ y ( i ) j u s t as y ( i )p t r _ y => y ( 1 1 : 2 0) ! p t r_ y ( 1 ) i s now y ( 1 1 ) . . .p t r _ y => z ( 2 , 1 : 4 ) ! l o a ds row 2 o f z i n t r o p tr _y ( : )p t r _z 2 => z ( 2 : 4 , 2 : 4 ) ! p tr _z 2 i s 3x3 s ub ma tr ix o f z
ALLOCATE( p t r _ z 1 ( 1 6 ) ) ! d i re c t a l l o c at i o n
Note that pointers can also be used as components of derived types,
making for very flexible data structures.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 86 / 140
Arrays and Array Syntax More Pointers
Pointer Considerations
-
7/29/2019 class11_Fortran.pdf
80/128
Some things to think about when using pointers in Fortran:
Can create complex (and difficult to maintain) code
Easy to create bugs that only arise at run-time
Inhibit compiler optimization (difficult to predict data patters anddisjoint memory structures)
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 87 / 140
Arrays and Array Syntax Elemental Operations
Elemental Operations
-
7/29/2019 class11_Fortran.pdf
81/128
We have already seen elemental operations in which conformableoperands can be used with intrinsic operators, e.g.
r e a l : : a ( 10 0 , 1 00 ). . .
a = SQRT( a )
will apply the square root operator individually to all of the elements of
a. Not only intrinsics can be elemental, and you can also use theelemental declaration in user-defined functions (Requires Fortran
95) as well.
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 88 / 140 Arrays and Array Syntax Array-valued Functions
Array-valued Functions
-
7/29/2019 class11_Fortran.pdf
82/128
Functions can return arrays - just be careful that you ensure that theinterface is an explicit one. An example:
1 PROGRAM a r r f u n c2 i m p l i c i t none3 i n te g e r , parameter : : n di m=364 i n te g e r , dimension ( n di m , n di m ) : : m1 , m25 :6 m2 = fu n k y (m1, 4 )7 :8 CONTAINS9 FUNCTION funky (m, sc al )
10 i n te g e r , i n t e n t ( i n ) : : ima ( : , : )11 i n te g e r , i n t e n t ( i n ) : : s c a l12 i n t e g e r : : f un ky ( SIZE (m,1 ) , SIZE ( m , 2 ) )13 fu n k y ( : , : ) = m( : , : ) s c a l14 END FUNCTION fu n ky15 END PROGRAM a r r f u n c
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 89 / 140 Arrays and Array Syntax Array-valued Functions
WHERE Statement
-
7/29/2019 class11_Fortran.pdf
83/128
Useful for performing array operations only on certain elements of an
array, but preserving the compact syntax:WHERE ( l o g i c a la rra yexpr )
a rra yassignmentsEND WHERE
1 WHERE ( p r e s s u re
-
7/29/2019 class11_Fortran.pdf
84/128
Fortran 95 introduced the concept of a FORALL statement which is
basically an array assignment with some explicit indexing:FORALL( i = 1:M) gr id ( i , i ) = diag ( i )FORALL ( i = 1 : M, j = 1 : N) g r i d ( i , j ) = u ( i )v ( j )FORALL ( i = 1 :M, j = 1 : N, eps ( i . j ) / = 0 . 0 ) g r i d ( i , j ) = 1 . 0 / eps ( i , j )
Implied is that the assignment is trivially data-parallel, i.e. it can be
carried out in any order, and therefore can be more efficient than amore traditional loop. Construct form:
FORALL( i = 2: N1, j = 2 :N1)U( i , j ) = U( i , j1) + U( i , j +1 ) + U( i1, j ) + U( i +1 , j )V( i , j ) = U( i , j )
END FORALL
where the results are executed in any order, held in temporary storage
(to avoid indeterminate results), and then updated in arbitrary order.
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 91 / 140 Arrays and Array Syntax Array-valued Functions
-
7/29/2019 class11_Fortran.pdf
85/128
Complete FORALL construct syntax:
[ name : ] FORALL ( i n d e x=l o we r : u p pe r [ : st r i d e ] [ , i n d e x2 =l o wer2 : u p pe r2 [ : str i d e 2 ] ] . . . &[ , sca l a rl o g i c a le xp r ] )
[ b o d y ]END FORALL [ name ]
The body of a FORALL construct can be quite general (containing
statements, additional FORALL or WHERE statements/constructs, etc.),but must not branch (e.g. goto) out of the construct. Any included
subprograms must be pure, in the sense of inducing no undesiredside-effects in the sense of inducing an order dependence that wouldimpede parallel processing.
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 92 / 140 Arrays and Array Syntax Array-valued Functions
PURE Procedures (Fortran 95)
-
7/29/2019 class11_Fortran.pdf
86/128
Programmer can assert that a function or subroutine is PURE: by
adding the PURE keyword to the function/subroutine statement:a pure function does not alter its dummy arguments (must beINTENT(IN))
INTENT of dummy arguments must be declared (IN for functions)
does not alter variables accessed by host or use associationcontains no local variables with SAVE attribute
contains no operations on external file
contains no STOP statements
any internal procedures must also be pureall intrinsic functions are pure.
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 93 / 140 Arrays and Array Syntax Intrinsics For Arrays
Array Intrinsics
-
7/29/2019 class11_Fortran.pdf
87/128
Fortran is designed around the notion of data manipulation, so it is nota great surprise that it has a number of built-in functions for array
manipulation, some of which we have already seen (elementaloperations, masking).
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 94 / 140 Arrays and Array Syntax Intrinsics For Arrays
Array Functions
-
7/29/2019 class11_Fortran.pdf
88/128
Set of operations that involve common extractions from arrays:
ALL(MASK [,dim]) all relations in mask are true [along dimension dim]ANY(MASK [,dim]) if any elements of mask are true [along dimension dim]COUNT(MASK [,dim]) number of elements of mask that are trueMAXLOC(ARRAY [,mask]) location of element with maximum valueMINLOC(ARRAY [,dim[,mask]]) location of element with minimum valueMAXVAL(ARRAY [,dim[,mask]]) maximum value [of true elements in mask, along dim]MINVAL(ARRAY [,dim[,mask]]) minimum value [of true elements in mask, along dim]PRODUCT(ARRAY [,dim[,mask]]) products [of true elements in mask, along dim] of valuesSUM (ARRAY [,dim[,mask]]) sum [of true elements in mask, along dim] of values
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 95 / 140 Arrays and Array Syntax Intrinsics For Arrays
Example of Array Extraction Intrinsics
-
7/29/2019 class11_Fortran.pdf
89/128
i n t e g e r : : i , j , max_e le me nt (2 ) , max_e le me nt_2 (2 )r e a l : : Amax, Amax_2, Amat(100 ,10 0)
Amat = RESHAPE( ( / (( 100( i 1)+ j , j = 1 , 1 0 0 ) , i = 1 , 1 0 0) / ) , ( / 1 00 , 100 / ) , ORDER = ( / 2 , 1 / ) )
max_element = MAXLOC( Amat ) ! f i n d s t he e le me nt o f Amax w i t h! max v a lu e [ A( 10 0 , 1 00 ) =
Amax = MAXVAL( Amat ) ! 10000 ]
max_element_2 = MAXLOC( Amat , Amat
-
7/29/2019 class11_Fortran.pdf
90/128
Very useful array inquiry functions:
ALLOCATED(ARRAY) logical if A has been allocatedLBOUND(ARRAY [,dim]) lower bound for dimension dim of A (integer vector if no dim)SHAPE(ARRAY) returns integer vector of shape of A
SIZE(ARRAY [,dim]) size of dimension dim of A (else all of A)UBOUND(ARRAY [,dim]) upper bound for dimension dim of A (integer vector if no dim)
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 97 / 140 Arrays and Array Syntax Intrinsics For Arrays
Array Reshaping
RESHAPE is a general intrinsic function which delivers an array of a
specific shape:
-
7/29/2019 class11_Fortran.pdf
91/128
specific shape:
RESHAPE( sourc e , shape [ , pad ] [ , o r de r ] )
returns an array whose shape is given by the constant rank-1 integerarray (nonnegative elements) shape derived from the array source. Iforder is absent, elements of pad are used to fill out remaining
elements in the result (whose size may then exceed that of source.order can be used to pad the result in non-element order.
1 r e a l : : A2 ( 2 , 2 )2 A2 = RESHAPE ( ( / 1 , 2 , 3 , 4 / ) , ( / 2 , 2 / ) ) ! c r ea te a 2 x2 m a tr i x f rom 1x4 v e c to r3 p r i n t , A2 : 1 , 1 1 , 2 = , A2 ( 1 , 1 ) , A2 ( 1 , 2 )4 p r i n t , A2 : 2 , 1 2 , 2 = , A2 ( 2 , 1 ) , A2 ( 2 , 2 )
produces (recall that Fortran is column-ordered):
1 A2 : 1 ,1 1 ,2 = 1.000000 3.0000002 A2 : 2 ,1 2 ,2 = 2.000000 4.000000
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC I Fall 2012 98 / 140 Arrays and Array Syntax Intrinsics For Arrays
Vector/Matrix Intrinsics
-
7/29/2019 class11_Fortran.pdf
92/128
Fortran 90 has several intrinsics for vector dot products matrixmultiplication and transposition:
DOT_PRODUCT(vector_1,vector_2) dot product of two rank-1 equal length vectors
MATMUL(matrix_1,matrix_2) matrix multiplicationTRANSPOSE(matrix) transposition of any rank-2 array
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC I Fall 2012 99 / 140 Fortran Intrinsics
Intrinsics Categories
There are roughly 75 new intrinsic routines (versus FORTRAN 77), but
-
7/29/2019 class11_Fortran.pdf
93/128
There are roughly 75 new intrinsic routines (versus FORTRAN 77), but
they roughly fall into 4 categories:1 Elemental procedures
2 Inquiry functions
3 Transformational functions
4 Nonelemental subroutinesI am going to group them a bit differently, and only cover the more
common ones. Consult a good reference1 for a thorough list.
1Metcalf, Reid, and CohenModern Fortran Explained, (Oxford, GreatBritain, 2011).
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC I Fall 2012 101 / 140 Fortran Intrinsics Mathematical Intrinsics
Mathematical Intrinsics
-
7/29/2019 class11_Fortran.pdf
94/128
Far too many to enumerate here - you can find a handy reference for
the full set in other references. Note that almost all support a genericinterface supporting available KIND types, and that most are
elemental.
SQRT, EXP, LOG, LOG10, SIN, COS, TAN, ASIN, ACOS,
ATAN, SINH, COSH, TANH
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC I Fall 2012 102 / 140 Fortran Intrinsics Numerical Intrinsics
Numerical Intrinsics
-
7/29/2019 class11_Fortran.pdf
95/128
The list of numerical intrinsics (with syntax and usage):
INT(a[,KIND]) convert to integer, type KINDREAL(a[,KIND]) convert to real, type KINDCMPLX(x[,y][,KIND]) convert x or (x,y) to complex, type KINDAINT(a[,KIND]) truncate real to lowest whole number, type KINDANINT(a[,KIND]) returns nearest whole number real, type KINDNINT(a[,KIND]) integer (type KIND) value nearest aABS(a) absolute value of a, same KIND as a
MOD(a,p) remainder of a modulo p, a-int(a/p)*p (has sign of a)MODULO(a,p) a-floor(a/p)*p (has sign of p)FLOOR(a[,kind]) greatest integer less than or equal to a, type KINDCEILING(a[,KIND]) least integer greater than or equal to a, type KINDSIGN(a,b) absolute value of a times sign of bDIM(x,y) max(x-y,0.0)MAX(a1,a2[,a3,...]) maximum of two or more numbersMIN(a1,a2[,a3,...]) minimum of two or more numbersAIMAG(z) imaginary part of complex number z, type real, KIND(z)CONJG(z) conjugate of complex number z
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC I Fall 2012 103 / 140 Fortran Intrinsics String Functions
String Functions
-
7/29/2019 class11_Fortran.pdf
96/128
Yes, Fortran does have string handling capability! And in fact, it ismuch improved. The following table gives a brief synopsis:
ACHAR(I) ASCII character of number IADJUSTL(STRING) Adjusts to the leftADJUSTR(STRING) Adjusts to the rightCHAR(I, kind) Returns character of number IIACHAR(C) ASCII number of char C
ICHAR(C) Number of char CINDEX(STRING, SUBSTRING, back) Starting pos of substring in stringLEN(STRING) Length of STRINGLEN_TRIM(STRING) Length of string without trailing blanksREPEAT(STRING, NCOPIES) String concatnationSCAN(STRING, SET, back) Position of 1st occurrence of any char in SET in STRINGTRIM(STRING) Returns string without trailing blanksVERIFY(STRING, SET, back) Position of 1st char in STRING not in SET
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC I Fall 2012 104 / 140 Fortran Intrinsics String Functions
-
7/29/2019 class11_Fortran.pdf
97/128
The following functions can be used for ASCII lexical stringcomparisons:
LGE( STRING_A , STRING_B)LGT( STRING_A , STRING_B )LLE( STRING_A , STRING_B)LLT ( STRING_A , STRING_B )
Note that if the strings are of differing length, the shorter will be paddedwith blanks for comparative purposes. All return default logical results.
M D Jones Ph D (CCR/UB) Scientific Programming With Modern Fortran HPC I Fall 2012 105 / 140 Fortran Intrinsics Bitwise Intrinsics
Bitwise Intrinsics
M d F t dd d t f it f bit i ti
-
7/29/2019 class11_Fortran.pdf
98/128
Modern Fortran added support for quite a few bitwise operations:
BIT_SIZE(I) number of bits in a wordBTEST(I, POS) .true. if POS number of I is 1IAND(I, J) logical addition of bit chars in I and JIBCLR(I, POS) puts a zero in the bit in POSIBITS(I, POS, LEN) uses LEN bits of word I beginning at POS, additional
bits are set to zero. POS + LEN 0).Positions that are vacated are set to zero.
ISHIFTC(I, SHIFT, size) performs logical shift a number of stepscircularly to the right if SHIFT < 0,circularly to the left if SHIFT > 0. If SIZEis given, it is required that 0 < SIZE
-
7/29/2019 class11_Fortran.pdf
99/128
Modern Fortran also has a built-in pseudorandom generator:
CALL RANDOM_NUMBER( h a r v e s t )CALL RANDOM_SEED( [ s i ze ] | [ p ut ] | [ g et ] )
harvest can be an array, and the range of the random numbers arethe interval [0, 1). size is intent OUT and returns the size of theinteger seed array, which can be input (put) or returned (get).
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 107 / 140
Fortran Intrinsics Timing & Random Numbers
Real-time Clock
Modern Fortran provides a pair of routines to access the real-time
-
7/29/2019 class11_Fortran.pdf
100/128
p p
clock:DATE_AND_TIME
DATE_AND_TIME([date] [,time] [,zone] [,values])
date character string in form ccyymmdd
time character string in form hhmmss.ssszone character string in form Shhmm, difference between local
and UTC
values integer vector of size at least 8 with year, month, day,
difference from UTC in minutes, hour, minutes, seconds,milliseconds
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 108 / 140
Fortran Intrinsics Timing & Random Numbers
-
7/29/2019 class11_Fortran.pdf
101/128
SYSTEM_CLOCKSYSTEM_CLOCK([count][,count_rate][,count_max])
count processor-dependent value of processor clock
(-huge(0) if no clock)
count_rate clock counts per second (0 if no clock)count_max maximum for count (0 if no clock)
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 109 / 140
Fortran Intrinsics Timing & Random Numbers
CPU Time
Fortran 95 only:
-
7/29/2019 class11_Fortran.pdf
102/128
CPU_TIMECPU_TIME(time)
time real assigned to processor-dependent time in seconds
(negative value if no clock)
r e a l : : t 1 , t 2:
CALL CPU_TIME( t1 ) ! F or t ra n 95 o n ly::
CALL CPU_TIME( t2 )p r i n t , Time s pe nt i n code : , t 2t1 , seconds
In my experience the CPU_TIME intrinsic is not very precise, however,and depends rather strongly on the compiler ...
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 110 / 140
Fortran Intrinsics Timing & Random Numbers
STOPWATCH
-
7/29/2019 class11_Fortran.pdf
103/128
STOPWATCH is not part of standard Fortran, but is a nice little package
written by William Mitchell at NIST:
http://math.nist.gov/StopWatch
which supports a more full featured set of timing routines (including
wall time, cpu time, and system time).
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 111 / 140
Fortran Intrinsics Array Functions
Array Functions
http://math.nist.gov/StopWatchhttp://math.nist.gov/StopWatch -
7/29/2019 class11_Fortran.pdf
104/128
The array intrinsic functions will be discussed in a special section
devoted to arrays ...
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 112 / 140
-
7/29/2019 class11_Fortran.pdf
105/128
Part III
Fortran Advanced Operations
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 113 / 140
Numerics in Modern Fortran Inquiry Functions
Numerical Inquiry Functions
There are a bunch of numerical inquiry functions in Fortran 90/95.
First the integer representation model is given by
-
7/29/2019 class11_Fortran.pdf
106/128
First, the integer representation model is given by
i= s
q1
k=0
dkrk,
wherei integer values sign (+,)r radix (r > 1)
q number of digits (q> 1)dk k-th digit, (0 dk < r)
M. D. Jones, Ph.D. (CCR/UB) Scientific Programming With Modern Fortran HPC-I Fall 2012 115 / 140
Numerics in Modern Fortran Inquiry Functions
The floating-point model is given by
p
-
7/29/2019 class11_Fortran.pdf
107/128
x= sbek=1
fkb
k,
where
x real value
s sign (+,)b base (b> 1)e exponent (q> 1)p number mantissa digits (p> 1)f
kk-th digit, (0
f
k