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.
64 4. The <strong>OpenMP</strong> run-time library<br />
In this example all the other threads, which do not own the lock, are not idle; <strong>in</strong>stead,<br />
they are perform<strong>in</strong>g another task. This is someth<strong>in</strong>g not possible to achieve us<strong>in</strong>g <strong>OpenMP</strong><br />
directives. For certa<strong>in</strong> problems, this possibility can be very useful, s<strong>in</strong>ce the latency times<br />
due to synchronizations are filled <strong>in</strong> with work.<br />
In the previous two examples simple locks are used. Nestable locks are useful when a<br />
subrout<strong>in</strong>e need<strong>in</strong>g them may be called from different locations <strong>in</strong>side a program. Then,<br />
it is not necessary to worry about who is call<strong>in</strong>g. For example:<br />
module data_types<br />
use omp_lib, only: OMP_nest_lock_k<strong>in</strong>d<br />
implicit none<br />
type number<br />
<strong>in</strong>teger :: n<br />
<strong>in</strong>teger(OMP_nest_lock_k<strong>in</strong>d) :: lck<br />
end type number<br />
end module data_types<br />
!---------------------------------------------------------!<br />
program Ma<strong>in</strong><br />
use omp_lib<br />
use data_types<br />
implicit none<br />
type(number) :: x<br />
x%n = 0<br />
call OMP_<strong>in</strong>it_lock(x%lck)<br />
!$OMP PARALLEL SECTIONS SHARED(x)<br />
!$OMP SECTION<br />
call add(x,20)<br />
!$OMP SECTION<br />
call substract(x,10)<br />
!$OMP END PARALLEL<br />
call OMP_destroy_lock(lck)<br />
end program Ma<strong>in</strong><br />
!---------------------------------------------------------!<br />
subrout<strong>in</strong>e add(x,d)<br />
use omp_lib<br />
use data_types<br />
implicit none<br />
type(number) :: x<br />
<strong>in</strong>teger :: d<br />
call OMP_set_nest_lock(x%lck)<br />
x%n = x%n + d<br />
call OMP_unset_nest_lock(x%lck)<br />
end subrout<strong>in</strong>e add