OpenMp检测并行区域之前嵌套并行性中的线程数

时间:2016-10-24 15:06:25

标签: c nested openmp

如何在并行区域启动之前检测OpenMp中的线程数?如果我使用嵌套并行性,则环境变量OMP_NUM_THREADS看起来像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导致我使用以下代码查询OMP_NUM_THREADS

#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;
}

不幸的是,如果我拨打getenv("OMP_NUM_THREADS"),我会发现4,4而不是4,64的嵌套并行性。这对我来说真的很奇怪。你对此有解释吗?

1 个答案:

答案 0 :(得分:0)

我已经通过打开一个嵌套的并行区域来查询所有线程来解决它:

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;
}

据我所知,在这种情况下,您无需在C中使用firstprivatelastprivate。但你必须在Fortran中做到这一点。