Compile programs on Quartz at IU

On this page:


Overview

Following is information about compiling serial and parallel C, C++, and Fortran programs using the GNU Compiler Collection, the Intel Compiler Suite, the NVIDIA HPC compilers, and the MVAPICH compilers available on Quartz.

Quartz uses the Lmod module management system to allow users to customize their software environments. For more about Lmod, see Use modules to manage your software environment on IU research supercomputers.

GNU Compiler Collection

Set up your user environment

The GNU Compiler Collection (the gnu module) is added by default as part of the standard user environment on Quartz. It includes compilers for C, C++, and Fortran codes:
GNU Compiler Collection
Language Compile command Manual page
C gcc man gcc
C++ g++ man g++
Fortran gfortran man gfortran

With the gnu module loaded, the GNU Scientific Library (GSL) numerical library for C and C++ programs is available. To add it to your user environment, load the gsl module:

module load gsl

FFTW compiled with GNU and MVAPICH is available if you also load the openmpi module. To add both to your user environment, enter:

module load openmpi fftw

Optimize your code

Adding the -O option to your compile command enables various optimization flags that tell the compiler to try to improve your code's performance. A higher -O level indicates higher optimization, but can add compilation time and potentially alter the semantics of your program:

UITS recommends using -O2 or -03 for production runs:

Optimization level Description
-O0
No optimization (this is the default)
-O or -O1
These levels are identical; the compiler tries to reduce code size and execution time, but doesn't perform optimizations that take a lot of compilation time.
-O2
This level turns on all -O1 optimization flags plus others that increase both performance and compilation time.
-O3
This turns on additional optimization flags along with those specified by -O2.

Examples

UITS recommends using the mtune=native and march=native options with the GNU compilers to generate instructions for the machine and CPU type. Compile by default in 64-bit. Add the -m32 switch to your compile command to compile in 32-bit; add the -m64 switch to return to 64-bit.

Following are example commands for compiling optimized serial programs with the GNU compilers:

  • To compile a serial C program (for example, my_program.c) and create an optimized executable (for example, my_program.exe), on the command line, enter:
    gcc -O2 -mtune=native -march=native my_program.c -o my_program.exe
  • To compile a serial C++ program (for example, my_program.cpp) and create an optimized executable (for example, my_program.exe), on the command line, enter:
    g++ -O2 -mtune=native -march=native my_program.cpp -o my_program.exe
  • To compile a serial Fortran program (for example, my_program.f) and create an optimized executable (for example, my_program.exe), on the command line, enter:
    gfortran -O2 -mtune=native -march=native program.f -o my_program.exe

To enable support for shared-memory multi-threaded (OpenMP) parallel programs, you must pass the -fopenmp flag to the compiler; for example:

gcc -fopenmp my_omp_program.c -o my_omp_program.exe

For distributed-memory examples, see the MVAPICH compiler section below.

Intel Compiler Suite

Set up your user environment

The Intel Compiler Suite is available on Quartz; it includes compilers for C, C++, and Fortran codes:

Intel Compiler Suite
Language Compile command Manual page
C icc man icc
C++ icpc man icpc
Fortran ifort man ifort

On Quartz, to switch your user environment to the Intel compilers, load the intel module; on the command line, enter:

module load intel

Lmod automatically replaces your current compiler environment with the new one, activates and/or deactivates any dependent modules, and sets environment variables allowing you to link applications to Intel performance libraries, including Integrated Performance Primitives (IPP), Math Kernel Library (MKL), and Threaded Building Blocks (TBB).

Optimize your code

Adding the -O option to your compile command enables various optimization flags that tell the compiler to try to improve your code's performance. A higher -O level indicates higher optimization, but can add compilation time and potentially alter the semantics of your program:

Optimization level Description
-O1
Optimize for maximum speed, but disable some optimizations that increase code size for a small speed benefit.
-O or -O2
These levels are identical. Optimize for maximum speed. This is the default.
-O3
Optimize for maximum speed and enable more aggressive optimizations that may not improve performance on some programs.
-O0 Disable optimizations.

UITS recommends using -O2 for production runs. You can also try the -fast switch to see if this improves your code's performance.

Examples

