Révision 247 Pi/C/MPI/Pi_MPI.c
Pi_MPI.c (revision 247) | ||
---|---|---|
1 | 1 |
// |
2 | 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>
|
|
3 |
// Cecill v2 Emmanuel QUEMENER <emmanuel.quemener@gmail.com>
|
|
4 |
// gcc -std=c99 -O3 -o Pi_MPI Pi_MPI.c -lm
|
|
5 | 5 |
|
6 | 6 |
// Needed for gethostname |
7 |
#define _BSD_SOURCE
|
|
7 |
#define _DEFAULT_SOURCE
|
|
8 | 8 |
#include <sys/unistd.h> |
9 | 9 |
|
10 | 10 |
#include <math.h> |
... | ... | |
13 | 13 |
#include <limits.h> |
14 | 14 |
#include <mpi.h> |
15 | 15 |
#include <stddef.h> |
16 |
|
|
17 | 16 |
#include <sys/time.h> |
18 | 17 |
|
19 | 18 |
// Marsaglia RNG very simple implementation |
... | ... | |
144 | 143 |
|
145 | 144 |
unsigned int seed_z=362436069,seed_w=52128862; |
146 | 145 |
LENGTH iterations=ITERATIONS,inside[8192],insides,part_inside,part_iterations; |
147 |
int numtasks,rank,rc,tag=1,i; |
|
148 |
float pi; |
|
146 |
int NumberProcesses,rank,rc,tag=1,i; |
|
149 | 147 |
|
150 | 148 |
char hostname[128]; |
151 | 149 |
|
... | ... | |
162 | 160 |
MPI_Abort(MPI_COMM_WORLD, rc); |
163 | 161 |
} |
164 | 162 |
|
165 |
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
|
|
163 |
MPI_Comm_size(MPI_COMM_WORLD,&NumberProcesses);
|
|
166 | 164 |
MPI_Comm_rank(MPI_COMM_WORLD,&rank); |
167 | 165 |
|
168 | 166 |
const int nitems=2; |
... | ... | |
184 | 182 |
MPI_Type_commit(&mpi_result_type); |
185 | 183 |
|
186 | 184 |
if (rank==0) { |
187 |
|
|
185 |
|
|
186 |
struct timeval tv1,tv2; |
|
187 |
|
|
188 | 188 |
if (argc > 1) { |
189 | 189 |
iterations=(LENGTH)atoll(argv[1]); |
190 | 190 |
} |
... | ... | |
201 | 201 |
printf ("\tMax long = %ld\n", LONG_MAX); |
202 | 202 |
printf ("\tMax long long = %lld\n\n", LLONG_MAX); |
203 | 203 |
|
204 |
part_iterations=((iterations%numtasks) == 0) ? iterations/numtasks:iterations/numtasks+1 ; |
|
204 |
part_iterations=((iterations%NumberProcesses) == 0) ? iterations/NumberProcesses:iterations/NumberProcesses+1 ; |
|
205 |
|
|
206 |
gettimeofday(&tv1, NULL); |
|
205 | 207 |
|
206 | 208 |
// Split part of code |
207 |
for (i=1;i<numtasks;i++) {
|
|
209 |
for (i=1;i<NumberProcesses;i++) {
|
|
208 | 210 |
|
209 | 211 |
#ifdef LONG |
210 | 212 |
rc = MPI_Send(&part_iterations, 1, MPI_LONG_LONG, i, tag, |
... | ... | |
226 | 228 |
hostname,rank,(long long)insides,useconds); |
227 | 229 |
|
228 | 230 |
// Join part of code |
229 |
for (i=1;i<numtasks;i++) {
|
|
231 |
for (i=1;i<NumberProcesses;i++) {
|
|
230 | 232 |
|
231 | 233 |
result recv; |
232 | 234 |
|
... | ... | |
241 | 243 |
insides+=inside[i]; |
242 | 244 |
} |
243 | 245 |
|
244 |
pi=4.*(float)insides/(float)(part_iterations*numtasks); |
|
246 |
float pi=4.*(float)insides/(float)(part_iterations*NumberProcesses); |
|
247 |
|
|
248 |
gettimeofday(&tv2, NULL); |
|
245 | 249 |
|
246 |
printf("\n\tPi=%.40f\n\twith error %.40f\n\twith %lld iterations\n\n",pi, |
|
247 |
fabs(pi-4*atan(1.))/pi,(long long)iterations); |
|
250 |
double elapsed=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L + |
|
251 |
(tv2.tv_usec-tv1.tv_usec))/1000000; |
|
252 |
|
|
253 |
double itops=(double)(part_iterations*NumberProcesses)/elapsed; |
|
254 |
|
|
255 |
printf("\nParallelRate %i\nElapsed Time %.2f\nItops %.0f\nLogItops %.2f\n",NumberProcesses,elapsed,itops,log10(itops)); |
|
248 | 256 |
|
257 |
LENGTH total=((iterations%NumberProcesses)==0)?iterations:(iterations/NumberProcesses+1)*NumberProcesses; |
|
258 |
|
|
259 |
printf("Inside/Total %ld %ld\nPi estimation %f\n\n",(long int)insides,(long int)total,(4.*(float)insides/total)); |
|
260 |
|
|
249 | 261 |
} |
250 | 262 |
else |
251 | 263 |
{ |
... | ... | |
284 | 296 |
|
285 | 297 |
MPI_Type_free(&mpi_result_type); |
286 | 298 |
MPI_Finalize(); |
299 |
|
|
287 | 300 |
} |
Formats disponibles : Unified diff