openmp代码未并行运行 [英] openmp code not running in parallel

查看:600
本文介绍了openmp代码未并行运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

omp_set_num_threads( 8 );
#pragma omp parallel for
for( int tx = 0; tx < numThread; tx++ )
{
    cout<<"\nThread :"<<omp_get_num_threads()<<"\n";

}

我的理解是上面的代码应该打印8.但是我得到的输出是

My understanding is that the above code is supposed to print 8. But the output I am getting is

Thread :1

Thread :1

Thread :1

Thread :1

Thread :1

Thread :1

Thread :1

Thread :1

请让我知道这里出了什么问题.我是openmp的初学者,所以我很确定自己一定犯了一些愚蠢的错误.

Please let me know what is going wrong here. I am beginner to openmp and so I am quite sure i must have made some stupid mistake.

预先感谢

推荐答案

我不确定这里发生了什么.

I'm not sure what's happening here.

可能是使用存根OpenMP库进行编译的情况,该存根提供所有OpenMP库API,但仅充当顺序模式(请参见

It can be the case that you compiled with a stub OpenMP library, which provides all the OpenMP library API, but only acting as if sequential mode (see this link for example for the corresponding Intel compiler switch).

另一种可能性是在您的环境中将OMP_THREAD_LIMIT环境变量设置为1.例如,请参见以下代码:

Another possibility is that the OMP_THREAD_LIMIT environment variable is set to 1 in your environment. See for example this code:

#include <iostream>
#include <omp.h>

int main() {
    omp_set_num_threads(8);
    #pragma omp parallel
    #pragma omp single
    std::cout << "Number of threads in the current parallel region is " << omp_get_num_threads() << std::endl;

    return 0;
}

在使用OpenMP支持进行编译并运行时,它会给我:

When compiled with OpenMP support and run, it gives me:

$ g++ -fopenmp nbthreads.cc -o nbthreads
$ ./nbthreads 
Number of threads in the current parallel region is 8
$ OMP_THREAD_LIMIT=1 ./nbthreads 
Number of threads in the current parallel region is 1

除了这两种可能性之外,我不知道.

Aside from these two possibilities, I've no idea.

感谢 Z玻色子 评论,我很确定我已经掌握了这个谜的关键.

thanks to Z boson's comment, I'm pretty sure I've got the key of the mystery.

使用与上文相同的代码,这就是我得到的:

With the very same code as the hereabove one, here is what I've got:

$ g++ -o nbthreads nbthreads.cc -lgomp
$ ./nbthreads 
Number of threads in the current parallel region is 1

因此,在编译/链接代码时,您只是错误地使用了-lgomp而不是-fopenmp.即使您明确要求更多线程,这也将为您提供相当于只有1个线程的OpenMP代码.

So you simply mistakenly used -lgomp instead of -fopenmp while compiling / linking your code. This gives you an OpenMP code equivalent to having only 1 thread, even if you explicitly ask for more.

这篇关于openmp代码未并行运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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