Community
cancel
Showing results for 
Search instead for 
Did you mean: 
ashoksaravanan
Beginner
36 Views

Problem using Barrier...

Hi all..

When trying to parallelize my application(using Island Migration Model), i had to synchronize some events in the parallel for loop. So i used barrier within the for loop.. But i got the error as 'use of barrier is denied inside parallel for...'

But i managed to synchronize at that point by calling a function and using the barrier over there...

My doubt is Why it has been denied? What ill-effects could it create if not denied..?

with regards,

Ashoksmiley [:-)]

0 Kudos
1 Reply
jimdempseyatthecove
Black Belt
36 Views

Ashok,

Take some time to read the user guide on OpenMP then re-read sectionsthat may seem non-obvious at first, but will be quite obvious once you understand the circumstances.

BARRIER results in:

Any thread of a team entering the barrier will not exit the barrier until all threads of the team enter the barrier.

This requires that:

IF any thread of a team enters a barrier THEN all threads of that team must enter the same barrier.

Now consider a parallel FOR with an even number of threads but an odd number of iterations (or where work distribution results in some threads not executing the last few iterations of the loop). Under this circumstance, some of the threads of the team will not enter the barrier while others are waiting at the barrier. This results in a deadlock.

You have to craft a different method to synchronize at that position in your loop. The synchronization technique has to be sensitive to not only a reduction in team members on the last few iterations but also to a team member reaching the synchronization point before a different team member begins its execution at the start of the parallel for. e.g. performing an atomic increment of number of threads beginning the loop (at the top of the loop) and performing an atomic decrement of the number of threads at the bottom of the loop will fail when some threads make it to the bottom test prior to one of the threads executing the atomic increment at the top of the loop.

I will leave it as an exercise to you to craft a thread-safe manner to perform the synchronization. It will be a good learning exercise. This is not all that difficult to do once you discover how to do this. Learning how to learn is more importent to your programming development than being given techniques.

Jim Dempsey

Reply