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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

2.3 Synchronization constructs<br />

In certa<strong>in</strong> cases it is not possible to leave each thread on its own and it is necessary to br<strong>in</strong>g<br />

them back to an order. This is generally achieved through synchronizations of the threads.<br />

These synchronizations can be explicit, like the ones <strong>in</strong>troduced <strong>in</strong> the present section,<br />

or implied to previously presented <strong>OpenMP</strong> directives. In both cases the functionality<br />

is the same and it is convenient to read the present section <strong>in</strong> order to understand the<br />

implications derived from the use or not use of the implied synchronizations.<br />

2.3.1 !$OMP MASTER/!$OMP END MASTER<br />

The code enclosed <strong>in</strong>side this directive-pair is executed only by the master thread of the<br />

team. Meanwhile, all the other threads cont<strong>in</strong>ue with their work: no implied synchronization<br />

exists <strong>in</strong> the !$OMP END MASTER clos<strong>in</strong>g-directive. The syntaxis of the directive-pair<br />

is as follows:<br />

!$OMP MASTER<br />

...<br />

!$OMP END MASTER<br />

In essence, this directive-pair is similar to us<strong>in</strong>g the !$OMP SINGLE/!$OMP END SINGLE<br />

directive-pair presented before together with the NOWAIT clause, only that the thread to<br />

execute the block of code is forced to be the master one <strong>in</strong>stead of the first arriv<strong>in</strong>g one.<br />

As <strong>in</strong> previous <strong>OpenMP</strong> directives, it is necessary that the block of code enclosed<br />

<strong>in</strong>side the directive-pair is structured, which means that no branch<strong>in</strong>g <strong>in</strong>to or out of the<br />

block is allowed. A simple example of use would be:<br />

!$OMP MASTER<br />

write(*,*) "Hello"<br />

!$OMP END MASTER<br />

This example is also shown <strong>in</strong> a graphical way <strong>in</strong> figure 2.4, where threads 1 till N p<br />

do not wait for the master thread at the clos<strong>in</strong>g-directive !$OMP END MASTER; <strong>in</strong>stead,<br />

they cont<strong>in</strong>ue with their execution while the master thread executes the l<strong>in</strong>es enclosed <strong>in</strong><br />

the directive-pair !$OMP MASTER/!$OMP END MASTER. After the clos<strong>in</strong>g-directive, the master<br />

thread is beh<strong>in</strong>d the other threads <strong>in</strong> its duty.<br />

2.3.2 !$OMP CRITICAL/!$OMP END CRITICAL<br />

This directive-pair restricts the access to the enclosed code to only one thread at a time. In<br />

this way it is ensured, that what is done <strong>in</strong> the enclosed code is done correctly. Examples<br />

of application of this directive-pair could be to read an <strong>in</strong>put from the keyboard/file or to<br />

update the value of a shared variable. The syntaxis of the directive-pair is the follow<strong>in</strong>g<br />

one:

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

Saved successfully!

Ooh no, something went wrong!