Can't get any speedup from parallelizing Quicksort using Pthreads

Posted by Murat Ayfer on Stack Overflow See other posts from Stack Overflow or by Murat Ayfer
Published on 2010-06-07T13:44:15Z Indexed on 2010/06/07 14:02 UTC
Read the original article Hit count: 520

Filed under:
|
|

I'm using Pthreads to create a new tread for each partition after the list is split into the right and left halves (less than and greater than the pivot). I do this recursively until I reach the maximum number of allowed threads.

When I use printfs to follow what goes on in the program, I clearly see that each thread is doing its delegated work in parallel. However using a single process is always the fastest. As soon as I try to use more threads, the time it takes to finish almost doubles, and keeps increasing with number of threads.

I am allowed to use up to 16 processors on the server I am running it on.

The algorithm goes like this: Split array into right and left by comparing the elements to the pivot. Start a new thread for the right and left, and wait until the threads join back. If there are more available threads, they can create more recursively. Each thread waits for its children to join.

Everything makes sense to me, and sorting works perfectly well, but more threads makes it slow down immensely.

I tried setting a minimum number of elements per partition for a thread to be started (e.g. 50000).

I tried an approach where when a thread is done, it allows another thread to be started, which leads to hundreds of threads starting and finishing throughout. I think the overhead was way too much. So I got rid of that, and if a thread was done executing, no new thread was created. I got a little more speedup but still a lot slower than a single process.

The code I used is below.

http://pastebin.com/UaGsjcq2

Does anybody have any clue as to what I could be doing wrong?

© Stack Overflow or respective owner

Related posts about c

    Related posts about pthreads