Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <mpi.h>
  4.  
  5. #define PI 3.141592653589793238462643
  6.  
  7. int main(int argc, char **argv) {
  8. MPI_Init(&argc, &argv);
  9.  
  10. int processes, pe, intervals;
  11. MPI_Comm_size(MPI_COMM_WORLD, &processes);
  12. MPI_Comm_rank(MPI_COMM_WORLD, &pe);
  13. double time1, time2;
  14. if (pe == 0) {
  15. //printf("Number of intervals: ");
  16. fflush(stdout);
  17. //scanf("%d", &intervals);
  18. intervals = atoi(argv[1]);
  19. time1 = MPI_Wtime();
  20. }
  21.  
  22.  
  23. MPI_Bcast(&intervals, 1, MPI_INT, 0, MPI_COMM_WORLD);
  24.  
  25. int count = intervals / processes;
  26. int start = count * pe;
  27. int end;
  28.  
  29. if(pe < processes-1){
  30. end = count * pe + count;
  31. }
  32.  
  33. if(pe == processes-1){
  34. end = count * pe + count + (intervals % processes);
  35. }
  36.  
  37. int i;
  38. double subtotal, total = 0;
  39. for (i = start; i < end; ++i) {
  40. subtotal += pow(-1, i) / (2 * i + 1);
  41. }
  42.  
  43. //printf("ile: %d\n", intervals % processes);
  44. printf("proc: %d\n", pe);
  45.  
  46. MPI_Reduce(&subtotal, &total, 1, MPI_DOUBLE, MPI_SUM,
  47. 0, MPI_COMM_WORLD);
  48.  
  49.  
  50.  
  51. if (pe == 0) {
  52. time2 = MPI_Wtime();
  53. total = total * 4;
  54. printf("Result: %.10lf\n", total);
  55. printf("Accuracy: %.10lf\n", PI - total);
  56. printf("Time: %.10lf\n", time2 - time1);
  57. }
  58.  
  59. MPI_Finalize();
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement