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.

62 4. The <strong>OpenMP</strong> run-time library<br />

The OMP test lock function returns .TRUE. if the simple lock associated with svar is<br />

successfully set; otherwise it returns .FALSE..<br />

The OMP test nest lock function returns the new value of the nest<strong>in</strong>g counter if the<br />

nestable lock associated with nvar is successfully set; otherwise it returns zero.<br />

4.2.5 OMP destroy lock and OMP destroy nest lock<br />

Once a lock is no longer needed, it is necessary to un<strong>in</strong>itialize the correspond<strong>in</strong>g lock<br />

variable. The present subrout<strong>in</strong>es are meant for that. Their <strong>in</strong>terface declarations are:<br />

subrout<strong>in</strong>e OMP_destroy_lock(svar)<br />

<strong>in</strong>teger(k<strong>in</strong>d = OMP_lock_k<strong>in</strong>d), <strong>in</strong>tent(<strong>in</strong>out) :: svar<br />

end subrout<strong>in</strong>e OMP_destroy_lock<br />

subrout<strong>in</strong>e OMP_destroy_nest_lock(nvar)<br />

<strong>in</strong>teger(k<strong>in</strong>d = OMP_nest_lock_k<strong>in</strong>d), <strong>in</strong>tent(<strong>in</strong>out) :: nvar<br />

end subrout<strong>in</strong>e OMP_destroy_nest_lock<br />

4.2.6 Examples<br />

To show the use of these subrout<strong>in</strong>es and functions, the follow<strong>in</strong>g example is given:<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 />

call OMP_set_lock(lck)<br />

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

call OMP_unset_lock(lck)<br />

!$OMP END PARALLEL<br />

call OMP_destroy_lock(lck)<br />

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

In this example the first th<strong>in</strong>g done, is the association of the lock variable lck with<br />

a simple lock, us<strong>in</strong>g OMP <strong>in</strong>it lock. Once <strong>in</strong>side the parallel region, the threads ask for<br />

the ownership of the lock lck with the aid of a call to OMP set lock. Thewrite to screen<br />

command follow<strong>in</strong>g this call is only executed by the thread which owns the lock. Once<br />

the work is done, the lock is released with the call to OMP unset lock so that another

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

Saved successfully!

Ooh no, something went wrong!