splitting an if/else statement with a directive

General OpenMP discussion

splitting an if/else statement with a directive

Postby ausairman » Mon Dec 19, 2011 1:13 am

Hi everyone,
I have a critical region of code that can only be executed by a single thread. Unfortunately, the check for whether this region needs to be executed is itself critical, resulting in this situation:

Code: Select all
#pragma omp critical (access)
{
   if (len != CAPACITY) {
      // do something critical
      len++;
   }
}
   else {
      // check failed, so non-critical
   }

   // some more code that applies to both cases but is non-critical goes here


Is this valid usage, referring to the split if/else statement?
ausairman
 
Posts: 12
Joined: Mon Jul 11, 2011 5:40 am

Re: splitting an if/else statement with a directive

Postby ftinetti » Mon Dec 19, 2011 4:28 am

Is this valid usage, referring to the split if/else statement?

I don't think so, but I do not have the spec at had, what does the compiler report on this code?
ftinetti
 
Posts: 556
Joined: Wed Feb 10, 2010 2:44 pm

Re: splitting an if/else statement with a directive

Postby MarkB » Fri Feb 17, 2012 5:59 am

ausairman wrote:Is this valid usage, referring to the split if/else statement?


It isn't valid, unfortunately: the critical construct must contain a structured block of code.
One possible fix would be:


Code: Select all
int flag = 0;
#pragma omp critical (access)
{
   if (len != CAPACITY) {
      // do something critical
      len++;
      flag = 1;
   }
}
   if (!flag)
      // check failed, so non-critical
   }

   // some more code that applies to both cases but is non-critical goes here
MarkB
 
Posts: 407
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: MarkB and 4 guests