/* * The proc 0 and proc 1 send to proc 2 separately, * proc 2 call MPI_PROBE to know whether the messages come, * if so, it calls MPI_RECV to receive the messages * Compile: mpicc mpi_probe01.c -o mpi_probe01 * Run: mpirun -np 3 mpi_probe01 */ #include //for input/output #include //for mpi routines main( int argc, char** argv) { int rank; //the rank of this process int tag=0; //not important here int n, i[1]; float x[1]; MPI_Status status; //not important here MPI_Init(&argc, &argv); //Initializing mpi MPI_Comm_rank(MPI_COMM_WORLD, &rank); //Getting my rank if( rank == 0 ) { printf("Proc 0: Sleep for 5 sec before send...\n"); sleep(5); printf("Proc 0: Sending message...\n"); MPI_Send(i, 1, MPI_INT, 2, tag, MPI_COMM_WORLD); } else if( rank == 1) { printf("Proc 1: Sleep for 3 sec before send...\n"); sleep(3); printf("Proc 1: Sending message...\n"); MPI_Send(x, 1, MPI_FLOAT, 2, tag, MPI_COMM_WORLD); } else //rank == 2 { for (n = 1; n <= 2; n ++) { printf("Proc 2: Wait for message comes...\n"); MPI_Probe(MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status); printf("Proc 2: New message...\n"); if (status.MPI_SOURCE == 0) { MPI_Recv(i, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status); } else { MPI_Recv(x, 1, MPI_FLOAT, 1, tag, MPI_COMM_WORLD, &status); } printf("Proc 2: Received message from Proc %d successfully!!\n", status.MPI_SOURCE); } } MPI_Finalize(); //Finalize MPI return 0; }