【问题标题】:MPI_Bcast hangingMPI_Bcast 挂起
【发布时间】:2014-11-05 14:25:28
【问题描述】:

我按照here的例子添加了一些代码进行测试,但是当我添加新代码时出现了一些奇怪的结果。结果如下所示。它挂在这里,无法继续。

[0]: Before Bcast, buf is 777
[1]: Before Bcast, buf is 32767
[0]: After Bcast, buf is 777

从两个角度来看很奇怪:

  1. code为if(rank==i) if(i==0)时不会挂机

  2. whey buffer[1] 在广播前不为 0。

代码如下:

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char** argv) {
    int rank;
    int buf;
    const int root=0;

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

    if(rank == root) {
       buf = 777;
    }

    printf("[%d]: Before Bcast, buf is %d\n", rank, buf);

    // newly added
    for(int i=0; i<2; i++)
    {
        if(rank==i)
        {
            if(i==1)
            MPI_Bcast(&buf, 1, MPI_INT, rank, MPI_COMM_WORLD);
        }
    }
    // end newly added

    /* everyone calls bcast, data is taken from root and ends up in everyone's buf */
    MPI_Bcast(&buf, 1, MPI_INT, root, MPI_COMM_WORLD);
    printf("[%d]: After Bcast, buf is %d\n", rank, buf);

    MPI_Finalize();
    return 0;
}

我真正想做的是实现功能: 每个处理器运行一部分处理,然后在得到结果后,广播它,其他处理器通过结合接收结果来更新自己的结果。

这里是主要部分代码(原代码来自libsvm)

struct decision_function
{
    double *alpha;
    double rho;
};

int gate_no = 2;
int p = 0;

int nr_class = 8;

for(int i=0;i<nr_class;i++)
{
    for(int j=i+1;j<nr_class;j++)
    {
        if(((world_rank==i-2*gate_no*(i/(2*gate_no))) && (i%(2*gate_no) < gate_no))||((world_rank==2*gate_no*(i/(2*gate_no)+1)-i-1)&&(i%(2*gate_no) >= gate_no)))
        {
            // some process for generating f[p] here
            ....
            MPI_Bcast(f[p].alpha, count[i]+count[j], MPI_DOUBLE, world_rank, MPI_COMM_WORLD);

        }
        ++p;
    }
}

但是这段代码不起作用,而且我遇到了一些错误。

Fatal error in PMPI_Bcast: Other MPI error, error stack:
PMPI_Bcast(1478)......................: MPI_Bcast(buf=0xcc7b40, count=2340, MPI_DOUBLE, root=1, MPI_COMM_WORLD) failed
MPIR_Bcast_impl(1321).................: 
MPIR_Bcast_intra(1119)................: 
MPIR_Bcast_scatter_ring_allgather(962): 
MPIR_Bcast_binomial(154)..............: message sizes do not match across processes in the collective

【问题讨论】:

    标签: c++ mpi


    【解决方案1】:

    这个区块

        if(rank==i)
        {
            if(i==1)
            MPI_Bcast(&buf, 1, MPI_INT, rank, MPI_COMM_WORLD);
        }
    

    表示只有进程 1 调用MPI_Bcast。由于它是一个集体操作,因此通信器中的所有进程都应该(同时)调用它。在这种情况下,进程 1 正在等待所有其他进程,如果我理解正确,则继续,直到他们下一次调用 MPI_Bcast 时,他们等待进程 1。然后等待。等等。

    我不确定您真正想要做什么,因此无法就如何解决此问题提供任何建设性的建议。

    【讨论】:

    • 非常感谢您的回复。我再次编辑了这个问题。希望它更清楚。
    • 我仔细阅读了您的 cmets。现在我终于明白了这个问题。这是因为所有进程都需要调用MPI_BcastThe question post 对我也有很大帮助。
    • 我在运行商业代码时遇到了类似的错误。我可以设置一个标志来延长完成 MPI_Bcast 所需的时间吗?
    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 2012-11-20
    • 2013-11-30
    • 1970-01-01
    • 2013-06-19
    • 2012-06-16
    • 2012-11-05
    相关资源
    最近更新 更多