我的一个使用 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/