---
title: "OpenMP Software on the FASRC cluster"
id: "5380"
type: "epkb_post_type_1"
slug: "openmp-software-on-odyssey"
published_at: "2010-01-05T20:06:15+00:00"
modified_at: "2025-07-08T19:22:30+00:00"
url: "https://docs.rc.fas.harvard.edu/kb/openmp-software-on-odyssey/"
markdown_url: "https://docs.rc.fas.harvard.edu/kb/openmp-software-on-odyssey.md"
excerpt: "Introduction OpenMP, or Open Multi-Processing, is an application programming interface (API) for shared-memory parallel programming for C, C++, and Fortran. It allows developers to create parallel applications that can utilize multiple processor cores (via multithreading) and GPUs on a single..."
taxonomy_epkb_post_type_1_category:
  - "Software"
---

# OpenMP Software on the FASRC cluster

## Introduction

[OpenMP](https://www.openmp.org/)
, or Open Multi-Processing, is an application programming interface (API) for shared-memory parallel programming for [C](https://docs.rc.fas.harvard.edu/kb/c-programming-language/)
, [C++](https://docs.rc.fas.harvard.edu/kb/cpp-programming-language/)
, and [Fortran](https://docs.rc.fas.harvard.edu/kb/fortran/)
. It allows developers to create [parallel](https://docs.rc.fas.harvard.edu/kb/glossary/#Parallel)
 applications that can utilize multiple processor [cores](https://docs.rc.fas.harvard.edu/kb/glossary/#Core)
 (via multithreading) and [GPUs](https://docs.rc.fas.harvard.edu/kb/glossary/#Graphics_Processing_Unit_GPU)
 on a single [node](https://docs.rc.fas.harvard.edu/kb/glossary/#Node)
. OpenMP simplifies the process of parallel programming by allowing developers to incrementally parallelize existing serial code through the addition of compiler directives and runtime library routines. Parallelism can then be enabled at compile-time by specifying a command-line option, and tuned at run-time (or disabled) by setting specific environment variables.

## Using OpenMP on the FASRC Cluster

- Supported compilers are provided by the “intel”, “gcc”, and “nvhpc” [environment modules](https://docs.rc.fas.harvard.edu/kb/modules-intro/) .
- Ensure that the same compiler environment module that was used at compile-time is loaded before running the application. This ensures the same version of the OpenMP library is dynamically linked and used at run-time.
- The environment variable `OMP_NUM_THREADS` is set to `OMP_NUM_THREADS=1`by default.

## Example Code

Below are simple OpenMP example codes in both Fortran and C++.

In the examples:

1. Each thread prints its thread number in a *critical* region, which enforces mutual exclusion (i.e., can be executed by only one thread at a time), to avoid mangling concurrent output.
2. All threads wait until all other threads have encountered the *barrier.*
3. A *single* thread outputs the number of threads executing the *parallel* region.

### Fortran

```
!=====================================================================
! Program: omp_test.f90
!=====================================================================
program omp_test
  use omp_lib
  implicit none
!$omp parallel
!$omp critical
  write(*,*) "Thread number:", omp_get_thread_num()
!$omp end critical
!$omp barrier
!$omp single
  write(*,*) "Number of threads = ", omp_get_num_threads()
!$omp end single
!$omp end parallel
end program
```

### C++

```
//====================================================================
// Program: omp_test.cpp
//====================================================================
#include <iostream>
#include <omp.h>
using namespace std;
int main()
{
#pragma omp parallel
  {
    #pragma omp critical
    cout << "Thread number: " << omp_get_thread_num() << endl;
    #pragma omp barrier
    #pragma omp single
    cout << "Number of threads = " << omp_get_num_threads() << endl;
  }
}
```

## Compiling the program

Intel Fortran:

```
[username@rclogin02 ~]$ module load intel/25.0.1-fasrc01
[username@rclogin02 ~]$ ifx -o omp_test.x omp_test.f90 -qopenmp
```

Intel C++:

```
[username@rclogin02 ~]$ module load intel/25.0.1-fasrc01
[username@rclogin02 ~]$ icpx -o omp_test.x omp_test.cpp -qopenmp
```

GNU Fortran:

```
[username@rclogin02 ~]$ module load gcc/14.2.0-fasrc01
[username@rclogin02 ~]$ gfortran -o omp_test.x omp_test.f90 -fopenmp
```

GNU C++:

```
[username@rclogin02 ~]$ module load gcc/14.2.0-fasrc01
[username@rclogin02 ~]$ g++ -o omp_test.x omp_test.cpp -fopenmp
```

NVIDIA Fortran:

```
[username@rclogin02 ~]$ module load nvhpc/24.11-fasrc01
[username@rclogin02 ~]$ nvfortran -o omp_test.x omp_test.f90 -mp
```

NVIDIA C++:

```
[username@rclogin02 ~]$ module load nvhpc/24.11-fasrc01
[username@rclogin02 ~]$ nvc++ -o omp_test.x omp_test.cpp -mp
```

## Running the program

The following SLURM batch-job submission script can be used to submit the job to the queue:

```
#!/bin/bash
#SBATCH --job-name=omp_test    # Job name
#SBATCH --output=omp_test.out  # Output file for both stdout and stderr
#SBATCH --partition=test       # Partition(s) (separate with commas if using multiple)
#SBATCH --time=10              # Time limit (in minutes)
#SBATCH --mem=1750             # Memory (in "M"egabytes if no suffix specified)
#SBATCH --cpus-per-task=8      # Number of CPUs
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK
module load gcc/14.2.0-fasrc01 # replace with "intel/<version>" or "nvhpc/<version>" as appropriate
srun ./omp_test.x              # srun uses $SLURM_CPUS_PER_TASK as an input environment variable   
```

The OMP_NUM_THREADS environmental variable is used to set the number of CPUs allocated to the job (by the Slurm `#SBATCH -c 8` directive). If you name the above script `omp_test.batch`, for instance, the job is submitted to the queue with

```
sbatch omp_test.batch
```

## References

- [Official OpenMP specification](https://www.openmp.org/specifications/)
- [OpenMP tutorial from LLNL](https://computing.llnl.gov/tutorials/openMP)
- FASRC Parallel Job Workflows training
  - [OpenMP segment](https://youtu.be/_8ap3sMS3nU?si=url3StRm755zVzzH&t=1870) (YouTube)
  - [Example OpenMP codes](https://github.com/fasrc/User_Codes/tree/master/Parallel_Computing/OpenMP) (GitHub)

- [Job Efficiency and Optimization Best Practices](https://docs.rc.fas.harvard.edu/kb/job-efficiency-and-optimization-best-practices/)

Bookmarkable Links

- [1. Introduction](#Introduction)
- [2. Using OpenMP on the FASRC Cluster](#Using_OpenMP_on_the_FASRC_Cluster)
- [3. Example Code](#Example_Code)
  - [3.1. Fortran](#Fortran)
  - [3.2. C++](#C)

- [4. Compiling the program](#Compiling_the_program)
- [5. Running the program](#Running_the_program)
- [6. References](#References)

Last UpdatedJuly 8, 2025
