【发布时间】:2014-06-25 19:39:20
【问题描述】:
#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
【问题讨论】:
-
您可能以错误的方式编译它。以
gcc为例,如果您分别编译和链接,请确保有-fopenmp用于链接和 编译。其他编译器也应该类似。 -
你是如何编译这个的?如果是在 Visual Studio 中,则必须explicitly enable OpenMP。
标签: c++ c parallel-processing openmp