Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

openmp

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

like image 699
nubela Avatar asked Jan 21 '23 10:01

nubela


2 Answers

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.

like image 132
Jeremiah Willcock Avatar answered Mar 22 '23 23:03

Jeremiah Willcock


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.

like image 35
Jens Gustedt Avatar answered Mar 23 '23 00:03

Jens Gustedt