gpt4 book ai didi

c++ - 我的收集缓冲区是空的?

转载 作者:行者123 更新时间:2023-11-30 17:43:43 27 4
gpt4 key购买 nike

我在从所有进程收集数据到进程主“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;
}

最佳答案

一开始我误读了你的帖子。抱歉。

看看这段代码:

        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 的“计数”将神奇地流入来自 main 的“计数”。他们不会。这些是不同的变量。在这种情况下,只需从 ProcessData 中返回计数即可。

查看 http://mpitutorial.com/mpi-scatter-gather-and-allgather/ 中的示例它们与您正在尝试做的事情非常相似。

编辑:

嗯..实际上,在第四次阅读你的代码后,我不明白你想发送什么以及发送到哪里。做出决定:您想将“计数”发送给所有工作人员,还是想读取所有工作人员的计数?如果您想要一个典型的情况,当一些工作人员读取输入文件的某些部分时,每个工作人员都会计算一些内容,然后“收集”结果 - 请参阅上面的链接。否则,你将不得不详细说明,因为我的想象力已经结束了。

关于c++ - 我的收集缓冲区是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20180274/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com