0

I am trying to parallelize my C++ code using Open MP but I am running into Segmentation fault, the serial version is running fine. I used gdb debugger and I know the error is occurring while erasing one of the elements of the STL List.

In the code below I have a vector which contains particle pairs with IDs(boxID1 and boxID2) and each particle has a Actual Contact List associated with it. Then I add and delete if or if not the particles are in contact in a given list.

Is there a way to avoid this error while erasing?

        Contact newcontact;
        bool isPresentIntheList;
        list<Contact>::iterator it;
        #pragma omp parallel for private(newcontact, isPresentIntheList, it)
        for(long int i=0; i<overlapVector.size(); i++)
        {
            isPresentIntheList = false;
            it = (*particlesPointerVector)[overlapVector[i].boxID2]->ActualContactList.begin();//iterator for Actual Contact List of the particle with ID = overlapVector[i].boxID2
            if((*particlesPointerVector)[overlapVector[i].boxID2]->ActualContactList.size() != 0) //if ACL size doesn't have a size = 0
            {
                for(long int j=0; it!=(*particlesPointerVector)[overlapVector[i].boxID2]->ActualContactList.end(); j++)
                {
                    if(it->particlei->ID == overlapVector[i].boxID1)
                    {
                        isPresentIntheList = true;
                    }

                    if(isPresentIntheList)
                    {
                        if(IsContact(*it, timestep))//checking if the overlapping pair is in contact or not
                        {
                            it++;//do nothing that means overlapping pair is in contact
                        }

                        else//remove that pair from ACL if it is not in contact
                        {

                            {
                                newcontact.particlei = (*it).particlei;
                                newcontact.particlej = (*it).particlej;
                                /*ERROR occurring here*/
                                it = (*particlesPointerVector)[overlapVector[i].boxID2]->ActualContactList.erase(it);//no it++ required as it automatically points to the next container after erasing

                            }

                        }

                    }
                    else if(!isPresentIntheList)
                        it++;
                }
        }
Rohit
  • 11
  • 3
  • I don't think that the standard container types are thread-safe, so I don't think you can safely execute multiple `erase`s concurrently. You may need to find a different approach to solving your problem. – templatetypedef Jul 07 '15 at 18:02
  • Step back and do a simple example of using a std::vector with OpenMP. See [this answer](http://stackoverflow.com/questions/18669296/c-openmp-parallel-for-loop-alternatives-to-stdvector/18671256#18671256) on how to fill a std::vector with OpenMP. You should be able to use the same idea for erase. – Z boson Jul 08 '15 at 08:03
  • Z boson, I am able to fill the List with OpenMP without any error but is is occurring only when I am trying to erase. I have tried several things but in vain – Rohit Jul 08 '15 at 20:04

0 Answers0