297 | | ~~ ~~#include "mpi.h" |

298 | | ~~ ~~#include <stdio.h> |

299 | | ~~ ~~#include <math.h> |

300 | | |

301 | | ~~ ~~int main( int argc, char *argv[]) |

302 | | ~~ ~~{ |

303 | | ~~ ~~int done = 0, n, myid, numprocs, i; |

304 | | ~~ ~~double PI25DT = 3.141592653589793238462643; |

305 | | ~~ ~~double mypi, pi, h, sum, x; |

306 | | ~~ ~~double startwtime = 0.0, endwtime; |

307 | | ~~ ~~int namelen; |

308 | | ~~ ~~char processor_name[MPI_MAX_PROCESSOR_NAME]; |

309 | | |

310 | | ~~ ~~MPI_Init(&argc,&argv); |

311 | | ~~ ~~MPI_Comm_size(MPI_COMM_WORLD,&numprocs); |

312 | | ~~ ~~MPI_Comm_rank(MPI_COMM_WORLD,&myid); |

313 | | ~~ ~~MPI_Get_processor_name(processor_name,&namelen); |

314 | | |

315 | | ~~ ~~printf("Process %d on %s\n", myid, processor_name); |

316 | | |

317 | | ~~ ~~n = 100000000; |

318 | | |

319 | | ~~ ~~startwtime = MPI_Wtime(); |

320 | | |

321 | | ~~ ~~h = 1.0 / (double) n; |

322 | | ~~ ~~sum = 0.0; |

323 | | ~~ ~~for (i = myid + 1; i <= n; i += numprocs) |

324 | | ~~ ~~{ |

325 | | ~~ ~~x = h * ((double)i - 0.5); |

326 | | ~~ ~~sum += 4.0 / (1.0 + x*x); |

327 | | ~~ ~~} |

328 | | ~~ ~~mypi = h * sum; |

329 | | |

330 | | ~~ ~~MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); |

331 | | |

332 | | ~~ ~~if (myid == 0) |

333 | | ~~ ~~{ |

334 | | ~~ ~~printf("pi is approximately %.16f, Error is %.16f\n", |

335 | | ~~ ~~pi, fabs(pi - PI25DT)); |

336 | | ~~ ~~endwtime = MPI_Wtime(); |

337 | | ~~ ~~printf("wall clock time = %f\n", endwtime-startwtime); |

338 | | ~~ ~~} |

339 | | |

340 | | ~~ ~~MPI_Finalize(); |

341 | | |

342 | | ~~ ~~return 0; |

343 | | ~~ ~~} |

| 297 | #include "mpi.h" |

| 298 | #include <stdio.h> |

| 299 | #include <math.h> |

| 300 | |

| 301 | int main( int argc, char *argv[]) |

| 302 | { |

| 303 | int done = 0, n, myid, numprocs, i; |

| 304 | double PI25DT = 3.141592653589793238462643; |

| 305 | double mypi, pi, h, sum, x; |

| 306 | double startwtime = 0.0, endwtime; |

| 307 | int namelen; |

| 308 | char processor_name[MPI_MAX_PROCESSOR_NAME]; |

| 309 | |

| 310 | MPI_Init(&argc,&argv); |

| 311 | MPI_Comm_size(MPI_COMM_WORLD,&numprocs); |

| 312 | MPI_Comm_rank(MPI_COMM_WORLD,&myid); |

| 313 | MPI_Get_processor_name(processor_name,&namelen); |

| 314 | |

| 315 | printf("Process %d on %s\n", myid, processor_name); |

| 316 | |

| 317 | n = 100000000; |

| 318 | |

| 319 | startwtime = MPI_Wtime(); |

| 320 | |

| 321 | h = 1.0 / (double) n; |

| 322 | sum = 0.0; |

| 323 | for (i = myid + 1; i <= n; i += numprocs) |

| 324 | { |

| 325 | x = h * ((double)i - 0.5); |

| 326 | sum += 4.0 / (1.0 + x*x); |

| 327 | } |

| 328 | mypi = h * sum; |

| 329 | |

| 330 | MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); |

| 331 | |

| 332 | if (myid == 0) |

| 333 | { |

| 334 | printf("pi is approximately %.16f, Error is %.16f\n", |

| 335 | pi, fabs(pi - PI25DT)); |

| 336 | endwtime = MPI_Wtime(); |

| 337 | printf("wall clock time = %f\n", endwtime-startwtime); |

| 338 | } |

| 339 | |

| 340 | MPI_Finalize(); |

| 341 | |

| 342 | return 0; |

| 343 | } |