Parallel Programming in Fortran 95 using OpenMP - People
Parallel Programming in Fortran 95 using OpenMP - People
Parallel Programming in Fortran 95 using OpenMP - People
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
26 2. <strong>OpenMP</strong> constructs<br />
thread 0 thread 1 thread N p<br />
❄<br />
E xecution<br />
parallel region<br />
!$OMP BARRIER<br />
❄<br />
✞<br />
☎<br />
❄✝<br />
❝ ✆<br />
...<br />
❄ ❄ ❄<br />
❄<br />
Figure 2.6: Graphical representation of the effect of the !$OMP BARRIER directive on a team of<br />
threads.<br />
2.3.4 !$OMP ATOMIC<br />
When a variable <strong>in</strong> use can be modified from all threads <strong>in</strong> a team, it is necessary to<br />
ensure that only one thread at a time is writ<strong>in</strong>g/updat<strong>in</strong>g the memory location of the<br />
considered variable, otherwise unpredictable results will occur. In the follow<strong>in</strong>g example<br />
!$OMP DO<br />
do i = 1, 1000<br />
a = a + i<br />
enddo<br />
!$OMP END DO<br />
the desired result would be that a conta<strong>in</strong>s the sum of all values of the do-loop variable<br />
i, but this will not be the case. The present directive targets to ensure that a<br />
specific memory location is updated atomically, rather than expos<strong>in</strong>g it to the possibility<br />
of multiple, simultaneously writ<strong>in</strong>g threads. Its syntaxis is very simple:<br />
!$OMP ATOMIC<br />
This directive only affects the immediately follow<strong>in</strong>g statement. Not all possible statements<br />
are allowed, s<strong>in</strong>ce most cannot be treated <strong>in</strong> an atomic way. Only the follow<strong>in</strong>g<br />
ones can be used together with the !$OMP ATOMIC directive: