c++ - MPI 通讯器错误

标签 c++ mpi parallel-processing

我的一个使用 MPI 的程序出现了问题,我刚刚修复了它,但是,我似乎不明白首先出了什么问题。我对编程相关的东西很陌生,所以请原谅。

程序是:

#include <iostream>
#include <cstdlib>
#include <mpi.h>

#define RNumber     3

using namespace std;

int main() {
    /*Initiliaze MPI*/
    int     my_rank;    //My process rank
    int         comm_sz;    //Number of processes
    MPI_Comm    GathComm;   //Communicator for MPI_Gather
    MPI_Init(NULL, NULL);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);

    /*Initialize an array for results*/
    long rawT[RNumber];
    long * Times = NULL;        //Results from threads
    if (my_rank == 0) Times = (long*) malloc(comm_sz*RNumber*sizeof(long));

    /*Fill rawT with results at threads*/
    for (int i = 0; i < RNumber; i++) {
        rawT[i] = i;
    }


    if (my_rank == 0) {
        /*Main thread recieves data from other threads*/
        MPI_Gather(rawT, RNumber, MPI_LONG, Times, RNumber, MPI_LONG, 0, GathComm);

    }
    else {
        /*Other threads send calculation results to main thread*/
        MPI_Gather(rawT, RNumber, MPI_LONG, Times, RNumber, MPI_LONG, 0, GathComm);
    }

    /*Finalize MPI*/
    MPI_Finalize();
    return 0;
};

执行时程序返回以下消息:

Fatal error in PMPI_Gather: Invalid communicator, error stack: PMPI_Gather(863): MPI_Gather(sbuf=0xbf824b70, scount=3, MPI_LONG, rbuf=0x98c55d8, rcount=3, MPI_LONG, root=0, comm=0xe61030) failed PMPI_Gather(757): Invalid communicator Fatal error in PMPI_Gather: Invalid communicator, error stack: PMPI_Gather(863): MPI_Gather(sbuf=0xbf938960, scount=3, MPI_LONG, rbuf=(nil), rcount=3, MPI_LONG, root=0, comm=0xa6e030) failed PMPI_Gather(757): Invalid communicator

在我完全删除 GathComm 并将其替换为 MPI_COMM_WORLD 默认通信器后,一切正常。

有人能好心解释一下我做错了什么以及这种调整是如何使一切正常进行的吗?

最佳答案

这是因为 GathComm 尚未分配有效的通信器。 “MPI_Comm GathComm;”仅声明用于保存通信器的变量,但不创建通信器。

如果您只想在操作中包含所有过程,则可以使用默认通信器 (MPI_COMM_WORLD)。

当您想要将进程组织到单独的组中或使用 virtual communication topologies. 时,自定义通信器非常有用。

要了解更多信息,请查看this article其中描述了组、通信器和拓扑。

关于c++ - MPI 通讯器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7915891/

相关文章:

c++ - 我们能否仅根据参数是值还是引用来重载函数?

编译具有 MPI 支持的 Valgrind

c - 使用 MPI_Finalize() 后出现段错误

graphics - 学生项目构想:并行计算

python - KMeans 并行处理失败

c++ - 如何使用 OpenCV 正确旋转不规则矩形?

c++ - 带有跳跃列表的双向链表以排序方式插入

c++ - 将 null 从 C++/CLI 发送到托管 C#

c++ - MPI_Scatter,分散对角元素

c# - 如何在 Reactive Extensions 中实现并行 Fan-out 处理?