Laboratory for Scientific Computing

Programming C/C++/Fortran

Programming

In general, the best languages for programming for HPC applications are C, C++, and Fortran (not necessarily in that order), with Python following closely behind. There is a course in C++ as part of the MPhil in Scientific Computing, and anyone is welcome at the lectures.

There are also courses in C++, Fortran, and Python, run by the UCS, which can be found at UCS Training.

When trying to learn more about these languages, you may find tutorials on the Internet, but be warned that these do not necessarily teach good practice in programming, and may mislead or otherwise confuse.

GCC

The main compiler available on the LSC network is the GNU Compiler Collection. This includes compilers:

  • gcc - Compiles C programs
  • g++ - Compiles C++ programs
  • gfortran - Compiles Fortran programs

The default version of these is 9.4. If you need a more up-to-date version, then use gcc-10 or gcc-12 (or the C++/Fortran equivalents).

Compiler flags

Ideally, you should use no optimization while debugging your programs, and compile with all compiler warnings turned on. For GCC, the following should suffice:

gcc -Wall -Wextra -ansi -pedantic -g

Once you are (reasonably) certain that your code is working correctly, you can apply optimization. Simply add the -O3 flag when compiling. This can make a substantial difference to your code's performance, especially for C++.

Clang

Another compiler is the Clang compiler, which may produce slightly inferior code to GCC (code-dependent), but tends to have substantially better compile-time diagnostics.

This is available as clang (version 10 by default) or clang-12 for version 12.

Intel Compilers

The Intel C++ and C compilers are available at /lsc/opt/intel. To activate these, run:

source /lsc/opt/intel/setvars.sh

and then the Intel compilers: icx, icx-cc, ifort will be available in your PATH.

Other Compilers

If you need access to NAG compilers, please speak to Philip Blakely (pmb39), as these are commercial compilers which would need to be purchased. You will need to make a reasonable case for requiring these, though, such as a demonstration that your code will definitely run faster with these compilers (they are available on Darwin).

MPI

The MPI implementation installed on the LSC network is OpenMPI (version 4.0.3). Compiling with these simply requires use of the MPI wrapper scripts:

  • mpicc - Compiles C programs
  • mpic++ - Compiles C++ programs
  • mpif77 - Compiles Fortran 77 programs
  • mpif90 - Compiles Fortran 90 programs

These just call the underlying compiler with a few extra command-line options; run mpic++ -show to see these options. If you want to use a different compiler (e.g. gcc-10), then you need to set:

export OMPI_CXX=g++-10
export OMPI_CC=gcc-10

in your .bashrc.

Other MPI implementations

The MPICH2 implementation of MPI is also available on the LSC network; use mpicc.mpich as the compiler instead (as well as mpirun.mpich to run codes. If you attempt to run a code compiled with OpenMPI compilers by using mpirun.mpich (or vice-versa) then confusing things may happen.

OpenMP

OpenMP (not to be confused with OpenMPI) is a parallelisation framework consisting of extensions to the C, C++, and Fortran languages. Support for this depends on the compiler you are using. The GCC compilers have OpenMP support, but need the -fopenmp flag to be passed to them to enable this.