开发者

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

开发者 https://www.devze.com 2023-02-11 17:00 出处:网络
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 progr

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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消