简体   繁体   中英

MPI C Program Hangs During MPI_Recv/MPI_Send

I've just started learning MPI programming in C, and I'm in the middle of an assignment that's asking me to have my processes send their messages to the next higher ranked process, with the last process sending its message back to process 0. I've begun doing tests, but even with this simpler code, the program still hangs. It might be a problem with the last process sending a message back to process zero.

Here's my code:

#include <stdio.h>
#include <string.h>  /* For strlen             */
#include <mpi.h>     /* For MPI functions, etc */

const int MAX_STRING = 100;

int main(void) {
char       greeting[MAX_STRING];
int        my_rank, p;

/* Start up MPI */
MPI_Init(NULL, NULL);

/* Get the number of processes */
MPI_Comm_size(MPI_COMM_WORLD, &p);

/* Get my rank among all the processes */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

if (my_rank == 0) {
   MPI_Recv(greeting, MAX_STRING, MPI_CHAR, 2, 
      0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
   printf("%s\n", greeting);
   sprintf(greeting, "Greetings from Dank Meme 0");
   MPI_Send(greeting, strlen(greeting)+1, MPI_CHAR, my_rank+1, 0,
      MPI_COMM_WORLD);
} else if (my_rank == 1){
   MPI_Recv(greeting, MAX_STRING, MPI_CHAR, 0, 
         0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
   printf("%s\n", greeting);
   sprintf(greeting, "Greetings from Dank Meme 1");
   MPI_Send(greeting, strlen(greeting)+1, MPI_CHAR, 2, 0,
         MPI_COMM_WORLD);
} else if (my_rank == 2) {
   MPI_Recv(greeting, MAX_STRING, MPI_CHAR, 1, 
      0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
   printf("%s\n", greeting);
   sprintf(greeting, "Greetings from Dank Meme 2");
   MPI_Send(greeting, strlen(greeting)+1, MPI_CHAR, 0, 0,
      MPI_COMM_WORLD);      
}

/* Shut down MPI */
MPI_Finalize();
return 0;
}  /* main */

Thanks for the help!

The problem is indeed that all your processes block on MPI_Recv . To remedy this, you could proceed as:

initialize message
if( my_rank != 0 ){
  receive message from my_rank-1
}
send message to (my_rank + 1)%p
if( my_rank == 0 ){
  receive message from p - 1
}

First, all processes except the master my_rank==0 wait for the message, while process 0 continues directly to send the message to its neighbour (rank 1 ). This then releases process 1 (blocked in MPI_Recv ) which in turn proceeds to send the message to process 2 etc. Finally, process 0 receives the message from the last process p-1 .

See, eg, this tutorial (section Ring program).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM