May 31st & June 1st are university holidays.
Next monthly maintenance June 7th 7am-11am - [Details]
Annual MGHPCC Power Shutdown Aug 9-12 [Details and Schedule]
STATUS PAGE No known issues.

Search Docs by Keyword

OpenMP Software on the FASRC cluster


This page will help you compile and run OpenMP applications on the cluster. Currently we have both the Intel and GNU compiler suites available.

Example Code

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

! Program: omp_test.f90
program omp_test
  implicit none
  integer(4) :: nthreads
  integer(4) :: tid
  integer(4) :: omp_get_num_threads
  integer(4) :: omp_get_thread_num
!$omp parallel private( tid )
  tid = omp_get_thread_num()
  write(6,*)"Thread ID:", tid
  if ( tid == 0 ) then
     nthreads = omp_get_num_threads()
     write(6,*)"Number of threads = ", nthreads
  end if
!$omp end parallel
  stop "End of program."
end program omp_test


// Program: omp_test.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
#include <omp.h>
using namespace std;
int main()
  int nthreads, tid;
#pragma omp parallel private( tid)
    tid = omp_get_thread_num();
    nthreads = omp_get_num_threads();
    cout << "Thread ID: " << tid << endl;
    if ( tid == 0 ){
      cout << "Number of threads = " << nthreads << endl;
  return 0;

Compiling the program

Intel, Fortran 90:

[username@rclogin02 ~]$ ifort -o omp_test.x omp_test.f90 -openmp

Intel, C++:

[username@rclogin02 ~]$ icpc -o omp_test.x omp_test.cpp -openmp

GNU, Fortran 90:

[username@rclogin02 ~]$ gfortran -o omp_test.x omp_test.f90 -fopenmp

GNU, C++:

[username@rclogin02 ~]$ g++ -o omp_test.x omp_test.cpp -fopenmp

Running the program

You could use the following SLURM batch-job submission script to submit the job to the queue:

#SBATCH -J omp_test
#SBATCH -o omp_test.out
#SBATCH -e omp_test.err
#SBATCH -p general
#SBATCH -t 30
#SBATCH --mem=1750
#SBATCH -c 8
srun -c $SLURM_CPUS_PER_TASK ./omp_test.x

The OMP_NUM_THREADS environmental variable is used to set the number of threads to the desired number. If you name the above script omp_test.batch, for instance, the job is submitted to the queue with

sbatch omp_test.batch


Was this article helpful?
0 out Of 5 Stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
How can we improve this article?
Need help?
© The President and Fellows of Harvard College
Except where otherwise noted, this content is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.