openmp code not running in parallel

我的未来我决定 提交于 2019-12-23 15:53:24

问题


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

}

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

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.

Thanks in advance


回答1:


I'm not sure what's happening here.

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).

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;
}

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.


Edit: 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

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.



来源:https://stackoverflow.com/questions/33160934/openmp-code-not-running-in-parallel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!