0

I have a mesh with huge number of segments, I want to apply filter and fill std::set set_, which is private member of class A. There is function called fill_vec() which is going to fill the vector using a for loop:

fill_set()
{
  for(mesh::SegIterator it = A.beginSeg(); it != A.endSeg(); ++it )
  {
    mesh::Segment Seg_ = *it;
    int SignP = 0;
    int SignN = 0;


    for(mesh::PointIterator itp = Seg_.beginPoint(); itp != Seg_.endPoint(); ++itp )
    {
      double old_, new_;
      ... 
      ...

      if( old_ > 0.0 && new_ > 0.0 )
        SignP++;
      if( old_ < 0.0 && new_ < 0.0 )
        SignN++;
    }
    if( (SignP == 1 && SignN == 1) || (SignP == 1 && SignN == 2) )
    {
      set_.insert(Seg_);
    }
}

I am trying peform above code in parallel using OpenMP and C++03. I saw some solutions like as this. Any other safe and neat solution?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
H'H
  • 1,638
  • 1
  • 15
  • 39
  • Is mesh::SegIterator a RandomAccessIterator or just a BidirectionalIterator (or even just a ForwardIterator)? – SirGuy Aug 18 '15 at 20:04
  • I don't see why you can't use the same techniques [I described for std::vector](https://stackoverflow.com/questions/18669296/c-openmp-parallel-for-loop-alternatives-to-stdvector/18671256#18671256). Write private sets for each thread and the merge them with `insert` in a critical section. – Z boson Aug 20 '15 at 15:19

1 Answers1

3

Try changing from it != A.endSeg() to it < A.endSeg(). The problem with != is the loop is not countable. The compiler can't know for sure that the loop will ever end. Switching it to < should make it countable.

jefflarkin
  • 1,279
  • 6
  • 14