Parallel Sort Algorithm

前端 未结 5 755
星月不相逢
星月不相逢 2020-11-28 05:00

I\'m looking for a simple implementation of a parallelized (multi-threaded) sort algorithm in C# that can operate on List or Arrays, and possibly using

5条回答
  •  情深已故
    2020-11-28 05:31

    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:

    1. 2,000,000 random Model objects
    2. Sorting objects by a comparison delegate that compares two DateTime properties.
    3. Mono JIT compiler version 2.4.2.3
    4. 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.

提交回复
热议问题