introduction to fortran

44
Managed by UT-Battelle for the Department of Energy Introduction to FORTRAN Suzanne Parete-Koon OLCF

Upload: adila

Post on 19-Mar-2016

31 views

Category:

Documents


0 download

DESCRIPTION

Introduction to FORTRAN. Suzanne Parete-Koon OLCF. “ Fortran changed the terms of communication between humans and computers. ” ~ New York Times. FOR mula TRAN slation Language – developed by IBM in the 1950s. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to FORTRAN

Managed by UT-Battellefor the Department of Energy

Introduction to FORTRAN

Suzanne Parete-Koon

OLCF

Page 2: Introduction to FORTRAN

2 Managed by UT-Battellefor the Department of Energy 2

Page 3: Introduction to FORTRAN

3 Managed by UT-Battellefor the Department of Energy

“Fortran changed the terms of communication between humans and computers.” ~ New York Times • FORmula TRANslation Language – developed by IBM

in the 1950s.

• Still widely used today. ~50% of OLCF and NICS production simulation codes use it.

• Has a very user friendly syntax

3

Page 4: Introduction to FORTRAN

4 Managed by UT-Battellefor the Department of Energy

Fortran basics Variables Selection Loops Arrays First hour, we will get used to FORTRAN –Hello World,Variables,

loops, selection, dog years. 2nd hour we will use what what we learned with arrays and dot

products. When this session is over you should be able to write a “hello

world” in Fortran

U. S. Department Of Energy 4

Page 5: Introduction to FORTRAN

5 Managed by UT-Battellefor the Department of Energy

Setup 4 steps

Step 1 Login and setup pinLog in to the machine:

ssh std00##@mars.nics.utk.edu

Follow the prompts to set up your PIN.

Step 2 Start an interactive jobqsub -I -A UT-NTNLEDU –l size=16,walltime=02:00:00

U. S. Department Of Energy 5

Upper case i Lowercase L

Page 6: Introduction to FORTRAN

6 Managed by UT-Battellefor the Department of Energy

Setup

Step 3 Copy the code files to your lustre directorycp –r /lustre/medusa/std0001/fortran /lustre/medusa/std00##/

Change to the fortran folder your lustre dir.

cd /lustre/medusa/std00##/fortran

Step 4 Switch to the gnu compilerModule swap PrgEnv-cray PrgEnv-gnu

U. S. Department Of Energy 6

Page 7: Introduction to FORTRAN

7 Managed by UT-Battellefor the Department of Energy

Step 0 : vi Tutorial

You will be writing this code with a text editor.

To create an open a file with vi:

vi filename.f90

To write in vi, type i, to stop writing hit esc.

To delete, when in write mode, delete key

To save hit esc :w

To quit hit esc :q

7

Page 8: Introduction to FORTRAN

8 Managed by UT-Battellefor the Department of Energy

Program Structure

U. S. Department Of Energy 8

Program program name

Variable declarations

Execution statements

Subprograms

End program name

Page 9: Introduction to FORTRAN

9 Managed by UT-Battellefor the Department of Energy

Hello World Fortran

U. S. Department Of Energy 9

Type this in:

vi hello.f90

Type i to start writing.

Use the delete key to delete

Hit the esc key to stop writing.

Type :w to save

Type :q to exit

program hello write(*,*)“Hello World!” end program hello

Vi Cheat sheet

To start vi hello.f90To write iDelete if in write mode delete if not in write mode xTo stop writing escSave :wExit :q

Page 10: Introduction to FORTRAN

10 Managed by UT-Battellefor the Department of Energy

Hello World Fortran

U. S. Department Of Energy 10

hello.f90

write(*,*) – means write in the

default format, to the screen.

To compile:

gfortan hello.f90

To run:

./a.out

program hello write(*,*)“Hello World” end program hello

Vi Cheat sheet

To start vi hello.f90To write iDelete if in write mode delete if not in write mode xTo stop writing escSave :wExit :q

Page 11: Introduction to FORTRAN

11 Managed by UT-Battellefor the Department of Energy

Hello World Fortran

11

Page 12: Introduction to FORTRAN

12 Managed by UT-Battellefor the Department of Energy

Variables FORTRAN FORTRAN supports six different data types: Integer !32 bits Real !32, 64 bits Double precision !64 bits Character Complex Logical

