简体   繁体   English

MPI C程序在MPI_Recv / MPI_Send期间挂起

[英]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. 我刚刚开始学习C语言中的MPI编程,并且正处于作业的中间,要求我让我的进程将其消息发送到排名更高的下一个进程,最后一个进程将其消息发送回到进程0。我已经开始进行测试,但是即使使用了这些简单的代码,该程序仍然挂起。 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 . 问题确实是您的所有进程都在MPI_RecvMPI_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 ). 首先,除主my_rank==0之外的所有进程都等待消息,而进程0继续直接将消息发送给其邻居(等级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 . 然后,这释放了进程1 (在MPI_Recv被阻止),该MPI_Recv又继续将消息发送到进程2等。最后,进程0从最后一个进程p-1接收消息。

See, eg, this tutorial (section Ring program). 参见,例如,本教程 (Ring程序一节)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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