Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
129 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...