UW AMath High Performance Scientific Computing
 
AMath 483/583 Class Notes
 
Spring Quarter, 2011

Table Of Contents

Previous topic

Makefiles

Next topic

Useful gfortran flags

This Page

Fortran

General References:

  • See the Fortran section of the bibliography for links.

History

FORTRAN stands for FORmula TRANslator and was the first major high level language to catch on. The first compiler was written in 1954-57. Before this, programmers generally had to write programs in assembly language.

Many version followed: Fortran II, III, IV. Fortran 66 followed a set of standards formulated in 1966.

See

for brief histories.

Fortran 77

The standards established in 1977 lead to Fortran 77, or f77, and many codes are still in use that follow this standard.

Fortran 77 does not have all the features of newer versions and many things are done quite differently.

One feature of f77 is that lines of code have a very rigid structure. This was required in early versions of Fortran due to the fact that computer programs were written on Punch cards. All statements must start in column 7 or beyond and no statement may extend beyond column 72. The first 6 columns are used for things like labels (numbers associated with particular statements). In f77 any line that starts with a ‘c’ in column 1 is a comment.

We will not use f77 in this class but if you need to work with Fortran in the future you may need to learn more about it because of all the legacy codes that still use it.

Fortran 90/95

Dramatically new standards were introduced with Fortran 90, and these were improved in mostly minor ways in Fortran 95. There is now a Fortran 2003 standard but few compilers implement this fully yet.

For this class we will use the Fortran 90/95 standards, which we will refer to as Fortran 90 for brevity.

Compilers

Unlike Python code, a Fortran program must pass through several stages before being executed. There are several different compilers that can turn Fortran code into an executable, as described more below.

In this class we will use gfortran, which is an open source compiler, part of the GNU Project. See http://gcc.gnu.org/fortran/ for more about gfortran.

There is an older compiler in this suite called g77 which compiles Fortran 77 code, but gfortran can also be used for Fortran 77 code and has replaced g77.

There are several commercial compilers which are better in some ways, in particular they sometimes do better optimization and produce faster running executables. They also may have better debugging tools built in. Some popular ones are the Intel and Portland Group compilers.

File extensions

For the gfortran compiler, Fortran 77 code should have the extension .f while Fortran 90/95 code has the extension .f90 or .f95. We will use .f90.

Compiling, linking, and running a Fortran code

Suppose we have a Fortran file named demo1.f90, for example the program below. We can not run this directly the way we did a Python script. Instead it must be converted into object code, a version of the code that is in a machine language specific to the type of computer. This is done by the compiler.

Then a linker must be used to convert the object code into an executable that can actually be executed.

This is broken into two steps because often large programs are split into many different .f90 files. Each one can be compiled into a separate object file, which by default has the same name but with a .o extension (for example, from demo1.f90 the compiler would produce demo1.o). One may also want to call on library routines that have already been compiled and reside in some library. The linker combines all of these into a single executable.

For more details on the process, see for example:

For the simplest case of a self-contained program in one file, we can combine both stages in a single gfortran command, e.g.

$ gfortran demo1.f90

By default this will produce an executable named a.out for obscure historical reasons (it stands for assembler output, see wikipedia).

To run the code you would then type:

$ ./a.out

Note we type ./a.out to indicate that we are executing a.out from the current directory. There is an environment variable PATH that contains your search path, the set of directories that are searched whenever you type a command name at the Unix prompt. Often this is set so that the current directory is the first place searched, in which case you could just type a.out instead of ./a.out. However, it is generally considered bad practice to include the current directory in your search path because bad things can happen if you accidentally execute a file.

If you don’t like the name a.out you can specify an output name using the -o flag with the gfortran command. For example, if you like the Windows convention of using the extension .exe for executable files:

$ gfortran demo1.f90 -o demo1.exe
$ ./demo1.exe

will also run the code.

Note that if you try one of the above commands, there will be no file demo1.o created. By default gfortran removes this file once the executed is created.

Later we will see that it is often useful to split up the compile and link steps, particularly if there are several files that need to be compiled and linked. We can do this using the -c flag to compile without linking:

