Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include "mpi.h"
  2. #include <stdio.h>
  3. #include <math.h>
  4. int main( int argc, char *argv[] )
  5. {
  6. int n, myid, numprocs, i;
  7. double PI25DT = 3.141592653589793238462643;
  8. double mypi, pi, sum;
  9. MPI_Init(&argc,&argv);
  10. MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
  11. MPI_Comm_rank(MPI_COMM_WORLD,&myid);
  12. n=atoi(argv[1])*numprocs;
  13. sum = 0.0;
  14. for (i = myid ; i < n; i += numprocs) {
  15. sum += pow((-1),i)*(4.0 / (2*i+1));
  16. printf("%d, %d \n", myid, i);
  17. }
  18. mypi = sum;
  19. MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0,
  20. MPI_COMM_WORLD);
  21. if (myid == 0)
  22. printf("pi is approximately %.16f, Error is %.16f\n",pi, fabs(pi - PI25DT));
  23.  
  24. MPI_Finalize();
  25. return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement