Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openMP:why am I not getting different thread ids when i uses " #pragma omp parallel num_threads(4)"

Why am I not getting different thread ids when I uses " #pragma omp parallel num_threads(4)". All the thread ids are 0 in this case. But when I comment the line and use default number of threads, I got different thread ids. Note:- variable I used variable tid to get thread 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);
     }

  }


}

Output of above code:-

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

Output when I comment the line mentioned above:-

Hello World from thread = 3
Hello World from thread = 0
Number of threads = 4
Hello World from thread = 1
Hello World from thread = 2
like image 814
jayesh hathila Avatar asked Nov 03 '12 13:11

jayesh hathila


People also ask

How many threads does default OpenMP use?

When run, an OpenMP program will use one thread (in the sequential sections), and several threads (in the parallel sections). There is one thread that runs from the beginning to the end, and it's called the master thread.

How do I find my thread ID on OpenMP?

EffectThe omp_get_thread_num routine returns the thread number of the calling thread, within the team that is executing the parallel region to which the routine region binds. The thread number is an integer between 0 and one less than the value returned by omp_get_num_threads, inclusive.

What is the maximum number of threads in OpenMP?

The OMP_THREAD_LIMIT environment variable sets the maximum number of OpenMP threads to use for the whole OpenMP program. The defaut number in Sun's implementation is 1024. If this environment variable is set to one, then all parallel regions will be executed by one thread.


1 Answers

You are creating two nested parallel regions. It is the same as doing this:

#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() returns the number of threads in the innermost region. So you are executing four threads, each of which is executing one thread.

The inner parallel region is only executing one thread, because you haven't enabled nested parallelism. You can enable it by calling omp_set_nested(1).

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

If instead of making two nested parallel regions, you wanted to make a single parallel region and specify two properties, you can do this:

#pragma omp parallel num_threads(4) private(nthreads,tid)
{
  .
  .
  .
}
like image 100
Vaughn Cato Avatar answered Nov 02 '22 19:11

Vaughn Cato