1 / 109

Introduction to Fortran

Introduction to Fortran. Jon Johansson May 1, 2007. Downloads. The talk and exercises are available from http://sciviz.aict.ualberta.ca/Fortran/ or http://www.ualberta.ca/AICT/RESEARCH/Courses/2007/Workshop/. Agenda. History, Source Format, Kind Types, gfortran exercise 1

orly
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 Jon Johansson May 1, 2007

  2. Downloads • The talk and exercises are available from • http://sciviz.aict.ualberta.ca/Fortran/ • or • http://www.ualberta.ca/AICT/RESEARCH/Courses/2007/Workshop/

  3. Agenda • History, Source Format, Kind Types, gfortran • exercise 1 • Names, Program Structure, Flow Control • exercise 2 • Flow Control cont., Arrays • exercise 4 • Arrays cont., Input/Output • exercise 3 • Subprograms • exercise 5 • Timing Code, Profiling, OpenMP

  4. Fortran History • Fortran is usually credited with being the first high-level programming language • the first language to use natural language keywords • DO, REAL, INTEGER, … • the first language to be compiled • necessary because the computer can’t understand the programming language

  5. Fortran History • John W. Backus and a group at IBM started to design the IBM Mathematical Formula Translating System, or Fortran0 in 1954 • the work was completed in 1957 • the authors claimed that the resulting code would be as efficient as handcrafted machine code • it was pretty close

  6. Fortran History • The IBM 704 Data Processing System was a large-scale computer designed for engineering and scientific calculations • Fortran included many features that were specific to the IBM 704, the first computer on which it was implemented http://www.fh-jena.de/~kleine/history/languages/FortranAutomaticCodingSystemForTheIBM704.pdf

  7. Fortran History • "The 704 is the first large-scale, commercially available computer to employ fully automatic floating point arithmetic commands. …” • IBM 704 speed: • multiplies or divides in 240 microseconds • approximately 4,000 operations per second • floating point addition or subtraction operations require 84 microseconds • approximately 12,000 operations per second • 704 memory: • up to 32,768 36-bit words (32 kilo-words)

  8. Fortran Standards

  9. Fortran History • Each new version of Fortran adds extensions to the language while retaining compatibility with previous versions • For a long time Fortran was the dominant language of programming in scientific and engineering applications • Now mostly used in High Performance Computing • lots of legacy codes being maintained and developed

  10. Source Code Format - Fixed • the fixed source format comes from the design of the 704 and the need to use 80 column punch cards • the Hollerith card format: • cards have 12 rows and 80 columns • decimal digits are encoded in rows 0-9 • other characters are encoded using these rows plus rows 11-12 above row 0

  11. Source Code Format - Fixed • source file with one of the extensions *.f, *.f77, [*.for] • columns 1: ‘C’ or ‘*’ start a comment line • columns 1-5: reserved for statement labels • column 6: a character here is used to indicate that a statement has been continued from the previous statement • can use any character except the number zero • columns 7-72: Fortran statements go here • columns 73-80: characters past column 72 are ignored

  12. Source Code Format - Free • source file with one of the extensions: *.f90, *.f95, [*.hpf] • a statement line can be up to 132 characters long • ! indicates that the rest of the line is a comment • this can be anywhere • & at the end of a line continues the line

  13. Fortran Alphabet • Letters • A B C D E F G H I J K L M N O P Q R S T U V W X Y Z • upper and lower case letters are equivalent • Digits • 0 1 2 3 4 5 6 7 8 9 • Special Characters • space ' " ( ) * + - / : = _ ! & $ ; < > % ? , .

  14. Fortran Variable Types • Fortran has five intrinsic variable types • INTEGER: a string of digits with an optional sign • 0, 314, -4502 • REAL: decimal or exponential representation • 3.1416 or 3.1416E0 • COMPLEX: real and imaginary parts of type real • (1.23, -5.56) • LOGICAL: 1 to 4 bytes but only 2 values • .FALSE. or .TRUE. • CHARACTER: characters enclosed in “” or ‘’ • ‘One string’“Another string”

  15. Variable Representation • variables are basically pointers to some part of memory • the number of bytes is part of the declaration of the variable ( default sizes are frequently 4 bytes)

  16. Variable Declaration • variables can be declared using the appropriate data type statement: INTEGER i = 4, j = 1, k = 0 REAL radius(100), theta, phi COMPLEX spherical_harm LOGICAL more_data = .TRUE. CHARACTER*80 test_string = ‘Boo’

  17. Integer Representation • integers in Fortran are signed • INTEGER lengths can be 1, 2, 4, or 8 bytes • the default INTEGER length is 4 bytes

  18. Floating Point Representation • there are ANSI/IEEE standards to define how floating point numbers are represented and behave in a computer • IEEE Standard 754-1985 • defines binary floating point arithmetic • IEEE Standard 854-1987 • defines decimal floating point arithmetic

  19. Floating Point Representation • given a certain number of bytes allocated to a floating point number, how do we use the bytes to represent the sign, fractional part and the exponent? • for a 4-byte REAL • 8 bits are for the (signed) exponent • 24 bits are for the (signed) mantissa

  20. Floating Point Representation • Span and Precision of IEEE 754 Floating-Point Formats

  21. Floating Point Representation • processors generally support single and double precision operations in hardware • quadruple precision can be implemented in software • quadruple precision is not required by the Fortran standard but is an extension in many compilers • this can be slow

  22. Fortran Constants - KINDs • Fortran 95 tries to move away from declaring the length of variables with either DOUBLE PRECISION REAL*n • note that the statements REAL*4 and REAL*8 are not part of the Fortran standard, but are widely supported • now use KINDS • this is intended to provide true portability for code

  23. Fortran Constants - KINDs • the ‘KIND number’ is an integer that specifies the length of the variable • one common practice is to use the length of the variable in bytes as the KIND number • the NAG Fortran compiler uses the digits 1, 2, and 3 for the KIND numbers • G77 3.4.6 uses prime numbers for KINDs

  24. Fortran Constants - KINDs KIND(X) • returns the kind of the actual argument • X can be of type INTEGER, REAL, COMPLEX, LOGICAL or CHARACTER • the argument X does not have to be assigned any value KIND(3) ! Integer KIND(12.0) ! Real KIND(1.6D6) ! Double

  25. Fortran Constants - KINDs SELECTED_INT_KIND(R) • returns an integer kind with the requested number of digits • R must be scalar integer • the result of SELECTED_INT_KIND is an integer from zero and upward • if the desired kind is not available you will get -1 • if several implemented types satisfy the condition, the one with the least decimal range is used • if there still are several types or kinds that satisfy the condition, the one with the smallest kind number will be used

  26. Fortran Constants - KINDs SELECTED_REAL_KIND(p, r) • returns the kind for floating-point numbers with numerical precision at least P digits and one decimal exponent range between -R and +R • the parameters P and R must be scalar integers • at least one of P and R must be given. • the result of SELECTED_REAL_KIND is also an integer from zero and upward • if the desired kind is not available, • -1 is returned if the precision is not available • -2 if the exponent range is not available • -3 if no one of the requirements are available • if several implemented types satisfy the condition, the one with the least decimal precision is returned, and if there are several of them, the one with the least kind number is returned

  27. The GNU Fortran Compiler • part of the GNU compiler collection • documentation online at • http://gcc.gnu.org/onlinedocs/gfortran/ • under development since 2000 • still a work in progress • supports most of Fortran 77, 90, 95 and some of 2003

  28. The GNU Fortran Compiler • invoke the compiler with a command of the form gfortran [options] input-file • many of the options are common to gcc with some Fortran specific ones • for example, to compile a program contained in a file named “MyCalc.f90” gfortran –o MyCalc MyCalc.f90 • the ‘–o’ option tells the compiler the name to give the executable

  29. The GNU Fortran Compiler • some useful compiler options -O capital ‘Oh’ • optimize the executable -Onn is an integer from 1 to 3 • the compiler tries to reduce code size and execution time -fbounds-check • check array subscripts against the declared minimum and maximum values -fopenmp • enable the OpenMP extensions

  30. The GNU Fortran Compiler • more useful compiler options -Ldir • add directory dir to the list of directories to be searched for -l -llibrary-name • search the library named library when linking -Idir • Add the directory dir to the head of the list of directories to be searched for header files

  31. Exercise 1 • KIND values • Download the program TestKinds.f90 • Compile and run the program with the commands: gfortran -o TestKinds TestKinds.f90 ./TestKinds • answer the questions in the exercise

  32. Agenda • History, Source Format, Kind Types, gfortran • exercise 1 • Names, Program Structure, Flow Control • exercise 2 • Flow Control cont., Arrays • exercise 4 • Arrays cont., Input/Output • exercise 3 • Subprograms • exercise 5 • Timing Code, Profiling, OpenMP

  33. Fortran Names • a name (identifier) must obey the following rules in Fortran 90/95 • it can have up to 31 characters • the first character is a letter • remaining characters can be letters, digits or the underscore • case of letters does not matter • range, Range, rAnGe all point to the same memory location

  34. Fortran Names • Fortran 77 • names can be up to 6 characters • Fortran 2003 • names can be up to 63 characters

  35. Fortran Program Structure • a Fortran program consists of one or more program units. • a program unit is usually a sequence of statements that define the data environment and the steps necessary to perform calculations • it is terminated by an END statement • a program unit can be either a main program, an external subprogram, a module, or a block data program unit • an executable program contains one main program, and, optionally, any number of the other kinds of program units

  36. every Fortran program must have one and only one main program The keyword PROGRAM is optional it can be followed by the program name END is required as the last statement of the program PROGRAM [name][specification statements][executable statements]... END [PROGRAM [name]] Fortran Program Structure

  37. Fortran Program Structure • Fortran programs, functions and subroutines have the same basic structure • functions and subroutines can be • External - self contained (not necessarily Fortran) • Internal - inside a program unit • Module - member of a module • Fortran 77 has only external procedures

  38. functions and subroutines have the form a function returns a value a subroutine has no return value FUNCTION [name] (Arg-list) [specification statements][executable statements]... END [FUNCTION [name]] SUBROUTINE [name] (Arg-list) [specification statements][executable statements]... END [SUBROUTINE [name]] Fortran Program Structure

  39. Fortran Program Structure • the ordering rules for statements • these apply to all program units and subprograms • vertical lines delineate varieties of statements that may be interspersed • horizontal lines delineate varieties of statements that must not be interspersed. • USE statements, if any, must appear immediately after the program unit heading

  40. Fortran Program Structure • internal or module subprograms must follow a CONTAINS statement • between USE and CONTAINS statements in a subprogram, non-executable statements generally precede executable statements • the FORMAT statement, DATA statement, and ENTRY statement may appear among the executable statements • this session considers programs structured as shown in pink

  41. Implicit Typing • Fortran permits real and integer variables to be typed and declared implicitly without their being defined by declaration statements • by default the types are given by the first letter of the variable name • integer: [i-n] • real: [a-h] and [o-z]

  42. Implicit Typing • Fortran 90 allows us to disable implicit typing with the statement IMPLICIT NONE • at the start of the program, function and subroutine • this helps reduce errors due to simple spelling mistakes

  43. Fortran Specification Statements • specification statements define the environment for the executable statements IMPLICIT NONE INTEGER :: i, j, k • type declaration statements • integer • real • complex • character • logical, type(type-name)

  44. Variable Declaration • to declare a REAL with the default size on the system REAL :: x, y, z(100) • the separator :: is optional if you don’t use attributes in the declaration REAL x, y, z(100) ! is ok REAL, ALLOCATABLE z(:) !not ok

  45. Fortran Executable Statements • the executable statements specify the actions that are to be performed • assignment operator • arithmetic operators • relational operators • loops • conditional branching • array operations

  46. Intrinsic Mathematical Functions • Fortran provides many intrinsic mathematical functions • all trigonometric functions take arguments in radians • the functions are defined only for floating point arguments, not integers • sqrt(4) causes ‘4’ to be converted to floating point and the return value is a REAL

  47. Intrinsic Mathematical Functions • the generic names of the mathematical functions are given below • COS , ACOS • SIN , ASIN • TAN, ATAN, ATAN2 • EXP, LOG, LOG10 • COSH, SINH, TANH • SQRT

  48. Flow Control • Fortran includes a number of constructs to control the program flow • DO: controls the repeated execution of a block of statements or constructs • IF: conditionally executes one statement based on the value of a logical expression • CASE: conditionally executes one block of constructs or statements depending on the value of a scalar expression

  49. Fortran – Relational Operators • Fortran 90 includes alternative forms for the relational operators • do tests to control flow if (x < y) then r = sqrt(y**2 – x**2) end if

  50. DO Construct • controls the repeated execution of a block of statements or constructs • this construct is called a loop DO i=1, nstep, stepsize block END DO • nstep is the maximum value that the loop variable will take • stepsize is the increment for the loop variable

More Related