Introduction to Fortran
In the world of high-performance computing there are two main choices of programming language, C and Fortran. Highly tuned compilers are available for both languages, along with function libraries which help in creating parallel programs.
Each language has different strengths, Fortran's array syntax allows for computations over large datasets to be done efficiently and trivially parallelised, while C gives you control over memory to make use of more complicated data structures. Both languages are continuing to be improved, the last release of the Fortran standard was in 2008, mainly adding co-arrays for shared memory programming.
Many models in the Earth Sciences are programmed in Fortran, including the Met Office Unified Model and MOM amongst others.
A basic Fortran program is
! hello.f90 program hello implicit none write(*,*) 'Hello' end program
This program simply writes 'Hello' to the console.
On NCI Fortran programs are compiled using the ifort compiler.
$ module load intel-compiler $ ifort -warn all -warn errors hello.f90 $ ./a.out Hello
The 'module' command makes ifort available for use. The default program name when you compile using ifort is 'a.out', you can change what the filename is by adding '-o OUTPUT' to the ifort command. The warning flags '-warn all -warn errors' makes sure that ifort checks for any programming errors, make sure to fix them!
Variables in Fortran are declared like
type(kind=4), options :: name
'type' is the variable type, usually one of 'integer', 'real' or 'logical'. To change how many bytes are used in a variable change the kind, for instance 'real(kind=8)' is a 64 bit (8 byte) IEEE floating point number. The kind is optional, the default value for ifort is 4 bytes. Usually you won't worry about kinds.
Various options are possible, for instance 'dimension(2,3)' makes the variable a 2x3 array, 'parameter' ensures the variable is unchanged by the program (it's value must be set in the declaration)
integer, allocatable :: foo(:) real(kind=4), dimension(2,3) :: bar integer(kind=8), parameter :: buz = 12
Fortran programs and functions have two main sections, variable declaration and the program proper. All variables used should be declared, the command 'implicit none' at the start of a function ensures this. Variable declaration cannot be mixed with program statements.
program variables implicit none integer(kind=8) :: run_count real, dimension(4) :: x, y real, dimension(4,4) :: temperature real, parameter :: pi = 3.1415926 ... end program
type field integer :: nx,ny real, allocable :: x(:), y(:) real, allocable :: data(:,:) end type