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.

618 appendix dD.6 DeadlockIt is important to avoid deadlock when using the MPI_Send() and MPI_Recv() commands.Deadlock occurs when one or more nodes wait for a nonoccurring eventto take place. This can arise if each node waits to receive a message that is not sent.Compile and execute deadlock.c:> mpicc deadlock.c -o deadlock Compile> qsub run_mpi.sh deadlock ExecuteTake note of the job ID returned, which we will call xxxx. Wait a few seconds andthen look at the output of the program:> cat output/MPI_job-xxxx Examine outputThe output should list how many nodes (slots) were assigned to the job. Becauseall these nodes are now deadlocked, we have to cancel this job:> qdel xxxx Cancel deadlocked job> qdel all Alternate cancelThere are a number of ways to avoid deadlock. The program MPIstring.c usedthe function MPI_Sendrecv() to handle much of the message passing, and thisdoes not cause deadlock. It is possible to use MPI_Send() and MPI_Recv(), butyou should be careful to avoid deadlock, as we do in MPIdeadlock-fixed.c inListing D.6.✞☎/ / deadlock−fixed . c : MPI deadlock . c without deadlock by Phil Carter#include #include "mpi .h"#define MAXLEN 100main ( int argc , char ∗argv []) {int myrank , numprocs , torank , i ;char tosend [MAXLEN] , received [MAXLEN];MPI_Status status ;MPI_Init ( &argc , &argv ) ;MPI_Comm_rank ( MPI_COMM_WORLD, &myrank ) ;MPI_Comm_size ( MPI_COMM_WORLD, &numprocs ) ;if ( myrank == numprocs − 1 ) torank = 0;else torank = myrank + 1 ; / / Save string to send in tosend :sprintf ( tosend , "Message sent from node %d to node %d\n" , myrank , torank ) ;for ( i = 0; i < numprocs ; i++ ) {if ( myrank == i ) MPI_Send ( tosend , MAXLEN, MPI_CHAR, torank , i , MPI_COMM_WORLD) ;else if ( myrank == i +1 || ( i == numprocs − 1 && myrank == 0) )MPI_Recv ( received , MAXLEN, MPI_CHAR, i , i , MPI_COMM_WORLD, &status ) ;}printf ("%s" , received ) ; // Print string after successful receiveMPI_Finalize () ;exit (0) ;}✝−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 618

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

Saved successfully!

Ooh no, something went wrong!