root / Pi / C / MPI / Pi_MPI.c @ 77
Historique | Voir | Annoter | Télécharger (5,49 ko)
1 |
//
|
---|---|
2 |
// Estimation of Pi using Monte Carlo exploration process
|
3 |
// gcc -std=c99 -O3 -o Pi Pi.c -lm
|
4 |
// Emmanuel Quemener <emmanuel.quemener@ens-lyon.fr>
|
5 |
|
6 |
// Needed for gethostname
|
7 |
#define _BSD_SOURCE
|
8 |
#include <sys/unistd.h> |
9 |
|
10 |
#include <math.h> |
11 |
#include <stdio.h> |
12 |
#include <stdlib.h> |
13 |
#include <limits.h> |
14 |
#include <mpi.h> |
15 |
|
16 |
#ifdef TIME
|
17 |
#include <sys/time.h> |
18 |
#endif
|
19 |
|
20 |
// Marsaglia RNG very simple implementation
|
21 |
#define znew ((z=36969*(z&65535)+(z>>16))<<16) |
22 |
#define wnew ((w=18000*(w&65535)+(w>>16))&65535) |
23 |
#define MWC (znew+wnew)
|
24 |
#define SHR3 (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5)) |
25 |
#define CONG (jcong=69069*jcong+1234567) |
26 |
#define KISS ((MWC^CONG)+SHR3)
|
27 |
|
28 |
#define MWCfp MWC * 2.328306435454494e-10f |
29 |
#define KISSfp KISS * 2.328306435454494e-10f |
30 |
|
31 |
#define ITERATIONS 1000000000 |
32 |
|
33 |
#ifdef LONG
|
34 |
#define LENGTH long long |
35 |
#else
|
36 |
#define LENGTH int |
37 |
#endif
|
38 |
|
39 |
unsigned int rotl(unsigned int value, int shift) { |
40 |
return (value << shift) | (value >> (sizeof(value) * CHAR_BIT - shift)); |
41 |
} |
42 |
|
43 |
unsigned int rotr(unsigned int value, int shift) { |
44 |
return (value >> shift) | (value << (sizeof(value) * CHAR_BIT - shift)); |
45 |
} |
46 |
|
47 |
LENGTH MainLoopGlobal(LENGTH iterations,unsigned int seed_w,unsigned int seed_z) |
48 |
{ |
49 |
unsigned int z=seed_z; |
50 |
unsigned int w=seed_w; |
51 |
|
52 |
LENGTH total=0;
|
53 |
|
54 |
for (LENGTH i=0;i<iterations;i++) { |
55 |
|
56 |
float x=MWCfp ;
|
57 |
float y=MWCfp ;
|
58 |
|
59 |
// Matching test
|
60 |
int inside=((x*x+y*y) < 1.0f) ? 1:0; |
61 |
total+=inside; |
62 |
} |
63 |
|
64 |
return(total);
|
65 |
|
66 |
} |
67 |
|
68 |
int main(int argc, char *argv[]) { |
69 |
|
70 |
unsigned int seed_z=362436069,seed_w=52128862; |
71 |
LENGTH iterations=ITERATIONS,inside[1024],insides,part_inside,part_iterations;
|
72 |
int numtasks,rank,rc,tag=1,i; |
73 |
float pi;
|
74 |
|
75 |
char hostname[128]; |
76 |
|
77 |
gethostname(hostname, sizeof hostname);
|
78 |
|
79 |
#ifdef TIME
|
80 |
struct timeval start,end;
|
81 |
long int useconds; |
82 |
#endif
|
83 |
|
84 |
MPI_Status Stat; |
85 |
MPI_Request RequestSend, RequestRecv, RequestSend2, RequestRecv2; |
86 |
|
87 |
rc = MPI_Init(&argc,&argv); |
88 |
if (rc != MPI_SUCCESS) {
|
89 |
printf ("Error starting MPI program. Terminating.\n");
|
90 |
MPI_Abort(MPI_COMM_WORLD, rc); |
91 |
} |
92 |
|
93 |
MPI_Comm_size(MPI_COMM_WORLD,&numtasks); |
94 |
MPI_Comm_rank(MPI_COMM_WORLD,&rank); |
95 |
|
96 |
if (rank==0) { |
97 |
|
98 |
if (argc > 1) { |
99 |
iterations=(LENGTH)atoll(argv[1]);
|
100 |
} |
101 |
else {
|
102 |
printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
|
103 |
printf("\t\t#1 : number of iterations (default 1 billion)\n\n");
|
104 |
} |
105 |
|
106 |
printf ("Sizeof int = %lld bytes.\n", (long long)sizeof(int)); |
107 |
printf ("Sizeof long = %lld bytes.\n", (long long)sizeof(long)); |
108 |
printf ("Sizeof long long = %lld bytes.\n", (long long)sizeof(long long)); |
109 |
|
110 |
printf ("Max int = %u\n", INT_MAX);
|
111 |
printf ("Max long = %ld\n", LONG_MAX);
|
112 |
printf ("Max long long = %lld\n", LLONG_MAX);
|
113 |
|
114 |
part_iterations=iterations/numtasks+1;
|
115 |
|
116 |
// Split part of code
|
117 |
for (i=1;i<numtasks;i++) { |
118 |
|
119 |
#ifdef LONG
|
120 |
rc = MPI_Isend(&part_iterations, 1, MPI_LONG_LONG, i, tag,
|
121 |
MPI_COMM_WORLD,&RequestSend); |
122 |
#else
|
123 |
rc = MPI_Isend(&part_iterations, 1, MPI_INT, i, tag,
|
124 |
MPI_COMM_WORLD,&RequestSend); |
125 |
#endif
|
126 |
} |
127 |
MPI_Wait(&RequestSend, &Stat); |
128 |
|
129 |
#ifdef TIME
|
130 |
gettimeofday(&start,(struct timezone *)0); |
131 |
#endif
|
132 |
|
133 |
insides=MainLoopGlobal(part_iterations,seed_w,seed_z); |
134 |
|
135 |
#ifdef TIME
|
136 |
gettimeofday(&end,(struct timezone *)0); |
137 |
useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
|
138 |
|
139 |
printf("\tOn %s with rank %i, find %lld inside in %lu useconds.\n",
|
140 |
hostname,rank,(long long)insides,useconds); |
141 |
#else
|
142 |
printf("\tOn %s with rank %i, find %lld inside\n",hostname,rank,
|
143 |
(long long)insides); |
144 |
|
145 |
#endif
|
146 |
|
147 |
// Join part of code
|
148 |
for (i=1;i<numtasks;i++) { |
149 |
#ifdef LONG
|
150 |
rc = MPI_Irecv(&inside[i], 1, MPI_LONG_LONG, i, tag,
|
151 |
MPI_COMM_WORLD, &RequestRecv2); |
152 |
#else
|
153 |
rc = MPI_Irecv(&inside[i], 1, MPI_INT, i, tag,
|
154 |
MPI_COMM_WORLD, &RequestRecv2); |
155 |
#endif
|
156 |
MPI_Wait(&RequestRecv2, &Stat); |
157 |
|
158 |
printf("\tReceive %lu inside from rank %i\n",(unsigned long)inside[i],i); |
159 |
insides+=inside[i]; |
160 |
} |
161 |
|
162 |
pi=4.*(float)insides/(float)((iterations/numtasks)*numtasks); |
163 |
|
164 |
printf("\n\tPi=%.40f\n\twith error %.40f\n\twith %lld iterations\n\n",pi,
|
165 |
fabs(pi-4*atan(1.))/pi,(long long)iterations); |
166 |
|
167 |
} |
168 |
else
|
169 |
{ |
170 |
// Receive information from master
|
171 |
#ifdef LONG
|
172 |
rc = MPI_Irecv(&part_iterations, 1, MPI_LONG_LONG, 0, tag, |
173 |
MPI_COMM_WORLD, &RequestRecv); |
174 |
#else
|
175 |
rc = MPI_Irecv(&part_iterations, 1, MPI_INT, 0, tag, |
176 |
MPI_COMM_WORLD, &RequestRecv); |
177 |
#endif
|
178 |
MPI_Wait(&RequestRecv, &Stat); |
179 |
|
180 |
printf("\tOn %s with rank %i, receive from master %lld\n",
|
181 |
hostname,rank,(long long)part_iterations); |
182 |
|
183 |
#ifdef TIME
|
184 |
gettimeofday(&start,(struct timezone *)0); |
185 |
#endif
|
186 |
|
187 |
part_inside=MainLoopGlobal(part_iterations,rotr(seed_w,rank),rotl(seed_z,rank)); |
188 |
|
189 |
|
190 |
#ifdef TIME
|
191 |
gettimeofday(&end,(struct timezone *)0); |
192 |
useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
|
193 |
|
194 |
printf("\tOn %s with rank %i, find %lld inside in %lu useconds.\n",
|
195 |
hostname,rank,(long long)part_inside,useconds); |
196 |
#else
|
197 |
printf("\tOn %s with rank %i, find %lld inside\n",hostname,rank,
|
198 |
(long long)part_inside); |
199 |
|
200 |
#endif
|
201 |
|
202 |
#ifdef LONG
|
203 |
rc = MPI_Isend(&part_inside, 1, MPI_LONG_LONG, 0, tag, MPI_COMM_WORLD,&RequestSend2); |
204 |
#else
|
205 |
rc = MPI_Isend(&part_inside, 1, MPI_INT, 0, tag, MPI_COMM_WORLD,&RequestSend2); |
206 |
#endif
|
207 |
MPI_Wait(&RequestSend2, &Stat); |
208 |
|
209 |
} |
210 |
|
211 |
MPI_Finalize(); |
212 |
|
213 |
} |