并行排序算法.doc_第1页
并行排序算法.doc_第2页
并行排序算法.doc_第3页
并行排序算法.doc_第4页
并行排序算法.doc_第5页
免费预览已结束,剩余19页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

并行排序算法先简单说一下给的A,B,C 三种算法(见上面引用的那篇博客),A算法将耗时的平方和开平方计算放到比较函数中,导致Array.Sort 时,每次亮亮比较都要执行平方和开平方计算,其平均算法复杂度为 O(nlog2n) 。 而B 将平方和开平方计算提取出来,算法复杂度降低到 O(n) ,这也就是为什么B比A效率要高很多的缘故。C 和 B 相比,将平方函数替换成了 x*x ,由于少了远程函数调用和Pow函数本身的开销,效率有提高了不少。我在C的基础上编写了D算法,D算法采用并行计算技术,在我的双核笔记本电脑上数据量比较大的情况下,其排序效率较C要提高30%左右。下面重点介绍这个并行排序算法。算法思路其实很简单,就是将要排序的数组按照处理器数量等分成若干段,然后用和处理器数量等同的线程并行对各个小段进行排序,排序结束和,再在单一线程中对这若干个已经排序的小段进行归并排序,最后输出完整的排序结果。考试大考虑到和.Net 2.0 兼容,没有用微软提供的并行库,而是用多线程来实现。下面是测试结果:n A B C D32768 0.7345 0.04122 0.0216 0.025465535 1.5464 0.08863 0.05139 0.05149131072 3.2706 0.1858 0.118 0.108262144 6.8423 0.4056 0.29586 0.21849524288 15.0342 0.9689 0.7318 0.49061048576 31.6312 1.9978 1.4646 1.0742097152 66.9134 4.1763 3.0828 2.3095从测试结果上看,当要排序的数组长度较短时,并行排序的效率甚至还没有不进行并行排序高,这主要是多线程的开销造成的。当数组长度增大到25万以上时,并行排序的优势开始体现出来,随着数组长度的增长,排序时间最后基本稳定在但线程排序时间的 74% 左右,其中并行排序的消耗大概在50%左右,归并排序的消耗在 14%左右。由此也可以推断,如果在4CPU的机器上,其排序时间最多可以减少到单线程的 14 + 25 = 39%。8 CPU 为 14 + 12.5 = 26.5%。目前这个算法在归并算法上可能还有提高的余地,如果哪位高手能够进一步提高这个算法,不妨贴出来一起交流交流。下面分别给出并行排序和归并排序的代码:并行排序类 ParallelSortParalletsort 类是一个通用的泛型,调用起来非常简单,下面给一个简单的int型数组的排序示例:class IntComparer : IComparer IComparer Members #region IComparer Memberspublic int Compare( int x, int y)return x.CompareTo(y);#endregionpublic void SortInt( int array)Sort.ParallelSort parallelSort = new Sort.ParallelSort ();parallelSort.Sort(array, new IntComparer();只要实现一个T类型两两比较的接口,然后调用ParallelSort 的 Sort 方法就可以了,是不是很简单?下面是 ParallelSort类的代码using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace Sort/*/ / ParallelSort/public class ParallelSort enum StatusIdle = 0 ,Running = 1 ,Finish = 2 ,class ParallelEntitypublic Status Status;public T Array;public IComparer Comparer;public ParallelEntity(Status status, T array, IComparer comparer)Status = status;Array = array;Comparer = comparer;private void ThreadProc(Object stateInfo)ParallelEntity pe = stateInfo as ParallelEntity;lock (pe)pe.Status = ParallelSort .Status.Running;Array.Sort(pe.Array, pe.Comparer);pe.Status = ParallelSort .Status.Finish;public void Sort(T array, IComparer comparer)/ Calculate process countint processorCount = Environment.ProcessorCount;/ If array.Length too short, do not use Parallel sortif (processorCount = 1 | array.Length processorCount)Array.Sort(array, comparer);return ;/ Split arrayParallelEntity partArray = new ParallelEntityprocessorCount;int remain = array.Length;int partLen = array.Length / processorCount;/ Copy data to splited arrayfor ( int i = 0 ; i processorCount; i + )if (i = processorCount - 1 )partArrayi = new ParallelEntity(Status.Idle, new Tremain, comparer);elsepartArrayi = new ParallelEntity(Status.Idle, new TpartLen, comparer);remain -= partLen;Array.Copy(array, i * partLen, partArrayi.Array, 0 , partArrayi.Array.Length);/ Parallel sortfor ( int i = 0 ; i processorCount - 1 ; i + )ThreadPool.QueueUserWorkItem( new WaitCallback(ThreadProc), partArrayi);ThreadProc(partArrayprocessorCount - 1 );/ Wait all threads finishfor ( int i = 0 ; i processorCount; i + )while ( true )lock (partArrayi)if (partArrayi.Status = ParallelSort .Status.Finish)break ;Thread.Sleep( 0 );/ Merge sortMergeSort mergeSort = new MergeSort ();List source = new List (processorCount);foreach (ParallelEntity pe in partArray)source.Add(pe.Array);mergeSort.Sort(array, source, comparer);多路归并排序类 MergeSortusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Sort/*/ / MergeSort/public class MergeSort public void Sort(T destArray, List source, IComparer comparer)/ Merge Sortint mergePoint = new int source.Count;for ( int i = 0 ; i source.Count; i + )mergePointi = 0 ;int index = 0 ;while (index destArray.Length)int min = - 1 ;for ( int i = 0 ; i = sourcei.Length)continue ;if (min 0 )min = i;elseif (comparer.Compare(sourceimergePointi, sourceminmergePointmin) 0 )min = i;if (min 0 )continue ;destArrayindex + = sourceminmergePointmin;mergePointmin + ;主函数及测试代码 在蛙蛙池塘 代码基础上修改using System;using System.Collections.Generic;using System.Diagnostics;namespace Vector4Testpublic class Vectorpublic double W;public double X;public double Y;public double Z;public double T;internal class VectorComparer : IComparer public int Compare(Vector c1, Vector c2)if (c1 = null | c2 = null )throw new ArgumentNullException( Both objects must not be null );double x = Math.Sqrt(Math.Pow(c1.X, 2 )+ Math.Pow(c1.Y, 2 )+ Math.Pow(c1.Z, 2 )+ Math.Pow(c1.W, 2 );double y = Math.Sqrt(Math.Pow(c2.X, 2 )+ Math.Pow(c2.Y, 2 )+ Math.Pow(c2.Z, 2 )+ Math.Pow(c2.W, 2 );if (x y)return 1 ;else if (x y)return - 1 ;elsereturn 0 ;internal class VectorComparer2 : IComparer public int Compare(Vector c1, Vector c2)if (c1 = null | c2 = null )throw new ArgumentNullException( Both objects must not be null );if (c1.T c2.T)return 1 ;else if (c1.T c2.T)return - 1 ;elsereturn 0 ;internal class Programprivate static void Print(Vector vectors)/ foreach (Vector v in vectors)/ / Console.WriteLine(v.T);/ private static void Main( string args)Vector vectors = GetVectors();Console.WriteLine( string .Format( n = 0 , vectors.Length);Stopwatch watch1 = new Stopwatch();watch1.Start();A(vectors);watch1.Stop();Console.WriteLine( A sort time: + watch1.Elapsed);Print(vectors);vectors = GetVectors();watch1.Reset();watch1.Start();B(vectors);watch1.Stop();Console.WriteLine( B sort time: + watch1.Elapsed);Print(vectors);vectors = GetVectors();watch1.Reset();watch1.Start();C(vectors);watch1.Stop();Console.WriteLine( C sort time: + watch1.Elapsed);Print(vectors);vectors = GetVectors();watch1.Reset();watch1.Start();D(vectors);watch1.Stop();Console.WriteLine( D sort time: + watch1.Elapsed);Print(vectors);Console.ReadKey();private static Vector GetVectors()int n = 1 21 ;Vector vectors = new Vectorn;Random random = new Random();for ( int i = 0 ; i n; i + )vectorsi = new Vector();vectorsi.X = random.NextDouble();vectorsi.Y = random.NextDouble();vectorsi.Z = random.NextDouble();vectorsi.W = random.NextDouble();return vectors;private static void A(Vector vectors)Array.Sort(vectors, new VectorComparer();private static void B(Vector vectors)int n = vectors.Length;for ( int i =

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论