Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. import mpi.*;
  2. import java.lang.*;
  3.  
  4. class Monte{
  5.  
  6. public static double fh(double x){
  7. return 4/(1+x*x);
  8. }
  9.  
  10. public static double fi(double x){
  11. return Math.sqrt(x + Math.sqrt(x));
  12. }
  13.  
  14. public static void main(String[] args){
  15. int N = 1000;
  16. int MASTER = 0;
  17. int id, processes;
  18.  
  19. double sumf1 = 0;
  20. double sumf2 = 0;
  21. double sum_allf1 =0;
  22. double sum_allf2 = 0;
  23.  
  24. double rand = Math.random();
  25.  
  26. MPI.Init(args);
  27.  
  28. processes = MPI.COMM_WORLD.Size();
  29. id = MPI.COMM_WORLD.Rank();
  30.  
  31. double start = MPI.Wtime();
  32.  
  33. if (id != MASTER) {
  34. for (int i = 0; i < N; i++){
  35. sumf1 = fh(rand);
  36. sumf2 = fi(rand);
  37. }
  38.  
  39. sumf1 /= N;
  40. sumf2 /= N;
  41. }
  42.  
  43. MPI.COMM_WORLD.Reduce(sumf1, 0, sum_allf1, 0, 1, MPI.DOUBLE, MPI.SUM, MASTER);
  44. MPI.COMM_WORLD.Reduce(sumf2, 0, sum_allf2, 0, 1, MPI.DOUBLE, MPI.SUM, MASTER);
  45.  
  46. if (id == MASTER) {
  47. // -1 in the following lines because master does not participate in the sampling
  48.  
  49.  
  50. System.out.println(" F1: " + (sum_allf1 / (double) (processes - 1)));
  51. System.out.println(" F2: " + (sum_allf2 / (double) (processes - 1)));
  52. System.out.println("TIME (S): " + (MPI.Wtime() - start));
  53.  
  54. MPI.Finalize();
  55. return;
  56. }
  57.  
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement