OpenMp在并行区域之前检测嵌套并行中的线程数 [英] OpenMp detecting number of threads in nested parallelism before parallel region

查看:171
本文介绍了OpenMp在并行区域之前检测嵌套并行中的线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在并行区域开始之前检测OpenMp中的线程数?如果我使用嵌套并行机制,则环境变量OMP_NUM_THREADS看起来像4,64.

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

此答案导致我使用以下代码查询OMP_NUM_THREADS的实现:

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

不幸的是,如果我调用getenv("OMP_NUM_THREADS"),则会观察到4,4而不是4,64的嵌套并行性.这对我来说真的很奇怪.您对此有解释吗?

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?

推荐答案

我已经解决了这一问题,方法是打开一个嵌套的并行区域来查询所有线程:

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

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

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.

这篇关于OpenMp在并行区域之前检测嵌套并行中的线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