Introduction to Fortran - ?· Introduction to Fortran 1 Introduction to Fortran • Why Fortran? - A…

Download Introduction to Fortran - ?· Introduction to Fortran 1 Introduction to Fortran • Why Fortran? - A…

Post on 06-Sep-2018

214 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

<ul><li><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 1</p><p>Introduction to Fortran Why Fortran?</p><p>- 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.</p><p> Why not Fortran?- Compilers (particularly free ones) not so easy to find on different environments.- Some things easier to implement in C.</p><p> Often a question of personal taste and the fact which language you learned first.</p><p> Different generations of Fortran1:</p><p>1. FORTRAN662. FORTRAN77 These two must not be used anymore!</p><p>3. Fortran 90 These two are now in use.4. Fortran 95 No large differences between F90 and F95. </p><p>5. Fortran 2003 Most features implemented by most compilers.</p><p>6. Fortran 2008 Some features implemented by many compilers.</p><p>- 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 </p><p>http://www.fortranplus.co.uk/resources/fortran_2003_2008_compiler_support.pdf</p><p>1. For more info on history of programming languages see e.g. http://www.levenez.com/lang/</p><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 2</p><p>Introduction to Fortran Material </p><p>- 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</p><p>- CSC has published a free Fortran 95/2003 guide in Finnish (see http://www.csc.fi/csc/julkaisut/oppaat)</p><p>- gfortran home page: http://gcc.gnu.org/wiki/GFortran</p><p>- Small example programs presented in these lecture notes can be downloaded from http://www.physics.helsinki.fi/courses/s/stltk/progs/</p><p>- 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...)</p><p>- Note, however, that it is a language reference manual not a textbook for learning Fortran.</p></li><li><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 3</p><p>Introduction to Fortran Structure of a Fortran program:</p><p>program name_of_the_program! This is a comment implicit none type and variable definitions</p><p> . . .</p><p> executable statements . . .</p><p> stop</p><p>contains ! Comment againlocal subroutine definitions...</p><p>end program name_of_the_program</p><p>- implicit none is the most important statement in F90</p><p>- In older Fortrans implicit type definitions were commonly used: </p><p>variables beginning with i,j,k,l,m,n are integersothers are real.</p><p>- If you leave out implicit none the same convention is used in F90+! Then...you get what you deserve!</p><p>- 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.</p><p>- Comments begin with exclamation mark. The rest of the line is ignored (just as the //-comment in C++).</p><p>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!</p><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 4</p><p>Introduction to Fortran Structure of a Fortran procedure definition</p><p> subroutine never returns a value (as a C function defined as void)- Refer to subroutine as call sub(p1,p2)</p><p> function always returns a value- Refer to function simply by using it in an expression or as a procedure parameter x=func(p1,p2)</p><p>subroutine sub(p1,p2) implicit none procedure parameter definitions local type and variable definitions</p><p> . . .</p><p> executable statements . . .</p><p> return</p><p>contains ! Comment again local subroutine definitions . . .</p><p>end subroutine sub</p><p>function func(p1,p2) implicit none definitions of the function and it parameters local type and variable definitions</p><p> . . .</p><p> executable statements . . .func=... ! assign a return value</p><p> return</p><p>contains ! Comment again local subroutine definitions . . .</p><p>end function func</p></li><li><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 5</p><p>Introduction to Fortran Built-in datatypes</p><p>- Integer</p><p>integer,parameter :: ik=selected_int_kind(9)integer(kind=ik) :: i</p><p>- 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 .</p><p>- Example</p><p>- Default integer (i.e. if you say only integer :: i) is in most environments of size 32 bits (corresponding to ).</p><p>parameter qualifier tells that thisobject is a constant; its value cannot be changed.</p><p>n 10p n 10p</p><p>program intkind implicit none integer,parameter :: ik=selected_int_kind(2) integer(kind=ik) :: i integer :: j</p><p> i=1 do j=1,20 i=i*2 write(0,*) j,i end do</p><p> stopend program intkind</p><p>progs&gt; gfortran intkind.f90progs&gt; ./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</p><p>Change from 2 to 4 in the codeprogs&gt; 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>p</p><p>p 9=</p><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 6</p><p>Introduction to Fortran- Real</p><p>integer,parameter :: rk=selected_real_kind(6,15)real(kind=rk) :: x (or more simply real(rk) :: x)</p><p>- 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.</p><p>- Example</p><p>- Default real (using only real :: x) is in most systems 32 bit long (i.e. corresponds to float in C). - But not always! </p><p>- 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 </p><p>point numbers)</p><p>program realkind implicit none integer,parameter :: rk=selected_real_kind(6,15) real(kind=rk) :: x integer :: j</p><p> x=1.0 do j=1,64 x=x*100.0 write(0,*) j,x end do</p><p> stopend program realkind</p><p>progs&gt; gfortran realkind.f90progs&gt; 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 ...</p><p>Use (10,40) instead of (6,15)progs&gt; ./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 ...</p></li><li><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 7</p><p>Introduction to Fortran- Complex</p><p>integer,parameter :: rk=selected_real_kind(10,40)complex(kind=rk) :: zz=(1.0,1.0) </p><p>- Boolean</p><p>logical :: firsttime=.true....if (firsttime) then</p><p>...firsttime=.false.</p><p>end if</p><p>- Strings</p><p>character(len=80) :: cc=No, joo! write(0,*) c</p><p>- Substring: c(5:10), c(:4), c(11:)</p><p>- Note that string handling in F90 is much easier than in C, methinks:</p><p>Assigning and concatenationcharacter(len=80) :: c,d,ec=firstd=laste=trim(c)// and //trim(d)write(0,*) e</p><p>Comparisoncharacter(len=80) :: c,dc=first...d=lastif (c==d) then</p><p>write(0,*) equal!endif</p><p>Function trim strips the trailing spaces from the string.</p><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 8</p><p>Introduction to Fortran- Own type definitions (structs)</p><p>program typedeftest implicit none type person character(len=20) :: first,last integer :: age end type person type(person) :: student student%first=Matti student%last=Meiklinen student%age=75</p><p>! Not sure whether outputting the whole! struct is standard comforming.</p><p> write(6,*) student</p><p> stopend program typedeftest</p><p>progs&gt; gfortran typedef.f90progs&gt; a.out Matti Meiklinen 75</p></li><li><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 9</p><p>Introduction to Fortran Conditional execution</p><p> if (logical expression) thenexecutable statements</p><p>else if (logical expression) thenexecutable statements</p><p>elseexecutable statements</p><p>end if</p><p>program casetest implicit none integer :: i character(len=10) :: c read(5,*) i</p><p> select case(i) case(1) c=one case(2) c=two case(3) c=three case default c=many end select write(0,*) c stopend program casetest</p><p>If-then-elseCase</p><p>- Instead of integers you can also use logical and character expressions in the case construct.</p><p>- Indivdual cases can also be lists of values and ranges, e.g. case(4:10,12,14)</p><p>- No need to use break commands as in C.</p><p>In case there is only one statement after the if construc-tion then-endif pair can be left out; e.g.;if (i&gt;10) a(i)=0.0 </p><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 10</p><p>Introduction to Fortran Comparison operators</p><p>- In FORTRAN77 the comparison operators had the dotted forms given below in the last column:</p><p>- Both forms can be used in F90+.</p><p> Logical operators- These only have the dotted forms.</p><p>Operator Newform</p><p>Old form</p><p>equal == .eq.</p><p>not equal /= .ne.</p><p>less &lt; .lt.</p><p>greater &gt; .gt.</p><p>less or equal</p><p>= .ge.</p><p>Operator FortranNegation .not.</p><p>Logical and .and.</p><p>Logical or .or.</p><p>Logical equivalence .eqv.</p><p>Logical inequivalence .neqv.</p><p>Examples:</p><p>.not.(ac) </p><p>a&gt;=b .or. b&gt;=c</p></li><li><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 11</p><p>Introduction to Fortran Iteration (do loop)</p><p>[name:] do iter_var=first,last[,step]executable statements</p><p>end do [name]</p><p>- Inside the do loop you can use the statement exit to stop the iteration (jump out of the loop) or cycle to stop the cur-rent iteration and continue the next one.</p><p>- Examples:</p><p>- In F77 real iteration variables were allowed. This is no more the case in F90+.- Naming the loops is helpful if you have many loops within each other and want e.g. jump out from the outermost one. - It also makes the code more readable.</p><p>loop1: do i=1,ndo j=i,m</p><p>if (a(j)&gt;b(i)) thena(j)=b(i)cycle loop1</p><p>end ifend do</p><p>end do loop1</p><p>Do loop without an iteration variable:</p><p>do executable statements...if (x&gt;-1.0) exit ! To prevent an infinite loop</p><p>end do</p><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 12</p><p>Introduction to Fortran Arrays</p><p>- In F77 only statically allocated arrays were allowed.- F90 introduced allocatable arrays. </p><p>- Some restrictions of allocatable arrays: they cant be used in 1. formal parameters of subroutines (use modules instead)2. function results3. user datatypes (use pointers instead)</p><p>Static allocation</p><p>integer,dimension(10) :: kreal,dimension(10,10) :: xreal,dimension(100) :: x1real(kind=rk),dimension(4,4,4) :: y</p><p>Or more tersely</p><p>integer :: k(10)real :: x(10,10),x1(100)real(kind=rk) :: y(4,4,4)</p><p>Dynamic allocation</p><p>real(kind=rk),dimension(:),allocatable :: xreal(kind=rk),dimension(:,:),allocatable :: yinteger :: n,err...print *,Give array size.read *,n allocate(x(n),y(0:n,0:n),stat=err)if (err/=0) thenprint *,"Could not allocate."stop</p><p>end if...x=1.0y=2.0...deallocate(x,y)</p></li><li><p> Tools for High Performance Computing 2013: 2. Introduction to Fortran 13</p><p>Introduction to Fortran- Arrays or sections of arrays can be easily handled:</p><p>real :: x(100),y(100),z(100)x=1.0x(4:6)=2.0y=2.0z=x*y ! Multiply element by element</p><p>- More about array handling later.</p><p> Pointers (material from http://www.personal.psu.edu/faculty/j/h/jhm/f90/lectures/42.html)- Pointers in Fortran are more abstract than those in C.- Those variables that you want to be pointed by pointers must have the target qualifier: </p><p>real, target :: a, b(1000), c(10,10)integer, target :: i, j(100), k(20,20)</p><p>- Pointers itself have the pointer qualifier:real, pointer :: pa, aptr, pb(:), pc1(:), pc2(:,:)</p><p>- To associate a pointer with a variable you use the =&gt; operatorpa =&gt; a</p><p>- Now you can use pa and a interchangibly:pa = 1.23456print *, a = , a</p><p>- With array pointer you can do tricks aspb =&gt; bpb =&gt; b(101:200)pc2 =&gt; c(3:5, 4:6)</p><p>- Pointers in F90+ can be seen as a generalization of the equivalence1 statement in F77.- Personal opinion: Not much needed in HPC.</p><p>1. equivalence(x,y)--&gt; variables x and y can now be used interchangibly. In F77, however, this is a static definition. A good tool f...</p></li></ul>

Recommended

View more >