-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiEstimation.cpp
More file actions
executable file
·57 lines (55 loc) · 1.75 KB
/
Copy pathPiEstimation.cpp
File metadata and controls
executable file
·57 lines (55 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include "/home/pnpninja/PC/Monte Carlo/prng_engine.hpp"
#include "mpi.h"
#define sitmo_rand_max 4294967295
using namespace std;
int main(int argc,char* argv[])
{
//Initialization of variables
int numprocs,myid,rc;
long int all_valid_points;
double iterations,eachpi,pisum,pi,start,total_time;
//Initialization on MPI Variables
MPI_Init(&argc,&argv);
MPI_Status status;
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
//Get number of iterations in MASTER Process / Process 0
if(myid == 0)
{
cout<<"Enter the number of iterations in each thread: ";
cin>>iterations;
start = MPI_Wtime();
}
//Broadcast it to all other processes
MPI_Bcast(&iterations,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
//Parallel Random Generator with seed as MPI_Wtime()
sitmo::prng_engine eng(myid);
long int valid_points = 0;
//Calculate how many points are inside
for(int a=1;a<=iterations/numprocs;a++)
{
double x = (double(eng())/(double(sitmo_rand_max)));
double y = (double(eng())/(double(sitmo_rand_max)));
x = (2.0 * x) - 1.0;
y = (2.0 * y) - 1.0;
if((x*x)+(y*y)<=1.0)
valid_points++;
}
//Calculate each Pi generated in each process and print it
eachpi = 4.0 * (double)valid_points / (double)(iterations/numprocs);
printf("\nThread %d : %lf",myid,eachpi);
rc = MPI_Reduce(&eachpi,&pisum,1, MPI_DOUBLE, MPI_SUM,0, MPI_COMM_WORLD);
if(myid == 0)
{
//Average of Pi
double pi = pisum/(double(numprocs));
total_time = MPI_Wtime() - start;
cout<<"\nValue of Pi by Dartboard Method is "<<pi;
cout<<"\nError is : "<<pi-3.14159265358979323846;
cout<<"\nTime of execution : "<<total_time<<"\n";
}
MPI_Finalize();
}