Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMP set_num_threads() is not working

I am writing a parallel program using OpenMP in C++.

I want to control the number of threads in the program using omp_set_num_threads(), but it does not work.

#include <iostream> #include <omp.h> #include "mpi.h"  using namespace std;  int myrank; int groupsize; double sum; double t1,t2; int n = 10000000;  int main(int argc, char *argv[]) {     MPI_Init( &argc, &argv);     MPI_Comm_rank( MPI_COMM_WORLD, &myrank );     MPI_Comm_size(MPI_COMM_WORLD,&groupsize);      omp_set_num_threads(4);      sum = 0;     #pragma omp for  reduction(+:sum)     for (int i = 0; i < n; i++)         sum+= i/(n/10);      cout<<"sum="<<sum<<endl;     cout<<"threads="<<omp_get_num_threads()<<endl;      MPI_Finalize();     return 0; } 

The program outputs:

sum = 4.5e+007 threads=1 

How to control the number of threads?

like image 556
Nurlan Avatar asked Jun 19 '12 06:06

Nurlan


People also ask

How do I count the number of threads in OpenMP?

The omp_get_num_threads function returns the number of threads in the team currently executing the parallel region from which it is called.

What happens if you call omp_get_num_threads () immediately after you set the number of threads?

omp_get_num_threads. Returns the number of threads in the parallel region.

Can you use OpenMP in C++?

OpenMP is a library for parallel programming in the SMP (symmetric multi-processors, or shared-memory processors) model. When programming with OpenMP, all threads share memory and data. OpenMP supports C, C++ and Fortran. The OpenMP functions are included in a header file called omp.


1 Answers

Besides calling omp_get_num_threads() outside of the parallel region in your case, calling omp_set_num_threads() still doesn't guarantee that the OpenMP runtime will use exactly the specified number of threads. omp_set_num_threads() is used to override the value of the environment variable OMP_NUM_THREADS and they both control the upper limit of the size of the thread team that OpenMP would spawn for all parallel regions (in the case of OMP_NUM_THREADS) or for any consequent parallel region (after a call to omp_set_num_threads()). There is something called dynamic teams that could still pick smaller number of threads if the run-time system deems it more appropriate. You can disable dynamic teams by calling omp_set_dynamic(0) or by setting the environment variable OMP_DYNAMIC to false.

To enforce a given number of threads you should disable dynamic teams and specify the desired number of threads with either omp_set_num_threads():

omp_set_dynamic(0);     // Explicitly disable dynamic teams omp_set_num_threads(4); // Use 4 threads for all consecutive parallel regions #pragma omp parallel ... {     ... 4 threads used here ... } 

or with the num_threads OpenMP clause:

omp_set_dynamic(0);     // Explicitly disable dynamic teams // Spawn 4 threads for this parallel region only #pragma omp parallel ... num_threads(4) {     ... 4 threads used here ... } 
like image 84
Hristo Iliev Avatar answered Oct 02 '22 08:10

Hristo Iliev