OpenMp detecting number of threads in nested paral

2019-09-21 14:31发布

问题:

How do I detect the number of threads in OpenMp before the parallel region starts? If I use nested parallelism the environment variable OMP_NUM_THREADS looks like 4,64.

get_nested_num_threads();
#pragma omp parallel
{
// starting 4 threads
  #pragma omp parallel
  {
    // starting 64 threads for each of the 4
  }
}

This answer leads to my implementation of querying OMP_NUM_THREADS with the following code:

#include <string.h>
#include <stdlib.h>

int get_nested_num_threads(){

  char delimiter[] = ",";
  char *ptr = NULL;
  char *num_threads =  NULL;
  num_threads = getenv("OMP_NUM_THREADS");
  int threads=1, nested=0;

  ptr = strtok(num_threads, delimiter);

  while ( ptr != NULL ){
    threads *= atoi(ptr);
    ptr = strtok(NULL,delimiter);
    nested += 1;
  }

  assert( nested <= 2 );
  return threads;
}

Unfortunately if I call getenv("OMP_NUM_THREADS") then I observe a nested parallelism of 4,4 instead of 4,64. Which is really strange to me. Do you have an explanation for that?

回答1:

I've solved it, by opening a nested parallel region to query all threads:

int get_nested_num_threads(){
  int threads=1;

#pragma omp parallel shared(threads)
  {
  #pragma omp single
    {
      threads = omp_get_num_threads();

      #pragma omp parallel shared(threads)
      {
        #pragma omp single
        {
          threads *= omp_get_num_threads();
        }
      }
    }
  }

  return threads;
}

As far as I know, you do not need to use firstprivate and lastprivate in C for this case. But you have to do it in Fortran.



标签: c nested openmp