$ gfortran -c demo1.f90              # produces demo1.o
$ gfortran demo1.o -o demo1.exe      # produces demo1.exe

There are many other compiler flags that can be used, see linux man page for gfortran for a list.

Sample codes

The first example simply assigns some numbers to variables and then prints them out. The comments below the code explain some features.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
! $CLASSHG/codes/fortran/demo1.f90

program demo1

! Fortran 90 program illustrating data types.
! Changed one variable name to illustrate 'hg diff'

implicit none   ! to give error if a variable not declared
real :: x
real (kind=8) :: y, z
integer :: m

m = 3
print *, " "
print *, "M = ",M   ! note that M==m  (case insensitive)


print *, " "
print *, "x is real (kind=4)"
x = 1.e0 + 1.23456789e-6
print *, "x = ", x


print *, " "
print *, "y is real (kind=8)"
print *, "  but 1.e0 is real (kind=4):"
y = 1.e0 + 1.23456789e-6
print *, "y = ", y


print *, " "
print *, "z is real (kind=8)"
z = 1. + 1.23456789d-6
print *, "z = ", z

end program demo1

Comments:

  • Exclamation points are used for comments

  • The implicit none statement in line 7 means that any variable to be used must be explicitly declared. See fortran_implicit for more about this.

  • Lines 8-10 declare four variables x, y, z, n. Note that x is declared to have type real which is a floating point number stored in 4 bytes, also known as single precision. This could have equivalently been written as:

    real (kind=4) :: x

    y and z are floating point numbers stored in 8 bytes (corresponding to double precision in older versions of Fortran). This is generally what you want to use.

  • Fortran is not case-sensitive, so N and n refer to the same variable!!

  • 1.23456789e-10 specifies a 4-byte real number. The 8-byte equivalent is 1.23456789d-10, with a d instead of e. This is apparent from the output below.

Compiling and running this program produces:

$ gfortran demo1.f90 -o demo1.exe
$ ./demo1.exe

 N =            3

 x is real (kind=4)
 x =    1.000001

 y is real (kind=8)
   but 1.e0 is real (kind=4):
 y =    1.00000119209290

 z is real (kind=8)
 z =    1.00000123456789

For most of what we’ll do in this class, we will use real numbers with (kind=8). Be careful to specify constants using the d rather than e notation if you need to use scientific notation.

(But see Default 8-byte real numbers below for another approach.)

Intrinsic functions

There are a number of built-in functions that you can use in Fortran, for example the trig functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
! $CLASSHG/codes/fortran/builtinfcns.f90

program builtinfcns

    implicit none
    real (kind=8) :: pi, x, y

    ! compute pi as arc-cosine of -1:
    pi = acos(-1.d0)  ! need -1.d0 for full precision!

    x = cos(pi)
    y = sqrt(exp(log(pi)))**2

    print *, "pi = ", pi
    print *, "x = ", x
    print *, "y = ", y

end program builtinfcns

This produces:

$ gfortran builtinfcns.f90
$ ./a.out
 pi =    3.14159265358979
 x =   -1.00000000000000
 y =    3.14159265358979

See http://www.nsc.liu.se/~boein/f77to90/a5.html for a good list of other intrinsic functions.

Default 8-byte real numbers

Note that you can declare variables to be real without appending (kind=8) if you compile programs with the gfortran flag -fdefault-real-8, e.g. if we modify the program above to:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
! $CLASSHG/codes/fortran/builtinfcns2.f90

program builtinfcns

    implicit none
    real :: pi, x, y    ! note kind is not specified

    ! compute pi as arc-cosine of -1:
    pi = acos(-1.0)

    x = cos(pi)
    y = sqrt(exp(log(pi)))**2

    print *, "pi = ", pi
    print *, "x = ", x
    print *, "y = ", y

end program builtinfcns

Then:

$ gfortran builtinfcns2.f90
$ ./a.out
 pi =    3.141593
 x =   -1.000000
 y =    3.141593

gives single precision results, but we can obtain double precisions with:

$ gfortran -fdefault-real-8 builtinfcns2.f90
$ ./a.out
 pi =    3.14159265358979
 x =   -1.00000000000000
 y =    3.14159265358979

