OpenMP on Spiedie (advanced)


Things covered in this tutorial:

  1. Writing batch scripts for multi-core programs
  2. Suitable Partitions for job requirements
  3. Compiling and linking on the fly

We assume you have familiarity with the OpenMP interface.

Click here if you would like to learn more

Writing batch scripts for multi-core programs

We are going to run a multi-core program which will utilize shared memory parallelism in a single node for high performance computing.

Note: If you would like to utilize parallelization using multiple nodes, checkout the MPI tutorial here

We’ve provided a simple openmp program source code here.

The simple program prints out a message for each active thread:

#include <stdio.h>
#include <omp.h>
 
int main() {
  #pragma omp parallel
  {
    int id = omp_get_thread_num();
    int total = omp_get_num_threads();
    printf("Greetings from process %d out of %d \n", id, total);
  }
  printf("parallel for ends.\n");
  return 0;
}

Let’s write a run script omp_run.sh to run our program.

First of all, we will include the shebang, as usual,

#!/bin/bash

Let’s name our job with

#SBATCH --job-name=OMP_TEST

Also, we can assign our output file name with:

#SBATCH --output=omp_output.log

Since we are using OpenMP and shared memory parallelism, we will only be using a single compute node:

#SBATCH -N 1

We will set the number of cores to 4 with:

#SBATCH -c 4

It is good practice to clear the environment from any previously loaded modules

module purge > /dev/null 2>&1

The following line sets We are now going to dynamically assign the number of OpenMP threads the program utilizes depending on the -c parameter we used above.

The following line sets the OMP_NUM_THREADS to the same values as -c:

if [ -n "$SLURM_CPUS_PER_TASK" ]; then
  omp_threads=$SLURM_CPUS_PER_TASK

In case the -c is not set, we fall back to 1 core (as is the default):

else
  omp_threads=1

Export the value to complete the process:

export OMP_NUM_THREADS=$omp_threads

Suitable Partitions for job requirements

Depending on the number of cores being used, it is a good idea to consider with partition would be the suitable for our program. The details for the partitions and the number of cores available per compute node can be found here

If you are using more thant 20 cores, it is recommended to assign the partition to KNL nodes.

In our test, we will use the standard partition due to our relatively small number of cores.

#SBATCH --partition-standard

Compiling and linking on the fly

It is good practice to compile and link custom code during your run, in order to make sure the programs run correctly.

So we can load the gcc module with:

module load gcc

We can call the compiler as usual:

gcc -o spiedie_omp open_mp.c -fopenmp

and, run the program:

./spiedie_omp

Submitting the job

We can submit our script by simply using:

sbatch omp_run.sh

We can also change the number of cores being assigned by using:

sbatch -c 2 omp_run.sh