From HPC
Jump to: navigation , search

Programming Details

Fortran (formerly FORTRAN, derived from Formula Translation) is a general-purpose, imperative programming language that is especially suited to numeric computation and scientific computing. The GFortran compiler is fully compliant with the Fortran 95 Standard and includes legacy F77 support. In addition, a significant number of Fortran 2003 and Fortran 2008 features are implemented. Intel's Fortran compiler (ifort) is also compliant with Fortran back to F77 and earlier to most implementations.

Icon tick.png There is direct support for openMP and openMPI.

Programming example

Non-Parallel example

program variableTesting
implicit none

   ! declaring variables
   integer :: total,average
   complex :: cx
   logical :: done
   character(len=80) :: message ! a string of 80 characters

   !assigning values
   total = 20000
   average = 1666
   done = .true.
   message = "A big Hello from HPC"

   cx = (3.0, 5.0) ! cx = 3.0 + 5.0i

        if (total .ge. average) then
                print *, total, " greater or equal than average"
                print *, total, " less than average"

   Print *, average
   Print *, cx
   Print *, done
   Print *, message

end program variableTesting

Parallel example (openMP)

program helloworld
  use omp_lib
  implicit none

  print *,'Hello from thread number',OMP_GET_THREAD_NUM()

end program helloworld

Modules available

The following modules are available:

  • module add gcc/4.9.3 (GNU compiler)
  • module add intel/compiler/64/2016.2.181 (Intel compiler)

Note : It is generally accepted that the intel compiler generates faster code on Intel hardware than gcc, although gcc is generally more compatible with a wide range of hardware.

Design considerations

  • Array storage in Fortran is column-major. That is to say, when stored in memory as a linear array, the matrix will be arranged as . Thus, accessing the elements of column-wise is most efficient. A useful way to remember this is that in Fortran, the first index varies fastest.

Accessing the memory in the order in which it is stored will be faster, and also means any vectorisation optomisations can be easily performed by the compiler.

In Fortran, this means writing loops like:

do j = 1, n
    do i = 1, n
        ! operate on A(i,j) ...
    end do
end do

This is the opposite way around to languages like C, which is row major.

  • When reading or writing an array, use the array name and not a DO loop or an implied DO-loop that specifies each element number. Fortran 95/90 array syntax allows you to reference a whole array by using its name in an expression.
real ::  array(100,100)

array = 0.0

array = array + 1           ! Increment all elements

This is particularly true for I/O operations:

- Write Whole Arrays or Strings - Write Array Data in the Natural Storage Order - Use Memory for Intermediate Results - Enable Implied-DO Loop Collapsing

  • Use the intent keyword to indicate which variables are passed and returned within subroutines and functions, this allows for compiler optimizations.

An example is shown below:

function fun( time, x ) result(dx)
  real, intent(in) :: time
  real, intent(in),dimension(:) :: x
  real, dimension(size(x)) :: dx

    dx(1) = 0.1 * ( 1.0-x(1) ) - 0.2 * x(2)
    dx(2) = -0.05 * x(2)
end function fun
  • Be careful for incorrect mismatching of integer and real variables. An example is shown below, note how the integer division results in a rounded down result (ie 2/3 = 0, hence 2**(0) = 1 an incorrect result).
program variableTesting
   implicit none
   real :: outerror, outcorrect

   outerror  = 2**(2/3)

   outcorrect = 2**(2.0/3.0)

   Print *,"outerror = ",outerror
   Print *,"outcorrect = ",outcorrect

end program variableTesting

This produces the following erroneous and correct outputs:

[dlbird@login01 FORTRAN]$ ./a.out
 outerror =    1.00000000
 outcorrect =    1.58740103
  • Go to statements are sometimes useful but they are discouraged because they are generally difficult to manage;

Debugging code

There does exist a number of FORTRAN code analysis tools (static and dynamic checking), a particular useful one is FORCHECK.

Also is camFORT a tool for cross checking variables and they computation elements within a program, this is particularly useful for high mathematical/ scientific codes.

Fortran pre-processing

The pre-processor is a text processing tool which is usually integrated into the compiler;

It is a separate stage and occurs prior to compilation;

#ifdef DEBUG
    print *, ‘count is’, counter

To assign the macro DEBUG, compile

[username@login01 ~]$  gfortran -c -DDEBUG code.F


The program would be compiled in the following way, optional Intel compiler available too:

[username@login01 ~]$  module add gcc/4.9.3
[username@login01 ~]$  gfortran -o testFortran  testFortran.f03
  • Good practice when compiling is to only use compiler optimization flags at the final stages towards production release, Intel's fortran compiler usually has -O2 set by default, so placing compiler flag -O0 would be necessary to turn this off.
  • Note : mpifort would be used as the compiler wrapper if the above example used openMPI.

Documentation generators

Suitable automatic source code generators for fortran are FORD and doxygen

Usage Examples

Batch example


#SBATCH -J openmpi-single-node
#SBATCH --ntasks-per-node 28
#SBATCH -o %N.%j.%a.out
#SBATCH -e %N.%j.%a.err
#SBATCH -p compute
#SBATCH --exclusive


module purge
module add gcc/4.9.3

export I_MPI_DEBUG=5
export I_MPI_FABRICS=shm:tmi
export I_MPI_FALLBACK=no

[username@login01 ~]$ sbatch demo.job
Submitted batch job 239552

HDF5 library

HDF5 is a versatile data model that can represent very complex data objects and a wide variety of metadata, which is ideal for data which has a large disparity to be stored in a standard format description. NetCDF is a similar system as well.

(Example from the HDF group)


  USE HDF5 ! This module contains all necessary modules


  CHARACTER(LEN=8), PARAMETER :: filename = "dsetf.h5" ! File name
  CHARACTER(LEN=4), PARAMETER :: dsetname = "dset"     ! Dataset name

  INTEGER(HID_T) :: file_id       ! File identifier
  INTEGER(HID_T) :: dset_id       ! Dataset identifier
  INTEGER(HID_T) :: dspace_id     ! Dataspace identifier

  INTEGER(HSIZE_T), DIMENSION(2) :: dims = (/4,6/) ! Dataset dimensions
  INTEGER     ::   rank = 2                        ! Dataset rank

  INTEGER     ::   error ! Error flag

  ! Initialize FORTRAN interface.
  CALL h5open_f(error)

  ! Create a new file using default properties.
  CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error)

  ! Create the dataspace.
  CALL h5screate_simple_f(rank, dims, dspace_id, error)

  ! Create the dataset with default properties.
  CALL h5dcreate_f(file_id, dsetname, H5T_NATIVE_INTEGER, dspace_id, &
       dset_id, error)

  ! End access to the dataset and release resources used by it.
  CALL h5dclose_f(dset_id, error)

  ! Terminate access to the data space.
  CALL h5sclose_f(dspace_id, error)

  ! Close the file.
  CALL h5fclose_f(file_id, error)

  ! Close FORTRAN interface.
  CALL h5close_f(error)


Further Information

Icon home.png