实验4 pi 的计算(4)
发布时间:2021-06-08
发布时间: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));
上一篇:1管理沟通概述