Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. // LAB 22.03 - PRZYBLIZENIE PI
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <mpi.h>
  6. #include <math.h>
  7.  
  8. #define ROOT 0
  9. #define MSG_TAG 100
  10.  
  11. int main(int argc,char **argv)
  12. {
  13. int size,tid;
  14. float R=1;
  15.  
  16. MPI_Init(&argc, &argv);
  17.  
  18. MPI_Comm_size( MPI_COMM_WORLD, &size );
  19. MPI_Comm_rank( MPI_COMM_WORLD, &tid );
  20.  
  21. srand( tid );
  22.  
  23. float res;
  24.  
  25. if ( tid == 0 ) {
  26. MPI_Status status;
  27. int i;
  28. long double pi;
  29. float sum_results = 0;
  30. // pewnie jakiś for tutaj
  31. for(i = 1; i<size; i++) {
  32. MPI_Recv( &res, 1, MPI_FLOAT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  33. sum_results+=res;
  34. }
  35. pi = (long double)(sum_results / (size-1));
  36. printf("Przybliżenie pi po zebraniu danych od %d procesów wynosi %.20llf\n", i, pi);
  37. } else {
  38. int in_circle_counter = 0;
  39. float x, y, left;
  40. int number_of_iterations = 800000000, i;
  41. for (i = 0; i < number_of_iterations; i++) {
  42. x = rand() / (float)RAND_MAX;
  43. y = rand() / (float)RAND_MAX;
  44. left = x*x + y*y;
  45. if (left <= R) in_circle_counter++;
  46. }
  47. float pi_estimated = 4 * ((float)in_circle_counter / number_of_iterations);
  48. // printf("pi_estimated %f, id: %d\n", pi_estimated, tid);
  49. MPI_Send( &pi_estimated, 1, MPI_FLOAT, ROOT, MSG_TAG, MPI_COMM_WORLD );
  50. }
  51.  
  52. MPI_Finalize();
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement