OpenMP: Get total number of running threads

Posted by Konrad Rudolph on Stack Overflow See other posts from Stack Overflow or by Konrad Rudolph
Published on 2011-01-16T16:19:03Z Indexed on 2011/01/17 15:53 UTC
Read the original article Hit count: 195

Filed under:
|
|

I need to know the total number of threads that my application has spawned via OpenMP. Unfortunately, the omp_get_num_threads() function does not work here since it only yields the number of threads in the current team.

However, my code runs recursively (divide and conquer, basically) and I want to spawn new threads as long as there are still idle processors, but no more.

Is there a way to get around the limitations of omp_get_num_threads and get the total number of running threads?

If more detail is required, consider the following pseudo-code that models my workflow quite closely:

function divide_and_conquer(Job job, int total_num_threads):
  if job.is_leaf(): # Recurrence base case.
    job.process()
    return

  left, right = job.divide()

  current_num_threads = omp_get_num_threads()
  if current_num_threads < total_num_threads: # (1)
    #pragma omp parallel num_threads(2)
      #pragma omp section
        divide_and_conquer(left, total_num_threads)
      #pragma omp section
        divide_and_conquer(right, total_num_threads)

  else:
    divide_and_conquer(left, total_num_threads)
    divide_and_conquer(right, total_num_threads)

  job = merge(left, right)

If I call this code with a total_num_threads value of 4, the conditional annotated with (1) will always evaluate to true (because each thread team will contain at most two threads) and thus the code will always spawn two new threads, no matter how many threads are already running at a higher level.

I am searching for a platform-independent way of determining the total number of threads that are currently running in my application.

© Stack Overflow or respective owner

Related posts about c++

Related posts about parallel-processing