openMP:why am I not getting different thread ids when i uses " #pragma omp parallel num_threads(4)"
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)
{
.
.
.
}