[英]Performance issues of #pragma omp atomic with OMP_NUM_THREADS=1
[英]#pragma omp parallel num_threads is not working
#include<omp.h>
#include<stdio.h>
#include<stdlib.h>
void main(int argc, int *argv[]){
#pragma omp parallel num_threads(3)
{
int tid = omp_get_thread_num();
printf("Hello world from thread = %d \n",tid);
if(tid == 0){
int nthreads = omp_get_num_threads();
printf("Number of threads = %d\n",nthreads);
}
}
}
我正在學習 OpenMP,但我不明白為什么當我指定了線程數 3 時它只執行一個線程? 程序輸出:
Hello world from thread = 0
Number of threads = 1
在 VisualStudio 中只需打開 OMP。 可以參考https://msdn.microsoft.com/de-de/library/fw509c3b(v=vs.120).aspx
您需要使用-fopenmp
編譯您的程序。
g++ a.cc -fopenmp
omp_get_num_threads() 返回正在使用的線程總數
omp_get_thread_num() 返回當前線程 ID
你應該使用前一個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.