OpenMp detecting number of threads in nested parallelism before parallel region

橙三吉。 提交于 2019-12-12 06:49:55

问题


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.



来源:https://stackoverflow.com/questions/40221679/openmp-detecting-number-of-threads-in-nested-parallelism-before-parallel-region

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!