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

Table Of Contents

Previous topic

Array storage in Fortran

Next topic

Fortran Input / Output

This Page

Fortran modules

The general structure of a Fortran module:

module <MODULE-NAME>
    ! Declare variables
contains
    ! Define subroutines or functions
end module <MODULE-NAME>

A program or subroutine can use this module:

program <NAME>
    use <MODULE-NAME>
    ! Declare variables
    ! Executable statements
end program <NAME>

A very simple module is:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
! $CLASSHG/codes/fortran/multifile2/sub1m.f90

module sub1m

contains

subroutine sub1()
    print *, "In sub1"
end subroutine sub1

end module sub1m

and a program that uses this:

1
2
3
4
5
6
7
! $CLASSHG/codes/fortran/multifile2/main.f90

program demo
    use sub1m
    print *, "In main program"
    call sub1()
end program demo

Some reasons to use modules

  • Can define global variables in modules to be used in several different routines.

    In Fortran 77 this had to be done with common blocks — much less elegant.

  • Subroutine/function interface information is generated to aid in checking that proper arguments are passed.

    It’s often best to put all subroutines and functions in modules for this reason.

  • Can define new data types to be used in several routines.

Compiling modules

Modules must be compiled before any program units that use the module. When a module is compiled, a .o file is created, but also a .mod file is created that must be present in order to compile a unit that uses the module.

Circles module example

Here is an example of a module that defines one parameter pi and two functions:

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

module circle_mod

    implicit none
    real(kind=8) :: pi = 3.141592653589793d0

contains

    real(kind=8) function area(r)
        real(kind=8), intent(in) :: r
        area = pi * r**2
    end function area

    real(kind=8) function circumference(r)
        real(kind=8), intent(in) :: r
        circumference = 2.d0 * pi * r
    end function circumference

end module circle_mod

This might be used as follows:

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

program main

    use circle_mod
    implicit none
    real(kind=8) :: a

    ! print parameter pi defined in module:
    print *, 'pi = ', pi

    ! test the area function from module:
    a = area(2.d0)
    print *, 'area for a circle of radius 2: ', a

end program main

This gives the following output:

pi =    3.14159265358979
area for a circle of radius 2:    12.5663706143592

Note:

  • A parameter can be defined with a specific value that will then be available to all program units using the module.
  • It is also possible to declare variables that can be shared between all program units using the module. But then it’s not permitted to set the value of the variable in the module.
  • More examples to come....