GNU compilers¶
The GNU/GCC compilers are part (together with OpenMPI and BLAS/LAPACK, see here) of the 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
- Check available versions of foss compiler toolchain
module spider foss - Load the desired version of the toolchain
module load foss/2022b
Compilers¶
The following compilers are available for every toolchain version. You can use the 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 using at least the 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 most codes |
| -O3 | More aggressive than -O2 with longer compile times. Recommended for codes with loops involving intensive floating point calculations. |
| -Ofast | -O3 optimization with extras. |
| -ffast-math | Higher performance with floating-point calculations, at the risk of a slight loss of precision. |
| -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 the following Hello World! code:
Simple test of compiler
- Copy the following code to a file:
#include <stdio.h> int main() { printf("Hello, World!"); return 0; } - Compile the code:
gcc hello.c -o hello.x - Run the binary:
./hello.x