Révision 247 Pi/C/MPI/Pi_aMPI.c
Pi_aMPI.c (revision 247) | ||
---|---|---|
1 | 1 |
// |
2 | 2 |
// Estimation of Pi using Monte Carlo exploration process |
3 |
// Cecill v2 Emmanuel QUEMENER <emmanuel.quemener@gmail.com> |
|
3 | 4 |
// gcc -std=c99 -O3 -o Pi_aMPI Pi_aMPI.c -lm |
4 |
// Emmanuel Quemener <emmanuel.quemener@ens-lyon.fr> |
|
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 |
... | ... | |
145 | 144 |
|
146 | 145 |
unsigned int seed_z=362436069,seed_w=52128862; |
147 | 146 |
LENGTH iterations=ITERATIONS,inside[8192],insides,part_inside,part_iterations; |
148 |
int numtasks,rank,rc,tag=1,i; |
|
149 |
float pi; |
|
147 |
int NumberProcesses,rank,rc,tag=1,i; |
|
150 | 148 |
|
151 | 149 |
char hostname[128]; |
152 | 150 |
|
... | ... | |
166 | 164 |
MPI_Abort(MPI_COMM_WORLD, rc); |
167 | 165 |
} |
168 | 166 |
|
169 |
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
|
|
167 |
MPI_Comm_size(MPI_COMM_WORLD,&NumberProcesses);
|
|
170 | 168 |
MPI_Comm_rank(MPI_COMM_WORLD,&rank); |
171 | 169 |
|
172 |
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
|
|
170 |
MPI_Comm_size(MPI_COMM_WORLD,&NumberProcesses);
|
|
173 | 171 |
MPI_Comm_rank(MPI_COMM_WORLD,&rank); |
174 | 172 |
|
175 | 173 |
const int nitems=2; |
... | ... | |
191 | 189 |
MPI_Type_commit(&mpi_result_type); |
192 | 190 |
|
193 | 191 |
if (rank==0) { |
192 |
|
|
193 |
struct timeval tv1,tv2; |
|
194 | 194 |
|
195 | 195 |
if (argc > 1) { |
196 | 196 |
iterations=(LENGTH)atoll(argv[1]); |
... | ... | |
206 | 206 |
|
207 | 207 |
printf ("\tMax int = %u\n", INT_MAX); |
208 | 208 |
printf ("\tMax long = %ld\n", LONG_MAX); |
209 |
printf ("\tMax long long = %lld\n", LLONG_MAX); |
|
209 |
printf ("\tMax long long = %lld\n\n", LLONG_MAX);
|
|
210 | 210 |
|
211 |
part_iterations=((iterations%numtasks) == 0) ? iterations/numtasks:iterations/numtasks+1 ; |
|
211 |
part_iterations=((iterations%NumberProcesses) == 0) ? iterations/NumberProcesses:iterations/NumberProcesses+1 ; |
|
212 |
|
|
213 |
gettimeofday(&tv1, NULL); |
|
212 | 214 |
|
213 | 215 |
// Split part of code |
214 |
for (i=1;i<numtasks;i++) {
|
|
216 |
for (i=1;i<NumberProcesses;i++) {
|
|
215 | 217 |
|
216 | 218 |
#ifdef LONG |
217 | 219 |
rc = MPI_Isend(&part_iterations, 1, MPI_LONG_LONG, i, tag, |
... | ... | |
221 | 223 |
MPI_COMM_WORLD,&RequestSend); |
222 | 224 |
#endif |
223 | 225 |
} |
224 |
if (numtasks>1) {
|
|
226 |
if (NumberProcesses>1) {
|
|
225 | 227 |
MPI_Wait(&RequestSend, &Stat); |
226 | 228 |
} |
227 | 229 |
|
... | ... | |
236 | 238 |
hostname,rank,(long long)insides,useconds); |
237 | 239 |
|
238 | 240 |
// Join part of code |
239 |
for (i=1;i<numtasks;i++) {
|
|
241 |
for (i=1;i<NumberProcesses;i++) {
|
|
240 | 242 |
|
241 | 243 |
result recv; |
242 | 244 |
|
243 | 245 |
rc = MPI_Irecv(&recv, 1, mpi_result_type, i, tag, |
244 | 246 |
MPI_COMM_WORLD, &RequestRecv2); |
245 | 247 |
|
246 |
if (numtasks>1) {
|
|
248 |
if (NumberProcesses>1) {
|
|
247 | 249 |
MPI_Wait(&RequestRecv2, &Stat); |
248 | 250 |
} |
249 | 251 |
|
... | ... | |
255 | 257 |
insides+=inside[i]; |
256 | 258 |
} |
257 | 259 |
|
258 |
pi=4.*(float)insides/(float)(part_iterations*numtasks);
|
|
260 |
gettimeofday(&tv2, NULL);
|
|
259 | 261 |
|
260 |
printf("\n\tPi=%.40f\n\twith error %.40f\n\twith %lld iterations\n\n",pi, |
|
261 |
fabs(pi-4*atan(1.))/pi,(long long)iterations); |
|
262 |
double elapsed=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L + |
|
263 |
(tv2.tv_usec-tv1.tv_usec))/1000000; |
|
264 |
|
|
265 |
double itops=(double)(part_iterations*NumberProcesses)/elapsed; |
|
266 |
|
|
267 |
printf("\nParallelRate %i\nElapsed Time %.2f\nItops %.0f\nLogItops %.2f\n",NumberProcesses,elapsed,itops,log10(itops)); |
|
262 | 268 |
|
269 |
LENGTH total=((iterations%NumberProcesses)==0)?iterations:(iterations/NumberProcesses+1)*NumberProcesses; |
|
270 |
|
|
271 |
printf("Inside/Total %ld %ld\nPi estimation %f\n\n",(long int)insides,(long int)total,(4.*(float)insides/total)); |
|
272 |
|
|
263 | 273 |
} |
264 | 274 |
else |
265 | 275 |
{ |
Formats disponibles : Unified diff