introduction to fortran
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/1.jpg)
Copyright 2007, University of Alberta
Introduction to Fortran
Jon Johansson
May 1, 2007
![Page 2: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/2.jpg)
Copyright 2007, University of Alberta
Downloads
• The talk and exercises are available from• http://sciviz.aict.ualberta.ca/Fortran/
• or• http://www.ualberta.ca/AICT/RESEARCH/Courses
/2007/Workshop/
![Page 3: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/3.jpg)
Copyright 2007, University of Alberta
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
![Page 4: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/4.jpg)
Copyright 2007, University of Alberta
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
![Page 5: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/5.jpg)
Copyright 2007, University of Alberta
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
![Page 6: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/6.jpg)
Copyright 2007, University of Alberta
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
![Page 7: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/7.jpg)
Copyright 2007, University of Alberta
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)
![Page 8: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/8.jpg)
Copyright 2007, University of Alberta
Fortran Standards
Name Date Standard Definition
FORTRAN66 1966ANSI X3.9-1966 - the first standard for a programming
language
FORTRAN77 1977 ANSI X3.9-1978 and ISO 1539-1980
Fortran90 1991 ANSI X3.198-1992
Fortran95 Dec 1997 ISO/IEC 1539-1:1997
Fortran2003 Nov 2004 ISO/IEC 1539-1:2004
![Page 9: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/9.jpg)
Copyright 2007, University of Alberta
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
![Page 10: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/10.jpg)
Copyright 2007, University of Alberta
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
![Page 11: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/11.jpg)
Copyright 2007, University of Alberta
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
![Page 12: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/12.jpg)
Copyright 2007, University of Alberta
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
![Page 13: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/13.jpg)
Copyright 2007, University of Alberta
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 ' " ( ) * + - / : = _ ! & $ ; < > % ? , .
![Page 14: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/14.jpg)
Copyright 2007, University of Alberta
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”
![Page 15: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/15.jpg)
Copyright 2007, University of Alberta
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)
![Page 16: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/16.jpg)
Copyright 2007, University of Alberta
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’
![Page 17: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/17.jpg)
Copyright 2007, University of Alberta
Integer Representation
• integers in Fortran are signed• INTEGER lengths can be 1, 2, 4, or 8 bytes• the default INTEGER length is 4 bytes
# of Bytes Min Value Max Value
1 -128 127
2 -32,768 32,767
4 -2,147,483,648 2,147,483,647
8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807
![Page 18: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/18.jpg)
Copyright 2007, University of Alberta
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
![Page 19: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/19.jpg)
Copyright 2007, University of Alberta
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
![Page 20: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/20.jpg)
Copyright 2007, University of Alberta
Floating Point Representation
• Span and Precision of IEEE 754 Floating-Point Formats
Format # of Bytes
Approximate Absolute Nonzero
Minimum
Approximate Absolute
Maximum
Approximate Precision (decimal digits)
Single 4 1.2E-38 3.4E+38 6 – 9
Double 8 2.2D-308 1.8D+308 15 – 17
Extended Double
≥ 10 ≤ 3.4D-4932 ≥ 1.2D+4932 ≥ 18 – 21
Quadruple 16 3.4Q-4932 1.2Q+4932 33 – 36
![Page 21: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/21.jpg)
Copyright 2007, University of Alberta
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
![Page 22: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/22.jpg)
Copyright 2007, University of Alberta
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
![Page 23: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/23.jpg)
Copyright 2007, University of Alberta
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
![Page 24: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/24.jpg)
Copyright 2007, University of Alberta
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) ! IntegerKIND(12.0) ! RealKIND(1.6D6) ! Double
![Page 25: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/25.jpg)
Copyright 2007, University of Alberta
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
![Page 26: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/26.jpg)
Copyright 2007, University of Alberta
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
![Page 27: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/27.jpg)
Copyright 2007, University of Alberta
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
![Page 28: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/28.jpg)
Copyright 2007, University of Alberta
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
![Page 29: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/29.jpg)
Copyright 2007, University of Alberta
The GNU Fortran Compiler
• some useful compiler options-O capital ‘Oh’ optimize the executable
-On n 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
![Page 30: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/30.jpg)
Copyright 2007, University of Alberta
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
![Page 31: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/31.jpg)
Copyright 2007, University of Alberta
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
![Page 32: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/32.jpg)
Copyright 2007, University of Alberta
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
![Page 33: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/33.jpg)
Copyright 2007, University of Alberta
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
![Page 34: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/34.jpg)
Copyright 2007, University of Alberta
Fortran Names
• Fortran 77• names can be up to 6 characters
• Fortran 2003• names can be up to 63 characters
![Page 35: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/35.jpg)
Copyright 2007, University of Alberta
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
![Page 36: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/36.jpg)
Copyright 2007, University of Alberta
Fortran Program Structure
• 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]]
![Page 37: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/37.jpg)
Copyright 2007, University of Alberta
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
![Page 38: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/38.jpg)
Copyright 2007, University of Alberta
Fortran Program Structure
• 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]]
![Page 39: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/39.jpg)
Copyright 2007, University of Alberta
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
![Page 40: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/40.jpg)
Copyright 2007, University of Alberta
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
![Page 41: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/41.jpg)
Copyright 2007, University of Alberta
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]
![Page 42: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/42.jpg)
Copyright 2007, University of Alberta
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
![Page 43: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/43.jpg)
Copyright 2007, University of Alberta
Fortran Specification Statements
• specification statements define the environment for the executable statements
IMPLICIT NONEINTEGER :: i, j, k
• type declaration statements • integer• real• complex• character• logical, type(type-name)
![Page 44: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/44.jpg)
Copyright 2007, University of Alberta
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
![Page 45: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/45.jpg)
Copyright 2007, University of Alberta
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
![Page 46: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/46.jpg)
Copyright 2007, University of Alberta
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
![Page 47: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/47.jpg)
Copyright 2007, University of Alberta
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
![Page 48: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/48.jpg)
Copyright 2007, University of Alberta
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
![Page 49: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/49.jpg)
Copyright 2007, University of Alberta
Fortran – Relational Operators
• Fortran 90 includes alternative forms for the relational operators
• do tests to control flow
Fortran 77 Fortran 90
.LT. <
.LE. <=
.EQ. ==
.NE. /=
.GT. >
.GE. >= if (x < y) then
r = sqrt(y**2 – x**2)
end if
![Page 50: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/50.jpg)
Copyright 2007, University of Alberta
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
![Page 51: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/51.jpg)
Copyright 2007, University of Alberta
DO Construct
• print the odd integers starting from 1 and less than 100
DO i=1, 100, 2 WRITE (*, "(a,I4)") 'I = ',i END DO
• I = 1• I = 3• …• I = 99
![Page 52: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/52.jpg)
Copyright 2007, University of Alberta
Implied DO Loop
• control the execution of some iterative procedure • does not use the DO statement to control the execution
• this is a shorthand to provide a method for array initialization• cuts back on the number of lines of code required
• print the odd integers starting from 1 and less than 100WRITE (*, 100) (i, i=1, 100, 2)100 Format(‘I = ’, I4)
• I = 1• I = 3• …• I = 99
![Page 53: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/53.jpg)
Copyright 2007, University of Alberta
DO WHILE 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
![Page 54: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/54.jpg)
Copyright 2007, University of Alberta
DO WHILE Construct
• execute the range of a DO construct while a specified condition remains true
i = 99DO WHILE (i>0) WRITE (*, "(a,I4)") 'I = ',i i = i - 2END DO
• I = 99• I = 97• …• I = 1
![Page 55: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/55.jpg)
Copyright 2007, University of Alberta
Exercise 2
• Calculation of = 3.1415…• Implement the algorithm described or
download the program
PiApprox_01.f90 • answer the questions in the exercise
![Page 56: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/56.jpg)
Copyright 2007, University of Alberta
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
![Page 57: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/57.jpg)
Copyright 2007, University of Alberta
IF Construct
• conditionally executes one block of statements depending on the evaluation of a logical expression
IF (expr) THEN block
[ELSE IF (expr) THENblock] ...
[ELSEblock]
END IF• expr is a logical expression
![Page 58: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/58.jpg)
Copyright 2007, University of Alberta
IF Construct
• test values of x and deal with special ranges
IF ( x > 15.0 ) THEN
Call DealWithBigX(x)
ELSE IF ( x <= 0.0 ) THEN
Call DealWithLittleX(x)
ELSE
Call DealWithMediumX(x)
END IF
![Page 59: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/59.jpg)
Copyright 2007, University of Alberta
IF Statement
• when there is a single expression to evaluate
IF ( x + y < 4.0 ) &
z = sqrt( x**2 + y**2 )
![Page 60: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/60.jpg)
Copyright 2007, University of Alberta
SELECT CASE Construct
• conditionally executes one block of constructs or statements depending on the value of a scalar expression in a SELECT CASE statement
SELECT CASE (expr)[CASE (case-value)
block] ...[CASE DEFAULT
block]END SELECT
• expr scalar expression of type integer, logical, or character (enclosed in parentheses).
![Page 61: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/61.jpg)
Copyright 2007, University of Alberta
SELECT CASE Construct
SELECT CASE (i)CASE(1:3) ! Values: 1, 2, 3 …CASE(4) ! Values: 4 …CASE(5, 7, 9) ! Values: 5, 7, 9 …CASE(10:) ! Values: ≥10 …CASE DEFAULT ! Values: all others …
END SELECT
![Page 62: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/62.jpg)
Copyright 2007, University of Alberta
Arrays
• much of numerical work requires manipulation of arrays• so Fortran provides lots of tools for arrays
• arrays can be declared by using: • a type declaration statement• DIMENSION• ALLOCATABLE• TARGET• or, POINTER or COMMON
![Page 63: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/63.jpg)
Copyright 2007, University of Alberta
Arrays
• for exampleREAL MyArr(-1:29, 15:36)
• defines a 2 dimensional array whose• first index takes on values [-1, 29]• second index takes on values [15, 36]
• this array exists as long as the program unit that creates it runs• this is the way that Fortran 77 behaves
• declare the biggest arrays that you think you’ll ever need and that is the size of your program in memory
• the above is equivalent to using:REAL MyArrDimension MyArr(-1:29, 15:36)
![Page 64: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/64.jpg)
Copyright 2007, University of Alberta
Arrays
• Fortran stores higher dimensional arrays as a contiguous sequence of elements
• it is important to know that 2-dimensional arrays are stored by column • referred to as column-major
order• C uses row-major order
Fortran: elements stored in order -
[a11 a21 a31 a21 a22 a32 a13 a23 a33 ]
C: elements stored in order -
[a11 a12 a13 a21 a22 a23 a31 a32 a33 ]
![Page 65: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/65.jpg)
Copyright 2007, University of Alberta
Arrays
• the fact that the array elements are stored contiguously is part of the Fortran standard:
• this is from ISO/IEC 1539 : 1991 (E) section 14.6.3.1, item (6)• A nonpointer array of intrinsic type or sequence derived type
occupies a sequence of contiguous storage sequences, one for each array element, in array element order (6.2.2.2)
• in ISO/IEC 1539-1:2004(E) this is found in section 16.4.3.1, item (7)
![Page 66: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/66.jpg)
Copyright 2007, University of Alberta
Arrays
• why do we care about the array elements being in contiguous memory locations?• this is what allows us to count on optimizations
which access array elements in column orderdo j = 1, ny do i = 1, nx y(i, j) = DoSomething( y(i,j) ) end doend do
![Page 67: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/67.jpg)
Copyright 2007, University of Alberta
Exercise 4
• download the file
ColVsRowOrder.f90• this program creates a large array and then accesses
the elements in column order• add code to the program to measure the cpu time
required to access the array in row order and time it• compile and run the program• how different are the times for access?
![Page 68: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/68.jpg)
Copyright 2007, University of Alberta
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
![Page 69: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/69.jpg)
Copyright 2007, University of Alberta
Allocatable Arrays
• it is useful to be able to allocate and deallocate arrays as the program runs
REAL, Allocatable :: MyArr(:,:)• this declares a 2 dimensional array but no memory is
actually allocated• to allocate use
Allocate( MyArr(12,5:15) )• first dimension goes from 1 to 12• second dimension goes from 5 to 15
• release the memory with Deallocate( MyArr )
![Page 70: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/70.jpg)
Copyright 2007, University of Alberta
Array Expressions
• can write expressions containing arrays without explicitly writing do loops
Real x(10,10), y(10,10), z(10,10)
z = -x + 1 .0 ! z(i,j) = - x(i,j) + 1.0
z = x * y ! z(i,j) = x(i,j) * y(i,j)
z = x / y ! z(i,j) = x(i,j) / y(i,j)
z = Sin( x ) ! z(i,j) = Sin( x(i,j) )
• the above requires arrays of the same shape
![Page 71: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/71.jpg)
Copyright 2007, University of Alberta
Vector Multiplication
• vector multiplication:
DOT_PRODUCT(VECTOR_A, VECTOR_B) • makes a scalar product of two vectors
• must have the same length (same number of elements)
• note that if VECTOR_A is of type COMPLEX the result is
SUM(CONJG(VECTOR_A)*VECTOR_B)
![Page 72: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/72.jpg)
Copyright 2007, University of Alberta
Matrix Multiplication
• matrix multiplication:
MATMUL(MATRIX_A, MATRIX_B) • makes the matrix product of two matrices
• must be consistent, i.e. have dimensions like (M, K) and (K, N)
• at least one of the matrices must be of rank 2
![Page 73: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/73.jpg)
Copyright 2007, University of Alberta
Array Sections
• an array section is a portion of the array• we can address subsections of arrays using:
• a subscript triplet • [first-bound] : [last-bound] [:stride]
• a vector subscript• a one-dimensional array of integer values
(within the declared bounds for the dimension) that selects a section of a parent array
![Page 74: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/74.jpg)
Copyright 2007, University of Alberta
Array Sections
• define an array with 4 rows and 5 columns
Real x(1:4,1:5)
• x(1,1) is in the upper left corner
![Page 75: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/75.jpg)
Copyright 2007, University of Alberta
Array Sections
• the subscript triplet is used as followsReal x(10,10), y(10,10), z(3,5)
z = x(2:4,2:10:2) + y(1:5:2,1:9:2)
• the array sections of x and y are both the same size as z
• note that the correspondence is by position in the extent, not by subscript value• the order of scalar operations isn’t specified in the Fortran
standard to the compiler can arrange optimizations
![Page 76: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/76.jpg)
Copyright 2007, University of Alberta
Array Constructor
• an array constructor can be used to create and assign values to rank-one arrays (and array constants)
• the array constructor is denoted by:
(/ ... /) • the ellipse is a list of values assigned to the
array elements and is create by expressions or implied do loops
![Page 77: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/77.jpg)
Copyright 2007, University of Alberta
Array Constructor
• use a list of numbers to give values to an array
Integer i ! Loop variableReal :: x(4)x = (/ 3, 6, 9, 12 /)write(*,"(4F6.1)") x
• gives the output 3.0 6.0 9.0 12.0
![Page 78: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/78.jpg)
Copyright 2007, University of Alberta
Array Constructor
• use an implied DO loop to give values to an array
Integer i ! Loop variableReal :: x(4)x = (/ (i, i=1, 4) /)write(*,"(4F6.1)") x
• gives the output 1.0 2.0 3.0 4.0
![Page 79: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/79.jpg)
Copyright 2007, University of Alberta
Masked Array Assignments
• Fortran includes two constructs for masked array assignments• WHERE : applies a logical test to an array on an
element-by-element basis • FORALL : a generalization of WHERE. It allows
more general array shapes to be assigned
![Page 80: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/80.jpg)
Copyright 2007, University of Alberta
Input/Output
• to print something to the computer displayWrite(*, "(a)") ‘This is a string‘
• the ‘a’ is the format specifier for a string• the format string must be inside
'(…)' or "(…)"
![Page 81: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/81.jpg)
Copyright 2007, University of Alberta
Input/Output
• Fortran programs transfer data to and from external devices during there runs
• each device is associated with a Unit Number• the number is often in the range 1 to 99• the number cannot be negative
![Page 82: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/82.jpg)
Copyright 2007, University of Alberta
Input/Output
• to associate an external file with a unit use the OPEN() statement
• OPEN(20, FILE=‘output.txt’)• OPEN(21, FILE=‘output.txt’, STATUS =
'NEW' )
![Page 83: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/83.jpg)
Copyright 2007, University of Alberta
Input/Output
Open(20, File=‘outfile.txt', &
Status='New')
Do i = 1, N
If ( i .NE. TestVal(i) ) Then
Write(20, "(I15)") i
End If
End Do
Close(20)
![Page 84: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/84.jpg)
Copyright 2007, University of Alberta
Input/Output
• Fortran has 1 function for data input and 2 for data output
• Input: • READ() - transfers input data from external sequential,
direct-access, or internal records
• Output• PRINT() - displays output on the screen
• on some systems TYPE is a synonym for PRINT• WRITE() - transfers output data to external sequential,
direct-access, or internal records
![Page 85: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/85.jpg)
Copyright 2007, University of Alberta
Input/Output
• The general format of READ and WRITE is• READ(clist) list• WRITE(clist) list
• where the required parts of clist for both READ and WRITE are• [UNIT=] unit-number• [FMT=] format-spec
• and other arguments are optional.! note * refers to default settings READ(*, *) x, yWRITE(*, *) x, y
• the first asterisk refers to the default unit number and• the second is the list directed i/o format specifier
![Page 86: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/86.jpg)
Copyright 2007, University of Alberta
Format Specifier
• There are 3 ways to give a format specifier:• 1) as an asterisk (*)
• the is called list-directed i/o• the format is defined by the computer system at
the moment the statement is executed
Write(*,*) 'Product = ', x*y
![Page 87: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/87.jpg)
Copyright 2007, University of Alberta
Format Specifier
• 2) as a statement label referring to a FORMAT statement containing the specifier between parentheses• this is the Fortran 77 way to do it
Write(*, 100) x*y
100 Format('Product = ', f8.2)
![Page 88: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/88.jpg)
Copyright 2007, University of Alberta
Format Specifier
• 3) as a character expression whose value commences with a format specification in parentheses• this is what we do in the sample programs
Write(*,’(a,f8.2)’) &
'Product = ', x*y
Read(*,’(2f8.2)’) x, y
![Page 89: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/89.jpg)
Copyright 2007, University of Alberta
Edit Descriptors
• the format specification uses a set of descriptors – here are some common ones
• ‘A’ – character string• A20 – the field width is 20 characters
• ‘I’ – integers• I9 – integer field is 9 digits wide
• ‘F’ – floating point numbers• F9.3 – real field is 9 spaces wide with 3 digits to the right of the
decimal point
• ‘E’ – real values with exponents• E12.3E2 – real field is 12 spaces wide with 3 digits to the right of
the decimal point and 2 digits in the exponent field
![Page 90: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/90.jpg)
Copyright 2007, University of Alberta
Exercise 3
• Sieve of Eratosthenes• Eratosthenes devised this algorithm for
finding all the prime numbers up to some integer N – implement the algorithm
• if time is short start by downloading the file
SieveE.f90• modify the program to answer the questions
on the exercise sheet
![Page 91: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/91.jpg)
Copyright 2007, University of Alberta
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
![Page 92: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/92.jpg)
Copyright 2007, University of Alberta
GOTO
• Fortran has a GOTO statement • when coupled with line labels it’s possible to
transfer control to any point in a program in a completely unreadable way
• it is generally considered bad practice to make use of the GOTO statement• there are alternatives
![Page 93: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/93.jpg)
Copyright 2007, University of Alberta
Subroutines
• a subroutine does not return any value and is the same as a void function in C/C++
• in Fortran all arguments are passed as the address of the variable in the calling program• referred to as call by reference in C/C++
![Page 94: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/94.jpg)
Copyright 2007, University of Alberta
Subroutines
! Calculate the spherical-polar radius! from 3 Cartesian coordinates and! return the radiusSUBROUTINE radius(x, y, z, r) Real, INTENT(IN) :: x, y, z Real, INTENT(OUT) :: r r = sqrt( x**2 + y**2 + z**2 )END SUBROUTINE radius
• INTENT(IN) and INTENT(OUT) prevent accidental changes of the variables in the calling program by generating an error at compile time
![Page 95: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/95.jpg)
Copyright 2007, University of Alberta
Functions
• a Fortran function always return a value just like corresponding functions in C/C++! Calculate the spherical-polar radius! from 3 Cartesian coordinates and! return the radiusFUNCTION radius(x,y,z) RESULT(r) Real, INTENT(IN) :: x, y, z Real :: r r = Sqrt( x**2 + y**2 + z**2 )END FUNCTION radius
• the function type must be declared in the calling program
![Page 96: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/96.jpg)
Copyright 2007, University of Alberta
Modules
• a container, inside of which, different variables and subprogram units can be defined
• the variables and subprograms can then be made available to different program units through the USE statement• USE gives a program unit accessibility to public entities in a
module
• essentially a place to clump global data• an alternative to COMMON blocks• can do object oriented programming with modules
![Page 97: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/97.jpg)
Copyright 2007, University of Alberta
Exercise 5
• download the program
IntentCheck.f90• there are errors in this program resulting in
different values returned for the result r• find the errors and rewrite the program using the
INTENT() clause to ensure that this type of error won’t happen again in the program
• describe the problem and the solution:
![Page 98: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/98.jpg)
Copyright 2007, University of Alberta
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
![Page 99: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/99.jpg)
Copyright 2007, University of Alberta
Timing Code
• you can manually add functions to your program to time sections as it runs
• CPU Time – amount of time it takes for the CPU to execute a set of instructions • the system keeps track of how much time the CPU
is working on your program
• Wall Time – amount of time, according to a clock on the wall, that it takes the cpu to execute the instructions
![Page 100: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/100.jpg)
Copyright 2007, University of Alberta
CPU Time
• Fortran 95 added a functioncpu_time()
• which returns a processor-dependent approximation of the processor time in seconds real :: start_time, finish_timecall cpu_time(start_time) … stuff to time …call cpu_time(finish_time)write(*, "(a,f8.1,a)") & ' Stuff completed in ', & finish_time - start_time, ' cpu seconds'
![Page 101: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/101.jpg)
Copyright 2007, University of Alberta
Wall Time
• Fortran 90 added a functionsystem_clock()
• this function returns integer data from a real-time clockinteger :: start_wall_time, finish_wall_timeinteger :: ticks_per_seccall system_clock(start_wall_time, ticks_per_sec) … stuff to time …call system_clock(finish_wall_time)write(*, "(a,f8.1,a)") ‘Stuff completed in ', & real(finish_wall_time - start_wall_time) & & / real(ticks_per_sec), & ' wall seconds‘
• Beware: the returned value can be reset to 0 if the count passes through the maximum integer on the system
![Page 102: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/102.jpg)
Copyright 2007, University of Alberta
Profiling
• given that your program works correctly you may want to make it run faster/more efficiently
• a profiler allows you to find the places where your program spends its time so you reduce that time• focus your efforts where it will do the most good
• GNU provides ‘gprof’ for profiling
![Page 103: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/103.jpg)
Copyright 2007, University of Alberta
Profiling
• ‘gprof’ produces an execution profile of your program
• the file ‘gmon.out’ contains the call graph profile• ‘gprof’ outputs:
• flat profile: shows how much time your program spent in each function and how many times that function was called
• call graph: shows, for each function, which functions called it, which other functions it called, and how many times
![Page 104: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/104.jpg)
Copyright 2007, University of Alberta
Profiling
• add the option ‘–pg’ to the compile line• execute the program normally
• get a file named ‘gmon.out’
• run the command ‘gprof’• for example:
gfortran -pg –o MyCalc MyCalc.f90
./MyCalc
gprof -l -b MyCalc gmon.prof
![Page 105: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/105.jpg)
Copyright 2007, University of Alberta
OpenMP
• OpenMP• introduce statements into your code
• in FORTRAN: !$OMP• in C: #pragma
• can compile serial and parallel executables from the same source code
• restricted to shared memory machines• not clusters!
• www.openmp.org
![Page 106: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/106.jpg)
Copyright 2007, University of Alberta
OpenMP
• make the program parallel incrementally• one loop at a time if that works for you
• the double precision calculation example program contains 4 lines around the main loop to make it parallel
• before the loop:!$ Call OMP_SET_NUM_THREADS(3) !$OMP Parallel!$OMP Do Reduction(+:sum) Private(x, f)
• after the loop!$OMP End Parallel
![Page 107: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/107.jpg)
Copyright 2007, University of Alberta
OpenMP
• the number of threads that OpenMP uses can be controlled by:
• using the OpenMP function from the serial part of the program!$ Call OMP_SET_NUM_THREADS(3)
• setting the environment variable (in BASH)export OMP_NUM_THREADS=3
• or using the NUM_THREADS clause
![Page 108: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/108.jpg)
Copyright 2007, University of Alberta
Calculation - OpenMP
• speedup graph for 1 to 4 processors
• quad precision• Opteron 285
procs• runs for 107 steps
and 109 steps
![Page 109: Introduction to Fortran](https://reader031.vdocument.in/reader031/viewer/2022020714/5681333e550346895d9a34e5/html5/thumbnails/109.jpg)
Copyright 2007, University of Alberta
Fortran
• there’s lots left to explore
• see your favourite compiler manual
• contact [email protected] for numerical help