Dlaczego nie uzyskuję różnych identyfikatorów wątków, gdy używam "#pragma omp parallel num_threads (4)". Wszystkie identyfikatory wątku mają w tym przypadku wartość 0. Ale kiedy skomentowałem linię i użyłem domyślnej liczby wątków, otrzymałem różne identyfikatory wątków. Uwaga: - zmienna I użyła zmiennej tid, aby uzyskać identyfikator wątku.openMP: dlaczego nie uzyskuję różnych identyfikatorów wątków, gdy używam "#pragma omp równoległego num_threads (4)"
#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);
}
}
}
Wynik powyższego kodu: -
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
wyjścia kiedy komentować linię mowa powyżej: -
Hello World from thread = 3
Hello World from thread = 0
Number of threads = 4
Hello World from thread = 1
Hello World from thread = 2
Uwaga: - do kompilacji zrobiłem gcc -fopenmp open.c -o hello –