Programming/C-Plusplus

From HPC
Jump to: navigation , search

Programming Details

C++ is a general-purpose, imperative computer programming language. It is derived from the the C language and shares many constructs with it, it also expands into object orientated pragramming paradigms.

Icon tick.png There is direct support for openMP and openMPI.


Programming example


#include <omp.h>
#include <iostream> 

int main () 
{
    int thread_number;
    #pragma omp parallel private(thread_number)
    {
        #pragma omp for schedule(static) nowait
        for (int i = 0; i < 100; i++) 
        {
            thread_number = omp_get_thread_num();
            cout << "Thread " << thread_number << " says " << i << endl;
        }
    }
    return 0;
}

Compilation

The program would be compiled in the following way, optional Intel compiler available too:


[username@login01 ~]$  module add gcc/8.2.0
[username@login01 ~]$  g++ -o test2 -fopenmp test2.c (openMP support)
[username@login01 ~]$ ./test2

Thread 22 says 32
Thread 11 says 39
.....

Modules Available

The following modules are available for C

  • module add gcc/8.2.0 (GNU compiler)
  • module add intel/2018 (Intel compiler)


Note: OpenMP is a library directive within the compiler and does not require any additional module to be loaded.

Usage Examples

Batch example


#!/bin/bash

#SBATCH -J openmpi-single-node
#SBATCH -N 1
#SBATCH --ntasks-per-node 28
#SBATCH -o %N.%j.%a.out
#SBATCH -e %N.%j.%a.err
#SBATCH -p compute
#SBATCH --exclusive

echo $SLURM_JOB_NODELIST

module purge
module add gcc/8.2.0

export I_MPI_DEBUG=5
export I_MPI_FABRICS=shm:tmi
export I_MPI_FALLBACK=no

/home/user/CODE_SAMPLES/OPENMP/demo


[username@login01 ~]$ sbatch demo.job
Submitted batch job 234552

Further Information

Navigation