七种方法求水仙花数

上传人:mg****85 文档编号:33605944 上传时间:2018-02-16 格式:DOC 页数:24 大小:533.50KB
返回 下载 相关 举报
七种方法求水仙花数_第1页
第1页 / 共24页
七种方法求水仙花数_第2页
第2页 / 共24页
七种方法求水仙花数_第3页
第3页 / 共24页
七种方法求水仙花数_第4页
第4页 / 共24页
七种方法求水仙花数_第5页
第5页 / 共24页
点击查看更多>>
资源描述

《七种方法求水仙花数》由会员分享,可在线阅读,更多相关《七种方法求水仙花数(24页珍藏版)》请在金锄头文库上搜索。

1、并行计算与多核多线程技术课程报告班级 学号姓名 _ 水仙花数_ 水仙花数目 录1.水仙花数的并行算法设计与实现71.1 .1 功能描述1.1.2 解决方案1.2 算法设计 71.2.1 串行算法设计1.2.2 并行算法设计1.3 基于 OpenMP 的并行算法实现 81.3.1 代码及注释(变量名 名字首字母 开头)1.3.2 执行结果截图(体现串行时间、并行时间和加速比)1.3.3 遇到的问题及解决方案1.4 基于 MPI 的并行算法实现 111.4.1 代码及注释(变量名 名字首字母 开头)1.4.2 执行结果截图(体现串行时间、并行时间和加速比)1.4.3 遇到的问题及解决方案1.5 基

2、于 Java(Runnable)的并行算法实现131.5.1 代码及注释(变量名 名字首字母 开头)1.5.2 执行结果截图(体现串行时间、并行时间和加速比)1.5.3 遇到的问题及解决方案1.6 基于 Windows(.NET)的并行算法实现201.6.1 代码及注释(变量名 名字首字母 开头)1.6.2 执行结果截图(体现串行时间、并行时间和加速比)_ 水仙花数1.6.3 遇到的问题及解决方案2. 理论基础222.1 并行计算机体系结构、并行计算模型、并行算法的概念2.2 并行计算机体系结构、并行计算模型、并行算法的关系2.3 实例说明并行计算机体系结构、并行计算模型、并行算法的关系_ 水

3、仙花数评价实践效果(正确度/加速比)理论基础难度工作量独立性认证结果_ 水仙花数_ 水仙花数1.水仙花数的并行算法设计与实现1.1 .1 功能描述水仙花数又称阿姆斯特朗数。是指一种三位数,其各个数之立方和等于该数本身。水仙花数只是自幂数的一种,严格来说三位数的 3 次幂数才成为水仙花数。1.1.2 解决方案并行思想:并行计算的原理就是把一个任务或者作业分配到多个处理器上并发执行。这样一来可以大大提高计算的效率。在本次课题中,要实现水仙花数的并行计算输出,就是把制定范围内的数用多个处理器进行计算,从而得到水仙花数的并行输出。再和串行执行方法进行比较,观察其优越性。核心算法:int hundred

4、s = 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#include #includeusing namespace std;int _tmain(int argc, _TCHAR* argv)printf(求水仙花数n);cout#include int main(int argc,char * argv)int id,numpro;double startime0

5、1,endtime01,startime02,endtime02;MPI_Init (MPI_Comm_rank(MPI_COMM_WORLD,MPI_Comm_size(MPI_COMM_WORLD,startime01 =MPI_Wtime();printf(水仙花数:n);_ 水仙花数if(id=0)/并行for (int xlh=100+ id; xlh100&xlh100&xlh1000)sum+;System.out.println(xlh);return sum;public int getSum()_ 水仙花数return sum;public static void main

6、(String args)throwsInterruptedExceptionJavaThread 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.currentTimeM

7、illis();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=endT

8、ime-startTime;System.out.println(加速比+t2/t1);_ 水仙花数1.5.2 执行结果截图(体现串行时间、并行时间和加速比)1.5.3 遇到的问题及解决方案错误代码及后果import java.lang.Thread;class JavaThread extends Threadprivate int start;private int end;private int sum=0;public JavaThread(int start,int end)super();this.start=start;this.end=end;_ 水仙花数public void

9、 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 Threadprivate int start;_ 水仙花数private int

10、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)的

11、并行算法实现1.6.1 代码及注释(变量名 名字首字母 开头)using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Diagnostics;class Threadsstatic void Main()Stopwatch t1 = new Stopwatch();Work work1 = new Work(0);ThreadStart thread1 = new ThreadStart(work1.Work1);/

12、调用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();/并行结束TimeS

13、pan timeSpan = t1.Elapsed;double pt = timeSpan.TotalMilliseconds;Console.Write(并行时间:0n, 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);

14、Console.Read();_ 水仙花数class Workprivate 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 + x

15、lh * 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 遇到的问题及解决方案遇到的问题:期间一直伴随着很多的拼写错误,还有很多其他错误想不起来了。马虎,编程不熟

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号