Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mpi.*;
- import java.lang.*;
- class Monte{
- public static double fh(double x){
- return 4/(1+x*x);
- }
- public static double fi(double x){
- return Math.sqrt(x + Math.sqrt(x));
- }
- public static void main(String[] args){
- int N = 1000;
- int MASTER = 0;
- int id, processes;
- double sumf1 = 0;
- double sumf2 = 0;
- double sum_allf1 =0;
- double sum_allf2 = 0;
- double rand = Math.random();
- MPI.Init(args);
- processes = MPI.COMM_WORLD.Size();
- id = MPI.COMM_WORLD.Rank();
- double start = MPI.Wtime();
- if (id != MASTER) {
- for (int i = 0; i < N; i++){
- sumf1 = fh(rand);
- sumf2 = fi(rand);
- }
- sumf1 /= N;
- sumf2 /= N;
- }
- MPI.COMM_WORLD.Reduce(sumf1, 0, sum_allf1, 0, 1, MPI.DOUBLE, MPI.SUM, MASTER);
- MPI.COMM_WORLD.Reduce(sumf2, 0, sum_allf2, 0, 1, MPI.DOUBLE, MPI.SUM, MASTER);
- if (id == MASTER) {
- // -1 in the following lines because master does not participate in the sampling
- System.out.println(" F1: " + (sum_allf1 / (double) (processes - 1)));
- System.out.println(" F2: " + (sum_allf2 / (double) (processes - 1)));
- System.out.println("TIME (S): " + (MPI.Wtime() - start));
- MPI.Finalize();
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement