×

Search anything:

Basic OpenMP functions

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

Reading time: 15 minutes

There are 3 basic functions in OpenMP namely omp_get_thread_num, omp_set_num_threads (nthreads) and omp_get_num_threads.

Functions

  • omp_get_thread_num() - get the thread rank in a parallel region (0- omp_get_num_threads() -1)
  • omp_set_num_threads(nthreads) - set the number of threads used in a parallel region
  • omp_get_num_threads() - get the number of threads used in a parallel region

Example


#include <omp.h>
int omp_get_thread_num();
# pragma omp parallel
{
  printf("Thread rank: %d\n", omp_get_thread_num());
}

Output

Note that the thread rank is not in order:


Thread rank:  2
Thread rank:  0
Thread rank:  3
Thread rank:  1
OpenGenus Tech Review Team

OpenGenus Tech Review Team

The official account of OpenGenus's Technical Review Team. This team review all technical articles and incorporates peer feedback. The team consist of experts in the leading domains of Computing.

Read More

Improved & Reviewed by:


Basic OpenMP functions
Share this