OpenMP 线程 ID 如何与递归一起使用?

How do OpenMP thread ids work with recursion?

这是一个简单的递归程序,每次递归调用都分成两部分。正如预期的那样,结果是对 rec 的 2 + 4 + 8 次调用,但线程数始终相同:两个,并且 id 在 0 和 1 之间来回跳动。我希望每次递归调用都保留 id,最后会创建 8 个线程。究竟是怎么回事?代码有问题吗?

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

void rec(int n) {
  if (n == 0)
    return;

  #pragma omp parallel num_threads(2)
  {
    printf("Currently at %d -- total %d\n", omp_get_thread_num(), omp_get_num_threads());
    rec(n - 1);
  }
}

int main() {
  omp_set_nested(1);
  rec(3);
}

您的代码按照 OpenMP 标准运行。在 OpenMP documentation 中您可以找到关于 omp_get_num_threads 的以下内容:

Summary: The omp_get_num_threads routine returns the number of threads in the current team.

Binding: The binding region for an omp_get_num_threads region is the innermost enclosing parallel region.

Effect: The omp_get_num_threads routine returns the number of threads in the team that is executing the parallel region to which the routine region binds. If called from the sequential part of a program, this routine returns 1.

omp_get_thread_num 具有相同的结合区域:

The binding region for an omp_get_thread_num region is the innermost enclosing parallel region.

表示omp_get_num_threadsomp_get_thread_num只绑定到最里面的parallel region,所以使用多少个嵌套parallel region都没有关系。您的每个并行区域都由 #pragma omp parallel num_threads(2) 定义,因此 omp_get_num_threads 的 return 值为 2(只要您有足够的线程可用)并且 return 的值为 omp_get_thread_num01.