【发布时间】:2011-12-16 11:37:32
【问题描述】:
我在使用 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;
};
程序在执行时返回以下消息:
PMPI_Gather 中的致命错误:无效的通信器,错误堆栈: PMPI_Gather(863): MPI_Gather(sbuf=0xbf824b70, scount=3, MPI_LONG, rbuf=0x98c55d8, rcount=3, MPI_LONG, root=0, comm=0xe61030) 失败 PMPI_Gather(757):无效的通信器 PMPI_Gather 中的致命错误: 无效的通信器,错误堆栈:PMPI_Gather(863): MPI_Gather(sbuf=0xbf938960, scount=3, MPI_LONG, rbuf=(nil), rcount=3, MPI_LONG, root=0, comm=0xa6e030) 失败 PMPI_Gather(757): 无效 沟通者
在我完全删除 GathComm 并将其替换为 MPI_COMM_WORLD 默认通信器后,一切正常。
谁能解释一下我做错了什么以及这个调整是如何让一切正常的?
【问题讨论】:
标签: c++ mpi parallel-processing