实验4 pi 的计算(4)

发布时间:2021-06-08

并行计算的pi的多种计算方法的实验

double f(double a)

{

return (4.0 / (1.0 + a*a));

}

int _tmain(intargc, char* argv[])

{

int done = 0, n, myid, numprocs, i,source=0,count=1; double PI25DT = 3.141592653589793238462643;

doublemypi, pi, h, sum, x;

doublestartwtime = 0.0, endwtime;

MPI_Init(&argc,&argv);

MPI_Comm_size(MPI_COMM_WORLD,&numprocs);

MPI_Comm_rank(MPI_COMM_WORLD,&myid);

while (!done)

{

{ fflush(stdout); { } startwtime = MPI_Wtime(); fprintf(stdout, "No number entered; quitting\n" ); n = 0; if (myid == source) fprintf(stdout, "Enter the number of intervals: (0 quits) "); if (scanf("%d",&n) != 1)

}

MPI_Bcast(&n, count, MPI_INT, source, MPI_COMM_WORLD); if (n == 0)

done = 1;

else {

h = 1.0 / (double) n; { } mypi = h * sum; if (myid == source) { printf("pi is approximately %.16f, Error is %.16f\n", pi, fabs(pi - endwtime = MPI_Wtime(); x = h * ((double)i - 0.5); sum = 0.0; for (i = myid + 1; i <= n; i += numprocs) sum += f(x); MPI_Reduce(&mypi, &pi, count, MPI_DOUBLE, MPI_SUM, source, MPI_COMM_WORLD); PI25DT));

实验4 pi 的计算(4).doc 将本文的Word文档下载到电脑

精彩图片

热门精选

大家正在看

× 游客快捷下载通道(下载后可以自由复制和排版)

限时特价:7 元/份 原价:20元

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219