/* deadlock1.c SJ An example of poor communication practice */ #include #include /* a variable needeed for call, result not used */ static MPI_Status tmpstat; int main(int argc, char **argv) { int PID, /* process-ID */ P; /* number of processes */ /* own variables */ int v1 = 1, v2, other; /* keep these 3 calls */ MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &PID); MPI_Comm_size(MPI_COMM_WORLD, &P); /* actual code */ if (P == 2) { other = 1-PID; /* this DEADLOCKS if MPI_Send() is blocking */ MPI_Send(&v1, 1, MPI_INT, other, 0, MPI_COMM_WORLD); MPI_Recv(&v2, 1, MPI_INT, other, 0, MPI_COMM_WORLD, &tmpstat); printf("%d: sent:%d received:%d\n", PID, v1, v2); /* this DEADLOCKS as MPI_Ssend() is blocking */ /* MPI_Ssend(&v1, 1, MPI_INT, other, 0, MPI_COMM_WORLD); */ /* MPI_Recv(&v2, 1, MPI_INT, other, 0, MPI_COMM_WORLD, &tmpstat); */ /* printf("%d: sent:%d received:%d\n", PID, v1, v2); */ /* this would DEADLOCK ALWAYS as MPI_Recv is always blocking */ /* MPI_Recv(&v2, 1, MPI_INT, other, 0, MPI_COMM_WORLD, &tmpstat); */ /* MPI_Send(&v1, 1, MPI_INT, other, 0, MPI_COMM_WORLD); */ /* printf("%d: sent:%d received:%d\n", PID, v1, v2); */ } else { printf("This example works only for 2 processes\n"); } /* keep this call */ MPI_Finalize(); exit(0); }