Guest User

Untitled

a guest
Jan 12th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <time.h>
  6. #include "mpi.h"
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10. int points = 100000;
  11. int rank;
  12. int nproc;
  13.  
  14. //coord pct
  15. double x,y;
  16.  
  17. int i;
  18.  
  19. //nr de pct din interiorul cercului pt fiecare proces
  20. int count = 0;
  21.  
  22. double z;
  23. double pi;
  24.  
  25. //nr de puncte din interiorul cercului in total(din toate procesele)
  26. int insideCirclePoints;
  27. //nr de puncte generate in total(din toate procesele)
  28. int totalPoints;
  29.  
  30. MPI_Init(&argc, &argv);
  31. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  32. MPI_Comm_size(MPI_COMM_WORLD, &nproc);
  33.  
  34. if(rank != 0)
  35. {
  36. srand48(time(NULL));
  37. for (i=0; i < points; ++i)
  38. {
  39. x = (double)drand48();
  40. y = (double)drand48();
  41.  
  42. //punct in cerc?
  43. z = ((x*x)+(y*y));
  44. if (z<=1)
  45. {
  46. count++;
  47. }
  48. }
  49. }
  50.  
  51.  
  52. MPI_Reduce(&count, &insideCirclePoints, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  53. MPI_Reduce(&points, &totalPoints, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  54. totalPoints -= points;
  55.  
  56. if (rank == 0)
  57. {
  58. pi = ((double)insideCirclePoints/(double)totalPoints)*4.0;
  59. printf("pi = %f\n", pi);
  60. }
  61. MPI_Finalize();
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment