首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MPI程序在MPI_Recv挂起

MPI程序在MPI_Recv挂起
EN

Stack Overflow用户
提问于 2018-07-26 20:52:24
回答 1查看 236关注 0票数 1

在C中运行下面的程序时,我得到了上面提到的错误。它使用MPI库。

代码语言:javascript
复制
#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个进程

代码语言:javascript
复制
mpirun -n 4 ./a.out

运行时输出

代码语言:javascript
复制
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……但没有收到它。

EN

回答 1

Stack Overflow用户

发布于 2018-07-26 20:56:12

您在不同的级别上使用不同的tag。为了使消息匹配,您必须对两个通信伙伴使用相同的标记。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51539308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档