/* * Test Problem * * Program Name EUIprob1.c * * This program evaluates the trapezoidal rule estimate * for an integral of F(x). In this case, F(x) is exp(x) * evaluated for the interval of 0 to 1. Each case provides * 2X increase in precision. Therefore each result should be * similar but, not the same. * * Testing for Nps 1 ... 11 should execute programs. * Testing for Nps 12 and above should stop the program. * * This program requires the following call(s) * MPI_Init * MPI_Comm_rank * MPI_Comm_size * MPI_Abort * MPI_Finalize */ #include #include #include #include "mpi.h" #define KASES 11 #define F(x) (exp(x)) main (int argc, char **argv) { double TN[KASES], SUM, X, H, A = 0.0, B = 1.0; int nworkers, whoami, i, errcode, N[KASES] = {100, 200, 400, 800, 1600, 3200, 6400, 12800, 25600, 51200, 102400}; /* * Initialize environment */ MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &whoami); MPI_Comm_size(MPI_COMM_WORLD, &nworkers); printf("Envirionment allocated %d workers out of %d\n",nworkers,KASES); if (whoami > KASES) MPI_Abort(MPI_COMM_WORLD, errcode); whoami = whoami + 1; H = (B-A)/(N[whoami]); SUM = 0.0; for (i = 1; i <= N[whoami]-1; i++) SUM = SUM+F(A+H*i); TN[whoami] = H*((F(A)+F(B))/2.+SUM); printf("PROBLEM 1 WITH A,B,whoami,N = %10.3f,%10.3f,%d,%d\n",A,B, whoami,N[whoami]); printf("GIVES TN = %10.6f\n",TN[whoami]); MPI_Finalize(); }