Wednesday, September 29, 2010

Threaded Merge Sort

What?

In my previous post about Merge Sort, the algorithm for merge sort was discussed. Although the pictures shows something which appears to be apparently parallel, but actually they are not. In fact, as the procedure introduced there was recursive, it works in a post-order fashion, meaning, first it solves the left half, then the right half, and then it works with the current range. The following picture shows the actual work-flow of the standard recursive merge sort algorithm:

The red lines and numbers shows the actual call sequence of the algorithm merge sort, for reference, the basic algorithm, is shown here again:

So, from the above example, we can see that no two calls are concurrent, i.e. parallel. Each call is one of the three calls made in MERGE-SORT(A, p, r) procedure stated above, also shown below (according to the picture, p=0, r=6):

Also note that, there are no overlapping calls at a same level of the tree, which is going to be a key factor.

How threading helps:

After a few observations, we can easily find out some interesting properties about merge sort algorithm. If we closely look at the merge sort tree structure, it becomes very clear that, int the recursive call tree, parent nodes depend on children, but siblings are independent. This property helps us to deduce a threaded version of merge sort algorithm where, we can really solve the left and right sub-portion of a range in a parallel fashion by making the calls threaded.

As the algorithm here is recursive, it might look confusing at the first glance, but it is not. Because, nodes share data only with their ancestors and in a call, we can wait until the threads finish their works. So, no concurrency problem appears here. Also, we don't need to worry about mutual exclusion of the shared data, because, there is no such situation in this algorithm, already stated above.

According to the above algorithm, the flow of program changes which is shown in the picture below:

Now because of being able to run multiple THREADED-MERGE-SORT() at a time, and the job scheduling features of modern computers, the effective number of iteration is dramatically reduced, resulting a much much elegant solution for the heavy duty areas like manipulating large data on drives.

A simple implementation:

Here a simple implementation is presented using pthread library in C++:

This may look a bit messy, but, actually, you may find a little difference with the original one, we just created threads instead of plain recursive calls, that's it.To learn more about pthread, check these out:

8 comments:

Hey pal! im implementing one as well onlywith arbitrary data as input. I was wondering when looking at your code if you really need to get the 'pthread_attr_t attr;' and then pass it as a param.. can u just pass NULL?

Most of the images are copied from internet then I edited them with ms paint, for example, images here are basically from wikipedia, then I edited them in paint to show the execution paths (red lines).

You can keep 1 process for controlling, partition the array in five groups and apply normal merge sort in each for corresponding segment. Then in the control process, finally combine 5 segments. There can be many other ways.