U. S. Department Of Energy 12

Page 13: Introduction to FORTRAN

13 Managed by UT-Battellefor the Department of Energy

Variable Declaration Syntax

Type :: variable nameInteger :: xReal :: fractionCharacter (len= 3) :: three_letter_word

13

Page 14: Introduction to FORTRAN

14 Managed by UT-Battellefor the Department of Energy

Hello+ World Fortran

U. S. Department Of Energy 14

cp hello.f90 hello+.f90

vi hello+.f90

Compile gfortan hello+.f90

To run./a.out

program hello implicit none integer:: x character (len=12):: phrase x=10 phrase="hello world!" write(*,*) phrase, x end program hello

Vi Cheat sheet

To start vi hello+.f90To write iDelete if in write mode delete if not in write mode xTo stop writing escSave :wExit :q

Page 15: Introduction to FORTRAN

15 Managed by UT-Battellefor the Department of Energy

Implicit None? In the 1950s computers only had a few KB of memory Programs needed to be as short as possible to fit Fortran Variables were implicit- you did not have to declare

them. All variables starting with i, j, k, l, m and n, if not declared, are

of the INTEGER type by default. Typos were not caught by the compiler

numberyears=nubmeryear+1

Use “implicit none” unless you like unnecessary head aches.

15

Page 16: Introduction to FORTRAN

16 Managed by UT-Battellefor the Department of Energy

Comments Everything following a ! is a comment and will be ignored by the compiler

U. S. Department Of Energy 16

!This program demonstrates the basicsprogram hello implicit none ! No implicit variables integer:: x ! Number of iterations character (len=12):: phrase x=10 phrase="hello world!" write(*,*) phrase, x ! Write to screen end program hello !End program

Page 17: Introduction to FORTRAN

17 Managed by UT-Battellefor the Department of Energy

Arithmetic Operations + Addition z= y+x - Subtraction y= z-x * multiplication z=y*x / Division y=z/x ** Exponentiation three_squared= 3**2

Operator Priority.

** is the highest; * and / are the next, followed by + and – Use () to ensure the wanted priority

Age=20+7*(h-2)

U. S. Department Of Energy 17

Page 18: Introduction to FORTRAN

18 Managed by UT-Battellefor the Department of Energy

Loops Fortran

The Do loop Syntax

integer :: index

. . .

do index=min,max

operation(index)

enddo

U. S. Department Of Energy 18

Integer:: IReal :: aa=1.01

do i=1,10 a=a+i enddo write(*,*) a

Page 19: Introduction to FORTRAN

19 Managed by UT-Battellefor the Department of Energy

Hello++ World Fortran

U. S. Department Of Energy 19

cp hello+.f90 hello++.f90

vi hello++.f90

To compile gfortan hello+.f90

To run:./a.out

program hello implicit none integer:: x, i character (len=12):: phrase x=10 phrase="hello world!” do i=1,x write(*,*) phrase, i enddo end program hello

Vi Cheat sheet

To start vi hello++.f90To write iDelete if in write mode delete if not in write mode xTo stop writing escSave :wExit :q

Page 20: Introduction to FORTRAN

20 Managed by UT-Battellefor the Department of Energy

Hello World++ Fortran

20

Page 21: Introduction to FORTRAN

21 Managed by UT-Battellefor the Department of Energy

Fortran your turn: Dog years If a dog ages 7 dog years for each human year, write a program

that coverts human years to dog years and writes the result to the screen. Do this for dogs between the ages of 1 and 10.

Start from scratch if you like

OR

vi fortran/dogyears.f90

For hints.

21

Vi Cheat sheet

To start vi dogyears.f90To write iDelete if in write mode delete if not in write mode xTo stop writing escSave :wExit :q

Page 22: Introduction to FORTRAN

22 Managed by UT-Battellefor the Department of Energy

Fortran your turn: Dog years

22

! If a dog ages 7 dog years for each ! human year, write a program that coverts !human years to dog years ! and writes the result. program dogyears implicit none ! declare two integers ,h and d, ! to hold human years and dog years ! Do a loop with h as the loop index. ! convert human years to dog years

!write the result

! Close the loop end program dogyears

Page 23: Introduction to FORTRAN

