root / Pi / C / Hybrid / Pi_Hybrid.c @ 82
Historique | Voir | Annoter | Télécharger (8,8 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 |
// Cecill v2
|
6 |
|
7 |
// Needed for gethostname
|
8 |
#define _BSD_SOURCE
|
9 |
#include <sys/unistd.h> |
10 |
|
11 |
#include <math.h> |
12 |
#include <stdio.h> |
13 |
#include <stdlib.h> |
14 |
#include <limits.h> |
15 |
#include <mpi.h> |
16 |
#include <stddef.h> |
17 |
|
18 |
#ifdef TIME
|
19 |
#include <sys/time.h> |
20 |
#endif
|
21 |
|
22 |
// Marsaglia RNG very simple implementation
|
23 |
#define znew ((z=36969*(z&65535)+(z>>16))<<16) |
24 |
#define wnew ((w=18000*(w&65535)+(w>>16))&65535) |
25 |
#define MWC (znew+wnew)
|
26 |
#define SHR3 (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5)) |
27 |
#define CONG (jcong=69069*jcong+1234567) |
28 |
#define KISS ((MWC^CONG)+SHR3)
|
29 |
|
30 |
#define ITERATIONS 1000000000 |
31 |
|
32 |
#define MWCfp MWC * 2.328306435454494e-10f |
33 |
#define KISSfp KISS * 2.328306435454494e-10f |
34 |
#define SHR3fp SHR3 * 2.328306435454494e-10f |
35 |
#define CONGfp CONG * 2.328306435454494e-10f |
36 |
|
37 |
#define PROCESS 1 |
38 |
|
39 |
#ifdef LONG
|
40 |
#define LENGTH long long |
41 |
#else
|
42 |
#define LENGTH int |
43 |
#endif
|
44 |
|
45 |
typedef struct compute_node { |
46 |
LENGTH iterations; |
47 |
int process;
|
48 |
} node; |
49 |
|
50 |
unsigned int rotl(unsigned int value, int shift) { |
51 |
return (value << shift) | (value >> (sizeof(value) * CHAR_BIT - shift)); |
52 |
} |
53 |
|
54 |
unsigned int rotr(unsigned int value, int shift) { |
55 |
return (value >> shift) | (value << (sizeof(value) * CHAR_BIT - shift)); |
56 |
} |
57 |
|
58 |
LENGTH MainLoopGlobal(LENGTH iterations,unsigned int seed_w,unsigned int seed_z) |
59 |
{ |
60 |
|
61 |
#if defined TCONG
|
62 |
unsigned int jcong=seed_z; |
63 |
#elif defined TSHR3
|
64 |
unsigned int jsr=seed_w; |
65 |
#elif defined TMWC
|
66 |
unsigned int z=seed_z; |
67 |
unsigned int w=seed_w; |
68 |
#elif defined TKISS
|
69 |
unsigned int jcong=seed_z; |
70 |
unsigned int jsr=seed_w; |
71 |
unsigned int z=seed_z; |
72 |
unsigned int w=seed_w; |
73 |
#endif
|
74 |
|
75 |
LENGTH total=0;
|
76 |
|
77 |
for (LENGTH i=0;i<iterations;i++) { |
78 |
|
79 |
#if defined TINT32
|
80 |
#define THEONE 1073741824 |
81 |
#if defined TCONG
|
82 |
unsigned int x=CONG>>17 ; |
83 |
unsigned int y=CONG>>17 ; |
84 |
#elif defined TSHR3
|
85 |
unsigned int x=SHR3>>17 ; |
86 |
unsigned int y=SHR3>>17 ; |
87 |
#elif defined TMWC
|
88 |
unsigned int x=MWC>>17 ; |
89 |
unsigned int y=MWC>>17 ; |
90 |
#elif defined TKISS
|
91 |
unsigned int x=KISS>>17 ; |
92 |
unsigned int y=KISS>>17 ; |
93 |
#endif
|
94 |
#elif defined TINT64
|
95 |
#define THEONE 4611686018427387904 |
96 |
#if defined TCONG
|
97 |
unsigned long x=(unsigned long)(CONG>>1) ; |
98 |
unsigned long y=(unsigned long)(CONG>>1) ; |
99 |
#elif defined TSHR3
|
100 |
unsigned long x=(unsigned long)(SHR3>>1) ; |
101 |
unsigned long y=(unsigned long)(SHR3>>1) ; |
102 |
#elif defined TMWC
|
103 |
unsigned long x=(unsigned long)(MWC>>1) ; |
104 |
unsigned long y=(unsigned long)(MWC>>1) ; |
105 |
#elif defined TKISS
|
106 |
unsigned long x=(unsigned long)(KISS>>1) ; |
107 |
unsigned long y=(unsigned long)(KISS>>1) ; |
108 |
#endif
|
109 |
#elif defined TFP32
|
110 |
#define THEONE 1.0f |
111 |
#if defined TCONG
|
112 |
float x=CONGfp ;
|
113 |
float y=CONGfp ;
|
114 |
#elif defined TSHR3
|
115 |
float x=SHR3fp ;
|
116 |
float y=SHR3fp ;
|
117 |
#elif defined TMWC
|
118 |
float x=MWCfp ;
|
119 |
float y=MWCfp ;
|
120 |
#elif defined TKISS
|
121 |
float x=KISSfp ;
|
122 |
float y=KISSfp ;
|
123 |
#endif
|
124 |
#elif defined TFP64
|
125 |
#define THEONE 1.0f |
126 |
#if defined TCONG
|
127 |
double x=(double)CONGfp ; |
128 |
double y=(double)CONGfp ; |
129 |
#elif defined TSHR3
|
130 |
double x=(double)SHR3fp ; |
131 |
double y=(double)SHR3fp ; |
132 |
#elif defined TMWC
|
133 |
double x=(double)MWCfp ; |
134 |
double y=(double)MWCfp ; |
135 |
#elif defined TKISS
|
136 |
double x=(double)KISSfp ; |
137 |
double y=(double)KISSfp ; |
138 |
#endif
|
139 |
#endif
|
140 |
|
141 |
// Matching test
|
142 |
unsigned long inside=((x*x+y*y) < THEONE) ? 1:0; |
143 |
total+=inside; |
144 |
} |
145 |
|
146 |
return(total);
|
147 |
|
148 |
} |
149 |
|
150 |
int main(int argc, char *argv[]) { |
151 |
|
152 |
unsigned int seed_z=362436069,seed_w=52128862,process=PROCESS; |
153 |
// Number of NP or OpenMP processes <1024
|
154 |
LENGTH iterations=ITERATIONS,insideMPI[1024],insideOpenMP[1024], |
155 |
part_inside,part_iterations,insides; |
156 |
int numtasks,rank,rc,tag=1,i; |
157 |
float pi;
|
158 |
|
159 |
// Hostname supposed to be <128 characters
|
160 |
char hostname[128]; |
161 |
|
162 |
gethostname(hostname, sizeof hostname);
|
163 |
|
164 |
#ifdef TIME
|
165 |
struct timeval start,end;
|
166 |
long int useconds; |
167 |
#endif
|
168 |
|
169 |
MPI_Status Stat; |
170 |
MPI_Request RequestSend,RequestRecv,RequestSend2,RequestRecv2; |
171 |
|
172 |
rc = MPI_Init(&argc,&argv); |
173 |
if (rc != MPI_SUCCESS) {
|
174 |
printf ("Error starting MPI program. Terminating.\n");
|
175 |
MPI_Abort(MPI_COMM_WORLD, rc); |
176 |
} |
177 |
|
178 |
MPI_Comm_size(MPI_COMM_WORLD,&numtasks); |
179 |
|
180 |
const int nitems=2; |
181 |
int blocklengths[2] = {1,1}; |
182 |
|
183 |
#ifdef LONG
|
184 |
MPI_Datatype types[2] = {MPI_LONG, MPI_INT};
|
185 |
#else
|
186 |
MPI_Datatype types[2] = {MPI_INT, MPI_INT};
|
187 |
#endif
|
188 |
|
189 |
MPI_Datatype mpi_node_type; |
190 |
MPI_Aint offsets[2];
|
191 |
|
192 |
offsets[0] = offsetof(node, iterations);
|
193 |
offsets[1] = offsetof(node, process);
|
194 |
|
195 |
MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_node_type); |
196 |
MPI_Type_commit(&mpi_node_type); |
197 |
|
198 |
MPI_Comm_rank(MPI_COMM_WORLD,&rank); |
199 |
|
200 |
if (rank==0) { |
201 |
|
202 |
if (argc > 1) { |
203 |
iterations=(LENGTH)atoll(argv[1]);
|
204 |
process=atoi(argv[2]);
|
205 |
} |
206 |
else {
|
207 |
printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
|
208 |
printf("\t\t#1 : number of iterations (default 1 billion)\n\n");
|
209 |
printf("\t\t#1 : number of OpenMP processes (default 1)\n\n");
|
210 |
} |
211 |
|
212 |
printf ("\n\tInformation about architecture:\n\n");
|
213 |
|
214 |
printf ("Sizeof int = %lld bytes.\n", (long long)sizeof(int)); |
215 |
printf ("Sizeof long = %lld bytes.\n", (long long)sizeof(long)); |
216 |
printf ("Sizeof long long = %lld bytes.\n", (long long)sizeof(long long)); |
217 |
|
218 |
printf ("Max int = %u\n", INT_MAX);
|
219 |
printf ("Max long = %ld\n", LONG_MAX);
|
220 |
printf ("Max long long = %lld\n\n", LLONG_MAX);
|
221 |
|
222 |
part_iterations=iterations/(numtasks*process)+1;
|
223 |
|
224 |
node send; |
225 |
send.iterations=part_iterations; |
226 |
send.process=process; |
227 |
|
228 |
// Split part of code
|
229 |
for (i=1;i<numtasks;i++) { |
230 |
rc = MPI_Isend(&send, 1, mpi_node_type, i, tag, MPI_COMM_WORLD, &RequestSend);
|
231 |
} |
232 |
MPI_Wait(&RequestSend,&Stat); |
233 |
|
234 |
#ifdef TIME
|
235 |
gettimeofday(&start,(struct timezone *)0); |
236 |
#endif
|
237 |
|
238 |
#pragma omp parallel for |
239 |
for (int i=0 ; i<process; i++) { |
240 |
insideOpenMP[i]=MainLoopGlobal(part_iterations, |
241 |
rotr(seed_w,process), |
242 |
rotl(seed_z,process)); |
243 |
printf("\t(%s,%i) found %lld for process %i\n",hostname,0, |
244 |
(long long)insideOpenMP[i],i); |
245 |
} |
246 |
printf("\n");
|
247 |
|
248 |
insides=0;
|
249 |
for (int i=0 ; i<process; i++) { |
250 |
insides+=insideOpenMP[i]; |
251 |
} |
252 |
|
253 |
#ifdef TIME
|
254 |
gettimeofday(&end,(struct timezone *)0); |
255 |
useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
|
256 |
|
257 |
printf("\tOn %s with %i find %lld inside in %lu useconds.\n",
|
258 |
hostname,rank,(long long)insides,useconds); |
259 |
#else
|
260 |
printf("\tOn %s with %i find %lld inside\n",hostname,rank,
|
261 |
(long long)insides); |
262 |
|
263 |
#endif
|
264 |
|
265 |
// Join part of code
|
266 |
for (i=1;i<numtasks;i++) { |
267 |
#ifdef LONG
|
268 |
rc = MPI_Irecv(&insideMPI[i], 1, MPI_LONG_LONG, i, tag,
|
269 |
MPI_COMM_WORLD, &RequestRecv2); |
270 |
#else
|
271 |
rc = MPI_Irecv(&insideMPI[i], 1, MPI_INT, i, tag,
|
272 |
MPI_COMM_WORLD, &RequestRecv2); |
273 |
#endif
|
274 |
MPI_Wait(&RequestRecv2,&Stat); |
275 |
printf("\tReceive %lu inside from rank %i\n",(unsigned long)insideMPI[i],i); |
276 |
insides+=insideMPI[i]; |
277 |
} |
278 |
|
279 |
pi=4.*(float)insides/(float)((iterations/numtasks)*numtasks); |
280 |
|
281 |
printf("\n\tPi=%.40f\n\twith error %.40f\n\twith %lld iterations\n\n",pi,
|
282 |
fabs(pi-4*atan(1.))/pi,(long long)iterations); |
283 |
|
284 |
} |
285 |
else
|
286 |
{ |
287 |
// Receive information from master
|
288 |
|
289 |
node recv; |
290 |
|
291 |
rc = MPI_Irecv(&recv, 1, mpi_node_type, 0, tag, MPI_COMM_WORLD, &RequestRecv); |
292 |
MPI_Wait(&RequestRecv,&Stat); |
293 |
|
294 |
printf("\t(%s,%i) receive from master %lld with %i process\n",
|
295 |
hostname,rank,(long long)recv.iterations,recv.process); |
296 |
|
297 |
#ifdef TIME
|
298 |
gettimeofday(&start,(struct timezone *)0); |
299 |
#endif
|
300 |
|
301 |
#pragma omp parallel for |
302 |
for (int i=0 ; i<recv.process; i++) { |
303 |
insideOpenMP[i]=MainLoopGlobal(recv.iterations,rotr(seed_w,rank+process),rotl(seed_z,rank+process)); |
304 |
printf("\t(%s,%i) found %lld for process %i\n",hostname,rank,
|
305 |
(long long)insideOpenMP[i],i); |
306 |
} |
307 |
printf("\n");
|
308 |
|
309 |
part_inside=0;
|
310 |
for (int i=0 ; i<recv.process; i++) { |
311 |
part_inside+=insideOpenMP[i]; |
312 |
} |
313 |
|
314 |
#ifdef TIME
|
315 |
gettimeofday(&end,(struct timezone *)0); |
316 |
useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
|
317 |
|
318 |
printf("\tOn %s rank %i find %lld inside in %lu useconds.\n",
|
319 |
hostname,rank,(long long)part_inside,useconds); |
320 |
#else
|
321 |
printf("\tOn %s rank %i find %lld inside\n",hostname,rank,
|
322 |
(long long)part_inside); |
323 |
|
324 |
#endif
|
325 |
|
326 |
#ifdef LONG
|
327 |
rc = MPI_Isend(&part_inside, 1, MPI_LONG_LONG, 0, tag, MPI_COMM_WORLD, &RequestSend2); |
328 |
#else
|
329 |
rc = MPI_Isend(&part_inside, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &RequestSend2); |
330 |
#endif
|
331 |
|
332 |
MPI_Wait(&RequestSend2, &Stat); |
333 |
} |
334 |
|
335 |
MPI_Type_free(&mpi_node_type); |
336 |
|
337 |
MPI_Finalize(); |
338 |
|
339 |
} |