I'm getting a very weird segmentation fault on the following loop. The goal is to have each processor do some checks of x/y points which are stored on the following vectors
Just to clarify: This is a multi-processor code not multi thread. This is how I'm getting the rank:
int my_rank = Utilities::MPI::this_mpi_process(mpi_communicator);
std::vector<std::vector<double> > Xcoord(n_proc);
std::vector<std::vector<double> > Ycoord(n_proc);
The Xcoord[i] is a vector of x coordinates that come from the i processor and the current processor will do some checks on them, which I'm not including them below:
The code loops through the n processors, first check if it has any information about the paarticular point, and if yes, saves the id of the point and the id of the processor.
std::vector<std::vector<int> > which_point(n_proc);
std::vector<std::vector<int> > which_proc(n_proc);
for (int i = 0; i < n_proc; ++i){
if (i == my_rank) continue;
for (unsigned int j = 0; j < Xcoord[i].size(); ++j){
bool yit = getYiterator(yxmap, Ycoord[i][j], itY);
if (yit){
bool xit = getXiterator(itY->second, Xcoord[i][j], itX);
if (xit){
itZ = itX->second.zmap.begin();
for (; itZ != itX->second.zmap.end(); ++itZ){
which_point[my_rank].push_back(j);
which_proc[my_rank].push_back(i);
}
}
}
}
}
(In the innermost loop the itX->second.zmap.size() is 3)
When I run the code in one processor everything works fine.
When I do so with 4 processors I'm getting segmentation faults.
If I remove one of the two lines
which_proc[my_rank].push_back(i); or
which_point[my_rank].push_back(j);
the code works even with 4 processors.
I've also noticed that the segmentation fault is always associated with rank 2. So if I set the condition if (my_rank != 2) in front of one of the two above lines the code works in 4 processors.
I've seen few posts on this issue but in most cases the errors occurred from empty pointers passing to push_back().
Here I'm pushing back just an integer number, which to me obviously exists when it's pushed to a vector.
Any idea how I could catch this error??
Thank you