root / Pi / C / Hybrid / Pi_Hybrid.c @ 65
Historique | Voir | Annoter | Télécharger (6,51 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 MWCfp MWC * 2.328306435454494e-10f |
31 |
#define KISSfp KISS * 2.328306435454494e-10f |
32 |
|
33 |
#define ITERATIONS 1000000000 |
34 |
|
35 |
#define PROCESS 1 |
36 |
|
37 |
#ifdef LONG
|
38 |
#define LENGTH long long |
39 |
#else
|
40 |
#define LENGTH int |
41 |
#endif
|
42 |
|
43 |
typedef struct compute_node { |
44 |
LENGTH iterations; |
45 |
int process;
|
46 |
} node; |
47 |
|
48 |
unsigned int rotl(unsigned int value, int shift) { |
49 |
return (value << shift) | (value >> (sizeof(value) * CHAR_BIT - shift)); |
50 |
} |
51 |
|
52 |
unsigned int rotr(unsigned int value, int shift) { |
53 |
return (value >> shift) | (value << (sizeof(value) * CHAR_BIT - shift)); |
54 |
} |
55 |
|
56 |
LENGTH MainLoopGlobal(LENGTH iterations,unsigned int seed_w,unsigned int seed_z) |
57 |
{ |
58 |
unsigned int z=seed_z; |
59 |
unsigned int w=seed_w; |
60 |
|
61 |
LENGTH total=0;
|
62 |
|
63 |
for (LENGTH i=0;i<iterations;i++) { |
64 |
|
65 |
float x=MWCfp ;
|
66 |
float y=MWCfp ;
|
67 |
|
68 |
// Matching test
|
69 |
int inside=((x*x+y*y) < 1.0f) ? 1:0; |
70 |
total+=inside; |
71 |
} |
72 |
|
73 |
return(total);
|
74 |
|
75 |
} |
76 |
|
77 |
int main(int argc, char *argv[]) { |
78 |
|
79 |
unsigned int seed_z=362436069,seed_w=52128862,process=PROCESS; |
80 |
// Number of NP or OpenMP processes <1024
|
81 |
LENGTH iterations=ITERATIONS,insideMPI[1024],insideOpenMP[1024], |
82 |
part_inside,part_iterations,insides; |
83 |
int numtasks,rank,rc,tag=1,i; |
84 |
float pi;
|
85 |
|
86 |
// Hostname supposed to be <128 characters
|
87 |
char hostname[128]; |
88 |
|
89 |
gethostname(hostname, sizeof hostname);
|
90 |
|
91 |
#ifdef TIME
|
92 |
struct timeval start,end;
|
93 |
long int useconds; |
94 |
#endif
|
95 |
|
96 |
MPI_Status Stat; |
97 |
|
98 |
rc = MPI_Init(&argc,&argv); |
99 |
if (rc != MPI_SUCCESS) {
|
100 |
printf ("Error starting MPI program. Terminating.\n");
|
101 |
MPI_Abort(MPI_COMM_WORLD, rc); |
102 |
} |
103 |
|
104 |
MPI_Comm_size(MPI_COMM_WORLD,&numtasks); |
105 |
|
106 |
const int nitems=2; |
107 |
int blocklengths[2] = {1,1}; |
108 |
|
109 |
#ifdef LONG
|
110 |
MPI_Datatype types[2] = {MPI_LONG, MPI_INT};
|
111 |
#else
|
112 |
MPI_Datatype types[2] = {MPI_INT, MPI_INT};
|
113 |
#endif
|
114 |
|
115 |
MPI_Datatype mpi_node_type; |
116 |
MPI_Aint offsets[2];
|
117 |
|
118 |
offsets[0] = offsetof(node, iterations);
|
119 |
offsets[1] = offsetof(node, process);
|
120 |
|
121 |
MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_node_type); |
122 |
MPI_Type_commit(&mpi_node_type); |
123 |
|
124 |
MPI_Comm_rank(MPI_COMM_WORLD,&rank); |
125 |
|
126 |
if (rank==0) { |
127 |
|
128 |
if (argc > 1) { |
129 |
iterations=(LENGTH)atoll(argv[1]);
|
130 |
process=atoi(argv[2]);
|
131 |
} |
132 |
else {
|
133 |
printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
|
134 |
printf("\t\t#1 : number of iterations (default 1 billion)\n\n");
|
135 |
printf("\t\t#1 : number of OpenMP processes (default 1)\n\n");
|
136 |
} |
137 |
|
138 |
printf ("\n\tInformation about architecture:\n\n");
|
139 |
|
140 |
printf ("Sizeof int = %lld bytes.\n", (long long)sizeof(int)); |
141 |
printf ("Sizeof long = %lld bytes.\n", (long long)sizeof(long)); |
142 |
printf ("Sizeof long long = %lld bytes.\n", (long long)sizeof(long long)); |
143 |
|
144 |
printf ("Max int = %u\n", INT_MAX);
|
145 |
printf ("Max long = %ld\n", LONG_MAX);
|
146 |
printf ("Max long long = %lld\n\n", LLONG_MAX);
|
147 |
|
148 |
part_iterations=iterations/(numtasks*process)+1;
|
149 |
|
150 |
node send; |
151 |
send.iterations=part_iterations; |
152 |
send.process=process; |
153 |
|
154 |
// Split part of code
|
155 |
for (i=1;i<numtasks;i++) { |
156 |
|
157 |
rc = MPI_Send(&send, 1, mpi_node_type, i, tag, MPI_COMM_WORLD);
|
158 |
} |
159 |
|
160 |
#ifdef TIME
|
161 |
gettimeofday(&start,(struct timezone *)0); |
162 |
#endif
|
163 |
|
164 |
#pragma omp parallel for |
165 |
for (int i=0 ; i<process; i++) { |
166 |
insideOpenMP[i]=MainLoopGlobal(part_iterations, |
167 |
rotr(seed_w,process), |
168 |
rotl(seed_z,process)); |
169 |
printf("\t(%s,%i) found %lld for process %i\n",hostname,0, |
170 |
(long long)insideOpenMP[i],i); |
171 |
} |
172 |
printf("\n");
|
173 |
|
174 |
insides=0;
|
175 |
for (int i=0 ; i<process; i++) { |
176 |
insides+=insideOpenMP[i]; |
177 |
} |
178 |
|
179 |
#ifdef TIME
|
180 |
gettimeofday(&end,(struct timezone *)0); |
181 |
useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
|
182 |
|
183 |
printf("\tOn %s with %i find %lld inside in %lu useconds.\n",
|
184 |
hostname,rank,(long long)insides,useconds); |
185 |
#else
|
186 |
printf("\tOn %s with %i find %lld inside\n",hostname,rank,
|
187 |
(long long)insides); |
188 |
|
189 |
#endif
|
190 |
|
191 |
// Join part of code
|
192 |
for (i=1;i<numtasks;i++) { |
193 |
#ifdef LONG
|
194 |
rc = MPI_Recv(&insideMPI[i], 1, MPI_LONG_LONG, i, tag,
|
195 |
MPI_COMM_WORLD, &Stat); |
196 |
#else
|
197 |
rc = MPI_Recv(&insideMPI[i], 1, MPI_INT, i, tag,
|
198 |
MPI_COMM_WORLD, &Stat); |
199 |
#endif
|
200 |
printf("\tReceive %lu inside from rank %i\n",(unsigned long)insideMPI[i],i); |
201 |
insides+=insideMPI[i]; |
202 |
} |
203 |
|
204 |
pi=4.*(float)insides/(float)((iterations/numtasks)*numtasks); |
205 |
|
206 |
printf("\n\tPi=%.40f\n\twith error %.40f\n\twith %lld iterations\n\n",pi,
|
207 |
fabs(pi-4*atan(1.))/pi,(long long)iterations); |
208 |
|
209 |
} |
210 |
else
|
211 |
{ |
212 |
// Receive information from master
|
213 |
|
214 |
node recv; |
215 |
|
216 |
rc = MPI_Recv(&recv, 1, mpi_node_type, 0, tag, MPI_COMM_WORLD, &Stat); |
217 |
|
218 |
printf("\t(%s,%i) receive from master %lld with %i process\n",
|
219 |
hostname,rank,(long long)recv.iterations,recv.process); |
220 |
|
221 |
#ifdef TIME
|
222 |
gettimeofday(&start,(struct timezone *)0); |
223 |
#endif
|
224 |
|
225 |
#pragma omp parallel for |
226 |
for (int i=0 ; i<recv.process; i++) { |
227 |
insideOpenMP[i]=MainLoopGlobal(recv.iterations,rotr(seed_w,rank+process),rotl(seed_z,rank+process)); |
228 |
printf("\t(%s,%i) found %lld for process %i\n",hostname,rank,
|
229 |
(long long)insideOpenMP[i],i); |
230 |
} |
231 |
printf("\n");
|
232 |
|
233 |
part_inside=0;
|
234 |
for (int i=0 ; i<recv.process; i++) { |
235 |
part_inside+=insideOpenMP[i]; |
236 |
} |
237 |
|
238 |
#ifdef TIME
|
239 |
gettimeofday(&end,(struct timezone *)0); |
240 |
useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
|
241 |
|
242 |
printf("\tOn %s rank %i find %lld inside in %lu useconds.\n",
|
243 |
hostname,rank,(long long)part_inside,useconds); |
244 |
#else
|
245 |
printf("\tOn %s rank %i find %lld inside\n",hostname,rank,
|
246 |
(long long)part_inside); |
247 |
|
248 |
#endif
|
249 |
|
250 |
#ifdef LONG
|
251 |
rc = MPI_Send(&part_inside, 1, MPI_LONG_LONG, 0, tag, MPI_COMM_WORLD); |
252 |
#else
|
253 |
rc = MPI_Send(&part_inside, 1, MPI_INT, 0, tag, MPI_COMM_WORLD); |
254 |
#endif
|
255 |
|
256 |
} |
257 |
|
258 |
MPI_Type_free(&mpi_node_type); |
259 |
|
260 |
MPI_Finalize(); |
261 |
|
262 |
} |