免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1038 | 回复: 0
打印 上一主题 下一主题

计算PI的值 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-14 11:52 |只看该作者 |倒序浏览

在fedora.unix-center.net上的实验结果,加速比基本上约等于2。
-bash-3.2$gcc –xopenmp –o 2 2.c
-bash-3.2$ ./2
the whole processing time of the program is 0.951687 seconds
pi=3.141593
-bash-3.2$gcc –o 1 1.c
-bash-3.2$ ./1
the whole processing time of the program is 1.791129 seconds
pi=3.141593

同样的程序运行在t10000.unix-center.net上,效果完全不一样,具体原因还在寻找中。

-bash-3.00$ CC -o 11 1.c
-bash-3.00$ ./11
the whole processing time of the program is 33.903705 seconds
pi=3.141593
-bash-3.00$ CC –xopenmp –O3 –o22  2.c

-bash-3.00$ ./22
the whole processing time of the program is 7.478114 seconds
pi=3.141593

串行代码1.c

#include
#include  
#include  
#include  
static long num_steps = 100000000;
double step;
int   main(void)
{

struct timeval starttime,endtime;
double timepast;
int i;  
double x, pi, sum = 0.0;
step = 1.0/(double) num_steps;
gettimeofday(&starttime,NULL);
     for (i=0;i
                                  x = (i+0.5)*step;  
                                sum = sum + 4.0/(1.0+x*x);   
              }
gettimeofday(&endtime,NULL);
        timepast=((double)(endtime.tv_sec-starttime.tv_sec)*1000000+(double)(endtime.tv_usec-starttime.tv_usec))/1000000;
          printf("the whole processing time of the program is %lf seconds\n",timepast);

                 pi = step * sum;
    printf("pi=%lf\n",pi);
     return 0;
}



并行代码2.c


#include
#include
#include  
#include  
#include  
static long num_steps = 100000000;
double step;
int   main(void)
{

struct timeval starttime,endtime;
double timepast;
int i;  
double x, pi, sum = 0.0;
step = 1.0/(double) num_steps;
omp_set_num_threads(4);
gettimeofday(&starttime,NULL);
#pragma omp parallel for private(x)  reduction(+:sum)
           for (i=0;i
                                  x = (i+0.5)*step;  
                                sum = sum + 4.0/(1.0+x*x);   
              }
gettimeofday(&endtime,NULL);
        timepast=((double)(endtime.tv_sec-starttime.tv_sec)*1000000+(double)(endtime.tv_usec-starttime.tv_usec))/1000000;
          printf("the whole processing time of the program is %lf seconds\n",timepast);

                 pi = step * sum;
         printf("pi=%lf\n",pi);
        return 0;
}





本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/86537/showart_1964048.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP