Skip to content

GNU compilers

The GNU/GCC compilers are part (together with OpenMPI and BLAS/LAPACK, see here) of foss toolchain. Programs compiled with optimization should have their output double-checked for accuracy. If the numeric output is incorrect or lacks the desired accuracy less-aggressive compile options should be tried.

Check available versions and load desired toolchain
  1. Check available versions of foss compiler toolchain
    module spider foss
    
  2. Load the desired version of toolchain
    module load foss/2022b
    

Compilers

The following compilers are available for every toolchain version. You can use man command for more information.

Command
Description
gcc C compiler
g++ C++ compiler
gfortran Fortran compiler

General compiler optimization flags

Recommended optimization flags

We suggest to use at least following optimization flags for compilation:

gcc -O3 -mavx512f mycode.c

The basic optimization flags are summarized below.

Command
Description
-O Optimized compilation
-O2 More extensive optimization, recommended for the most of codes
-O3 More aggressive than -O2 with longer compile times. Recommended for codes that loops involving intensive floating point calculations.
-Ofast -O3 optimization with extras.
-ffast-math Higher performance with floating-point calculations, risk of a slight precision lost.
-mtune=processor This flag does additional tuning for specific processor types, however it does not generate extra SIMD instructions.The tuning will involve optimizations for processor cache sizes, preferred ordering of instructions, and so on. We suggest to try -mtune=icelake-server.

SIMD Instructions flags

These flags will produce executables that contain specific SIMD instructions.

Command
Description
-march=native Creates an executable that uses SIMD instructions based on the CPU that is compiling the code. Additionally it includes the optimizations from the -mtune=native flag.
-march=arch This will generate SIMD instructions for a particular architecture and apply the -mtune optimizations. The useful values of arch are the same as for the -mtune flag above, e.g. icelake-server.
-mavx2 Generates code with AVX2 instructions.
-mavx512f Generates code with AVX512 instructions.

Simple test of compiler

You can simply test the gcc compiler with following Hello World! code:

Simple test of compiler
  1. Copy following code to file:
    #include <stdio.h>
    int main() {
    printf("Hello, World!");
    return 0;
    }
    
  2. Compile the code:
    gcc hello.c -o hello.x
    
  3. Run the binary:
    ./hello.x
    
Created by: Martin Blaško