为什么这个与MPI_Bcast相关的代码不会死锁?

3
为什么这段MPI代码不会出现死锁?
int main(int argc, char *argv[])
{
        int rank,size;

        MPI_Init(&argc, &argv);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
        MPI_Comm_size(MPI_COMM_WORLD, &size);

        if(2!=size) MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
        int buf1, buf2;
        buf1 = 1;
        buf2 = 1;
        if (0==rank) {
            MPI_Bcast(&buf1,1,MPI_INT,0,MPI_COMM_WORLD);
            MPI_Bcast(&buf2,1,MPI_INT,1,MPI_COMM_WORLD);
            printf("proc 0 done\n");
        }
        if (1==rank) {
            MPI_Bcast(&buf2,1,MPI_INT,1,MPI_COMM_WORLD);
            MPI_Bcast(&buf1,1,MPI_INT,0,MPI_COMM_WORLD);
            printf("proc 1 done\n");
        }
        MPI_Finalize();
}

结果如下:

结果为:

进程0已完成。

进程1已完成。


你能否尝试交换rank 1中的buf2和buf1? - Nadir
1个回答

2
考虑以下死锁变体的代码:
#include <stdlib.h>
#include <stdio.h>
#include <mpi.h>

int main(int argc, char *argv[])
{
        int rank,size;

        MPI_Init(&argc, &argv);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
        MPI_Comm_size(MPI_COMM_WORLD, &size);



        if(2!=size) MPI_Abort(MPI_COMM_WORLD, 1);

    int* buf1 = (int*)malloc(sizeof(int) * 10000);
    int* buf2 = (int*)malloc(sizeof(int) * 10000);

        buf1[0] = 1;
        buf2[0] = 1;
        if (0==rank) {
            MPI_Bcast(buf1,10000,MPI_INT,0,MPI_COMM_WORLD);
            MPI_Bcast(buf2,10000,MPI_INT,1,MPI_COMM_WORLD);
            printf("proc 0 done\n");
        }
        if (1==rank) {
            MPI_Bcast(buf2,10000,MPI_INT,1,MPI_COMM_WORLD);
            MPI_Bcast(buf1,10000,MPI_INT,0,MPI_COMM_WORLD);
            printf("proc 1 done\n");
        }
        MPI_Finalize();
}

唯一的变化就是将通信字节数增加了10000倍。为什么这会导致代码死锁?
请查看MPI文档中的通信模式部分。如果接收方正在等待接收,或者接收方已经分配了足够大小的接收缓冲区,发送操作就会成功返回。
默认情况下,MPI会分配一个接收缓冲区来捕获小的接收数据,例如您正在发送的单个整数。

你是指MPI急切限制吗?[http://blogs.cisco.com/performance/what-is-an-mpi-eager-limit] - Harald
我完全同意这在实践中确实是发生的事情。但它真的符合标准吗?通信模式是否真的适用于集体操作?标准规定 MPI_Bcast 的行为是:“返回时,将根进程缓冲区的内容复制到所有其他进程。”。 - Zulan
非常有帮助的答案。我会跟随MPI文档的链接并学习一些细节。 - xbf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接