Break a parallel loop

General OpenMP discussion

Re: Break a parallel loop

Postby MarkB » Fri May 18, 2012 1:22 am

chameleon wrote:It is very simple:


Well, conceptually, yes! However, it is easier to implement if the "break" is actually a directive rather than a base language statement. And then there are all the
possible interactions with other OpenMP constructs to be taken care of.
MarkB
 
Posts: 428
Joined: Thu Jan 08, 2009 10:12 am

Re: Break a parallel loop

Postby jgpallero » Fri May 18, 2012 1:43 am

MarkB wrote:
But when the loop begins the value of 'avanti' is the same as in its declaration int avanti=0; so the first time all threads read avanti=0. If one of them checks A[i]==1, 'avanti' is changed to avanto=1 and the flush() inside the if(A[i]==1) sets the new value to all 'avanti' private copie for each thread, so in the next step of the loop 'avanti' is updated and I think that the first flush() is not neccessary. Of course, probably I'm wrong, but I don't understand why.


The OpenMP memory model is quite tricky to get your head around! The flush after the write does not update other threads' temporary views of memory. Threads that subsequently read the value must do a flush first to make sure they are reading the value from memory, and not from their own temporary view. In practice, this translates to making sure the compiler really does do loads and stores of the variable, instead of keeping it in registers.


So then in my first algorithm the second flush() is not necessary?

Sorry but my bad understanding, but I can't understand why the flush() directive actually works. If the second flush() is inside a critical region, this means that that region will be executed secuetially by all threads and until one thread does not end, the next will not starts, so when flush() in invoked inside the region, the flushed variable will be updated and the new adopted value will be written in global memory. Then the next thread could read the correct value.

On the other hand, I do not understand the presence of the first flush(). For example, if only one thread modifies the 'avanti' variable in the if(A[i]==1) part of the algorithm, how can the first flush() know which value is correct for 'avanti'. Imagine whe have 2 threads. In the if(A[i]==1) part, the first one pass the text, so sets 'avanti=0' and flushes the variable. The second thread do not pass the text, so no flush() is necessary. I don't understan qhen in the next step of the loop the first flush() is necessary because 'avanti' was flushed in the previous step

Thanks
jgpallero
 
Posts: 11
Joined: Fri May 11, 2012 1:47 am

Re: Break a parallel loop

Postby MarkB » Fri May 18, 2012 2:08 am

MarkB wrote:So then in my first algorithm the second flush() is not necessary?

Sorry but my bad understanding, but I can't understand why the flush() directive actually works. If the second flush() is inside a critical region, this means that that region will be executed secuetially by all threads and until one thread does not end, the next will not starts, so when flush() in invoked inside the region, the flushed variable will be updated and the new adopted value will be written in global memory. Then the next thread could read the correct value.


I think you are right, but maybe not for the correct reason!

There is an implied flush of all shared variables at the exit of the critical region, so that makes the second flush redundant.

Flush does two things: it ensures that all previous writes get committed to memory, and also that all subsequent reads come from memory. The first flush is necessary to make sure that the subsequent read of avanti really does come from memory, and not from that threads temporary view of memory (e.g. a CPU register).

In any case, this shows just how difficult reasoning about flush really is, and therefore why it is much better not to use it!
MarkB
 
Posts: 428
Joined: Thu Jan 08, 2009 10:12 am

Previous

Return to Using OpenMP

Who is online

Users browsing this forum: Majestic-12 [Bot] and 7 guests