1

I am looking for the correct way to use OMP_PLACES and OMP_PROC_BIND=close in my code. when I run this, I get the eror that :

error: ‘OMP_PLACES’ was not declared in this scope


#include <iostream>
#include <random>
#include <fstream>
#include <iomanip>
#include <cfloat>
#include <omp.h>
#include <stdlib.h>
using namespace std; 

int main()
{
    const int N = 100;
    unsigned seed = 0;
    double sum = 0.0;
    double avg;
    srand(seed);
    default_random_engine g(seed);
    uniform_real_distribution<double> distribution(0.0f, nextafter(1.0f, DBL_MAX));
    #pragma omp parallel 
    {
        OMP_PLACES="cores(4)";
        OMP_PROC_BIND=close;
    
     #pragma omp for reduction (+: sum) 
     for (int i = 0; i < N; i++) {

        double x = distribution(g);
        sum = sum + x;
    }

    avg = sum / static_cast<double> (N);
    
     
    }

    

}
MA19
  • 510
  • 3
  • 15

1 Answers1

3

The OMP_PLACES and OMP_PROC_BIND variables must be set in the shell environment. So, if you are using bash, then you can do this:

$ export OMP_PLACES="cores(4)"
$ export OMP_PROC_BIND=close
$ ./your_application

An alternative would be to use the proc_bind() clause in your code:

// header omitted

int main()
{
    const int N = 100;
    unsigned seed = 0;
    double sum = 0.0;
    double avg;
    srand(seed);
    default_random_engine g(seed);
    uniform_real_distribution<double> distribution(0.0f, nextafter(1.0f, DBL_MAX));
    #pragma omp parallel proc_bind(close)
    {
        #pragma omp for reduction (+: sum) 
        for (int i = 0; i < N; i++) {
            double x = distribution(g);
            sum = sum + x;
        }
       // remainder of code to follow
    }
  ...
}

You would then still need the

$ export OMP_PLACES="cores(4)"

to specify the places that you want the code to run on.

PS: You may want to also check this SO page about C++ thread-safety of random number generators: C++11 Thread safety of Random number generators

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