openMP:warum bin ich nicht immer verschiedene thread-ids, wenn ich verwendet " #pragma omp parallel num_threads(4)"

Warum bin ich nicht immer verschiedene thread-ids, wenn ich verwendet " #pragma omp parallel num_threads(4)". Alle thread-ids sind in diesem Fall 0.
Aber wenn ich den Kommentar der Zeile und verwenden Sie die Standard-Anzahl der threads, habe ich verschiedene thread-ids.
Hinweis:- variable, die ich verwendet variable tid zu bekommen, 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);
     }

  }


}

Ausgabe des obigen Codes:-

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

Ausgabe, wenn ich kommentieren Sie die Zeile oben erwähnt:-

Hello World from thread = 3
Hello World from thread = 0
Number of threads = 4
Hello World from thread = 1
Hello World from thread = 2

InformationsquelleAutor der Frage jayesh hathila | 2012-11-03

Schreibe einen Kommentar