Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
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