为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > 七种方法求水仙花数[技巧]

七种方法求水仙花数[技巧]

2017-10-21 23页 doc 194KB 90阅读

用户头像

is_721103

暂无简介

举报
七种方法求水仙花数[技巧]七种方法求水仙花数[技巧] 班级 学号 姓名 目 录 1.水仙花数的并行算法设计与实现?????????7 1.1 .1功能描述 1.1.2 解决方案 1.2算法设计????????????????? ???7 1.2.1 串行算法设计 1.2.2并行算法设计 1.3 基于OpenMP的并行算法实现???????????8 1.3.1 代码及注释(变量名 名字首字母 开头) 1.3.2 执行结果截图(体现串行时间、并行时间和加速比) 1.3.3 遇到的问题及解决方案 1.4 基于MPI的并行算法实现???...
七种方法求水仙花数[技巧]
七种求水仙花数[技巧] 班级 学号 姓名 目 录 1.水仙花数的并行算法设计与实现?????????7 1.1 .1功能描述 1.1.2 解决 1.2算法设计????????????????? ???7 1.2.1 串行算法设计 1.2.2并行算法设计 1.3 基于OpenMP的并行算法实现???????????8 1.3.1 代码及注释(变量名 名字首字母 开头) 1.3.2 执行结果截图(体现串行时间、并行时间和加速比) 1.3.3 遇到的问题及解决方案 1.4 基于MPI的并行算法实现?????????????11 1.4.1 代码及注释(变量名 名字首字母 开头) 1.4.2 执行结果截图(体现串行时间、并行时间和加速比) 1.4.3 遇到的问题及解决方案 1.5 基于Java(Runnable)的并行算法实现???????13 1.5.1 代码及注释(变量名 名字首字母 开头) 1.5.2 执行结果截图(体现串行时间、并行时间和加速比) 1.5.3 遇到的问题及解决方案 1.6 基于Windows(.NET)的并行算法实现???????20 1.6.1 代码及注释(变量名 名字首字母 开头) 1.6.2 执行结果截图(体现串行时间、并行时间和加速比) 1.6.3 遇到的问题及解决方案 2. 理论基础?????????????????????22 2.1 并行计算机体系结构、并行计算模型、并行算法的概念 2.2并行计算机体系结构、并行计算模型、并行算法的关系 2.3实例说明并行计算机体系结构、并行计算模型、并行算法的关系 评价 实践效果(正确度/加速比) 理论基础 难度 工作量 独立性 认证结果 1.水仙花数的并行算法设计与实现 1.1 .1功能描述 水仙花数又称阿姆斯特朗数。是指一种三位数,其各个数之立方和等于该数本身。水仙花数只是自幂数的一种,严格来说三位数的3次幂数才成为水仙花数。 1.1.2 解决方案 并行思想: 并行计算的原理就是把一个任务或者作业分配到多个处理器上并发执行。这样一来可以大大提高计算的效率。在本次课题中,要实现水仙花数的并行计算输出,就是把制定范围内的数用多个处理器进行计算,从而得到水仙花数的并行输出。再和串行执行方法进行比较,观察其优越性。 核心算法: int hundreds = n / 100; int tens = n % 100 / 10; int ones = n % 10; Return cube(hundreds) + cube(tens) + cube(ones) == n; 1.2算法设计 1.2.1 串行算法设计 for(xlh=100;xlh<1000;xlh++) { j=xlh/100; z=(xlh-100*j)/10; t=xlh%10; if(j*j*j+z*z*z+t*t*t==xlh) printf("%d\n",xlh); } 1.2.2 并行算法设计 for (int xlh=100+ id; xlh< 1000; xlh+=2) //1000以内的水仙花数 { int j=xlh/100; //判断百位 int z=xlh%100/10;//判断十位 int t=xlh%10;//个位 if(j*j*j+z*z*z+t*t*t ==xlh)//确定是否是水仙花数 printf("%d\n",xlh);//输出水仙花数 } 1.3 基于OpenMP的并行算法实现 1.3.1 代码及注释(变量名 名字首字母 开头) #include "stdafx.h" #include "Windows.h" #include "math.h" #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { printf("求水仙花数\n"); cout<<"并行结果:"< #include int main(int argc,char * argv[]) { int id,numpro; double startime01,endtime01,startime02,endtime02; MPI_Init (&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&id); MPI_Comm_size(MPI_COMM_WORLD,&numpro); startime01 =MPI_Wtime(); printf("水仙花数:\n"); if(id==0)//并行 for (int xlh=100+ id; xlh< 1000; xlh+=2) //1000以内的水仙花数 { int j=xlh/100; //判断百位 int z=xlh%100/10;//判断十位 int t=xlh%10;//个位 if(j*j*j+z*z*z+t*t*t ==xlh)//确定是否是水仙花数 printf("%d\n",xlh);//输出水仙花数 } if(id==0) { endtime01=MPI_Wtime(); printf("并行时间:%f\n",endtime01-startime01);//输出并行时间 } if(id==0)//串行 { startime02=MPI_Wtime(); printf("水仙花数:\n"); for (int xlh =100; xlh < 1000; xlh+=1) { int j=xlh/100; int z=xlh%100/10; int t=xlh%10; if(j*j*j+z*z*z+t*t*t ==xlh) printf("%d\n",xlh); } endtime02=MPI_Wtime(); printf("串行时间:%f\n",endtime02-startime02); } MPI_Finalize(); system("pause"); return 0; } 1.4.2 执行结果截图(体现串行时间、并行时间和加速比) 1.4.3 遇到的问题及解决方案 后果 分析 环境配置错误,在配置MPI的环境时花了很多时间,在自己电脑上运行一直出错,后来在机房 运行成功的。 1.5 基于Java(Runnable)的并行算法实现 1.5.1 代码及注释(变量名 名字首字母 开头) import java.lang.Thread; public class JavaThread extends Thread { private int start; private int end; private int sum=0; public JavaThread(int start,int end) { super(); this.start=start; this.end=end; } public void run()//并行函数 { int a,b,c; for(int xlh=start;xlh<=end;xlh+=2) { a=xlh/100; b=(xlh-a*100)/10; c=xlh%10; try { sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } if(a*a*a+b*b*b+c*c*c==xlh&&xlh>100&&xlh<1000) { sum++; System.out.println(xlh); } } } public int Flower() throws InterruptedException//串行函数 { int a,b,c; sum=0; for(int xlh=start;xlh<=end;xlh++)//求水仙花数 { a=xlh/100; b=(xlh-a*100)/10; c=xlh%10; sleep(1); if(a*a*a+b*b*b+c*c*c==xlh&&xlh>100&&xlh<1000) { sum++; System.out.println(xlh); } } return sum; } public int getSum() { return sum; } public static void main(String []args)throwsInterruptedException { JavaThread thread1=new JavaThread(1,1000);//确定开始以及结束值 JavaThread thread2=new JavaThread(2,1000); System.out.println("并行结果:"); long startTime=System.currentTimeMillis();//并行开始 thread1.start(); thread2.start(); thread1.join(); thread2.join(); long endTime=System.currentTimeMillis(); System.out.println("并行时间:"+(endTime-startTime)); double t1=endTime-startTime; startTime=System.currentTimeMillis();//并行结束 System.out.println("串行结果:"); JavaThread thread=new JavaThread(1,1000); thread.Flower(); endTime=System.currentTimeMillis(); System.out.println("串行时间:"+(endTime-startTime)); double t2=endTime-startTime; System.out.println("加速比"+t2/t1); } } 1.5.2 执行结果截图(体现串行时间、并行时间和加速比) 1.5.3 遇到的问题及解决方案 错误代码及后果 import java.lang.Thread; class JavaThread extends Thread { private int start; private int end; private int sum=0; public JavaThread(int start,int end) { super(); this.start=start; this.end=end; } public void run()//并行函数 { int a,b,c; for(int xlh=start;xlh<=end;xlh+=2) { a=xlh/100; b=(xlh-a*100)/10; c=xlh%10; try { sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } 后果: 输出结果错误( 分析:应声明明JavaThread类继承 Thread类为公共类( 正确代码 import java.lang.Thread; public class JavaThread extends Thread { private int start; private int end; private int sum=0; public JavaThread(int start,int end) { super(); this.start=start; this.end=end; } public void run()//并行函数 { int a,b,c; for(int xlh=start;xlh<=end;xlh+=2) { a=xlh/100; b=(xlh-a*100)/10; c=xlh%10; try { sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } 分析 对,,编程掌握不熟悉。 1.6 基于Windows(.NET)的并行算法实现 1.6.1 代码及注释(变量名 名字首字母 开头) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Diagnostics; Threadsclass { static void Main() { Stopwatch t1 = new Stopwatch(); Work work1 = new Work(0); ThreadStart thread1 = new ThreadStart(work1.Work1);//调用work函数 Thread newthread1 = new Thread(thread1); Work work2 = new Work(1); ThreadStart thread2 = new ThreadStart(work2.Work1); Thread newthread2 = new Thread(thread2); Console.Write("并行结果:\n"); t1.Start();//并行开始 newthread1.Start(); newthread2.Start(); newthread1.XLHoin(); newthread2.XLHoin(); t1.Stop();//并行结束 TimeSpan timeSpan = t1.Elapsed; double pt = timeSpan.TotalMilliseconds; Console.Write("并行时间:{0}\n", pt); Console.Write("串行结果:\n"); t1.Start();//串行开始 new Work(0).func(); t1.Stop();//串行结束 TimeSpan timeSpan2 = t1.Elapsed; double st = timeSpan2.TotalMilliseconds; Console.WriteLine("串行时间:{0}", st); Console.WriteLine("加速比:{0}", st / pt); Console.Read(); } } class Work { private int start; public Work(int start) { this.start = start; } public void Work1()//并行函数求水仙花数 { int m; for (int i = 1; i < 10; i++) for (int xlh = 0; xlh < 10; xlh++) for (int k = start; k < 10; k += 2) { if (i * i * i + xlh * xlh * xlh + k * k * k == 100 * i + 10 * xlh + k) { m = i * i * i + xlh * xlh * xlh + k * k * k; Console.WriteLine(m); } } } public void func()//串行函数 { int m; for (int i = 1; i < 10; i++) for (int xlh = 0; xlh < 10; xlh++) for (int k = start; k < 10; k++) { if (i * i * i + xlh * xlh * xlh + k * k * k == 100 * i + 10 * xlh + k) { m = i * i * i + xlh * xlh * xlh + k * k * k; Console.WriteLine(m); } } } } 1.6.2 执行结果截图(体现串行时间、并行时间和加速比) 1.6.3 遇到的问题及解决方案 遇到的问题:期间一直伴随着很多的拼写错误,还有很多其他错误想不起来了。马虎,编程不熟练。导致编译过程很多错误。一直在改正改正。 解决方案:细心、耐心,恒心。 2. 理论基础 2.1 并行计算机体系结构、并行计算模型、并行算法的概念 答:(1)并行计算体系结构:并行计算科学中主要研究的是空间上的并行问题。从程序和算法设计人员的角度来看,并行计算又可分为数据并行和任务并行。一般来说,因为数据并行主要是将一个大任务化解成相同的各个子任务,比任务并行要容易处理。 空间上的并行导致了两类并行机的产生,按照Flynn的说法分为:单指令流多数据流(SIMD) 和多指令流多数据流(MIMD)。我们常用的串行机也叫做单指令流单数据流(SISD)。MIMD类的机器又可分为以下常见的五类:并行向量处理机(PVP)、对称多处理机(SMP)、大规模并行处理机(MPP)、工作站机群(COW)、分布式共享存储处理机(DSM)。 (2)并行计算模型:(1)访存模型:并行计算机有以下五种访存模型:均匀访存模型(UMA);非均匀访存模型(NUMA);全高速缓存访存模型(COMA);一致性高速缓存非均匀存储访问模型(CC-NUMA);非远程存储访问模型(NORMA)。 2)计算模型:不像串行计算机那样,全世界基本上都在使用冯?诺伊( 曼的计算模型;并行计算机没有一个统一的计算模型。不过,人们已经提出了几种有价值的参考模型:PRAM模型,BSP模型,LogP模型,C^3模型等。 (3)并行算法:并行算法是并行计算中非常重要的问题。并法研究应该确立一个“理论,设计,实现,应用”的系统方法,形成一个完善的 “架构—算法—编程” 方法论,这样才能保证并行算法不断发展并变得更加实用。 2.2并行计算机体系结构、并行计算模型、并行算法的关系 2.3实例说明并行计算机体系结构、并行计算模型、并行算法的关系
/
本文档为【七种方法求水仙花数[技巧]】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索