ImageVerifierCode 换一换
格式:DOC , 页数:24 ,大小:34KB ,
资源ID:2104668      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-2104668.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(并行排序算法.doc)为本站会员(wj)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

并行排序算法.doc

1、并行排序算法先简单说一下给的A,B,C 三种算法(见上面引用的那篇博客),A算法将耗时的平方和开平方计算放到比较函数中,导致Array.Sort 时,每次亮亮比较都要执行平方和开平方计算,其平均算法复杂度为 O(nlog2n) 。 而B 将平方和开平方计算提取出来,算法复杂度降低到 O(n) ,这也就是为什么B比A效率要高很多的缘故。C 和 B 相比,将平方函数替换成了 x*x ,由于少了远程函数调用和Pow函数本身的开销,效率有提高了不少。我在C的基础上编写了D算法,D算法采用并行计算技术,在我的双核笔记本电脑上数据量比较大的情况下,其排序效率较C要提高30%左右。下面重点介绍这个并行排序算

2、法。算法思路其实很简单,就是将要排序的数组按照处理器数量等分成若干段,然后用和处理器数量等同的线程并行对各个小段进行排序,排序结束和,再在单一线程中对这若干个已经排序的小段进行归并排序,最后输出完整的排序结果。考试大考虑到和.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.2184

3、9524288 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的机器上,其排序时间最多可以减少到单线程的 1

4、4 + 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.Compa

5、reTo(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 Syste

6、m.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;Ar

7、ray = 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)/ Calculat

8、e 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.Len

9、gth;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 -= part

10、Len;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

11、 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(arr

12、ay, 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 =

13、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 + = sourceminmer

14、gePointmin;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 Compar

15、e(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

16、.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

17、 ;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()

18、;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(

19、);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

20、;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

21、.Sort(vectors, new VectorComparer();private static void B(Vector vectors)int n = vectors.Length;for ( int i = 0 ; i n; i + )Vector c1 = vectorsi;c1.T = Math.Sqrt(Math.Pow(c1.X, 2 )+ Math.Pow(c1.Y, 2 )+ Math.Pow(c1.Z, 2 )+ Math.Pow(c1.W, 2 );Array.Sort(vectors, new VectorComparer2();private static vo

22、id C(Vector vectors)int n = vectors.Length;for ( int i = 0 ; i n; i + )Vector c1 = vectorsi;c1.T = Math.Sqrt(c1.X * c1.X+ c1.Y * c1.Y+ c1.Z * c1.Z+ c1.W * c1.W);Array.Sort(vectors, new VectorComparer2();private static void D(Vector vectors)int n = vectors.Length;for ( int i = 0 ; i n; i + )Vector c1 = vectorsi;c1.T = Math.Sqrt(c1.X * c1.X+ c1.Y * c1.Y+ c1.Z * c1.Z+ c1.W * c1.W);Sort.ParallelSort parallelSort = new Sort.ParallelSort ();parallelSort.Sort(vectors, new VectorComparer2();

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2