Note that if you plan to do this you might want to define a Unix alias, e.g.

$ alias gfort="gfortran -fdefault-real-8"

so you can just type:

$ gfort builtinfcns2.f90
$ ./a.out
 pi =    3.14159265358979
 x =   -1.00000000000000
 y =    3.14159265358979

Such an alias could be put in your .bashrc file.

We’ll also see how to specify compiler flags easily in a makefile.

Fortran Arrays

Note that arrays are indexed starting at 1 by default, rather than 0 as in Python. Also note that components of an array are accessed using parentheses, not square brackets!

Arrays can be dimensioned and used as in the following example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
! $CLASSHG/codes/fortran/array1

program array1

    ! demonstrate declaring and using arrays

    implicit none
    integer, parameter :: m = 3, n=2
    real (kind=8), dimension(m,n) :: A 
    real (kind=8), dimension(m) :: b 
    real (kind=8), dimension(n) :: x 
    integer :: i,j

    ! initialize matrix A and vector x:
    do j=1,n
        do i=1,m
            A(i,j) = i+j
            enddo
        x(j) = 1.
        enddo

    ! multiply A*x to get b:
    do i=1,m
        b(i) = 0.
        do j=1,m
            b(i) = b(i) + A(i,j)*x(j)
            enddo
        enddo

    print *, "A = "
    do i=1,m
        print *, A(i,:)   ! i'th row of A
        enddo
    print "(2d16.6)", ((A(i,j), j=1,2), i=1,3)
    print *, "x = "
    print "(d16.6)", x
    print *, "b = "
    print "(d16.6)", b

end program array1

Compiling and running this code gives the output:

A =
  2.00000000000000        3.00000000000000
  3.00000000000000        4.00000000000000
  4.00000000000000        5.00000000000000
x =
  1.00000000000000        1.00000000000000
b =
  5.00000000000000        7.00000000000000        9.00000000000000

Comments:

  • In printing A we have used a slice operation: A(i,:) refers to the i’th row of A. In Fortran 90 there are many other array operations that can be done more easily than we have done in the loops above. We will investigate this further later.
  • Here we set the values of m,n as integer parameters before declaring the arrays A,x,b. Being parameters means we can not change their values later in the program.
  • It is possible to declare arrays and determine their size later, using allocatable arrays, which we will also see later.

There are many array operations you can do, for example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
! $CLASSHG/codes/fortran/vectorops.f90

program vectorops

    implicit none
    real(kind=8), dimension(3) :: x, y

    x = (/10.,20.,30./)
    y = (/100.,400.,900./)

    print *, "x = "
    print *, x

    print *, "x**2 + y = "
    print *, x**2 + y

    print *, "x*y = "
    print *, x*y

    print *, "sqrt(y) = "
    print *, sqrt(y)

    print *, "dot_product(x,y) = "
    print *, dot_product(x,y)


end program vectorops

produces:

x =
  10.0000000000000        20.0000000000000        30.0000000000000
x**2 + y =
  200.000000000000        800.000000000000        1800.00000000000
x*y =
  1000.00000000000        8000.00000000000        27000.0000000000
sqrt(y) =
  10.0000000000000        20.0000000000000        30.0000000000000
dot_product(x,y) =
  36000.0000000000

Note that addition, multiplication, exponentiation, and intrinsic functions such as sqrt all apply component-wise.

Multidimensional arrays can be manipulated in similar manner. The produce to two arrays when computed with * is always component-wise. For matrix multiplication, use matmul. There is also a transpose function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
! $CLASSHG/codes/fortran/arrayops.f90

program arrayops

    implicit none
    real(kind=8), dimension(3,2) :: a
    real(kind=8), dimension(2,3) :: b
    real(kind=8), dimension(3,3) :: c
    real(kind=8), dimension(2) :: x
    real(kind=8), dimension(3) :: y
    integer i

    a = reshape((/1,2,3,4,5,6/), (/3,2/))

    print *, "a = "
    do i=1,3
        print *, a(i,:)   ! i'th row
        enddo

    b = transpose(a)

    print *, "b = "
    do i=1,2
        print *, b(i,:)   ! i'th row
        enddo

    c = matmul(a,b)
    print *, "c = "
    do i=1,3
        print *, c(i,:)   ! i'th row
        enddo

    x = (/5,6/)
    y = matmul(a,x)
    print *, "x = ",x
    print *, "y = ",y

