Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explict flush directive with OpenMP: when is it necessary and when is it helpful

Tags:

openmp

One OpenMP directive I have never used and don't know when to use is flush(with and without a list).

I have two questions:

1.) When is an explicit `omp flush` or `omp flush(var1, ...) necessary?  
2.) Is it sometimes not necessary but helpful (i.e. can it make the code fast)?

The main reason I can't understand when to use an explicit flush is that flushes are done implicitly after many directives (e.g. as barrier, single, ...) which synchronize the threads. I can't, for example, see way using flush and not synchronizing (e.g. with nowait) would be helpful.

I understand that different compilers may implement omp flush in different ways. Some may interpret a flush with a list as as one without (i.e. flush all shared objects) OpenMP flush vs flush(list). But I only care about what the specification requires. In other words, I want to know where an explicit flush in principle may be necessary or helpful.

Edit: I think I need to clarify my second question. Let me give an example. I would like to know if there are cases where removing an implicit flush (e.g. with nowait) and instead using an explicit flush instead but only on certain shared variables would be faster (and still give the correct result). Something like the following:

float a,b;
#pragma omp parallel
{
    #pragma omp for nowait // No barrier.  Do not flush on exit.
        //code which uses only shared variable a        
    #pragma  omp flush(a) // Flush only variable a rather than all shared variables.       
    #pragma omp for
       //Code which uses both shared variables a and b.
}

I think that code still needs a barrier after the the first for loop but all barriers have an implicit flush so that defeats the purpose. Is it possible to have a barrier which does not do a flush?

like image 209
Z boson Avatar asked Oct 30 '13 15:10

Z boson


People also ask

What is the need of OpenMP flush operation?

OpenMP flush operations are used to enforce consistency between a thread's temporary view of memory and memory, or between multiple threads' view of memory. If a flush operation is a strong flush, it enforces consistency between a thread's temporary view and memory.

Which of the facility of OpenMP is used to avoid race conditions?

The two threads modifying x may be interleaved with each other, meaning that the result will not necessarily be zero. One way to protect the modifications is to wrap the read-modify-write code in a critical region.

How does OpenMP provide a shared memory programming environment?

OpenMP Programming Model A shared memory process consists of multiple threads. OpenMP is an explicit, none-automatic, programming model which offers the programmer full control over parallelization. When a thread reaches a PARALLEL directive, it creates a team of threads and becomes the master of the team.

What is the difference between private and shared clauses in OpenMP?

The private variable is updated after the end of the parallel construct. The shared clause declares the variables in the list to be shared among all the threads in a team. All threads within a team access the same storage area for shared variables.


1 Answers

The flush directive tells the OpenMP compiler to generate code to make the thread's private view on the shared memory consistent again. OpenMP usually handles this pretty well and does the right thing for typical programs. Hence, there's no need for flush.

However, there are cases where the OpenMP compiler needs some help. One of these cases is when you try to implement your own spin lock. In these cases, you would need a combination of flushes to make things work, since otherwise the spin variables will not be updated. Getting the sequence of flushes correct will be tough and will be very, very error prone.

The general recommendation is that flushes should not be used. If at all, programmers should avoid flush with a list (flush(var,...)) at all means. Some folks are actually talking about deprecating it in future OpenMP.

Performance-wise the impact of flush should be more negative than positive. Since it causes the compiler to generate memory fences and additional load/store operations, I would expect it to slow down things.

EDIT: For your second question, the answer is no. OpenMP makes sure that each thread has a consistent view on the shared memory when it needs to. If threads do not synchronize, they do not need to update their view on the shared memory, because they do not see any "interesting" change there. That means that any read a thread makes does not read any data that has been changed by some other thread. If that would be the case, then you'd have a race condition and a potential bug in your program. To avoid the race, you need to synchronize (which then implies a flush to make each participating thread's view consistent again). A similar argument applies to barriers. You use barriers to start a new epoch in the computation of a parallel region. Since you're keeping the threads in lock-step, you will very likely also have some shared state between the threads that has been computed in the previous epoch.

BTW, OpenMP may keep private data for a thread, but it does not have to. So, it is likely that the OpenMP compiler will keep variables in registers for a while, which causes them to be out of sync with the shared memory. However, updates to array elements are typically reflected pretty soon in the shared memory, since the amount of private storage for a thread is usually small (register sets, caches, scratch memory, etc.). OpenMP only gives you some weak restrictions on what you can expect. An actual OpenMP implementation (or the hardware) may be as strict as it wishes to be (e.g., write back any change immediately and to flushes all the time).

like image 50
Michael Klemm Avatar answered Oct 29 '22 15:10

Michael Klemm