问题
I have problem gathering data from all process to process master "root" I can send the data MPI_Bcast but the on MPI_Gather I have all the problem in my countBuff I debug my output and this is what I have
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;
}
回答1:
At first I've misread your post. Sorry.
Look at this code:
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;
The problem is that MPI_Gather sends &count
. But read the code carefully. What is the value of the count
that will be sent?
Either you have misunderstood the Bcast and Gather - They are not related. Not at all! - or, you have wrongly assumed that the "count" from ProcessData will magically flow into "count" from main. They won't. Those are different variables. In such case, just return
the count from ProcessData.
Check the examples at http://mpitutorial.com/mpi-scatter-gather-and-allgather/ they are quite similar to what you are trying to do.
EDIT:
hm.. actually, after reading your code 4th time, I don't understand what and where you want to send. Make your mind: do you want to SEND the 'count' to all workers, or do you want to READ the counts from all workers? If you want a typical case, when some workers read some parts of the input file, then each calculates something, then the results are 'gathered' - see the link above. Otherwise, you will have to elaborate, because my imagination's ended.
回答2:
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);
}
来源:https://stackoverflow.com/questions/20180274/my-gather-buffer-is-empty