end program arrayops

produces:

a =
  1.00000000000000        4.00000000000000
  2.00000000000000        5.00000000000000
  3.00000000000000        6.00000000000000

b =
  1.00000000000000        2.00000000000000        3.00000000000000
  4.00000000000000        5.00000000000000        6.00000000000000

c =
  17.0000000000000        22.0000000000000        27.0000000000000
  22.0000000000000        29.0000000000000        36.0000000000000
  27.0000000000000        36.0000000000000        45.0000000000000

x =    5.00000000000000        6.00000000000000
y =    29.0000000000000        40.0000000000000        51.0000000000000

Loops

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
! $CLASSHG/codes/fortran/loops1.f90

program loops1

   implicit none
   integer :: i

   do i=1,3           ! prints 1,2,3
      print *, i
      enddo

   do i=5,11,2        ! prints 5,7,9,11
      print *, i
      enddo

   do i=6,2,-1        ! prints 6,5,4,3,2
      print *, i
      enddo

   i = 0
   do while (i < 5)   ! prints 0,1,2,3,4
      print *, i
      i = i+1
      enddo

end program loops1

The while statement used in the last example is considered obsolete. It is better to use a do loop with an exit statement if a condition is satisfied. The last loop could be rewritten as:

i = 0
do                 ! prints 0,1,2,3,4
   if (i>=5) exit
   print *, i
   i = i+1
   enddo

This form of the do is valid but is generally not a good idea. Like the while loop, this has the danger that a bug in the code may cause it to loop forever (e.g. if you typed i = i-1 instead of i = i+1).

A better approach for loops of this form is to limit the number of iterations to some maximum value (chosen to be ample for your application), and then print a warning message, or take more drastic action, if this is exceeded, e.g.:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
! $CLASSHG/codes/fortran/loops2.f90

program loops2

   implicit none
   integer :: i,j,jmax

   i = 0
   jmax = 100
   do j=1,jmax        ! prints 0,1,2,3,4
      if (i>=5) exit
      print *, i
      i = i+1
      enddo

   if (j==jmax+1) then
      print *, "Warning: jmax iterations reached."
      endif

end program loops2

Note: j is incremented before comparing to jmax.

if-then-else

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
! $CLASSHG/codes/fortran/ifelse1.f90

program ifelse1

    implicit none
    real(kind=8) :: x
    integer :: i

    i = 3
    if (i<2) then
        print *, "i is less than 2"
    else
        print *, "i is not less than 2"
    endif

    if (i<=2) then
        print *, "i is less or equal to 2"
    else if (i/=5) then
        print *, "i is greater than 2 but not equal to 5"
    else 
        print *, "i is equal to 5"
    endif

end program ifelse1

Comments:

  • The else clause is optional
  • You can have optional else if clauses

There is also a one-line form of an if statement that was seen in a previous example on this page:

if (i>=5) exit

This is equivalent to:

if (i>=5) then
    exit
    endif

Booleans

  • Compare with <, >, <=, >=, ==, /=. You can also use the older Fortran 77 style: .lt., .gt., .le., .ge., .eq., .neq..
  • Combine with .and. and .or.

For example:

((x>=1.0) .and. (x<=2.0)) .or. (x>5)

A boolean variable is declared with type logical in Fortran, as for example in the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
! $CLASSHG/codes/fortran/boolean1.f90

program boolean1

    implicit none
    integer :: i,k
    logical :: ever_zero

    ever_zero = .false.
    do i=1,10
        k = 3*i - 1
        ever_zero = (ever_zero .or. (k == 0))
        enddo

    if (ever_zero) then
        print *, "3*i - 1 takes the value 0 for some i"
    else
        print *, "3*i - 1 is never 0 for i tested"
    endif

end program boolean1