简体   繁体   中英

segmentation fault during push_back

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

The reason most often you end up segfaulting when trying to push_pack concurrently is because in one thread you may try to push_back , the vector is being reallocated (so all iterators are being invalidated), while the other thread tries to push_back to something that is now invalid memory (the other thread "doesn't know" that the vector was reallocated meanwhile, ie will use the invalid iterators).

Possible duplicate of Is std::vector or boost::vector thread safe?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM