Why OpenMP program runs only in one thread

≯℡__Kan透↙ 提交于 2019-12-10 16:44:58

问题


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?


回答1:


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.




回答2:


I had this problem in visual studio and finally I understood that I had forgotten to enable Open MP support in visual studio. It didn't give me any error but executed the program just for one thread




回答3:


use the function omp_set_num_threads(4) before calling the omp parallel section.

also, how do you determine the number of threads ?? embed your printfs in a critical section just to make sure everything is getting printed.




回答4:


I encountered the very same situation on my ubuntu desktop when I extends numpy module with C code. openmp only ran with one thread. I happened to remove libopenblas-base and install libatlas-base-dev.(to deal with numpy installation problem) Then multi-threading openmp came back:)

I have tested it on a ubuntu server with 64 cores and it works just as my desktop! I think this is because libopenblas conflicts with libraries like atlas.




回答5:


first choose project _> properties -> c/c++ -> language -> open mp support -> choose yes and then you will find above conformance mode (make it no )



来源:https://stackoverflow.com/questions/10203902/why-openmp-program-runs-only-in-one-thread

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