Home > Back-end >  How do OpenMP thread ids work with recursion?
How do OpenMP thread ids work with recursion?

Time:03-18

Here is a simple recursive program that splits into two for every recursive call. As expected, the result is 2 4 8 calls to rec, but the number of threads is always the same: two, and the ids bounce back and forth between 0 and one. I expected each recursive call to retain the id, and that 8 threads would be made in the end. What exactly is going on? Is there any issue with the code?

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

CodePudding user response:

Your code is working as expected by OpenMP standard. In OpenMP documentation you can find the following about 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 has the same binding region:

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

In means that omp_get_num_threads and omp_get_thread_num bind to the innermost parallel region only, so it does not matter how many nested parallel regions are used. Each of your parallel regions are defined by #pragma omp parallel num_threads(2), therefore the return value of omp_get_num_threads is 2 (as long as you have enough threads available) and the return value of omp_get_thread_num is either 0 or 1.

  • Related