我有问题从所有进程收集数据到进程主“root”我可以发送数据MPI_Bcast但是在MPI_Gather我在countBuff中遇到了所有问题我调试了我的输出,这就是我所拥有的
output
brodcast data of 0
brodcast data of 1
MPI_Gather data rank 0 1
from 0 to 1.00 KM:-842150451,from 1.00 to 2.00 KM:-842150451,from 2.00 to 5.00 KM:-842150451,grater than 5.00 KM:-842150451
MPI_Type_free1
delete countBuff
MPI_Finalize
brodcast data of 2
MPI_Gather data rank 0 0
MPI_Gather data rank 0 2
from 0 to 1.00 KM:-842150451,from 1.00 to 2.00 KM:-842150451,from 2.00 to 5.00 KM:-842150451,grater than 5.00 KM:-842150451
MPI_Type_free2
delete countBuff
MPI_Finalize
job aborted:
rank: node: exit code[: error message]
0:: -1073741819: process 0 exited without calling finalize
1:: 123
2:: 123
the code
void ProcesData(int rank,int numProcs)
{
static countType count;
MPI_Datatype recType = createRecType();
//read file and populate the vectors
ifstream foodbankFile("foodbanks.dat");
ifstream residenceFile("residences.dat");
// populate datavector
std::vector<Foodbank> foodbankData((std::istream_iterator<Foodbank>(foodbankFile)),
std::istream_iterator<Foodbank>());
Residence res;
int numLines = 0;
while(!residenceFile.eof())
{
residenceFile >> res.x >>res.y;
if ( numLines % numProcs == rank)
{
//call the process
//populate_distancesVector(res,foodbankData);
analysis_range(populate_distancesVector(res,foodbankData),count);
}
++numLines;
}
cout<<"brodcast data of "<<rank<<endl;
MPI_Bcast(&count, 1, recType, rank, MPI_COMM_WORLD);
MPI_Type_free(&recType);
//std::cout<< "for Rank"<<rank<< ",from 0 to 1.00 KM:"<<count.range1<<",%"<<count.preset1
//<<",from 1.00 to 2.00 KM:"<<count.range2<<",%"<<count.preset2<<",from 2.00 to 5.00 KM:"
//<<count.range3<<",%"<<count.preset3<<",grater than 5.00 KM:"<<count.range4<<",%"<<count.preset3<<std::endl;
}
int main(int argc, char* argv[])
{
if( MPI_Init(&argc, &argv) == MPI_SUCCESS )
{
// Get the number of processes and the rank of this process
int procRank,numProcs;
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
MPI_Comm_rank(MPI_COMM_WORLD, &procRank);
ProcesData(procRank,numProcs);
// Create a derived type for passing the rec array
MPI_Datatype recType = createRecType();
static countType count;
countType* countBuff = new countType[numProcs];
MPI_Gather(&count, 1, recType, &countBuff, 1, recType,0, MPI_COMM_WORLD);
cout<<"MPI_Gather data rank 0 "<<procRank<<endl;
//MPI_Allgather(&count, 1, recType, &countBuff, 1, recType,MPI_COMM_WORLD);
std::cout<<"from 0 to 1.00 KM:"<<countBuff[0].range1<<",from 1.00 to 2.00 KM:"
<<countBuff[0].range2<<",from 2.00 to 5.00 KM:"<<countBuff[0].range3
<<",grater than 5.00 KM:"<<countBuff[0].range4<<std::endl;
cout<<"MPI_Type_free"<<procRank<<endl;
MPI_Type_free(&recType);
cout<<"delete countBuff"<<endl;
cout<<"MPI_Finalize"<<endl;
MPI_Finalize();
}
return 0;
}
答案 0 :(得分:0)
起初我误读了你的帖子。遗憾。
看看这段代码:
ProcesData(procRank,numProcs);
// Create a derived type for passing the rec array
MPI_Datatype recType = createRecType();
static countType count;
countType* countBuff = new countType[numProcs];
MPI_Gather(&count, 1, recType, &countBuff, 1, recType,0, MPI_COMM_WORLD);
cout<<"MPI_Gather data rank 0 "<<procRank<<endl;
问题是MPI_Gather发送&count
。但仔细阅读代码。将要发送的count
的价值是多少?
要么你误解了Bcast和Gather - 他们没有关系。一点也不! - 或者,你错误地认为ProcessData中的“count”会从main主动地流入“count”。他们不会。这些是不同的变量。在这种情况下,只需return
来自ProcessData的计数。
查看http://mpitutorial.com/mpi-scatter-gather-and-allgather/处的示例,它们与您尝试的内容非常相似。
编辑:
实际上,在第四次阅读你的代码之后,我不明白你想要发送什么和在哪里。请记住:您是否希望向所有员工发送“计数”,或者您是否想要阅读所有员工的计数?如果你想要一个典型的案例,当一些工人读取输入文件的某些部分,然后每个人计算一些东西,然后结果被“收集” - 见上面的链接。否则,你必须详细说明,因为我的想象力已经结束了。答案 1 :(得分:0)
void processData(int rank,int numProcs)
{
static countType count;
MPI_Datatype recType = createRecType();
//read file and populate the vectors
ifstream foodbankFile("foodbanks.dat");
ifstream residenceFile("residences.dat");
// populate datavector
std::vector<Foodbank> foodbankData((std::istream_iterator<Foodbank>(foodbankFile)),
std::istream_iterator<Foodbank>());
Residence res;
int numLines = 0;
while(!residenceFile.eof())
{
residenceFile >> res.x >>res.y;
if ( numLines % numProcs == rank)
{
analysis_range(getShortestDistances(res,foodbankData),count);
}
++numLines;
}
countType* countBuff = new countType[numProcs];
MPI_Gather(&count, 1, recType, countBuff, 1, recType,0, MPI_COMM_WORLD);
if(rank == 0)
{
static countType countArggResult;
for (int p = 0; p < numProcs; ++p)
{
// out put result
}
}
//free virables
delete [] countBuff;
MPI_Type_free(&recType);
}