簡體   English   中英

MPI程序掛在MPI_Recv

[英]MPI program hangs at MPI_Recv

我在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();
}

通過調用使用了4個進程

mpirun -n 4 ./a.out

運行時輸出

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

在此之后它就掛了。 這使我相信MPI_Recv存在問題,因為它說已發送至任務1 ...,但未收到。

您在不同的等級上使用了不同的tag 為了使消息匹配,兩個通信伙伴必須使用相同的標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM