高性能计算报告_第1页
高性能计算报告_第2页
高性能计算报告_第3页
高性能计算报告_第4页
高性能计算报告_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

裔雌饿奸弄就险想告同学胜名:--名号;————班号;116122指导老师:郭明强中电池质右喉(款法)信息工程学就第一题.编写console程序.由下列图看出,电脑是双核CPU.多线程程序,采用windowsAPI函数创立线程代码#includez/stdafx.h〃#include<iostream>#includez/windows.h〃usingnamespacestd;DWORDWINAPIfirst(PVOIDpParam)(for(inti=0;i<10;i++)(printf(〃l\n〃);)return0;)DWORDWINAPIsecond(PVOIDpParam)(for(inti=0;i<10;i++)(printf(〃2\n〃);return0;)intmain(intargc,char*argv[])HANDLEhHandle_Calc[2];hHandle_Calc[O]=CreateThread(NULL,0,first,NULL,0,NULL);hHandle_Calc[l]=CreateThread(NULL,0,second,NULL,0,NULL);WaitForMultipleObjects(2,hHandleCalc,true,INFINITE);)其次题多线程实现计算e和兀的乘积代码#include,zstdafx.h〃#includez,windows.h〃Jtdefinenumsteps2000000#includc<iostream>usingnamespacestd;〃计算eDWORDWINAPIThreadCalc_E(PVOIDpParam)〃计算e子函数{doublefactorial=1;inti=1;doublee=1;for(;i<numsteps;i++)(factorial*=i;e+=1.0/factorial;)*((doub1e*)pParam)二e;printf(,zedoneE=%2.5f\n,z,e);return0;)〃计算PIDWORDWINAPIThreadCalc_PI(PVOIDpParam)〃计算pi子函数{inti=0;doublepi=0;for(;i<numsteps*10;i++)(pi+=1.0/(i*4.0+1.0);pi—1.0/(i*4.0+3.0);}pi=pi*4.0;*((double*)pParam)=pi;printf(z,pidonePI=%2.5f\n〃,pi);return0;intmain(intargc,char*argv口)〃进程的主线程入口点(HANDLEhHandle_Calc[2];doubleresult_e,result_pi;hHandleCalc[0]=CreateThread(NULL,0,ThreadCalc_E,(void*)(&resulte),0,NULL);hHandle_Calc[l]=CreateThread(NULL,0,ThreadCalcPI,(void*)(&result_pi),0,NULL);〃附加线程的创立WaitForMultipleObjects(2,hHandleCalc,true,INFINITE);〃等待子线程计算结束printf(,ze*pi=%2.5f\n,z,result_e*result_pi);〃打印出e*pi结果return0;)第三题.Parallel语句#includez/stdafx.h〃ttinclude^windows.h〃#includez/omp.h〃#include<iostream>usingnamespacestd;voidmain()(Spragmaompparallelnum_threads(8)(printf("Hello,World!,Threadld=%d\nz,,ompget_threadnum());)).For语句#includezzstdafx.h〃#include,zwindows.h〃#include,zomp.h〃#include<iostream>usingnamespacestd;voidmain()(intj=0;Spragmaompparallel(^pragmaompforfor(j=0;j<4;j++){printfj=%d,threadid=%d\n,z,j,ompgetthreadnum());

3.Sections和section语句的用法#include,zstdafx.h〃#inc1udez,windows.h〃#includez/omp.h〃#includc<iostream>usingnamespacestd;voidmain(intargc,char*argv)ttpragmaompsectionprintf(,zsectionttpragmaompsectionprintf(,zsectionttpragmaompsectionprintf(,zsection^pragmaompsectionprintf(,zsectionSpragmaompsectionprintf(/zsectionthreadid=%dthreadid=%dthreadid=%dthreadid=%d\n〃,omp_get_thread_num());\n〃,omp_get_threadnum());\n〃,ompget_thread^num());4.Threadprivate语句的用法#include,zstdafx.h〃#include,zwindows.h〃#includezzomp.h〃#include<iostream>usingnamespacestd;inta,b,i,tid;floatx;^pragmaompthreadprivate(a,x)voidmain(){〃关闭动态线程安排omp_setdynamic(0);printf(zzlstParallelRegion:\n",);^pragmaompparallelprivate(b,tid)(tid=ompgetthreadnum();a=tid;b=tid;x=1.l*tid+1.0;printf("Threading%d:a,b,x=%d%d%f\n,z,tid,a,b,x);}//endofparallelsection\-v*i*X**4***!*•^**4*\w,、.UlJ_fJL,£1个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个\[]j,printf(〃主线程中串行线程\n〃);printfC^ndParallelRegion:\n,z);Spragmaompparallelprivate(tid)(tid=ompgetthreadnum();printf(''Threading%d:a,b,x=%d%d%f\n,z,tid,a,b,x);}//endofparallelsection5.reduction语句的用法#include,zstdafx.h〃#includez/windows.h〃#include/zomp.h〃#include<iostream>usingnamespacestd;#include<omp.h>voidmain()(inti,n,chunk;floata[100],b[100],result;〃变量的初始化n=100;chunk=10;result=0.0;for(i=0;i<n;i++)(a[i]=i*2.0;b[i]=i*3.0;)^pragmaompparallelfordcfault(shared)private(i)schedule(static,chunk)reduction(+:result)for(i=0;i<n;i++)(result=result+(a[i]*b[i]);printf(z,Finalresult=%f\n〃,result);第四题OpenMP实例分析与比拟串行#include,zstdafx.h〃#includc<time.h>#definenumsteps20000000intmain(intargc,char*argv口)(doublestart,stop;doublee,pi,factorial,product;inti;〃启动定时器start=clock();〃首先运用taylor绽开运算eprintf(,zestarted\nz/);e=1;factorial=1;for(i=1;i<num_steps;i++)(factorial*=i;e+=1.0/factorial;)printf(〃edone\n〃);〃然后计算pi运用taylor绽开printf(,zpistarted\nz/);pi=0;for(i=1;i<numsteps*10;i++)(pi+=L0/(i*4.0+1.0);pi-=L0/(i*4.0+3.0);)pi=pi*4.0;printf(,zpidone\n〃);product=e*pi;stop=clock();printf(Z/Reachedresult%fin%.3fsecond\n〃,product,(stop-start)/1000);return0;I并行ttinclude^stdafx.h〃#include<time.h>#definenumsteps20000000intmain(intargc,char*argv[])doublestart,stop;〃任务开头doublee,pi,factorial,product;inti;〃启动定时器start=clock();〃启动两个进程分别计算epiSpragmaompparallelsectionsshared(e,pi)(Spragmaompsection(printfC'estarted\n/z);e=1;factorial=1;for(i=1;i<numsteps;i++)(factorial*=i;e+=1.0/factorial;)printf(z/edone\nz/);)^pragmaompsection(printf(z,pistarted\nz,);pi=0;for(i=1;i<num_steps*10;i++)(pi+=1.0/(i*

温馨提示

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

评论

0/150

提交评论