Chinaunix首页 | 论坛 | 博客
  • 博客访问: 401087
  • 博文数量: 77
  • 博客积分: 3149
  • 博客等级: 中校
  • 技术积分: 828
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-25 11:48
文章存档

2012年(5)

2011年(2)

2010年(11)

2009年(44)

2008年(15)

我的朋友

分类: LINUX

2009-06-14 11:52:27

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<= num_steps; 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<= num_steps; 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;

}

 

 

 

阅读(1738) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~