Skip to content

Compiler Flags

Most compilers provide ways to optimize for multiple aspects of a code. In most cases optimization is done using the -O and -f options of the compiler. The exact optimizations and how to enable them depend on the compiler. For more details refer to the compiler documentation, e.g. the GCC optimization options.

Which compilers and toolchains are available is described on the page Compilers and Toolchains.

Optimization levels

Flag Meaning
-O0 No optimization. Fast compilation, useful together with -g for debugging.
-O2 Good default for production runs.
-O3 More aggressive optimization (e.g. more loop transformations and vectorization). Often faster, but not always.
-Ofast, -ffast-math Allow mathematically unsafe floating-point optimizations.

Fast math

-Ofast and -ffast-math change the floating-point semantics and may alter numerical results. Verify the results of your application before using these flags in production.

Target architecture

By default compilers generate code for a generic CPU of the architecture. To use all features of the CPU, e.g. wide vector instructions, the target architecture has to be specified. The CPU architectures differ between the partitions of HoreKa 2:

Partition CPU GCC / LLVM flag
HoreKa Onyx (cpu, dev-cpu) AMD EPYC 9655 (Zen 5) -march=znver5
HoreKa Blue (large) Intel Xeon Platinum 8368 (Ice Lake) -march=icelake-server
HoreKa Jade (gpu-b200, dev-gpu-b200) NVIDIA Grace (ARM) -mcpu=neoverse-v2
HoreKa Teal (gpu-h100, dev-gpu-h100) AMD EPYC 9354 (Zen 4) -march=znver4
HoreKa Teal (gpu-h200-8) Intel Xeon Platinum 8568Y+ (Emerald Rapids) -march=emeraldrapids
HoreKa Ruby (gpu-h200) AMD EPYC 9454 (Zen 4) -march=znver4

The architecture flags require a sufficiently recent compiler version (e.g. znver5 needs GCC 14 or newer, emeraldrapids needs GCC 13 or newer). Alternatively use -march=native (on ARM -mcpu=native), which optimizes for the CPU of the node the compiler is running on.

Flags depend on the compiler

Names and meaning of optimization and architecture flags differ between compilers and compiler versions. The flags in the tables on this page are examples; check the documentation of the compiler and version you use.

Compiler Optimization Target architecture
GCC (gcc, g++, gfortran) -O2 / -O3 -march=<arch> or -march=native
LLVM (clang, flang) -O2 / -O3 -march=<arch> or -march=native
AOCC (clang, flang) -O2 / -O3 -march=znver4 / -march=znver5
Intel (icx, icpx, ifx) -O2 / -O3 on Intel nodes -xHost or -march=<arch>, on AMD nodes -march=skylake-avx512 (do not use -xHost)
NVHPC (nvc, nvc++, nvfortran) -O2 / -fast -tp=host

Vectorization reports

To check whether loops are vectorized, use e.g. -fopt-info-vec (GCC), -Rpass=loop-vectorize (LLVM/AOCC), -qopt-report (Intel) or -Minfo=vect (NVHPC).

Further optimizations

  • Link-time optimization (-flto) allows optimizations across source files.
  • Profile-guided optimization (e.g. -fprofile-generate and -fprofile-use for GCC) uses data of a test run to optimize the code. The test run should be representative for your production runs.
  • Use optimized libraries (BLAS, FFTW, ...) instead of your own implementations, see Compilers and Toolchains.
  • Measure the effect of each optimization with a representative test case and use profiling to find the hot spots.

Compile where Run

Code compiled with architecture-specific flags (e.g. -march=native) on a login node may not run, or may run slowly, on nodes with a different CPU.

Be aware, the CPU architectures are different for the different partitions of HoreKa 2. Please see the cluster description.

It is recommended to compile the code on the same architecture/system it will be run on later, e.g. in an interactive job (salloc, see Slurm) or as the first step of the batch job on a node of the target partition.