Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <mpi.h>
  3. #include <string>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. double f(double x)
  11. {
  12. return sqrt(1 - x*x);
  13. }
  14.  
  15. int main(int argc, char** argt)
  16. {
  17. srand (time(NULL));
  18. MPI_Init(&argc, &argt);
  19. int rank, size;
  20. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  21. MPI_Comm_size(MPI_COMM_WORLD, &size);
  22.  
  23. int n = 0;
  24. double poczatek_calkowania = -1.0, koniec_calkowania = 1.0;
  25.  
  26. if(rank == 0)
  27. {
  28. cout << "Podaj liczbe przdzialow calkowania n = ";
  29. cin >> n;
  30. }
  31.  
  32.  
  33. MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
  34.  
  35. double delta = (koniec_calkowania - poczatek_calkowania) / n;
  36. double suma = 0.0;
  37.  
  38.  
  39. for(int i=rank; i<n; i+=size)
  40. {
  41. int przedzial = i;
  42.  
  43. double x = poczatek_calkowania + przedzial*delta;
  44. suma += delta*f(x);
  45. }
  46.  
  47. double calkowanie = 0.0;
  48.  
  49. MPI_Reduce(&suma, &calkowanie, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
  50.  
  51. if(rank == 0)
  52. {
  53. double pi = 2*calkowanie;
  54. cout << endl << "Pi = " << pi << endl;
  55. }
  56.  
  57. MPI_Finalize();
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement