- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
用OpenMP实现pi值计算
计算Pi值
程序清单:
Monte Carlo方法
串行程序:
#include stdafx.h
#includestdio.h
#includestdlib.h
#includetime.h
int _tmain(int argc, _TCHAR* argv[])
{
long long max
long long i,count=0;
double x,y,bulk,starttime,endtime;
time_t t;
starttime=clock();// 产生以当前时间开始的随机种子
srand((unsigned) time(t));
for(i=0;imax;i++)
{
x=rand(); x=x/32767;
y=rand(); y=y/32767;
if((x*x+y*y)=1)
count++;
}
bulk=4*(double(count)/max);
endtime= clock();
printf(Pi is %f \n, bulk);
printf(Running time is %f \n, endtime-starttime);
return 0;
}
并行程序:
#include stdafx.h
#includestdio.h
#includeomp.h
#includestdlib.h
#includetime.h
#define NUM_THREADS 4
int _tmain(int argc, _TCHAR* argv[])
{
long long max
long long i,count=0;
double x,y,bulk,starttime,endtime;
time_t t;
starttime=clock();// 产生以当前时间开始的随机种子
srand((unsigned) time(t));
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel for reduction(+:count) private(x,y)
for(i=0;imax;i++)
{
x=rand(); x=x/32767;
y=rand(); y=y/32767;
if((x*x+y*y)=1)
count++;
}
bulk=4*(double(count)/max);
endtime= clock();
printf(pi is %f \n, bulk);
printf(Running time is %f \n, endtime-starttime);
return 0;
}
积分法
串行程序:
#include stdafx.h
#includestdio.h
#includestdlib.h
#includetime.h
static long num_steps = 100000000;
double step;
int _tmain(int argc, _TCHAR* argv[])
{
int i;
double x, pi, starttime,endtime,sum = 0.0;
step = 1.0/(double) num_steps;
time_t t;
starttime=clock();
srand((unsigned) time(t));
for (i=0;i num_steps; i++)
{
x = (i+0.5)*step;
sum = sum + 4.0/(1.0+x*x);
}
pi = step * sum;
endtime= clock();
printf(Pi is %f \n, pi);
printf(Running time is %f \n, endtime-starttime);
return 0;
}
并行程序:
#include stdafx.h
#includestdio.h
#includestdlib.h
#includetime.h
#includeomp.h
static long num_steps = 100000000;
double step;
#d
您可能关注的文档
最近下载
- 社会体育学复习资料与题库.pdf VIP
- 高职体育课程教学大纲.docx VIP
- 中国抗菌药物临床合理应用指南专家讲座.pptx VIP
- 最新ISO9001、ISO14001、ISO45001三体系设计部和业务部内审检查记录(带记录).pdf VIP
- 人音版四年级上册音乐全册教学设计教案.pdf
- 浙教版小学一年级上册《劳动》全册教学课件.pptx
- 管理信息系统开发项目式教程 第3版 教学课件 作者 陈承欢单元1 管理信息系统的开发综述.ppt VIP
- 2025年秋季八年级历史上册新教材解读课件(统编版2024).pptx VIP
- 商业运营管理培训.pptx VIP
- 医疗器械生产企业质量控制与成品放行指南2025年培训PPT.pdf
文档评论(0)