OpenMP: Set number of threads using omp_set_num_threads

OpenMP allows setting number of threads using omp_set_num_threads() function while the program is executing. This function is a part of Runtime Library Routines provided by OpenMP. This function helps developer to have greater control over number of threads used inside the OpenMP codes. This function can be called any number of times inside the OpenMP code thus providing flexibility of having different number of threads for different parallel regions.

Please note that this function should be called before program enters in the given parallel region. Also, if the parallel region uses num_threads clause to set number of threads, then the value set by omp_set_num_threads() function will be ignored (only for that parallel region).

Syntax

The syntax for C/C++ is as follows

void omp_set_num_threads(int num_threads);

And for Fortran the syntax is as follows

subroutine omp_set_num_threads(num_threads)
integer num_threads

Example

Let’s look at one example for better understanding of this function

#include<stdio.h>
#include<omp.h>

int main()
{
	int num_thds, myid;
	
	printf("\nPart1:");
	
	// Set number of threads equal to 4
	omp_set_num_threads(4);
	
	// Parallel Region starts
	#pragma omp parallel private(num_thds, myid)
	{
		// Get total number of threads in this parallel region
		num_thds = omp_get_num_threads();
		
		// Get unique identification number for the given thread among all the threads in this parallel region
		myid = omp_get_thread_num();
		
		printf("\nPart1: from thd num %d out of %d thds!", myid, num_thds);
	}

	printf("\n\nPart2:");
	
	// Set number of threads equal to 2
	omp_set_num_threads(2);
	
	// Parallel Region starts
	#pragma omp parallel private(num_thds, myid)
	{
		// Get total number of threads in this parallel region
		num_thds = omp_get_num_threads();
		
		// Get unique identification number for the given thread among all the threads in this parallel region
		myid = omp_get_thread_num();
		
		printf("\nPart2: from thd num %d out of %d thds!", myid, num_thds);
	}
	printf("\nProgram Exit!\n");
}

In this example we have two parallel regions. We wish to use 4 threads in first parallel region and 2 threads in second parallel region. So, we have set 4 threads using omp_set_num_threads() just before first parallel region starts. After the completion of the first parallel region, we have called omp_set_num_threads() again to set the number of threads to 2. If we do not use omp_set_num_threads() second time, the second parallel region will also execute with 4 threads.

To compile this code, we can use following command –

gcc -fopenmp omp_set_num_threads.c

Output of this code will look something like/equivalent to this –

Part1:
Part1: from thd num 1 out of 4 thds!
Part1: from thd num 0 out of 4 thds!
Part1: from thd num 2 out of 4 thds!
Part1: from thd num 3 out of 4 thds!

Part2:
Part2: from thd num 1 out of 2 thds!
Part2: from thd num 0 out of 2 thds!
Program Exit!

As we can see from the output of this code, first parallel region is running with 4 threads whereas second parallel region is running with 2 threads.

Resources

  1. OpenMP official website