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.

4.2. Lock rout<strong>in</strong>es 63<br />

thread can perform the write process. F<strong>in</strong>ally, s<strong>in</strong>ce the lock is no longer needed after<br />

the parallel region, it is elim<strong>in</strong>ated with the aid of OMP destroy lock.<br />

The previous example could also have been implemented us<strong>in</strong>g the <strong>OpenMP</strong> directive<br />

!$OMP CRITICAL:<br />

program Ma<strong>in</strong><br />

use omp_lib<br />

implicit none<br />

<strong>in</strong>teger(k<strong>in</strong>d = OMP_<strong>in</strong>teger_k<strong>in</strong>d) :: ID<br />

!$OMP PARALLEL SHARED(LCK) PRIVATE(ID)<br />

ID = OMP_get_thread_num()<br />

!$OMP CRITICAL<br />

write(*,*) "My thread is ", ID<br />

!$OMP END CRITICAL<br />

!$OMP END PARALLEL<br />

end program Ma<strong>in</strong><br />

Look<strong>in</strong>g at the previous example, the question rises about the necessity or use of the<br />

run-time lock rout<strong>in</strong>es, if <strong>OpenMP</strong> directives are able of do<strong>in</strong>g the same. Additionally to<br />

the previously mentioned example with the matrix A, there are other cases where it is not<br />

possible to emulate the effect of the lock rout<strong>in</strong>es us<strong>in</strong>g <strong>OpenMP</strong> directives. For example:<br />

program Ma<strong>in</strong><br />

use omp_lib<br />

implicit none<br />

<strong>in</strong>teger(k<strong>in</strong>d = OMP_lock_k<strong>in</strong>d) :: lck<br />

<strong>in</strong>teger(k<strong>in</strong>d = OMP_<strong>in</strong>teger_k<strong>in</strong>d) :: ID<br />

call OMP_<strong>in</strong>it_lock(lck)<br />

!$OMP PARALLEL SHARED(LCK) PRIVATE(ID)<br />

ID = OMP_get_thread_num()<br />

do while(.not.OMP_test_lock(lck))<br />

... !work to do while the thread is wait<strong>in</strong>g to get owner of the lock<br />

enddo<br />

... !work to do as the owner of the lock<br />

!$OMP END PARALLEL<br />

call OMP_destroy_lock(lck)<br />

end program Ma<strong>in</strong>

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

Saved successfully!

Ooh no, something went wrong!