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