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.

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

2.3.6 !$OMP ORDERED/!$OMP END ORDERED<br />

In certa<strong>in</strong> do-loops some of the statements executed at each iteration need to be evaluated<br />

<strong>in</strong> the same order as if the do-loop would be executed sequentially. For example:<br />

do i = 1, 100<br />

A(i) = 2 * A(i-1)<br />

enddo<br />

In this do-loop it is necessary to have computed iteration 59 before be<strong>in</strong>g able of<br />

comput<strong>in</strong>g correctly iteration 60, for <strong>in</strong>stance. In such a situation a parallel treatment of<br />

the entire do-loop is not possible! The follow<strong>in</strong>g more general case<br />

do i = 1, 100<br />

block1<br />

block2<br />

block3<br />

enddo<br />

<strong>in</strong> which block2 needs to be evaluated <strong>in</strong> the correct order while the other two blocks<br />

can be executed <strong>in</strong> any order, is go<strong>in</strong>g to be target. One possible solution to this problem<br />

could be to split the do-loop <strong>in</strong>to three do-loops and to parallelize only the first and the<br />

last one. Another option is to use the present directive-pair to <strong>in</strong>dicate to the compiler<br />

the part of the code that needs to be executed sequentially. The result would be:<br />

!$OMP DO ORDERED<br />

do i = 1, 100<br />

block1<br />

!$OMP ORDERED<br />

block2<br />

!$OMP END ORDERED<br />

block3<br />

enddo<br />

!$OMP END DO<br />

where it is mandatory to add the ORDERED clause to the !$OMP DO open<strong>in</strong>g-directive (see<br />

page 52 for further <strong>in</strong>formation regard<strong>in</strong>g the ORDERED clause). The sequence <strong>in</strong> which the<br />

different blocks of code are executed, for the case of hav<strong>in</strong>g a team with three threads, is<br />

shown graphically <strong>in</strong> figure 2.7.<br />

The !$OMP ORDERED/!$OMP END ORDERED directive-pair only makes sense <strong>in</strong>side the dynamic<br />

extent of parallelized do-loops. On one hand the directive-pair allows only one<br />

thread at a time <strong>in</strong>side its scope and on the other hand it allows the entrance of the<br />

threads only follow<strong>in</strong>g the order of the loop iterations: no thread can enter the ORDERED<br />

section until it is guaranteed that all previous iterations have been completed.

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

Saved successfully!

Ooh no, something went wrong!