OpenMP and STL

Use this forum to discuss the book: Using OpenMP - Portable Shared Memory Parallel Programming, by Barbara Chapman, Gabriele Jost and Ruud van der Pas Read the http://openmp.org/forum/viewtopic.php?f=8&t=465 for book info and to download the examples. Post your feedback about the book and examples to this forum
Forum rules
The OpenMP Forums are now closed to new posts. Please visit Stack Overflow if you are in need of help: https://stackoverflow.com/questions/tagged/openmp
Locked
sajis997

OpenMP and STL

Post by sajis997 »

Hello forum,

I am having problem(segmentation fault) with the following code snippet:

Code: Select all


   //create a pointer to the hitpoint
   HitPoint hitPoint;

   //loop over all the pixels in the image
#pragma omp parallel for schedule(dynamic,1), private(hitPoint)
   for(int y = 0; y < height; ++y)
   {
      for(int x = 0; x < width; ++x)
      {
	 hitPoint = traceAndCreateHitPoint(x,y);

	 //store the hit point inside the vector
	 mHitPoints.push_back(&hitPoint);
      }

      #pragma omp critical
      // Print progress approximately every 5%.
      if ((y+1) % (height/20) == 0 || (y+1) == height)
         std::cout << (100*(y+1)/height) << "% done" << std::endl;      
   }




Is there any issue that need to be resolved when using openmp with STL. If i comment the STL command , the code runs fine.


Regards
Sajjadul

MarkB
Posts: 808
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Re: OpenMP and STL

Post by MarkB »

None of the STL containers are guaranteed safe to access using multiple threads in this way. You need to make sure only one thread at a time calls push_back(): putting the call in a critical section should work, though this may not be the most optimal solution.

Locked