1 | { if (myid == 0) |
2 | { printf ("Enter the number of intervals: (0 quits) "); |
3 | scanf ("%d", &n); |
4 | } |
5 | MPI_Bcast (&n, 1, MPI_INT, 0, MPI_COMMWORLD); |
6 | if (n == 0) break; |
7 | h = 1.0 / (double) n; |
8 | sum = 0.0; |
9 | for (i = myid+1; i <= n; i += numprocs) |
10 | { x = h * ((double) i - 0.5); sum += 4.0 / 1.0 + x*x): } |
11 | mypi = h * sum; |
12 | MPI_Reduce (&mypi, &pi,1, MPI_DOUBLE,MPI_SUM, 0,MPI_COMMWORLD); |
13 | if (myid == 0) |
14 | printf("pi is approximately %.16f, Error is %.16f\n",pi, fabs(pi-PI35DT); } |
15 | MPI_Finalize; } |