为什么OpenMP程序只在一个线程中运行 [英] Why OpenMP program runs only in one thread

查看:193
本文介绍了为什么OpenMP程序只在一个线程中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是用一个简单的c程序尝试OpenMP

  test(){
for(int i = 0 ; i <100000000; i ++);
}
main(){
printf(CPU的数量:%d \\\
,omp_get_num_procs());
#pragma omp parallel for num_threads(4)
for(int i = 0; i <100; i ++)test();
}

编译为 g ++ -fopenmp 。它可以正确打印出我有4个CPU,但所有测试功能都在线程0 下运行。
我试图修改 OMP_NUM_THREADS 。但它没有效果。
我有一切与在线示例一样,但为什么我不能让它工作?



非常感谢您的意见!

您的问题在这里:

  

#pragma omp parallel for num_thread(4)< ---

正确的子句为 num_threads(4),而不是 num_thread(4)。不正确的openmp编译指示被忽略,所以你最终得到一个顺序程序。 :)



我很惊讶你没有得到编译器警告,因为我这样做了。


I just tried OpenMP with a simple c program

test() {
   for(int i=0;i<100000000;i++);
}
main() {
    printf("Num of CPU: %d\n", omp_get_num_procs());
    #pragma omp parallel for num_threads(4)
    for(int i=0;i<100;i++) test();
}

Compiled with g++ -fopenmp. It can correctly print out that I have 4 CPUs, but all test functions are running at thread 0. I tried to modify the OMP_NUM_THREADS. But it has no effect also. I had everything the same as the online examples but why wouldn't I get it to work?

Thanks so much for your comments!

解决方案

Your problem is here:

#pragma omp parallel for num_thread(4) <---

The correct clause is num_threads(4), not num_thread(4). Incorrect openmp pragmas are ignored and so you ended up with a sequential program. :)

I'm surprised you didn't get a compiler warning, because I did.

这篇关于为什么OpenMP程序只在一个线程中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