Tại sao tôi không nhận được các id chuỗi khác nhau khi tôi sử dụng "#pragma omp song song num_threads (4)". Tất cả các id thread là 0 trong trường hợp này. Nhưng khi tôi bình luận dòng và sử dụng số lượng mặc định của chủ đề, tôi đã nhận id thread khác nhau. Lưu ý: - biến tôi đã sử dụng biến tid để lấy id luồng.openMP: tại sao tôi không nhận được id luồng khác nhau khi tôi sử dụng "#pragma omp song song 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);
}
}
}
Sản lượng mã trên: -
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 khi tôi nhận xét dòng đề cập ở trên: -
Hello World from thread = 3
Hello World from thread = 0
Number of threads = 4
Hello World from thread = 1
Hello World from thread = 2
Lưu ý: - cho biên soạn tôi đã làm gcc -fopenmp open.c -o hello –