【问题标题】:MPI Implimentation stalling after two passes两次通过后 MPI 实现停止
【发布时间】:2018-10-16 03:42:40
【问题描述】:

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

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);
  }
}

输出:

Potato: 44
Node 3 has the potato, Passing to node 2.
Node 2 has the potato, Passing to node 3.

【问题讨论】:

  • 第一个问题好!尝试在每个节点打印出potato 的值;可能会揭示一些东西。无论如何,它不会受到伤害。
  • 感谢您的建议!当我打印土豆的值时,它的行为符合预期。
  • 好 - 只是想排除愚蠢的问题。
  • 欢迎来到 SO !请注意,此类问题应包括minimal reproducible example

标签: c mpi


【解决方案1】:

您的代码缺少一些循环。在您的示例中,要让节点 3 第二次接收到 patato,必须再次调用 MPI_Recv

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);

    }
  }
}

【讨论】:

  • 严格来说,这是不正确的 w.r.t。 MPI 标准,因为当游戏结束时,MPI_Send() to self 在发布 recv 之前被调用,这有可能导致死锁。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多