|
1 |
//
|
|
2 |
// Estimation of Pi using Monte Carlo exploration process
|
|
3 |
// gcc -std=c99 -O3 -o Pi Pi.c -lm
|
|
4 |
//
|
|
5 |
|
|
6 |
#include <math.h>
|
|
7 |
#include <stdio.h>
|
|
8 |
#include <stdlib.h>
|
|
9 |
#include <mpi.h>
|
|
10 |
|
|
11 |
// Marsaglia RNG very simple implementation
|
|
12 |
#define znew ((z=36969*(z&65535)+(z>>16))<<16)
|
|
13 |
#define wnew ((w=18000*(w&65535)+(w>>16))&65535)
|
|
14 |
#define MWC (znew+wnew)
|
|
15 |
#define SHR3 (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5))
|
|
16 |
#define CONG (jcong=69069*jcong+1234567)
|
|
17 |
#define KISS ((MWC^CONG)+SHR3)
|
|
18 |
|
|
19 |
#define MWCfp MWC * 2.328306435454494e-10f
|
|
20 |
#define KISSfp KISS * 2.328306435454494e-10f
|
|
21 |
|
|
22 |
#define ITERATIONS 1000000000
|
|
23 |
|
|
24 |
#ifdef LONG
|
|
25 |
#define LENGTH unsigned long
|
|
26 |
#else
|
|
27 |
#define LENGTH unsigned int
|
|
28 |
#endif
|
|
29 |
|
|
30 |
LENGTH MainLoopGlobal(LENGTH iterations,unsigned int seed_w,unsigned int seed_z)
|
|
31 |
{
|
|
32 |
unsigned int z=seed_z;
|
|
33 |
unsigned int w=seed_w;
|
|
34 |
|
|
35 |
unsigned long total=0;
|
|
36 |
|
|
37 |
for (LENGTH i=0;i<iterations;i++) {
|
|
38 |
|
|
39 |
float x=MWCfp ;
|
|
40 |
float y=MWCfp ;
|
|
41 |
|
|
42 |
// Matching test
|
|
43 |
int inside=((x*x+y*y) < 1.0f) ? 1:0;
|
|
44 |
total+=inside;
|
|
45 |
}
|
|
46 |
|
|
47 |
return(total);
|
|
48 |
|
|
49 |
}
|
|
50 |
|
|
51 |
int main(int argc, char *argv[]) {
|
|
52 |
|
|
53 |
unsigned int seed_w=10,seed_z=10;
|
|
54 |
LENGTH iterations=ITERATIONS,inside[1024],insides,part_inside,part_iterations;
|
|
55 |
int numtasks,rank,rc,tag=1,i;
|
|
56 |
float pi;
|
|
57 |
|
|
58 |
MPI_Status Stat;
|
|
59 |
|
|
60 |
rc = MPI_Init(&argc,&argv);
|
|
61 |
if (rc != MPI_SUCCESS) {
|
|
62 |
printf ("Error starting MPI program. Terminating.\n");
|
|
63 |
MPI_Abort(MPI_COMM_WORLD, rc);
|
|
64 |
}
|
|
65 |
|
|
66 |
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
|
|
67 |
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
|
|
68 |
|
|
69 |
if (rank==0) {
|
|
70 |
|
|
71 |
if (argc > 1) {
|
|
72 |
iterations=(LENGTH)atol(argv[1]);
|
|
73 |
}
|
|
74 |
else {
|
|
75 |
printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
|
|
76 |
printf("\t\t#1 : number of iterations (default 1 billion)\n\n");
|
|
77 |
}
|
|
78 |
|
|
79 |
part_iterations=iterations/numtasks;
|
|
80 |
// Split part of code
|
|
81 |
for (i=1;i<numtasks;i++) {
|
|
82 |
rc = MPI_Send(&part_iterations, 1, MPI_UNSIGNED_LONG, i, tag,
|
|
83 |
MPI_COMM_WORLD);
|
|
84 |
}
|
|
85 |
|
|
86 |
insides=MainLoopGlobal(part_iterations,seed_w,seed_z);
|
|
87 |
|
|
88 |
// Join part of code
|
|
89 |
for (i=1;i<numtasks;i++) {
|
|
90 |
rc = MPI_Recv(&inside[i], 1, MPI_UNSIGNED_LONG, i, tag,
|
|
91 |
MPI_COMM_WORLD, &Stat);
|
|
92 |
printf("\tReceive %lu inside from %i\n",(unsigned long)inside[i],i);
|
|
93 |
insides+=inside[i];
|
|
94 |
}
|
|
95 |
|
|
96 |
pi=4.*(float)insides/(float)((iterations/numtasks)*numtasks);
|
|
97 |
|
|
98 |
printf("\tPi=%f with error %f and %lu iterations\n\n",pi,
|
|
99 |
fabs(pi-4*atan(1))/pi,(unsigned long)iterations);
|
|
100 |
}
|
|
101 |
else
|
|
102 |
{
|
|
103 |
// Receive information from master
|
|
104 |
rc = MPI_Recv(&part_iterations, 1, MPI_UNSIGNED_LONG, 0, tag,
|
|
105 |
MPI_COMM_WORLD, &Stat);
|
|
106 |
|
|
107 |
printf("\tOn %i, receive from master %lu\n",
|
|
108 |
rank,(unsigned long)part_iterations);
|
|
109 |
|
|
110 |
part_inside=MainLoopGlobal(part_iterations,seed_w,seed_z);
|
|
111 |
|
|
112 |
printf("\tOn %i, find %lu inside\n",rank,(unsigned long)part_inside);
|
|
113 |
|
|
114 |
rc = MPI_Send(&part_inside, 1, MPI_UNSIGNED_LONG, 0, tag, MPI_COMM_WORLD);
|
|
115 |
}
|
|
116 |
|
|
117 |
MPI_Finalize();
|
|
118 |
|
|
119 |
}
|