【问题标题】:openMP:why am I not getting different thread ids when i uses " #pragma omp parallel num_threads(4)"openMP:为什么当我使用“#pragma omp parallel num_threads(4)”时没有得到不同的线程 ID
【发布时间】:2012-10-23 23:10:06
【问题描述】:

为什么我在使用“#pragma omp parallel num_threads(4)”时没有得到不同的线程 ID。在这种情况下,所有线程 id 都是 0。 但是当我评论该行并使用默认线程数时,我得到了不同的线程 ID。 注意:- 变量我使用变量 tid 来获取线程 id。

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]) 
{
int nthreads, tid;
int x = 0;

#pragma omp parallel num_threads(4)
#pragma omp parallel private(nthreads, tid)
  {
  /* Obtain thread number */
 tid = omp_get_thread_num();
  printf("Hello World from thread = %d\n", tid);

  // /* Only master thread does this */
   if (tid == 0) 
     {
     nthreads = omp_get_num_threads();
     printf("Number of threads = %d\n", nthreads);
     }

  }


}

以上代码的输出:-

Hello World from thread = 0
Hello World from thread = 0
Number of threads = 1
Hello World from thread = 0
Number of threads = 1
Hello World from thread = 0
Number of threads = 1
Number of threads = 1

当我评论上述行时的输出:-

Hello World from thread = 3
Hello World from thread = 0
Number of threads = 4
Hello World from thread = 1
Hello World from thread = 2

【问题讨论】:

  • 注意:- 为了编译我做了 gcc -fopenmp open.c -o hello

标签: c multithreading parallel-processing openmp


【解决方案1】:

您正在创建两个嵌套的并行区域。和这样做是一样的:

#pragma omp parallel num_threads(4)
{
  #pragma omp parallel private(nthreads, tid)
  {
    /* Obtain thread number */
    tid = omp_get_thread_num();
    printf("Hello World from thread = %d\n", tid);

    // /* Only master thread does this */
    if (tid == 0) 
    {
      nthreads = omp_get_num_threads();
      printf("Number of threads = %d\n", nthreads);
    }
  }
}

omp_get_num_threads() 返回最里面区域的线程数。所以你正在执行四个线程,每个线程都执行一个线程。

内部并行区域只执行一个线程,因为您没有启用嵌套并行。您可以通过调用omp_set_nested(1)来启用它。

http://docs.oracle.com/cd/E19205-01/819-5270/aewbi/index.html

如果您想创建一个并行区域并指定两个属性,而不是创建两个嵌套的并行区域,则可以这样做:

#pragma omp parallel num_threads(4) private(nthreads,tid)
{
  .
  .
  .
}

【讨论】:

  • 我试过“#pragma omp_set_nested(1)”,但它不起作用。我写在“#pragma omp parallel num_threads(4)”上面,哪里错了?
  • @jayeshhathila: omp_set_nested() 是一个常规函数
  • 我也得到了 tid=0 的原因,谢谢
【解决方案2】:

也可以通过将环境变量 OMP_NESTED 设置为 true 来启用嵌套

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多