CS 596 MPI HW Due date : 4/26/00 ------------------------------------------------------------------- 1. Your homework is to develop a parallel version of the serial integration code handed out in class in either Fortan or C using MPI. 2. Compile and run the serial code (either Fortran or C) using hpc queue on ultra. Keep note of the integration result as well as the time it took to run this code. Even though this is a serial code I have put in MPI there so that you can use the MPI_Wtime(). Compile the serial code as follows : tmcc -fast -xarch=v9a -lmpi integrate.c tmf90 -fast -xarch=v9a -lmpi integrate.f 3. Start from the serial code handed out in class and develop a MPI code that will do the integration on arbitrary number of processors in parallel. 4. Run the parallel code on one processor. Next run the parallel code on 2 processors and 4 processors. 5. What you need to hand in: i) Hard copy of the serial and the parallel MPI codes ii) Timing results of the serial code on 1 processor, parallel MPI code on 1 processor, 2 processors, and 4 processors. ----------------------------------------------------------------------- /* This is a serial integration code and should be run on one processor You are going to develop a parallel MPI version of this code for your MPI HW I have put in the MPI routines so that you can measure time with MPI_Wtime */ #include #include #include #define DIVISIONS (int)1e9 #define MAXX (double)100. #define MINX (double)0. /*********************** The integration function ************************/ double f(x) double x; { return (x*x); } /*********************** main ************************/ main(argc, argv) int argc; char **argv; { int i; double x; double dx; double total; double average; double t0, t1; int npes, iam; MPI_Init(&argc, &argv) ; MPI_Comm_size(MPI_COMM_WORLD, &npes) ; MPI_Comm_rank(MPI_COMM_WORLD, &iam) ; printf("Running for maxx = %f, minx = %f,divisions=%d\n", MAXX,MINX,DIVISIONS); t0 = MPI_Wtime() ; /* slice size */ dx = (MAXX - MINX) / (double)DIVISIONS; /* integrate */ for (i=0 , total=0.0 ; i< DIVISIONS ; i++) { x = MINX + i * dx; average = ( f(x) + f(x+dx) ) / 2.0; total += (average * dx); } t1 = MPI_Wtime() ; printf("Time %f in sec\n", t1-t0) ; printf ("result of the integration from %f to %f is %f\n", (float)MINX, (float)MAXX, (float)total); MPI_Finalize(); } --------------------------------------------------------------- c This is a serial integration code c and should be run on one processor c c You are going to develop a parallel MPI c version of this code for your MPI HW c I have put the MPI routines so that c you can measure time with MPI_Wtime program integrate include 'mpif.h' parameter (DIVISIONS=1e9) double precision x double precision dx double precision total double precision average double precision MAXX double precision MINX double precision t0, t1 integer ierr, nPEs, iam call MPI_Init(ierr) call MPI_Comm_size(MPI_COMM_WORLD, nPEs, ierr) call MPI_Comm_rank(MPI_COMM_WORLD, iam, ierr) MAXX = 100.0 MINX = 0.0 write (*,*) "Running for maxx = ",MAXX,", minx = ",MINX, . "divisions=",DIVISIONS t0 = MPI_WTIME() c slice size dx = (MAXX - MINX) / DIVISIONS c integrate total=0.0 do i=0,DIVISIONS-1 x = MINX + i * dx average = ( f(x) + f(x+dx) ) / 2.0 total = total + (average * dx) enddo t1 = MPI_WTIME() write(*,*) ' Time to run=',t1-t0,'sec' write (*,*) "result of the integration from ",MINX," to ",MAXX," . is ",total call MPI_FINALIZE(ierr) end C***************************** C The integration function C***************************** function f(x) double precision x f = (x*x) return end