12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

616 appendix d}for ( i =0; i < avgwidth ; i++ ) fprintf (out ,"%f\n" ,x[2][ i ]) ;for ( i =1; i < numprocs−1; i++ ) {MPI_Recv ( results , avgwidth , MPI_DOUBLE, i , 3 , MPI_COMM_WORLD, &status ) ;for ( j = 0; j < avgwidth ; j ++) fprintf (out , "%f\n" ,results [ j ]) ;}MPI_Recv ( results , maxwidth , MPI_DOUBLE, numprocs−1, 3 , MPI_COMM_WORLD, &status ) ;for ( j =0; j < maxwidth ; j ++ ) fprintf (out ,"%f\n" ,results [ j ]) ;fprintf (out ,"%f\n" ,0.0) ;/ / R edgefprintf (out ,"\n") ; / / Empty line for gnuplot}}}if ( myrank == 0)printf ("Data stored in eqstringmpi .dat\nComputation time : %f s\n" ,MPI_Wtime ( )−startwtime ) ;MPI_Finalize () ;exit (0) ;✝double init_string (int index ) {if ( index < (L−1)∗4/5) return 1.0∗ index /((L−1)∗4/5) ;return 1.0∗ (L−1−index ) /((L−1)−(L−1)∗4/5) ;/ / Half of a sine wave}Listing D.8 MPIstring.c solves the wave equation for a string using several processors via MPIcommands.Notice how the MPI_Recv() and MPI_Send() commands require a pointer astheir first argument, or an array element. When sending more than one element ofan array to MPI_Send(), send a pointer to the first element of the array as the firstargument, and then the number of elements as the second argument. Observe nearthe end of the program how the MPI_Send() call is used to send len+1elementsof the 2-D array x[][], starting with the element x[2][0]. MPI sends these elementsin the order in which they are stored in memory. In C, arrays are stored in rowmajororder with the first element of the second row immediately following thelast element of the first row, and so on. Accordingly, this MPI_Send() call sends len+1elements of row 2, starting with column 0, which means all of row 2. If we hadspecified len+5elements instead, MPI would have sent all of row 2 plus the firstfour elements of row 3.In MPIstring.c, the calculated future position of the string is stored in row 2 ofx[3][len + 1], with different sections of the string stored in different columns. Row1 stores the present positions, and row 0 stores the past positions. This is differentfrom the column-based algorithm used in the serial program eqstring.c, followingthe original Fortran program, which was column-, rather than row-based. Thiswas changed because MPI reads data by rows. The initial displacement of thestring is given by the user-supplied function init_string(). Because the first timestep requires only the initial displacement, no message passing is necessary. Forlater times, each node sends the displacement of its rightmost point to the nodewith the next highest rank. This means that the rightmost node (rank = numprocs−101<strong>COPYRIGHT</strong> <strong>2008</strong>, PRINCET O N UNIVE R S I T Y P R E S SEVALUATION COPY ONLY. NOT FOR USE IN COURSES.ALLpup_06.04 — <strong>2008</strong>/2/15 — Page 616

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!