26.06.2015 Views

Parallel Programming in Fortran 95 using OpenMP - People

Parallel Programming in Fortran 95 using OpenMP - People

Parallel Programming in Fortran 95 using OpenMP - People

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

24 2. <strong>OpenMP</strong> constructs<br />

thread 0 thread 1 thread N p<br />

!$OMP CRITICAL<br />

✞<br />

☎<br />

❄✝<br />

❝ ✆<br />

❄<br />

E xecution<br />

parallel region<br />

...<br />

write(1,*) data<br />

!$OMP END CRITICAL<br />

❄<br />

❄<br />

Figure 2.5: Graphical representation of the example expla<strong>in</strong><strong>in</strong>g the work<strong>in</strong>g pr<strong>in</strong>ciple of the<br />

!$OMP CRITICAL/!$OMP END CRITICAL directive-pair.<br />

2.3.3 !$OMP BARRIER<br />

This directive represents an explicit synchronization between the different threads <strong>in</strong> the<br />

team. When encountered, each thread waits until all the other threads have reached this<br />

po<strong>in</strong>t.Itssyntaxisisverysimple:<br />

!$OMP BARRIER<br />

Two restrictions need to be taken <strong>in</strong>to account when us<strong>in</strong>g this explicit synchronization:<br />

• The !$OMP BARRIER directive must be encountered by all threads <strong>in</strong> a team or by<br />

none at all. Several situations may not fulfill this condition. For example:<br />

!$OMP CRITICAL<br />

!$OMP BARRIER<br />

!$OMP END CRITICAL<br />

S<strong>in</strong>ce only one thread at a time is execut<strong>in</strong>g the content of the CRITICAL region, it is<br />

impossible for the other threads to reach the explicit synchronization. The result is<br />

that the program reaches a state without exit. This situation is called a deadlock<br />

and it is necessary to avoid it.<br />

Other examples, where also a deadlock happens, could be:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!