MPI死锁 [英] Deadlock with MPI

查看:297
本文介绍了MPI死锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试MPI,想知道这段代码是否会导致死锁.

I'm experimenting with MPI and was wondering if this code could cause a deadlock.

MPI_Comm_rank (comm, &my_rank);
if (my_rank == 0) {
   MPI_Send (sendbuf, count, MPI_INT, 1, tag, comm);
   MPI_Recv (recvbuf, count, MPI_INT, 1, tag, comm, &status);
} else if (my_rank == 1) {
   MPI_Send (sendbuf, count, MPI_INT, 0, tag, comm);
   MPI_Recv (recvbuf, count, MPI_INT, 0, tag, comm, &status);
}

推荐答案

MPI_Send可能阻止也可能不会阻止.它将一直阻塞,直到发送方可以重用发送方缓冲区为止.当缓冲区已发送到较低的通信层时,某些实现将返回给调用方.当另一端有匹配的MPI_Recv()时,其他一些将返回到调用方.因此,该程序是否会死锁取决于您的MPI实现.

MPI_Send may or may not block. It will block until the sender can reuse the sender buffer. Some implementations will return to the caller when the buffer has been sent to a lower communication layer. Some others will return to the caller when there's a matching MPI_Recv() at the other end. So it's up to your MPI implementation whether if this program will deadlock or not.

由于该程序在不同的MPI实现中的行为有所不同,因此您可以考虑重新编写它,以免出现死锁:

Because of this program behaves differently among different MPI implementations, you may consider rewritting it so there won't be possible deadlocks:

MPI_Comm_rank (comm, &my_rank);
if (my_rank == 0) {
   MPI_Send (sendbuf, count, MPI_INT, 1, tag, comm);
   MPI_Recv (recvbuf, count, MPI_INT, 1, tag, comm, &status);
} else if (my_rank == 1) {
   MPI_Recv (recvbuf, count, MPI_INT, 0, tag, comm, &status);
   MPI_Send (sendbuf, count, MPI_INT, 0, tag, comm);
}

始终请注意,对于每个MPI_Send(),都必须有一个配对的MPI_Recv(),它们在时间上都是平行的".例如,这可能以死锁结束,因为成对的发送/接收呼叫未及时对齐.他们彼此交叉:

Always be aware that for every MPI_Send() there must be a pairing MPI_Recv(), both "parallel" in time. For example, this may end in deadlock because pairing send/recv calls are not aligned in time. They cross each other:

RANK 0                          RANK 1
----------                      -------
MPI_Send() ---            ----  MPI_Send()    |
              ---      ---                    |
                 ------                       |
                   --                         | TIME
                 ------                       |
              ---      ---                    |
MPI_Recv() <--            --->  MPI_Recv()    v

另一方面,这些进程不会死锁,当然,前提是在同一通信器域中确实有两个进程的等级为0和1.

These processes, on the other way, won't end in deadlock, provided of course, that there are indeed two processes with ranks 0 and 1 in the same communicator domain.

RANK 0                          RANK 1
----------                      -------
MPI_Send() ------------------>  MPI_Recv()   |
                                             | TIME
                                             |
MPI_Recv() <------------------  MPI_Send()   v

如果传播器com的大小不允许等级1(仅0),则上述固定程序可能会失败.这样,if-else不会采用else路线,因此,没有进程将在监听MPI_Send()并且等级0将死锁.

The above fixed program may fail if the size of the communicator com does not allow rank 1 (only 0). That way, the if-else won't take the else route and thus, no process will be listening for the MPI_Send() and rank 0 will deadlock.

如果需要使用当前的通信布局,则可能更喜欢使用MPI_Isend()MPI_Issend()来进行非阻塞发送,从而避免死锁.

If you need to use your current communication layout, then you may prefer to use MPI_Isend() or MPI_Issend() instead for nonblocking sends, thus avoiding deadlock.

这篇关于MPI死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