首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >MPI实现在两次传递后停止

MPI实现在两次传递后停止
EN

Stack Overflow用户
提问于 2018-10-16 01:40:21
回答 1查看 61关注 0票数 2

我对MPI比较陌生,所以我不确定为什么这段代码不能像预期的那样工作。这个想法是将一个整数传递给一个随机节点,然后递减它,直到它达到0。当我尝试运行它时,它传递了两次整数并停止。有人能给我指个方向吗?谢谢!

代码语言:javascript
复制
if (rank == 0)
{
  potato = rand() % 100 + size; // generate a random number between the number of processors and 100
  sendTo = rand() % (size - 1) + 1; // generate a number (not 0) to represent the process to send the potato to

  MPI_Send(&potato, 1, MPI_INT, sendTo, 0, MPI_COMM_WORLD); // send the potato
}

else // any process other than 0
{

  MPI_Recv(&potato, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); //receive potato

  if (potato == -1) // check for termination int
    return;

  --potato; // decrement potato

  if (potato != 0)
  {
    do
    {
      sendTo = rand() % (size - 1) + 1; // send to a process 1 through size - 1
    } while (sendTo == rank || sendTo == 0); // make sure it won't send the potato to itself or 0

    printf("Node %d has the potato, passing to node %d.\n", rank, sendTo);
    MPI_Send(&potato, 1, MPI_INT, sendTo, 0, MPI_COMM_WORLD);

  }

  else // potato == 0
  {
    printf("Node %d is it, game over.\n", rank);

    potato = -1;
    for (int i = 1; i < size; ++rank) // send termination message
      MPI_Send(&potato, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
  }
}

输出:

代码语言:javascript
复制
Potato: 44
Node 3 has the potato, Passing to node 2.
Node 2 has the potato, Passing to node 3.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-16 03:42:41

您的代码缺少某些循环。在您的示例中,为了让节点3第二次接收帕托,必须再次调用MPI_Recv

代码语言:javascript
复制
if (rank == 0)
{
  potato = rand() % 100 + size; // generate a random number between the number of processors and 100
  sendTo = rand() % (size - 1) + 1; // generate a number (not 0) to represent the process to send the potato to

  MPI_Send(&potato, 1, MPI_INT, sendTo, 0, MPI_COMM_WORLD); // send the potato
}

else // any process other than 0
{
  /* Here is the loop beginning, while patato is not -1, continue reading*/
  while (1)
  {
    MPI_Recv(&potato, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); //receive potato

    if (potato == -1) // check for termination int
      return;

    --potato; // decrement potato

    if (potato != 0)
    {
      do
      {
        sendTo = rand() % (size - 1) + 1; // send to a process 1 through size - 1
      } while (sendTo == rank || sendTo == 0); // make sure it won't send the potato to itself or 0

      printf("Node %d has the potato, passing to node %d.\n", rank, sendTo);
      MPI_Send(&potato, 1, MPI_INT, sendTo, 0, MPI_COMM_WORLD);

    }

    else // potato == 0
    {
      printf("Node %d is it, game over.\n", rank);

      potato = -1;
      for (int i = 1; i < size; ++rank) // send termination message
        MPI_Send(&potato, 1, MPI_INT, i, 0, MPI_COMM_WORLD);

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

https://stackoverflow.com/questions/52822089

复制
相关文章

相似问题

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