-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpi_pi_es.c
109 lines (97 loc) · 2.5 KB
/
mpi_pi_es.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <mpi.h>
#define ITERS 0
#define IN_CIRCLE 1
double randDouble();
int main(void)
{
int comm_sz, my_rank, i, n;
long long int iter, localInCircle = 0, inCircle;
double x, y, ds, pi = 0;
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
srandom((unsigned)time(NULL) + my_rank);
n = comm_sz - 1;
if (my_rank == 0) {
printf("Enter the number of iterations.\n");
scanf("%lld", &iter);
for (; iter%n != 0; ++iter); // Make sure the number of iterations is divided by n
for (i=0; i <= n; i++) {
MPI_Send(&iter, 1, MPI_FLOAT, i, ITERS, MPI_COMM_WORLD);
}
}
else
{
MPI_Recv(&iter, 1, MPI_FLOAT, 0, ITERS, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for (i = 0; i < iter/n; ++i)
{
x = randDouble();
y = randDouble();
ds = y*y + x*x;
if (ds <= 1) {
localInCircle++;
}
}
}
MPI_Reduce(&localInCircle,&inCircle,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
if (my_rank == 0)
{
pi = (4 * inCircle) / (double)iter;
printf("PI is: %f\n", pi);
}
MPI_Finalize();
return 0;
}
double randDouble()
{
return (double)rand() / (double)RAND_MAX ;
}
//method 2
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>
double rand_double(){
return 2 * (rand() / (double)RAND_MAX - 1);
}
int main()
{
double pi_estimate, distance_squared, x, y;
int comm_sz, my_rank,i,n;
long long int sum, number_in_circle = 0, toss;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
MPI_Comm_size(MPI_COMM_WORLD,&comm_sz);
srandom((unsigned)time(NULL) + my_rank);
n = comm_sz - 1;
if (my_rank == 0)
{
printf("Enter number of the total tosses: \n ");
scanf("%lld",&toss);
for(;toss % n != 0;++toss) ;
for(i = 0; i <= n; i++){
MPI_Send(&toss,1,MPI_FLOAT,i, 0,MPI_COMM_WORLD);
}
} else {
MPI_Recv(&toss,1,MPI_FLOAT,0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for( i = 0; i < toss/n; ++i){
//srand((long)time(NULL));
x = rand_double();
y = rand_double();
distance_squared = x * x + y * y;
if (distance_squared <= 1)
number_in_circle++;
}
}
MPI_Reduce(&number_in_circle,&sum,1, MPI_LONG_LONG_INT,MPI_SUM,0,MPI_COMM_WORLD);
if(my_rank == 0)
{
pi_estimate = 4 * sum /(double)toss;
printf("PI is: %f\n", pi_estimate);
}
MPI_Finalize();
return 0;
}