简体   繁体   English

MPI程序挂在MPI_Recv

[英]MPI program hangs at MPI_Recv

I got the above mentioned error on running the following program in C. It uses the MPI library. 我在C中运行以下程序时遇到上述错误。它使用MPI库。

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
  int numranks, rank, dest, tag, source, rc, count;
  char inmsg, outmsg='x';
  MPI_Status Stat;

  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD, &numranks);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  printf("Task %d starting...\n",rank);

  if (rank == 0) {
    if (numranks > 2) 
      printf("Numranks=%d. Only 2 needed. Ignoring extra...\n",numranks);
    dest = rank + 1;
    source = dest;
    tag = rank;
    rc = MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
    printf("Sent to task %d...\n",dest);
    rc = MPI_Recv(&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat);
    printf("Received from task %d...\n",source);
  }
  else if (rank == 1) {
    dest = rank - 1;
    source = dest;
    tag = rank;
    rc = MPI_Recv(&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat);
    printf("Received from task %d...\n",source);
    rc = MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
    printf("Sent to task %d...\n",dest);
}

if (rank < 2) {
    rc = MPI_Get_count(&Stat, MPI_CHAR, &count);
    printf("Task %d: Received %d char(s) from rank %d with tag %d \n",rank, count, Stat.MPI_SOURCE, Stat.MPI_TAG);
  }
MPI_Finalize();
}

Used 4 processes by calling 通过调用使用了4个进程

mpirun -n 4 ./a.out

Runtime output 运行时输出

Task 0 starting...
Numranks=4. Only 2 needed. Ignoring extra...
Task 2 starting...
Task 3 starting...
Sent to task 1...
Task 1 starting...

After this it just hangs. 在此之后它就挂了。 This leads me to believe that there is an issue with MPI_Recv because it says sent to task 1... but does not receive it. 这使我相信MPI_Recv存在问题,因为它说已发送至任务1 ...,但未收到。

You are using different tag s on the different ranks. 您在不同的等级上使用了不同的tag In order for the messages to match, you must use the same tag for both communication partners. 为了使消息匹配,两个通信伙伴必须使用相同的标签。

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

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