1 / 44

Introduction to FORTRAN

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.

adila
Download Presentation

Introduction to FORTRAN

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to FORTRAN Suzanne Parete-Koon OLCF

  2. “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. 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

  4. Setup 4 steps Step 1 Login and setup pin Log in to the machine: ssh std00##@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   Lowercase L Upper case i

  5. 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

  6. 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. Program Structure Program program name Variable declarations Execution statements Subprograms End program name

  8. Hello World Fortran Type this in: vi hello.f90 Type ito start writing. Use the delete key to delete Hit the esc key to stop writing. Type :w to save Type :qto exit 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 program hello write(*,*)“Hello World!” end program hello

  9. Hello World Fortran hello.f90 write(*,*) – means write in the default format, to the screen. To compile: gfortan hello.f90 To run: ./a.out 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 program hello write(*,*)“Hello World” end program hello

  10. Hello World Fortran

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

  12. Variable Declaration Syntax Type :: variable name • Integer :: x • Real :: fraction • Character (len= 3) :: three_letter_word

  13. Hello+ World Fortran cp hello.f90 hello+.f90 vi hello+.f90 Compilegfortan hello+.f90 To run./a.out 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 program hello implicit none integer:: x character (len=12):: phrase x=10 phrase="hello world!" write(*,*) phrase, x end program hello

  14. 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. Comments • Everything following a !is a comment and will be ignored by the compiler !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

  16. 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)

  17. Loops Fortran • The Do loop Syntax integer :: index . . . do index=min,max operation(index) enddo Integer:: I Real :: a a=1.01 do i=1,10 a=a+i enddo write(*,*) a

  18. Hello++ World Fortran cp hello+.f90 hello++.f90 vi hello++.f90 To compile gfortan hello+.f90 To run:./a.out 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 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

  19. Hello World++ Fortran

  20. 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. 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

  21. 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. 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

  22. Fortran your turn: Dog years 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

  23. Dog Years

  24. Selection FORTRAN Syntax for if statements IF (logical-expression) THEN statements-1 ELSE statements-2 END IF

  25. 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

  26. 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) 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

  27. Fortran your turn: Dog years 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

  28. Dog years+

  29. Arrays Fundamentals • An array is a collection of data of the sametype. • 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.

  30. 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.

  31. 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.

  32. 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.

  33. Arrays fundamentals • This program is in /lustre/medusa/std00##/fortran/ array.f90 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

  34. Arrays fundamentals

  35. 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+.f90 To write i Delete if in write mode delete if not in write mode x To stop writing esc Save :w Exit :q

  36. Arrays fundamentals 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 To Compile: gfortran array.f90 To run ./a.out

  37. Arrays fundamentals

  38. 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

  39. Arrays fundamentals 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 To Compile: gfortran array.f90 To run ./a.out

  40. Arrays fundamentals

  41. Questions? http://www.olcf.ornl.gov

  42. Subroutines Fortran • Recurring code Subroutine(parameters) body end subroutine Program mainpr call subroutine(par1) do something with par1 End mainpr 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

  43. Row major C Colum major FORTRAN C fortran

More Related