Why is my OpenMP implementation slower than a single threaded implementation?

一世执手 提交于 2019-12-23 12:25:31

问题


I am learning about OpenMP concurrency, and tried my hand at some existing code I have. In this code, I tried to make all the for loops parallel. However, this seems to make the program MUCH slower, at least 10x slower, or even more than the single threaded version.

Here is the code: http://pastebin.com/zyLzuWU2

I also used pthreads, which turns out to be faster than the single threaded version.

Now the question is, what am I doing wrong in my OpenMP implementation that is causing this slowdown?

Thanks!

edit: the single threaded version is just the one without all the #pragmas


回答1:


One problem I see with your code is that you are using OpenMP across loops that are very small (8 or 64 iterations, for example). This will not be efficient due to overheads. If you want to use OpenMP for the n-queens problem, look at OpenMP 3.0 tasks and thread parallelism for branch-and-bound problems.




回答2:


I think your code is much too complex to be reviewed here. One error that I saw immediately is that it is not even correct. At places where you are using an omp parallel for to do sums you must use reduction(+: yourcountervariable) to have the results of the different threads correctly assembled together. Otherwise one thread may overwrite the result of the others.




回答3:


At least two reasons:

  1. You're only doing 8 iterations of a very simple loop. Your runtime will be completely dominated by the overhead involved in setting up all the threads.

  2. In some places, the critical section will cause contention; all the threads will be trying to access the critical section continuously, and block each other.



来源:https://stackoverflow.com/questions/5042351/why-is-my-openmp-implementation-slower-than-a-single-threaded-implementation

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