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++;
}
}