欢迎来到天天文库
浏览记录
ID:49660248
大小:815.50 KB
页数:40页
时间:2020-03-03
《多核并行编程实验报告.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、.多核并行编程实验报告Word资料.一、实验目的使用三种编程方法实现积分法或蒙特卡洛法计算pi值(选择实现其中一种,建议蒙特卡洛法)·串行方法·Windows环境多线程编程·Windows环境OpenMP库并行编程二、实验原理·积分法计算pi值的基本原理因此可以用上述公式来计算π的值。·蒙特卡罗法计算pi值的基本原理·蒙特卡洛算法可理解为通过大量实验,模拟实际行为,来收集统计数据。本例中,算法随机产生一系列点,模拟这些点落在如下图所示的正方形区域内的情况。其几何解释如下·如图所示,正方形边长为1,左下顶点与原点重合,两边分别与x轴重合。曲线为1/4圆弧,圆心位
2、于原点,与正方形左下定点重合,半径为1。正方形面积S1=1,圆弧内面积S2=p/4。算法模拟大量点随机落在此正方形区域内,落在圆弧内的点的数量(n2)与点的总数(n1)的比例与面积成正比关系。即·Word资料.三、实验环境·系统硬件环境:通过CPU-Z获取本机CPU型号,二级cache容量,处理器内核数和逻辑线程数等信息·操作系统:winxp32位(学校机房电脑)·开发工具:Visualstudio2010四、?????????????Word资料.Word资料.???1.???????#include#include#
3、include#includestaticlongnum_steps=1000000000;//块数可以修改doublestep;usingnamespacestd;intmain(intargc,char*argv[]){inti;doublex,pi,starttime,endtime,sum=0.0;step=1.0/(double)num_steps;time_tt;starttime=clock();srand((unsigned)time(&t));for(i=0;i4、5)*step;sum=sum+4.0/(1.0+x*x);}pi=step*sum;endtime=clock();printf("块数=%d",num_steps);printf("Pi=%f",pi);printf("time=%f",endtime-starttime);return0;}Word资料.块数1000000块数10000000Word资料.块数10000000???100000000Word资料.2.WinAPI???????#include#include#include5、s.h>#include#includeusingnamespacestd;staticlongnum_step;constintThreadnum=10;//修改此处线程数为1,4,8doublestep,pi=0,sum=0.0;CRITICAL_SECTIONg;DWORDWINAPIthreadFunc(LPVOIDpParam)Word资料.{intnum=*((int*)pParam);doublex,sum1=0;step=1.0/(double)num_step;for(inti=num;i6、;i+=Threadnum){x=(i+0.5)*step;sum1+=4.0/(1.0+x*x);}EnterCriticalSection(&g);sum+=sum1;LeaveCriticalSection(&g);return0;}voidmain(){cout<<"输入分块的数目:";cin>>num_step;LARGE_INTEGERtemp;doubledFreq;//系统时钟QueryPerformanceFrequency(&temp);dFreq=(double)temp.QuadPart;//获得计数器的时刻LONGLONGStart,7、End;QueryPerformanceCounter(&temp);Start=temp.QuadPart;//获得计数器的初始值HANDLEhthread[Threadnum];InitializeCriticalSection(&g);for(inti=0;i8、tion(&g);pi=step*su
4、5)*step;sum=sum+4.0/(1.0+x*x);}pi=step*sum;endtime=clock();printf("块数=%d",num_steps);printf("Pi=%f",pi);printf("time=%f",endtime-starttime);return0;}Word资料.块数1000000块数10000000Word资料.块数10000000???100000000Word资料.2.WinAPI???????#include#include#include5、s.h>#include#includeusingnamespacestd;staticlongnum_step;constintThreadnum=10;//修改此处线程数为1,4,8doublestep,pi=0,sum=0.0;CRITICAL_SECTIONg;DWORDWINAPIthreadFunc(LPVOIDpParam)Word资料.{intnum=*((int*)pParam);doublex,sum1=0;step=1.0/(double)num_step;for(inti=num;i6、;i+=Threadnum){x=(i+0.5)*step;sum1+=4.0/(1.0+x*x);}EnterCriticalSection(&g);sum+=sum1;LeaveCriticalSection(&g);return0;}voidmain(){cout<<"输入分块的数目:";cin>>num_step;LARGE_INTEGERtemp;doubledFreq;//系统时钟QueryPerformanceFrequency(&temp);dFreq=(double)temp.QuadPart;//获得计数器的时刻LONGLONGStart,7、End;QueryPerformanceCounter(&temp);Start=temp.QuadPart;//获得计数器的初始值HANDLEhthread[Threadnum];InitializeCriticalSection(&g);for(inti=0;i8、tion(&g);pi=step*su
5、s.h>#include#includeusingnamespacestd;staticlongnum_step;constintThreadnum=10;//修改此处线程数为1,4,8doublestep,pi=0,sum=0.0;CRITICAL_SECTIONg;DWORDWINAPIthreadFunc(LPVOIDpParam)Word资料.{intnum=*((int*)pParam);doublex,sum1=0;step=1.0/(double)num_step;for(inti=num;i6、;i+=Threadnum){x=(i+0.5)*step;sum1+=4.0/(1.0+x*x);}EnterCriticalSection(&g);sum+=sum1;LeaveCriticalSection(&g);return0;}voidmain(){cout<<"输入分块的数目:";cin>>num_step;LARGE_INTEGERtemp;doubledFreq;//系统时钟QueryPerformanceFrequency(&temp);dFreq=(double)temp.QuadPart;//获得计数器的时刻LONGLONGStart,7、End;QueryPerformanceCounter(&temp);Start=temp.QuadPart;//获得计数器的初始值HANDLEhthread[Threadnum];InitializeCriticalSection(&g);for(inti=0;i8、tion(&g);pi=step*su
6、;i+=Threadnum){x=(i+0.5)*step;sum1+=4.0/(1.0+x*x);}EnterCriticalSection(&g);sum+=sum1;LeaveCriticalSection(&g);return0;}voidmain(){cout<<"输入分块的数目:";cin>>num_step;LARGE_INTEGERtemp;doubledFreq;//系统时钟QueryPerformanceFrequency(&temp);dFreq=(double)temp.QuadPart;//获得计数器的时刻LONGLONGStart,
7、End;QueryPerformanceCounter(&temp);Start=temp.QuadPart;//获得计数器的初始值HANDLEhthread[Threadnum];InitializeCriticalSection(&g);for(inti=0;i8、tion(&g);pi=step*su
8、tion(&g);pi=step*su
此文档下载收益归作者所有