Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Managed by UT-Battelle for the Department of Energy Introduction to FORTRAN Suzanne Parete-Koon OLCF."— Presentation transcript:

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

2 2Managed by UT-Battelle for the Department of Energy 2

3 3Managed by UT-Battelle for 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

4 4Managed by UT-Battelle for 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.  2 nd 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

5 5Managed by UT-Battelle for the Department of Energy Setup 4 steps Step 1 Login and setup pin Log in to the machine: ssh std00##@mars.nics.utk.edustd00##@mars.nics.utk.edu Follow the prompts to set up your PIN. Step 2 Start an interactive job qsub - I -A UT-NTNLEDU –l size=16,walltime=02:00:00 U. S. Department Of Energy 5 Upper case i Lowercase L  

6 6Managed by UT-Battelle for the Department of Energy Setup Step 3 Copy the code files to your lustre directory cp –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 compiler Module swap PrgEnv-cray PrgEnv-gnu U. S. Department Of Energy 6

7 7Managed by UT-Battelle for 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

8 8Managed by UT-Battelle for the Department of Energy Program Structure U. S. Department Of Energy 8 Program program name Variable declarations Execution statements Subprograms End program name

9 9Managed by UT-Battelle for 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 program hello write(*,*)“Hello World!” end program hello Vi Cheat sheet To start vi hello.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q Vi Cheat sheet To start vi hello.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q

10 10Managed by UT-Battelle for 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 program hello write(*,*)“Hello World” end program hello Vi Cheat sheet To start vi hello.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q Vi Cheat sheet To start vi hello.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q

11 11Managed by UT-Battelle for the Department of Energy Hello World Fortran 11

12 12Managed by UT-Battelle for 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

13 13Managed by UT-Battelle for the Department of Energy Variable Declaration Syntax Type :: variable name  Integer :: x  Real :: fraction  Character (len= 3) :: three_letter_word 13

14 14Managed by UT-Battelle for 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 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+.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q Vi Cheat sheet To start vi hello+.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q

15 15Managed by UT-Battelle for 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

16 16Managed by UT-Battelle for 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 basics program 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 !This program demonstrates the basics program 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

17 17Managed by UT-Battelle for 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

18 18Managed by UT-Battelle for 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:: I Real :: a a=1.01 do i=1,10 a=a+i enddo write(*,*) a Integer:: I Real :: a a=1.01 do i=1,10 a=a+i enddo write(*,*) a

19 19Managed by UT-Battelle for 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 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++.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q Vi Cheat sheet To start vi hello++.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q

20 20Managed by UT-Battelle for the Department of Energy Hello World++ Fortran 20

21 21Managed by UT-Battelle for 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.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q Vi Cheat sheet To start vi dogyears.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q

22 22Managed by UT-Battelle for 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 ! 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

23 23Managed by UT-Battelle for 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 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

24 24Managed by UT-Battelle for the Department of Energy Dog Years

25 25Managed by UT-Battelle for 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

26 26Managed by UT-Battelle for the Department of Energy Selection FORTRAN if (x < 10)then write(*,*) “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

27 27Managed by UT-Battelle for 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+.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q Vi Cheat sheet To start vi dogyears+.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q

28 28Managed by UT-Battelle for 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 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

29 29Managed by UT-Battelle for the Department of Energy Dog years+

30 30Managed by UT-Battelle for 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) a  b =a*bCos(θ) Used to find the angle between vectors Use to find projection of a vector etc. U. S. Department Of Energy 30

31 31Managed by UT-Battelle for 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

32 32Managed by UT-Battelle for 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.

33 33Managed by UT-Battelle for 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] 0A[0][1] 1A[0][2] 2A[0][3] 3 A[1][0] 4A[1][1] 5A[1][2] 6A[1][3] 7 A[2][0] 8A[2][1] 9A[2][2] 10A[2][3] 11

34 34Managed by UT-Battelle for 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 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

35 35Managed by UT-Battelle for the Department of Energy Arrays fundamentals

36 36Managed by UT-Battelle for 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. V i Cheat sheet To start vi array+.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q V i Cheat sheet To start vi array+.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q

37 37Managed by UT-Battelle for 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 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

38 38Managed by UT-Battelle for the Department of Energy Arrays fundamentals

39 39Managed by UT-Battelle for 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

40 40Managed by UT-Battelle for 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 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

41 41Managed by UT-Battelle for the Department of Energy Arrays fundamentals

42 42Managed by UT-Battelle for the Department of Energy Questions? http://www.olcf.ornl.gov

43 43Managed by UT-Battelle for 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**2 end subroutine square program sq implicit none integer :: i,isq,icub i = 4 call square(i,isq) print*,"i,i^2=",i,isq,icub end program sq

44 44Managed by UT-Battelle for 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] 0A[0][1] 1A[0][2] 2A[0][3] 3 A[1][0] 4A[1][1] 5A[1][2] 6A[1][3] 7 A[2][0] 8A[2][1] 9A[2][2] 10A[2][3] 11


Download ppt "Managed by UT-Battelle for the Department of Energy Introduction to FORTRAN Suzanne Parete-Koon OLCF."

Similar presentations


Ads by Google