C/C++

Using Intel Compilers

To check the version of Intel Compilers, type:

icc --version

output:

icc (ICC) 17.0.1 20161005

Copyright (C) 1985-2016 Intel Corporation. All rights reserved.

If you need to use different version of intel compilers than the default version in HPC, please check the modules and then load the appropriate one. check the Intel modules:

module avail intel

output:

-------------------------- /usr/local/share/modulefiles/Core ---------------------------

intel/17 (D)    intel/18    intel/20    intel/20-1

Load the appropriate module:

module load intel/<version>

Compiling a Code & Executing

Compiling a C or C++ Program

Interactive

Request the compute node:

srun --pty bash

Copy the content below in a hello.c file

 hello.c:

#include <stdio.h>

int main()

{

    printf("Hello World\n");

    return 0;

}

Compile the code to create "hello" as an executable

icc -o hello hello.c

Run the Executable

./hello

Your output will be: Hello World

Batch

Copy the content in your file (let's say job.slurm).

#!/bin/bash

#SBATCH -o hello.o%j

#SBATCH --time=00:02:00

#SBATCH -N 1

#SBATCH -n 1

#Compiling section

# module load intel -- intel is loaded by default

icc -o hello hello.c

#Copying files to scratch

cp -r hello $PFSDIR

#Running job

cd $PFSDIR

./hello

cp -ru * $SLURM_SUBMIT_DIR

Submit the job:

sbatch job.slurm

You will find the output at hello.o<jobid>

To compile the C++ source file "test.cpp" with the Intel C++ compiler, use the command

 icc -o test test.cpp

Note that the Intel C compiler is used for source files ending in ".c" and the Intel C++ compiler is used for source files ending in ".cpp" or ".C".

Using GNU compilers

Compiler Version

To check the version of GNU Compilers, type:

gcc --version

output:

gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)

Copyright (C) 2015 Free Software Foundation, Inc.

If you need a newer version, you can list installed versions with:

module avail gcc

Output:

-------------------/usr/local/share/modulefiles/Core --------------------------

gcc/6.3.0 (L,D)    gcc/7.3.0    gcc/9.2.0

and then load the version you need, e.g. module load gcc/9.2.0

Compiling a Code & Executing

Compiling a C or C++ Program 

Interactive:

Request the compute node:

srun --pty /bin/bash

Note: GNU compilers use different commands for compiling C and C++ source files.

Using BATCH:

We are going to compile and execute our C++ code in hello.cpp using BATCH:

Copy the following code in a file called "hello.slurm":

#!/bin/bash

#SBATCH -o hello.o%j

#SBATCH --time=00:02:00

#SBATCH -N 1

#SBATCH -n 1

#Compiling section

module load gcc

g++ -o hello hello.cpp

#Copying files to scratch

cp -r hello $PFSDIR

#Running job

cd $PFSDIR

./hello

cp -ru * $SLURM_SUBMIT_DIR

Submit the job:

sbatch hello.slurm

You will find the output at "hello.o<jobid>"

References: