0

I'm trying various ways to set my number of threads by either using clause or setting in the environment, but none of them has worked. Is there any way to force OpenMP to get a custom number of threads?

#include <cmath>
#include <random>
#include <iostream>
#include <chrono>
#include <cfloat>
#include <iomanip>
#include <cstdlib>
#include <omp.h>
#include <trng/yarn2.hpp>
#include <trng/mt19937_64.hpp>
#include <trng/uniform01_dist.hpp>
using namespace std;

double function(double x) {
    return cos(x) / landa;
}

int main() {
    int rank; 
     const int N = 1000000;
    double sum = 0.0;
    omp_set_num_threads(6);
    omp_set_dynamic(0);
    #pragma omp reduction(+ : sum)  //default(none) 
    {

    trng::yarn2 r;

    int size = omp_get_num_threads();
    cout<<size<<endl; 

    int rank = omp_get_thread_num();

    trng::uniform01_dist<> u;

    r.jump(2 * (rank * N / size));
    
    for (long i = rank * N/ size; i < (rank + 1) * N / size; ++i) {
       double x= u(r); 
        x = (-1.0) * log(1.0 - x); 
        sum = sum+function(x);
    
    }

    }

    return 0;
}
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
MA19
  • 510
  • 3
  • 15

2 Answers2

2
#pragma omp num_threads(6) reduction(+ : sum)  //default(none) 
{
 //OMP_NUM_THREADS=6; 
omp_set_num_threads(6);

This is wrong, you need to set the number of threads before the parallel region. Furthermore, you are missing the parallel clause there. So you can either do:

omp_set_num_threads(6)
#pragma omp parallel reduction(+ : sum)
{
   ...
}

or

#pragma omp parallel num_threads(6) reduction(+ : sum)

You do not need both.

The first approach (i.e., omp_set_num_threads) sets the number of threads to be used by all subsequent parallel regions, whereas the second approach num_threads explicitly set the number of threads to be used only by the enclosing parallel region.

If it still does not work then you have to explicitly disable dynamic teams:

By adding the following call:

omp_set_dynamic(0);
#pragma omp parallel ... 
{
   ...
}

From source one can read:

Summary The omp_set_dynamic routine enables or disables dynamic adjustment of the number of threads available for the execution of subsequent parallel regions by setting the value of the dyn-var ICV.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
2

The correct way to do this is using one of the following ways in your program:

#include <stdio.h>

#include <omp.h>

int main(void) {
    // option 1:
    omp_set_num_threads(3);
    #pragma omp parallel
    {
        #pragma omp single
        {
            printf("Number of threads: %d\n", omp_get_num_threads());
        }
    }

    // option 2:
    #pragma omp parallel num_threads(12)
    {
        #pragma omp single
        {
            printf("Number of threads: %d\n", omp_get_num_threads());
        }
    }

    return 0;
}

You can call the omp_set_num_threads function (option 1 in the code) with the number of threads that you want for the next and all future parallel regions. Note, you have to call this from the main thread of the program, if you put that call into a parallel region, you'll get a different effect.

You can also use the num_threads clause to set the number of threads for a specific parallel region. Other parallel regions will not be affected by this. In the code that you've posted, the syntax is wrong (you're missing the parallel in your OpenMP directive). Not sure, why compiler did not tell you that.

Finally, you can set the number of threads for all parallel regions of the program by setting OMP_NUM_THREADS from your shell, e.g., bash: export OMP_NUM_THREADS=27.

PS: Please try to provide smaller examples that actually compile and still show the problem that you're having. That makes things a lot easier for people to look at your problem and provide you with a solution.

Michael Klemm
  • 2,658
  • 1
  • 12
  • 15