Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
How do OpenMP thread ids work with recursion?

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);
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

It 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 is 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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!