Is there an omp function to flush a share variable?

General OpenMP discussion

Is there an omp function to flush a share variable?

Postby ntt2011 » Fri Jan 18, 2013 7:05 am

I have a question regarding a global shared variable, let's call it "globalFlag" which is mostly zero. But if it is set to 1 during a thread, I'd like to flush it so that all the threads know that it's set and skip the execution, saving computer time. Currently my code looks like this:

Code: Select all
globalFlag = 0
#pragma omp parallel for
for ( i=1 ; i<N; i++) {
       #pragma omp flush (globalFlag)
      if (!globalFlag)
             do_something_here(i)  // globalFlag may be set to 1 in this function.
      if (globalFlag) {
             #pragma omp flush (globalFlag)
      }
}


Which seems that I overdo the flush pragma, and slow down the program. My question is:
Can I drop the first "#pragma omp flush" statement?
Is the second "#pragma omp flush" statement execute everytime? or only when globalFlag is set?
ntt2011
 
Posts: 10
Joined: Sun Dec 30, 2012 6:03 am

Re: Is there an omp function to flush a share variable?

Postby ntt2011 » Mon Jan 21, 2013 3:39 am

I want to simplify my question. If I write:

Code: Select all
if (globalFlag) {
    #pragma omp flush (globalFlag)
}


Does the #pragma acts like a function, and it will be called only if the variable globalFlag is set? or it will be called regardless?
ntt2011
 
Posts: 10
Joined: Sun Dec 30, 2012 6:03 am

Re: Is there an omp function to flush a share variable?

Postby MarkB » Mon Jan 21, 2013 3:47 am

Hi there,

Code: Select all
   
    if (globalFlag) {
        #pragma omp flush (globalFlag)
    }


OpenMP directives do respect the control flow of the program, so this flush will only be executed if globalFlag is true.

If you have a version 3.1 compliant implementation, using atomic writes and reads would be safer and more portable!

Hope that helps,
Mark.
MarkB
 
Posts: 422
Joined: Thu Jan 08, 2009 10:12 am

Re: Is there an omp function to flush a share variable?

Postby ntt2011 » Mon Jan 21, 2013 9:07 am

Thank you. That's what I hope for to avoid unnecessesary flush.

So, in my original post, I guess I can omit the first flush command, because whenever globalFlash is set, the accompanied flush call would update globalFlash in all other threads.
ntt2011
 
Posts: 10
Joined: Sun Dec 30, 2012 6:03 am

Re: Is there an omp function to flush a share variable?

Postby MarkB » Tue Jan 22, 2013 3:16 am

Unfortunately you can't safely omit the first flush: it is necessary to ensure that the subsequent read is from memory and not from the threads temporary view of memory (e.g. a register). A flush after a write only guarantees the value is written to memory: it does not update other threads' view of the variable.

In order to transmit a value from one thread to another the following sequence must occur in time order:

1. The value is written to the variable by the first thread.
2. The variable is flushed by the first thread.
3. The variable is flushed by the second thread.
4. The value is read from the variable by the second thread.

(see Version 3.1 spec, page 15)

In your code, the first flush corresponds to step 3 in this sequence.
MarkB
 
Posts: 422
Joined: Thu Jan 08, 2009 10:12 am

Re: Is there an omp function to flush a share variable?

Postby ntt2011 » Tue Jan 22, 2013 9:49 am

Thanks. That's much clearer now. I thought I could cut corners. But I'd rather be safe.
ntt2011
 
Posts: 10
Joined: Sun Dec 30, 2012 6:03 am

Re: Is there an omp function to flush a share variable?

Postby MarkB » Tue Jan 22, 2013 10:03 am

ntt2011 wrote:Thanks. That's much clearer now. I thought I could cut corners. But I'd rather be safe.


You're welcome! I would not worry too much about the cost of the flush directive: it's unlikely to be significant unless the amount of work in do_something_here() is very small. If that is the case, then batching up iterations and only setting/checking the flag after each batch might help.
MarkB
 
Posts: 422
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 7 guests

cron