23 Managed by UT-Battellefor the Department of Energy

Fortran your turn: Dog years

23

program dogyears implicit none ! declare two integers ,h and d, !to hold human and dog years integer :: d,h ! Do a loop with h as the loop index. do h=1,10 ! convert human years to dog years

d=7*h !write the result write(*,*) "In dog years, fido is ",d,".” ! Close the loop enddo end program dogyears

Compile gfortran dogyears.f90 run ./a.out

Page 24: Introduction to FORTRAN

24 Managed by UT-Battellefor the Department of Energy

Dog Years

Page 25: Introduction to FORTRAN

25 Managed by UT-Battellefor the Department of Energy

Selection FORTRAN

Syntax for if statements

IF (logical-expression) THEN

statements-1

ELSE

statements-2

END IF

U. S. Department Of Energy 25

Page 26: Introduction to FORTRAN

26 Managed by UT-Battellefor the Department of Energy

Selection FORTRAN

if (x < 10)thenwrite(*,*) “low”

else write(*,*) “high”Endif

The conditions (a partial list) < less than> greater than= equal too<= less than or equal to

U. S. Department Of Energy 26

Page 27: Introduction to FORTRAN

27 Managed by UT-Battellefor the Department of Energy

Fortran your turn: Dog years. Use a selection statement to modify dogyears so that the dog ages 10 years per

human year for the first two years and 7 years per human year for the rest of its life.

cp dogyears.f90 dogyears+.f90

vi dogyears+.f90

Hints

If dog is 2 or less

d= h*10

Else

d=20+7*(h-2)

27

Vi Cheat sheet

To start vi dogyears+.f90To write iDelete if in write mode delete if not in write mode xTo stop writing escSave :wExit :q

Page 28: Introduction to FORTRAN

28 Managed by UT-Battellefor the Department of Energy

Fortran your turn: Dog years

28

program dogyears

implicit none ! declare two integers,h,d, to hold human/dog years integer :: d, do h=1,10 if(h<3)then d=10*h write(*,*) "In dog years, fido is ",d,"." else d=20+7*(h-2) write(*,*) "In dog years, fido is ",d,"." endif enddo

end program dogyears

Page 29: Introduction to FORTRAN

29 Managed by UT-Battellefor the Department of Energy

Dog years+

Page 30: Introduction to FORTRAN

30 Managed by UT-Battellefor the Department of Energy

Arrays Fundamentals An array is a collection of data of the same type. Used for vector and matrix operations

We will calculate a vector dot product.

a = (a1,a2,a3) b= (b1,b2,b3)

ab =a*bCos(θ)

Used to find the angle between vectors

Use to find projection of a vector etc.

U. S. Department Of Energy 30

Page 31: Introduction to FORTRAN

31 Managed by UT-Battellefor the Department of Energy

Arrays Fundamentals

An array is a collection of data of the same type.

Syntax:

type, DIMENSION(shape, shape ) :: name1,name2,name3

The rank shown above is 2. A 3 dimensional array would have (shape,shape,shape). Fortran 90 can handle up to rank 7.

The shape is the number of elements in that dimension.

There is one more attribute, extent, that allows you to control where the indices start. The default is to start at 1.

U. S. Department Of Energy 31

Page 32: Introduction to FORTRAN

32 Managed by UT-Battellefor the Department of Energy

Arrays Fundamentals One dimensional

– Real, dimension(3) :: A ! A 1D floating point array with thee elements– integer, dimension (5) :: B ! A 1D integer array with 5 elements

Two dimensional– Real, dimension(2,2):: A ! A 2D array, (2 by 2) – Integer, dimension(2,3):: B ! A 2D array (2 by 3)

– Not covered here, but arrays in Fortran can be allocated, after they are declared.

– Integer, dimension(x,y):: B ! A 2D array x and y can be set later in the program.

Page 33: Introduction to FORTRAN

33 Managed by UT-Battellefor the Department of Energy

Arrays fundamentals

A quick way to set up an array in f90:

integer, dimension(3):: A =(/ 1, 2, 3/)

This gets tricky for multi-D arrays because Fortran is column major.

A[0][0] 0 A[0][1] 1 A[0][2] 2 A[0][3] 3A[1][0] 4 A[1][1] 5 A[1][2] 6 A[1][3] 7