Following are example commands for compiling optimized serial programs with the Intel compilers (compile by default in 64-bit):

  • To compile a serial C program (for example, my_program.c) and create an optimized executable (for example, my_program.exe), on the command line, enter:
    icc -O my_program.c -o my_program.exe
  • To compile a serial C++ program (for example, my_program.cpp) and create an optimized executable (for example, my_program.exe), on the command line, enter:
    icpc -O my_program.cpp -o my_program.exe
  • To compile a serial Fortran program (for example, my_program.f) and create an optimized executable (for example, my_program.exe), on the command line, enter:
    ifort -O my_program.f -o my_program.exe

To enable support for shared-memory multi-threaded (OpenMP) parallel programs, you must pass the -openmp flag to the compiler; for example:

icc -O2 -openmp my_omp_program.c -o my_omp_program.exe

For distributed-memory examples, see the MVAPICH compiler section.

NVIDIA HPC compilers

Set up your user environment

The NVIDIA HPC compilers are available on Quartz. The NVIDIA HPC package includes compilers for C, C++, and Fortran codes:

NVIDIA HPC compilers
Language Compile command Manual page
C nvc man nvc
C++ nvc++ man nvc++
Fortran nvfortran man nvfortran

On Quartz, to switch your user environment to the NVIDIA HPC compilers, on the command line, enter:

module load nvhpc

Lmod automatically replaces your current compiler environment with the new one, and activates and/or deactivates any dependent modules accordingly.

FFTW compiled with NVHPC and Open MPI is available if you also load the openmpi module. To add both to your user environment, enter:

module load openmpi fftw

Optimize your code

Use the NVIDIA HPC compiler commands with the -O option to enable various local and global optimizations.

Optimization level Description
-O0
No optimization; the compiler generates a basic block for each statement; no scheduling or global optimizations are performed
-O1
Local optimization; scheduling of basic blocks and register allocation are performed
-O
Level -O2 global optimizations are performed, including traditional scalar optimizations, induction recognition, and loop invariant motion; no SIMD vectorization is enabled
-O2
Global optimization; all level -O1 local optimizations, plus the global optimizations described in -O; more advanced optimizations, such as SIMD code generation, cache alignment, and partial redundancy elimination, are enabled

Examples

Following are example commands for compiling optimized serial programs with the NVIDA HPC compilers:

  • To compile a serial C program (for example, my_program.c) and create an optimized executable (for example, my_program.exe), on the command line, enter:
    nvc -O my_program.c -o my_prog.exe
  • To compile a serial C++ program (for example, my_program.cpp) and create an optimized executable (for example, my_program.exe), on the command line, enter:
    nvc++ -O my_program.cpp -o my_program.exe
  • To compile a serial Fortran program (for example, my_program.f) and create an optimized executable (for example, my_program.exe), on the command line, enter:
    nvfortran -O my_program.f -o my_program.exe

MVAPICH compilers

Set up your user environment

MVAPICH compilers are added to your user environment on Quartz by default. MVAPICH compiler commands are available for C, C++, and Fortran codes. The compiler commands add the necessary flags for compiling and linking MVAPICH programs, and then invoke the respective back-end compilers to perform the actual commands:

MVAPICH compilers
Languages Compiler commands Manual pages
C mpicc man mpicc
C++ mpicxx man mpicxx
Fortran mpif77
mpifort
man mpif77
man mpifort

To optimize your code, use the optimization flags that correspond to the base compiler module (GNU or Intel) that you have loaded.

Examples

  • To compile and link an MVAPICH program written in C (for example, my_mvapich_program.c), on the command line, enter:
    mpicc my_mvapich_program.c -o my_mvapich_program.exe
  • To compile and link an MVAPICH program written in C++ (for example, my_mvapich_program.cxx), on the command line, enter:
    mpicxx my_mvapich_program.cxx -o my_mvapich_program.exe
  • To compile and link an MVAPICH program written in Fortran 90 (for example, my_mvapich_program.f), on the command line, enter:
    mpifortran my_mvapich_program.f -o my_mvapich_program.exe

Get help

If you have questions about compilers on the IU research supercomputers or need help, contact the UITS Research Applications and Deep Learning team.

This is document bgyd in the Knowledge Base.
Last modified on 2021-11-02 16:54:52.