# introduction to fortran - ?· introduction to fortran 1 introduction to fortran • why fortran? -...

Post on 06-Sep-2018

215 views

Embed Size (px)

TRANSCRIPT

Tools for High Performance Computing 2013: 2. Introduction to Fortran 1

Introduction to Fortran Why Fortran?

- A lot of legacy software written in it (well, often in Fortran 77).- High-level abstraction (as compared to C); e.g. handling of arrays.- Large number of numerical libraries built for Fortran.

Why not Fortran?- Compilers (particularly free ones) not so easy to find on different environments.- Some things easier to implement in C.

Often a question of personal taste and the fact which language you learned first.

Different generations of Fortran1:

1. FORTRAN662. FORTRAN77 These two must not be used anymore!

3. Fortran 90 These two are now in use.4. Fortran 95 No large differences between F90 and F95.

5. Fortran 2003 Most features implemented by most compilers.

6. Fortran 2008 Some features implemented by many compilers.

- From now on I call the language simply Fortran or F90+ (meaning all other version except FORTRAN77)- If there is need to distinguish between 90, 95, 2003, and 2008 versions will tell it explicitly. - A good source of compiler support of the latest Fortran features can be found at

http://www.fortranplus.co.uk/resources/fortran_2003_2008_compiler_support.pdf

1. For more info on history of programming languages see e.g. http://www.levenez.com/lang/

Tools for High Performance Computing 2013: 2. Introduction to Fortran 2

Introduction to Fortran Material

- Fortran 95/2003 Explained by Michael Metcalf, John Reid and Malcolm Cohen, Oxford University Press- A newer version Modern Fortran Explained by the same authors and publisher

- CSC has published a free Fortran 95/2003 guide in Finnish (see http://www.csc.fi/csc/julkaisut/oppaat)

- gfortran home page: http://gcc.gnu.org/wiki/GFortran

- Small example programs presented in these lecture notes can be downloaded from http://www.physics.helsinki.fi/courses/s/stltk/progs/

- IBM has a nice reference manual for their compiler at http://www-01.ibm.com/software/awdtools/fortran/xlfortran/library/(choose the XL Fortran Advanced Edition for Linux PDF files and there Language Reference...)

- Note, however, that it is a language reference manual not a textbook for learning Fortran.

Tools for High Performance Computing 2013: 2. Introduction to Fortran 3

Introduction to Fortran Structure of a Fortran program:

program name_of_the_program! This is a comment implicit none type and variable definitions

. . .

executable statements . . .

stop

contains ! Comment againlocal subroutine definitions...

end program name_of_the_program

- implicit none is the most important statement in F90

- In older Fortrans implicit type definitions were commonly used:

variables beginning with i,j,k,l,m,n are integersothers are real.

- If you leave out implicit none the same convention is used in F90+! Then...you get what you deserve!

- Subroutines defined after contains statement are local in the sense that they are not visible to any other routines and that they see the local variables.

- Comments begin with exclamation mark. The rest of the line is ignored (just as the //-comment in C++).

Urban legend (?): Fortran compilers used to ignore white space. This combinedwith implicit typing caused a typo go unnoticed: in a do loop start a period waswritten instead of a comma: do 100 i=1,10 do 100 i=1.10 . This was interpreted as an assignment: do100i=1.10 i.e. no loop at all! Note that Fortran is case insensitive!

Tools for High Performance Computing 2013: 2. Introduction to Fortran 4

Introduction to Fortran Structure of a Fortran procedure definition

subroutine never returns a value (as a C function defined as void)- Refer to subroutine as call sub(p1,p2)

function always returns a value- Refer to function simply by using it in an expression or as a procedure parameter x=func(p1,p2)

subroutine sub(p1,p2) implicit none procedure parameter definitions local type and variable definitions

. . .

executable statements . . .

return

contains ! Comment again local subroutine definitions . . .

end subroutine sub

function func(p1,p2) implicit none definitions of the function and it parameters local type and variable definitions

. . .

executable statements . . .func=... ! assign a return value

return

contains ! Comment again local subroutine definitions . . .

end function func

Tools for High Performance Computing 2013: 2. Introduction to Fortran 5

Introduction to Fortran Built-in datatypes

- Integer

integer,parameter :: ik=selected_int_kind(9)integer(kind=ik) :: i

- With the kind keyword and intrinsic function selected_int_kind() one can select the size of the datatype:selected_int_kind(p) returns the kind type that represents all integers in the range .

- Example

- Default integer (i.e. if you say only integer :: i) is in most environments of size 32 bits (corresponding to ).

parameter qualifier tells that thisobject is a constant; its value cannot be changed.

n 10p n 10p

program intkind implicit none integer,parameter :: ik=selected_int_kind(2) integer(kind=ik) :: i integer :: j

i=1 do j=1,20 i=i*2 write(0,*) j,i end do

stopend program intkind

progs> gfortran intkind.f90progs> ./a.out 1 2 2 4 3 8 4 16 5 32 6 64 7 -128 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0

Change from 2 to 4 in the codeprogs> a.out 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024 11 2048 12 4096 13 8192 14 16384 15 -32768

p

p 9=

Tools for High Performance Computing 2013: 2. Introduction to Fortran 6

Introduction to Fortran- Real

integer,parameter :: rk=selected_real_kind(6,15)real(kind=rk) :: x (or more simply real(rk) :: x)

- Parameters of the function selected_real_kind(p,r) tell the minimum decimal precision (p) and range of the exponent (r) of the real type.

- Example

- Default real (using only real :: x) is in most systems 32 bit long (i.e. corresponds to float in C). - But not always!

- This kind-qualifier is exactly the feature that should prevent problems with different word lengths.- By using it your code is easier to port to other systems. (Well, in practice, most machines today use IEEE floating

point numbers)

program realkind implicit none integer,parameter :: rk=selected_real_kind(6,15) real(kind=rk) :: x integer :: j

x=1.0 do j=1,64 x=x*100.0 write(0,*) j,x end do

stopend program realkind

progs> gfortran realkind.f90progs> a.out 1 100.0000 2 10000.00 3 1000000. 4 1.0000000E+08 5 1.0000000E+10 ... 18 1.0000001E+36 19 1.0000001E+38 20 +Infinity 21 +Infinity 22 +Infinity ...

Use (10,40) instead of (6,15)progs> ./a.out 1 100.000000000000 2 10000.0000000000 3 1000000.00000000 4 100000000.000000 ... 153 9.999999999999997E+305 154 9.999999999999996E+307 155 +Infinity 156 +Infinity ...

Tools for High Performance Computing 2013: 2. Introduction to Fortran 7

Introduction to Fortran- Complex

integer,parameter :: rk=selected_real_kind(10,40)complex(kind=rk) :: zz=(1.0,1.0)

- Boolean

logical :: firsttime=.true....if (firsttime) then

...firsttime=.false.

end if

- Strings

character(len=80) :: cc=No, joo! write(0,*) c

- Substring: c(5:10), c(:4), c(11:)

- Note that string handling in F90 is much easier than in C, methinks:

Assigning and concatenationcharacter(len=80) :: c,d,ec=firstd=laste=trim(c)// and //trim(d)write(0,*) e

Comparisoncharacter(len=80) :: c,dc=first...d=lastif (c==d) then

write(0,*) equal!endif

Function trim strips the trailing spaces from the string.

Tools for High Performance Computing 2013: 2. Introduction to Fortran

Recommended