A[2][0] 8 A[2][1] 9 A[2][2] 10 A[2][3] 11

Page 34: Introduction to FORTRAN

34 Managed by UT-Battellefor the Department of Energy

Arrays fundamentals

To Compile gfortran array.f90

To run ./a.out

program array implicit none integer, dimension(3):: A =(/ 1, 2, 3/) integer, dimension(3):: C integer :: i DO i = 1, 3 C(i)=A(i)**2 !square A and store in C write(*,*) i, A(i),C(i) END DO

end program array

– This program is in /lustre/medusa/std00##/fortran/ array.f90

Page 35: Introduction to FORTRAN

35 Managed by UT-Battellefor the Department of Energy

Arrays fundamentals

Page 36: Introduction to FORTRAN

36 Managed by UT-Battellefor the Department of Energy

Arrays fundamentals

Modify array.f90 to give you the dot product of A and B.

Step1. Enter Vector B and multiply the elements of A and B

cp array.f90 array+.f90

Enter a vector B =(/3, 2, 4/).

Multiply A*B and store it in C.

Write A, B, and C to the screen.

Vi Cheat sheet

To start vi array+.f90To write iDelete if in write mode delete if not in write mode xTo stop writing escSave :wExit :q

Page 37: Introduction to FORTRAN

37 Managed by UT-Battellefor the Department of Energy

Arrays fundamentals

To Compile: gfortran array.f90

To run ./a.out

program array implicit none integer, dimension(3):: A = (/ 1, 2, 3/) integer, dimension(3):: B = (/ 3, 2, 4/) integer, dimension(3):: C integer :: i DO i = 1, 3 C(i)=A(i)*B(i) !square A and store in C write(*,*) i, A(i),B(i),C(i) END DO

end program array

Page 38: Introduction to FORTRAN

38 Managed by UT-Battellefor the Department of Energy

Arrays fundamentals

Page 39: Introduction to FORTRAN

39 Managed by UT-Battellefor the Department of Energy

Arrays fundamentals

Step2 Sum the element products.

cp array+.f90 array++.f90

Many ways your could do this, but using just what we’ve covered. .

Create an integer variable called dot.

Initialize dot to 0

Then put dot = dot+c(i) in the loop

Out side the loop

Write(*,*) dot

Page 40: Introduction to FORTRAN

40 Managed by UT-Battellefor the Department of Energy

Arrays fundamentals

To Compile: gfortran array.f90

To run ./a.out

program array implicit none integer, dimension(3):: A = (/ 1, 2, 3/) integer, dimension(3):: B = (/ 3, 2, 4/) integer, dimension(3):: C integer :: dot integer :: i dot=0.0 DO i = 1, 3 C(i)=A(i)*B(i) !square A and store in C dot= dot+C(i) write(*,*) i, A(i),B(i),C(i) END DO write(*,*) "dot",dot end program array

Page 41: Introduction to FORTRAN

41 Managed by UT-Battellefor the Department of Energy

Arrays fundamentals

Page 42: Introduction to FORTRAN

42 Managed by UT-Battellefor the Department of Energy

Questions?

http://www.olcf.ornl.gov

Page 43: Introduction to FORTRAN

43 Managed by UT-Battellefor the Department of Energy

Subroutines Fortran Recurring code

Subroutine(parameters)

body

end subroutine

Program mainpr

call subroutine(par1)

do something with par1

End mainpr

U. S. Department Of Energy 43

subroutine square (i,isquare) integer, intent(in) :: i integer, intent(out) :: isquare isquare = i**2end subroutine square program sq implicit none integer :: i,isq,icub i = 4 call square(i,isq) print*,"i,i^2=",i,isq,icubend program sq

Page 44: Introduction to FORTRAN

44 Managed by UT-Battellefor the Department of Energy

Row major C Colum major FORTRAN

44

A[0][0]0

A[0][1]1

A[0][2]2

A[0][3]3

A[1][0]4

A[1][1]5

. . .

fortran

C

A[0][0] 0 A[0][1] 1 A[0][2] 2 A[0][3] 3A[1][0] 4 A[1][1] 5 A[1][2] 6 A[1][3] 7

A[2][0] 8 A[2][1] 9 A[2][2] 10 A[2][3] 11