Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include "mpi.h"
- int main(int argc, char *argv[])
- {
- int cpu, num;
- unsigned int i, n;
- double sub, pi, x, dx, sum;
- double PI25DT = 3.141592653589793238462643;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &num);
- MPI_Comm_rank(MPI_COMM_WORLD, &cpu);
- n = 4000000000;
- dx = 1.0 / (double) n;
- sum = 0.0;
- for (i = cpu; i < n; i += num) {
- x = dx * ((double)i - 0.5);
- sum += (4.0 / (1.0 + x*x));
- }
- sub = dx * sum;
- MPI_Reduce(&sub, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
- if (cpu == 0)
- printf("pi is approximately %.15f, Error is %.12f\n",
- pi, fabs(pi - PI25DT));
- MPI_Finalize();
- return 0;
- }
Add Comment
Please, Sign In to add comment