全部博文(77)
分类: 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;
}