3

I'm trying to calculate Pi in Qt5 using C++ and OpenMP (with focus on reduction data clause). In this program, I provide the calculation precision and the number of CPU cores engaged.

So far, I have the following code:

    int num_steps= ui->numberStepsLineEdit->text().toInt();
    double x=0.0;
    double sum = 0.0;
    #pragma omp parallel private(i,x)
    {
        #pragma omp for reduction(+:sum) schedule(static)
        for (int i=0; i<num_steps; i++)
        {
             x=(i+0.5)/(double)num_steps;
             sum = sum + 4.0/(1.0+x*x);
        }
     }
     double pi=sum/(double)num_steps;
     QString result= QString::number(pi, 'g', 10);
     ui->piLabel->setText(result);

The problem is that I need to specify the number of CPU cores engaged in the calculation, and I've looked on the internet for examples without success.

How can I set the number of CPU cores engaged in the calculation? (I don't want to set the number of threads).

Thank you a lot in advance.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
walid
  • 45
  • 1
  • 7
  • You can only set the number of threads. AFAIK, there is no way to set the number of cores. – P.W May 15 '19 at 10:09
  • why do you want to specify the number of cores? If you cannot do that then maybe there is another solution for the actual problem you are trying to solve – 463035818_is_not_an_ai May 15 '19 at 10:15
  • Possible duplicate of [OpenMP thread mapping to physical cores](https://stackoverflow.com/questions/4717251/openmp-thread-mapping-to-physical-cores) – Zulan May 15 '19 at 10:48
  • Thanks for answers guys. I read in API specification that you can use OMP_PLACES combined with OMP_PROC_BIND to achieve the goal. Still not sure though. – walid May 15 '19 at 11:23

2 Answers2

4

The way how you can emulate what you're asking for is to set the desired number of cores as the number of threads in the OpenMP code using the num_threads() clause or the omp_set_num_threads() API Call. Then use the OMP_PROC_BIND and OMP_PLACES environment variables to control the mapping of the threads to the number of cores you desire.

So, for instance, if you would like to use only four cores of the system, you'd do this:

#pragma omp parallel num_threads(4)
{
    // your parallel code 
}

$> OMP_PLACES=cores(4) OMP_PROC_BIND=compact ./a.out
Michael Klemm
  • 2,658
  • 1
  • 12
  • 15
  • Hi Michael, and thanks for the reply. That's what I started discovering (the use of OMP_PLACES and OMP_PROC_BIND), but, how can I set these variables inside my program? (I'm new to OpenMP and Qt). – walid May 15 '19 at 11:29
  • Unfortunately, you cannot set them (as of OpenMP v5.0) from your code as OpenMP does not expose setter functions for the internal control variables behind these environment variables. So, you'll have to stick to setting the environment variables before executing your application code. – Michael Klemm May 15 '19 at 11:32
0

With OpenMP you set the number of threads and the OS maps those to cores. See also: OpenMP thread mapping to physical cores

Gerriet
  • 1,302
  • 14
  • 20