Update I now achieve better than 1.7x speedup on a dual core machine.
I thought I would try writing a parallel sorter that worked in .NET 2.0 (I think, check me on this) and that doesn't use anything other than the ThreadPool
.
Here are the results of sorting a 2,000,000 element array:
Time Parallel Time Sequential
------------- ---------------
2854 ms 5052 ms
2846 ms 4947 ms
2794 ms 4940 ms
... ...
2815 ms 4894 ms
2981 ms 4991 ms
2832 ms 5053 ms
Avg: 2818 ms Avg: 4969 ms
Std: 66 ms Std: 65 ms
Spd: 1.76x
I got a 1.76x speedup - pretty close to the optimal 2x I was hoping for - in this environment:
- 2,000,000 random
Model
objects
- Sorting objects by a comparison delegate that compares two
DateTime
properties.
- Mono JIT compiler version 2.4.2.3
- Max OS X 10.5.8 on 2.4 GHz Intel Core 2 Duo
This time I used Ben Watson's QuickSort in C#. I changed his QuickSort
inner loop from:
QuickSortSequential:
QuickSortSequential (beg, l - 1);
QuickSortSequential (l + 1, end);
to:
QuickSortParallel:
ManualResetEvent fin2 = new ManualResetEvent (false);
ThreadPool.QueueUserWorkItem (delegate {
QuickSortParallel (l + 1, end);
fin2.Set ();
});
QuickSortParallel (beg, l - 1);
fin2.WaitOne (1000000);
fin2.Close ();
(Actually, in the code I do a little load balancing that does seem to help.)
I've found that running this parallel version only pays off when there are more than 25,000 items in an array (though, a minimum of 50,000 seems to let my processor breath more).
I've made as many improvements as I can think of on my little dual core machine. I would love to try some ideas on 8-way monster. Also, this work was done on a little 13" MacBook running Mono. I'm curious how others fare on a normal .NET 2.0 install.
The source code in all its ugly glory is availble here: http://www.praeclarum.org/so/psort.cs.txt. I can clean it up if there's any interest.