У меня проблема с сбором данных из всего процесса в главный мастер процесса "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;
}
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);
}
Сначала я неправильно прочитал ваш пост. Сожалею.
Посмотрите на этот код:
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 волшебным образом втекает в "счет" из основного. Они не будут. Это разные переменные. В таком случае просто return
счетчик из ProcessData.
Проверьте примеры на http://mpitutorial.com/mpi-scatter-gather-and-allgather/, они очень похожи на то, что вы пытаетесь сделать.
РЕДАКТИРОВАТЬ:
hm.. на самом деле, после прочтения вашего кода в 4-й раз, я не понимаю, что и куда вы хотите отправить. Сделайте свой ум: хотите ли вы ОТПРАВИТЬ "счет" всем работникам или хотите ПРОЧИТАТЬ счетчики от всех работников? Если вам нужен типичный случай, когда некоторые работники читают некоторые части входного файла, каждый вычисляет что-то, тогда результаты "собираются" - см. Ссылку выше. В противном случае вам придется разрабатывать, потому что мое воображение закончилось.