root / Pi / C / MPI / Pi_MPI.c @ 10
Historique | Voir | Annoter | Télécharger (3,04 ko)
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 |
printf("\tOn %i, find %lu inside\n",rank,(unsigned long)insides); |
88 |
|
89 |
// Join part of code
|
90 |
for (i=1;i<numtasks;i++) { |
91 |
rc = MPI_Recv(&inside[i], 1, MPI_UNSIGNED_LONG, i, tag,
|
92 |
MPI_COMM_WORLD, &Stat); |
93 |
printf("\tReceive %lu inside from %i\n",(unsigned long)inside[i],i); |
94 |
insides+=inside[i]; |
95 |
} |
96 |
|
97 |
pi=4.*(float)insides/(float)((iterations/numtasks)*numtasks); |
98 |
|
99 |
printf("\n\tPi=%f with error %f and %lu iterations\n\n",pi,
|
100 |
fabs(pi-4*atan(1))/pi,(unsigned long)iterations); |
101 |
} |
102 |
else
|
103 |
{ |
104 |
// Receive information from master
|
105 |
rc = MPI_Recv(&part_iterations, 1, MPI_UNSIGNED_LONG, 0, tag, |
106 |
MPI_COMM_WORLD, &Stat); |
107 |
|
108 |
printf("\tOn %i, receive from master %lu\n",
|
109 |
rank,(unsigned long)part_iterations); |
110 |
|
111 |
part_inside=MainLoopGlobal(part_iterations,seed_w,seed_z); |
112 |
|
113 |
printf("\tOn %i, find %lu inside\n",rank,(unsigned long)part_inside); |
114 |
|
115 |
rc = MPI_Send(&part_inside, 1, MPI_UNSIGNED_LONG, 0, tag, MPI_COMM_WORLD); |
116 |
} |
117 |
|
118 |
MPI_Finalize(); |
119 |
|
120 |
} |