00001
00002
00003
00004
00005 #include "mpi.h"
00006 #include <stdio.h>
00007 #include <math.h>
00008 #include <iostream>
00009
00010 using namespace std;
00011
00012 extern char** environ;
00013
00014 double f(double);
00015
00016 double f(double a)
00017 {
00018 return (4.0 / (1.0 + a * a));
00019 }
00020
00021 int main(int argc, char* argv[])
00022 {
00023
00024
00025 cout << "BooA" << endl;
00026
00027 int done = 0, n, myid, numprocs, i;
00028 double PI25DT = 3.141592653589793238462643;
00029 double mypi, pi, h, sum, x;
00030 double startwtime = 0.0, endwtime;
00031 int namelen;
00032 char processor_name[MPI_MAX_PROCESSOR_NAME];
00033
00034 for (int i = 0; i < argc; ++i)
00035 cout << "argv[" << i << "] = " << argv[i]
00036 << endl;
00037
00038 MPI_Init(&argc, &argv);
00039 MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
00040 MPI_Comm_rank(MPI_COMM_WORLD, &myid);
00041 MPI_Get_processor_name(processor_name, &namelen);
00042
00043 fprintf(stderr, "Process %d on %s\n",
00044 myid, processor_name);
00045
00046 n = 0;
00047 while (!done)
00048 {
00049 if (myid == 0)
00050 {
00051 if (n == 0) n = 100;
00052 else n = 0;
00053
00054 startwtime = MPI_Wtime();
00055 }
00056 MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
00057 if (n == 0)
00058 done = 1;
00059 else
00060 {
00061 h = 1.0 / (double) n;
00062 sum = 0.0;
00063 for (i = myid + 1; i <= n; i += numprocs)
00064 {
00065 x = h * ((double)i - 0.5);
00066 sum += f(x);
00067 }
00068 mypi = h * sum;
00069
00070 MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
00071
00072 if (myid == 0)
00073 {
00074 printf("pi is approximately %.16f, Error is %.16f\n",
00075 pi, fabs(pi - PI25DT));
00076 endwtime = MPI_Wtime();
00077 printf("wall clock time = %f\n",
00078 endwtime - startwtime);
00079 }
00080 }
00081 }
00082 MPI_Finalize();
00083 return 0;
00084 }